【发布时间】:2012-02-06 18:56:38
【问题描述】:
我正在开发从一个 zip 中获取文件并将它们放入另一个中的应用程序,它对文件很好,但如果源 zip 中有一个目录,它会失败,并出现以下异常:
Exception in thread "main" java.util.zip.ZipException: invalid entry size (expected 1374 but got 1024 bytes)
我正在使用以下代码:
public static void ZipExtractToZip(File inZip, File outZip) throws IOException
{
ZipInputStream zis = new ZipInputStream(new FileInputStream(inZip));
ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outZip)));
byte[] buffer = new byte[1024];
for (ZipEntry ze = zis.getNextEntry(); ze != null; ze = zis.getNextEntry())
{
zos.putNextEntry(ze);
for (int read = zis.read(buffer); read != -1; read = zis.read(buffer)) {
zos.write(buffer, 0, read);
}
zos.closeEntry();
}
zos.close();
zis.close();
}
我尝试了不同的缓冲区大小,但这无济于事,我需要一种获取动态缓冲区大小的方法。 欢迎提供示例和链接。
编辑:我更改了代码以使其可用
- 利亚姆,Hachi Software 首席执行官
【问题讨论】:
-
哪一行有异常?顺便说一句:我也会使用 BufferedInputSTream(不是说它会解决问题)
-
在 java.util.zip.ZipOutputStream.closeEntry(Unknown Source) at com.hachisoftware.mmi.system.Util.ZipExtractToZip(Util.java:26)
标签: java zip zipinputstream zipoutputstream