【发布时间】:2016-02-27 14:03:37
【问题描述】:
我想从.zip 文件中提取两个特定文件。我尝试了以下库:
ZipFile zipFile = new ZipFile("myZip.zip");
结果:
Exception in thread "main" java.util.zip.ZipException: error in opening zip file
我也试过了:
public void extract(String targetFileName) throws IOException
{
OutputStream outputStream = new FileOutputStream("targetFile.foo");
FileInputStream fileInputStream = new FileInputStream("myZip.zip");
ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(fileInputStream));
ZipEntry zipEntry;
while ((zipEntry = zipInputStream.getNextEntry()) != null)
{
if (zipEntry.getName().equals("targetFile.foo"))
{
byte[] buffer = new byte[8192];
int length;
while ((length = zipInputStream.read(buffer)) != -1)
{
outputStream.write(buffer, 0, length);
}
outputStream.close();
break;
}
}
}
结果:
也不例外,只是一个空的targetFile.foo 文件。
请注意,.zip 文件的类型为 SFX 7-zip,最初具有 .exe 扩展名,因此这可能是失败的原因。
【问题讨论】:
-
它是否适用于正则 zip 文件?
-
该格式与
ZipInputStream不兼容。见this answer