我想从一个文件中提取两个特定的.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
扩展名,因此这可能是失败的原因。