Ant压缩与解压缩
package com.test.utils; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.util.Enumeration; import org.apache.tools.ant.Project; import org.apache.tools.ant.taskdefs.Zip; import org.apache.tools.ant.types.FileSet; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipFile; public class ZipFileHandler { private final int buffer = 1024; private void createDirectory(String directory, String subDirectory) { String dir[]; File fl = new File(directory); try { if (subDirectory == "" && fl.exists() != true) { fl.mkdir(); } else if (subDirectory != "") { dir = subDirectory.replace('\\', '/').split("/"); for (int i = 0; i < dir.length; i++) { File subFile = new File(directory + File.separator + dir[i]); if (subFile.exists() == false) { subFile.mkdir(); } directory += File.separator + dir[i]; } } } catch (Exception ex) { System.out.println(ex.getMessage()); } } @SuppressWarnings("unchecked") public boolean unZip(String zipFilePath, String outputDirectory) { boolean flag = false; try { ZipFile zipFile = new ZipFile(zipFilePath); Enumeration e = zipFile.getEntries(); ZipEntry zipEntry = null; createDirectory(outputDirectory, ""); while (e.hasMoreElements()) { zipEntry = (ZipEntry) e.nextElement(); System.out.println("unzip " + zipEntry.getName()); if (zipEntry.isDirectory()) { String name = zipEntry.getName().trim(); name = name.substring(0, name.length() - 1); File f = new File(outputDirectory + File.separator + name); if (!f.exists()) { f.mkdir(); } } else { String fileName = zipEntry.getName(); fileName = fileName.replace('\\', '/'); if (fileName.indexOf("/") != -1) { createDirectory(outputDirectory, fileName.substring(0, fileName.lastIndexOf("/"))); fileName = fileName .substring(fileName.lastIndexOf("/") + 1); } File f = new File(outputDirectory + File.separator + zipEntry.getName()); f.createNewFile(); InputStream in = zipFile.getInputStream(zipEntry); FileOutputStream out = new FileOutputStream(f); byte[] by = new byte[buffer]; int c; while ((c = in.read(by)) != -1) { out.write(by, 0, c); } in.close(); out.close(); } } flag = true; System.out.println("unzip finished"); } catch (Exception ex) { System.out.println(ex.getMessage()); } return flag; } public boolean zip(String srcDirName, String zipFilePath) { boolean flag = false; try { File srcdir = new File(srcDirName); if (!srcdir.exists()) throw new RuntimeException(srcDirName + " is not exist!"); Project prj = new Project(); Zip zip_ = new Zip(); zip_.setProject(prj); zip_.setDestFile(new File(zipFilePath)); FileSet fileSet = new FileSet(); fileSet.setProject(prj); fileSet.setDir(srcdir); zip_.addFileset(fileSet); zip_.execute(); flag = true; System.out.println("zip finished"); } catch (Exception ex) { System.out.println(ex.getMessage()); } return flag; } public static void main(String[] args) { ZipFileHandler zh = new ZipFileHandler(); zh.unZip("C:\\test.zip", "C:\\test1"); zh.zip("C:\\test1", "C:\\test1.zip"); } }
点亮测试人生!QQ:408129370
百度阅读电子书地址:http://yuedu.baidu.com/ebook/f6dbb2a2f01dc281e53af0f3
讨论QQ群:536192476
个人公众号:
百度阅读电子书地址:http://yuedu.baidu.com/ebook/f6dbb2a2f01dc281e53af0f3
讨论QQ群:536192476
个人公众号: