2014-04-27 233 views
4

我是使用Java进行编程的初学者,目前正在编写一个必须能够压缩和解压缩.zip文件的应用程序。我可以使用下面的代码使用Java来解压压缩文件的内置Java拉链的功能以及Apache下议院IO库:将压缩目录压缩成带有Commons IO的压缩文件

public static void decompressZipfile(String file, String outputDir) throws IOException { 
    if (!new File(outputDir).exists()) { 
     new File(outputDir).mkdirs(); 
    } 
    ZipFile zipFile = new ZipFile(file); 
    Enumeration<? extends ZipEntry> entries = zipFile.entries(); 
    while (entries.hasMoreElements()) { 
     ZipEntry entry = entries.nextElement(); 
     File entryDestination = new File(outputDir, entry.getName()); 
     if (entry.isDirectory()) { 
      entryDestination.mkdirs(); 
     } else { 
      InputStream in = zipFile.getInputStream(entry); 
      OutputStream out = new FileOutputStream(entryDestination); 
      IOUtils.copy(in, out); 
      IOUtils.closeQuietly(in); 
      IOUtils.closeQuietly(out); 
     } 
    } 
} 

我怎么会去使用没有外部从目录中创建一个压缩文件除了我已经使用的库之外的库? (Java标准库和共享IO)

+2

在你的情况下,zip部分由java.util.Zip完成commons-IO只是提供一个实用程序来关闭文件。你在寻找像上面这样的解决方案吗? – AdityaKeyal

+0

是的,我正在寻找只使用提供的Java库和/或Commons-IO的解决方案,并且没有其他外部依赖关系。我已经编辑了这个问题文本,以便更清楚地了解这一点。我很新,因为这段代码是来自另一个SE问题,它提出它是“解压缩zip文件的Commons-IO方法”,我错误地认为该功能是由Commons-IO提供的。 – StackUnderflow

回答

7

以下方法(S)似乎递归成功压缩的目录:

public static void compressZipfile(String sourceDir, String outputFile) throws IOException, FileNotFoundException { 
    ZipOutputStream zipFile = new ZipOutputStream(new FileOutputStream(outputFile)); 
    compressDirectoryToZipfile(sourceDir, sourceDir, zipFile); 
    IOUtils.closeQuietly(zipFile); 
} 

private static void compressDirectoryToZipfile(String rootDir, String sourceDir, ZipOutputStream out) throws IOException, FileNotFoundException { 
    for (File file : new File(sourceDir).listFiles()) { 
     if (file.isDirectory()) { 
      compressDirectoryToZipfile(rootDir, sourceDir + File.separator + file.getName(), out); 
     } else { 
      ZipEntry entry = new ZipEntry(sourceDir.replace(rootDir, "") + file.getName()); 
      out.putNextEntry(entry); 

      FileInputStream in = new FileInputStream(sourceDir + file.getName()); 
      IOUtils.copy(in, out); 
      IOUtils.closeQuietly(in); 
     } 
    } 
} 

正如我在压缩代码片段所见,我使用IOUtils.copy()处理流数据转让。

+0

第10行出现错误,File.separator已被放入一个concat中。 –

+0

这一行有一个错误..在这一行FileInputStream in = new FileInputStream(sourceDir + file.getName());它应该是FileInputStream in = new FileInputStream(sourceDir + File.separator + file.getName()); – shiva

2

我修复了上述错误,它的工作原理非常完美。

public static void compressZipfile(String sourceDir, String outputFile) throws IOException, FileNotFoundException { 
    ZipOutputStream zipFile = new ZipOutputStream(new FileOutputStream(outputFile)); 
    Path srcPath = Paths.get(sourceDir); 
    compressDirectoryToZipfile(srcPath.getParent().toString(), srcPath.getFileName().toString(), zipFile); 
    IOUtils.closeQuietly(zipFile); 
} 

private static void compressDirectoryToZipfile(String rootDir, String sourceDir, ZipOutputStream out) throws IOException, FileNotFoundException { 
    String dir = Paths.get(rootDir, sourceDir).toString(); 
    for (File file : new File(dir).listFiles()) { 
     if (file.isDirectory()) { 
      compressDirectoryToZipfile(rootDir, Paths.get(sourceDir,file.getName()).toString(), out); 
     } else { 
      ZipEntry entry = new ZipEntry(Paths.get(sourceDir,file.getName()).toString()); 
      out.putNextEntry(entry); 

      FileInputStream in = new FileInputStream(Paths.get(rootDir, sourceDir, file.getName()).toString()); 
      IOUtils.copy(in, out); 
      IOUtils.closeQuietly(in); 
     } 
    } 
}