我是使用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)
在你的情况下,zip部分由java.util.Zip完成commons-IO只是提供一个实用程序来关闭文件。你在寻找像上面这样的解决方案吗? – AdityaKeyal
是的,我正在寻找只使用提供的Java库和/或Commons-IO的解决方案,并且没有其他外部依赖关系。我已经编辑了这个问题文本,以便更清楚地了解这一点。我很新,因为这段代码是来自另一个SE问题,它提出它是“解压缩zip文件的Commons-IO方法”,我错误地认为该功能是由Commons-IO提供的。 – StackUnderflow