【问题标题】:What is the "Jodatime" / "Apache commons" of Zip/Unzip Java utilities?什么是 Zip/Unzip Java 实用程序的“Jodatime”/“Apache commons”?
【发布时间】:2011-01-04 19:59:10
【问题描述】:

我确信那里有一个成熟的、广泛使用的 ZIP 文件实用程序,但我似乎无法找到。与 Apache Commons、Google Collections、Joda Time 一样成熟的东西

我正在尝试将 zip 文件作为字节数组 (ZipInputStream) 并将其解压缩到文件夹中。这似乎是一项非常乏味的任务。

我希望有一个语法糖 API,它可以做这样的事情:

public class MyDreamZIPUtils 
      public static void extractToFolder(ZipInputStream zin, File outputFolderRoot){
           ...
      }
      public static void extractToFolder(ZipFile zf, File outputFolderRoot){
           ...
      }

      public static zipFolder(File folderToZip, File zippedFileLocation){
           ...
      }

      public static zipFolder(File folderToZip, ByteArrayOutputStream zipResult){
           ...
      }

这样的吗? 我错过了什么吗?

【问题讨论】:

  • 是的,我也很失望,我对 commons-compress 的期望更高...嗯,这不是典型的 Java 风格吗?

标签: java zip utilities unzip utility


【解决方案1】:

http://commons.apache.org/compress/

我相信您可以在此基础上编写“语法糖”。

Javadoc:http://commons.apache.org/compress/apidocs/index.html

【讨论】:

    【解决方案2】:

    我只使用了 Java API 调用...我没有使用你所有的方法。您可以从这里找出它们...请注意,我并没有声称代码没有错误...使用风险自负:)

        public static void extractToFolder(ZipInputStream zin, File outputFolderRoot)
                    throws IOException {
    
                FileOutputStream fos = null;
                byte[] buf = new byte[1024];
                ZipEntry zipentry;
    
                for (zipentry = zin.getNextEntry(); zipentry != null; zipentry = zin.getNextEntry()) {
    
                    try {
                        String entryName = zipentry.getName();
                        System.out.println("Extracting: " + entryName);
                        int n;
    
                        File newFile = new File(outputFolderRoot, entryName);
                        if (zipentry.isDirectory()) {
                            newFile.mkdirs();
                            continue;
                        } else {
                            newFile.getParentFile().mkdirs();
                            newFile.createNewFile();
                        }
    
                        fos = new FileOutputStream(newFile);
    
                        while ((n = zin.read(buf, 0, 1024)) > -1)
                            fos.write(buf, 0, n);
    
                        fos.close();
                        zin.closeEntry();
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        if (fos != null)
                            try {
                                fos.close();
                            } catch (Exception ignore) {
                            }
                    }
    
                }
    
                zin.close();
    
            }
    
    
        public static void zipFolder(File folderToZip, File zippedFileLocation) throws IOException {
            // create a ZipOutputStream to zip the data to
            ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zippedFileLocation));
            String path = "";
            zipDir(folderToZip, zos, path);
            // close the stream
            zos.close();
        }
    
        private static void zipDir(File directory, ZipOutputStream zos, String path) throws IOException {
            File zipDir = directory;
            // get a listing of the directory content
            String[] dirList = zipDir.list();
            byte[] readBuffer = new byte[2156];
            int bytesIn = 0;
            // loop through dirList, and zip the files
            for (int i = 0; i < dirList.length; i++) {
                File f = new File(zipDir, dirList[i]);
                if (f.isDirectory()) {
                    zipDir(new File(f.getPath()), zos, path + f.getName() + "/");
                    continue;
                }
                FileInputStream fis = new FileInputStream(f);
                try {
                    ZipEntry anEntry = new ZipEntry(path + f.getName());
                    zos.putNextEntry(anEntry);
                    bytesIn = fis.read(readBuffer);
                    while (bytesIn != -1) {
                        zos.write(readBuffer, 0, bytesIn);
                        bytesIn = fis.read(readBuffer);
                    }
                } finally {
                    fis.close();
                }
            }
        }
    

    参考Java2s

    【讨论】:

    • 如果可以的话,我会给你 +3 的努力......如果 apache common compress 无法提供,我肯定会使用它......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-04
    • 2019-07-08
    • 1970-01-01
    • 2016-02-14
    • 2019-02-22
    • 1970-01-01
    相关资源
    最近更新 更多