Java 中如何进行zip包的解压缩呢?
有两种方式:
(1)使用jdk 自带的zip工具
(2)使用apache旗下的commons-compress
我下面要讲解的zip解压缩助手使用的是apache旗下的commons-compress.
工具运行界面如下:
核心代码:
解压:
- /***
- * 解压zip
- *
- * @param zipFile
- * @param decompressLoc
- * :解压之后的文件所在目录
- * @throws ArchiveException
- * @throws IOException
- */
- public static boolean deCompressRecursion(String zipFile,
- File decompressLoc, String charSet) throws ArchiveException,
- IOException {
- FileInputStream fin = new FileInputStream(zipFile);
- ArchiveInputStream archIns = new ArchiveStreamFactory()
- .createArchiveInputStream(ArchiveStreamFactory.ZIP, fin);
- ZipArchiveInputStream zipIn = (ZipArchiveInputStream) archIns;
- boolean isSuccess = deCompressRecursion(zipIn, decompressLoc, charSet);
- zipIn.close();
- return isSuccess;
- }
- /***
- * 递归解压缩.
- *
- * @param zipIn
- * @param decompressLoc
- * @return
- * @throws IOException
- */
- private static boolean deCompressRecursion(ZipArchiveInputStream zipIn,
- File decompressLoc, String charset) throws IOException {
- ZipArchiveEntry zipEntry;
- if (ValueWidget.isNullOrEmpty(charset)) {
- charset = SystemHWUtil.CHARSET_UTF;
- }
- while (!ValueWidget.isNullOrEmpty(zipEntry = zipIn.getNextZipEntry())) {
- byte[] rawName = zipEntry.getRawName();
- String fileName = new String(rawName, charset);
- // System.out.println(fileName);
- if (zipEntry.isDirectory()) {// 是目录
- File newFolder = new File(decompressLoc, fileName);// 若子目录不存在,则创建之
- System.out.println(newFolder.getAbsolutePath());
- if (!newFolder.exists()) {
- newFolder.mkdir();
- }
- // deCompressRecursion(zipIn, decompressLoc,charset);
- } else {// 是普通文件
- File singFile = new File(decompressLoc, fileName);
- System.out.println(singFile.getAbsolutePath());
- if (singFile.exists()) {// 若解压后的文件已经存在,则直接退出
- GUIUtil23.warningDialog("File \""
- + singFile.getAbsolutePath() + "\" does exist.");
- return false;
- }
- /**
- * 以下四行代码是后来添加的,为了解决父目录不存在的问题
- */
- File fatherFolder = singFile.getParentFile();
- if (!fatherFolder.exists()) {
- fatherFolder.mkdirs();
- }
- FileUtils.writeIn2Output(zipIn, new FileOutputStream(singFile),
- true, false);
- }
- }
- return true;
- }
压缩:
- /***
- * 压缩文件.
- *
- * @param zipFile
- * @param folderPaths
- * @return
- * @throws ArchiveException
- * @throws IOException
- */
- public static boolean compressZipRecursion(String zipFile,
- String folderPaths) throws ArchiveException, IOException {
- FileOutputStream fou = new FileOutputStream(zipFile);
- ArchiveOutputStream archOuts = new ArchiveStreamFactory()
- .createArchiveOutputStream(ArchiveStreamFactory.ZIP, fou);
- if (archOuts instanceof ZipArchiveOutputStream) {
- ZipArchiveOutputStream zipOut = (ZipArchiveOutputStream) archOuts;
- List<ZipArchiveEntry> zipEntrys = getZipFileListRecursion(new File(
- folderPaths), null);
- for (int i = 0; i < zipEntrys.size(); i++) {
- ZipArchiveEntry zipEntry2 = zipEntrys.get(i);
- zipOut.putArchiveEntry(zipEntry2);
- File file = new File(folderPaths, zipEntry2.getName());
- if (!file.exists()) {
- return false;
- }
- if (!file.isDirectory()) {
- FileInputStream fin = new FileInputStream(file);
- // 不要关闭zipOut,关闭之前要执行closeArchiveEntry()
- FileUtils.writeIn2Output(fin, zipOut, false, true);
- }
- }
- closeZip(zipOut, true);
- }
- return true;
- }
- /***
- * 压缩之后的收尾操作.
- *
- * @param zipOut
- * @throws IOException
- */
- private static void closeZip(ZipArchiveOutputStream zipOut,
- boolean iscloseArchiveEntry) throws IOException {
- if (iscloseArchiveEntry) {
- zipOut.closeArchiveEntry();// it is necessary
- }
- zipOut.flush();
- zipOut.finish();
- zipOut.close();
- }
上述代码见类:com.common.util.CompressZipUtil
项目名:zip_mgmt
项目源代码见附件:zip_mgmt.zip
项目使用maven 构建
IDE:eclipse
依赖的jar包:(1)io0007-find_progess-0.0.8-SNAPSHOT.jar
(2)is_chinese
学习笔记见附件java zip压缩.zip
参考:http://m.blog.csdn.net/blog/buyaore_wo/7047343
http://www.cnblogs.com/un4sure/archive/2011/09/27/2193298.html,
http://hw1287789687.iteye.com/blog/1976309