2018-06-26 8页 doc 22KB 26阅读
is_037433
暂无简介
* Title: *
* ** Description: *
* ** Copyright ?I 2008,2009 ?é?ð?Æ??. All right reserved *
* ** Company: ?é?ð?Æ?? *
* * @author chenwei * */ public class ZipUtil { /** * ÎÄ?þÑ?Ëõ???? * * @param out * zipÎÄ?þÑ?ËõÊä?öÁ? * @param f * Ñ?ËõÔ?ÎÄ?þ * @param base * Ä?Â? * @param first * ÊÇ?ñÊǵÚÒ??Î?Ù×? * @throws IOException * IOÒì?? */ private static void zipWork(ZipOutputStream out, File f, String base, boolean first) throws IOException { if (first) { if (f.isDirectory()) { out.putNextEntry(new org.apache.tools.zip.ZipEntry("/")); base = base + f.getName(); first = false; } else { base = f.getName(); } } if (f.isDirectory()) { File[] fl = f.listFiles(); base = base + "/"; for (int i = 0; i < fl.length; i++) { zipWork(out, fl[i], base + fl[i].getName(), first); } } else { out.putNextEntry(new org.apache.tools.zip.ZipEntry(base)); FileInputStream in = new FileInputStream(f); int b; while ((b = in.read()) != -1) { out.write(b); } in.close(); } } /** * zipÎÄ?þ?âÑ? * * @param zipFile * zipÎÄ?þÃû * @param unZipRoot * ?âÑ?Ä?Â? * @throws IOException * IOÒì?? * @throws ZipException * ZipÎÄ?þ?âÑ?Òì?? */ @SuppressWarnings("unchecked") private static void unZipFileByOpache(org.apache.tools.zip.ZipFile zipFile, String unZipRoot) throws ZipException, IOException { // ?ñµÃZIP ÎÄ?þÌõÄ? java.util.Enumeration e = zipFile.getEntries(); org.apache.tools.zip.ZipEntry zipEntry; while (e.hasMoreElements()) { zipEntry = (org.apache.tools.zip.ZipEntry) e.nextElement(); InputStream fis = zipFile.getInputStream(zipEntry); if (!zipEntry.isDirectory()) { StringBuffer zipFileName = new StringBuffer(); zipFileName.append(unZipRoot); zipFileName.append("/"); zipFileName.append(zipEntry.getName()); File file = new File(zipFileName.toString()); // ?ñµÃÎÄ?þ??Ä?Â? File parentFile = file.getParentFile(); parentFile.mkdirs(); FileOutputStream fos = new FileOutputStream(file); byte[] b = new byte[1024]; int len; while ((len = fis.read(b, 0, b.length)) != -1) { fos.write(b, 0, len); } fos.close(); fis.close(); } } } /** * zipÎÄ?þÑ?Ëõ???? * * @param zipFileName * zipÎÄ?þÃû * Ñ?ËõÎÄ?þÃû * @param dncoding * Ñ?Ëõ?àÂë * @throws IOException * IOÒì?? */ public static void ZipFile(String zipFileName, String inputFileName, String dncoding) throws IOException { // zipÊä?öÁ? org.apache.tools.zip.ZipOutputStream out = new org.apache.tools.zip.ZipOutputStream( new FileOutputStream(zipFileName)); // ÉèÖÃÎÄ?þÑ?ËõÎÄ?þ?ñÊ? out.setEncoding(dncoding); File inputFile = new File(inputFileName); zipWork(out, inputFile, "", true); out.close(); } /** * zipÎÄ?þ?âÑ????? * * @param unZipFileName * zipÎÄ?þÃû * @param unZipPath * ?âÑ?Â??? * @param dncoding * ?àÂë * @throws IOException * IOÒì?? */ public static void unZipFile(String unZipFileName, String unZipPath, String dncoding) throws IOException { // zipÎÄ?þ?ÔÏó org.apache.tools.zip.ZipFile zipFile = new org.apache.tools.zip.ZipFile( unZipFileName, dncoding); // ?âÑ?ÎÄ?þ- unZipFileByOpache(zipFile, unZipPath); } /** * ?âÊÔ???? * * @param args * @throws Exception */ public static void main(String[] args) throws Exception { // ZipFile("d:/test/Daily_0817.zip", "d:/test/Daily_0817", "UTF-8"); // unZipFile("d:/test/Daily_0817.zip", "d:/test/", "UTF-8"); } }