【发布时间】:2012-11-05 08:07:06
【问题描述】:
我正在使用非常标准的代码创建一个带有ZipOutputStream 的 zip 文件。出于某种原因,当我将其作为ZipInputStream 读回时,ZipEntry 具有size=-1。文件名正确存储在ZipEntry中。
(当我使用我的操作系统工具制作一个 zip 文件然后将其读回时,大小是正确的,所以我认为问题出在 ZipOutputStream 而不是 ZipInputStream)。
上下文是一个 Spring MVC 控制器。
我做错了什么? 谢谢。
代码如下:
// export zip file
String file = "/Users/me/Desktop/test.jpg";
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(file+".zip");
ZipOutputStream zos = new ZipOutputStream(fos);
zos.putNextEntry(new ZipEntry("test.jpg"));
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fis.read(buffer)) > 0) {
zos.write(buffer, 0, bytesRead);
}
zos.closeEntry();
zos.close();
fis.close();
// import same file
String file2 = "/Users/me/Desktop/test.jpg.zip";
FileInputStream fis2 = new FileInputStream(file2);
ZipInputStream zis = new ZipInputStream(fis2);
ZipEntry entry = zis.getNextEntry();
// here: entry.getSize() = -1, zip.buf is an array of zeros...
// but if I unzip the file on my OS I see that the original file has been zipped...
【问题讨论】:
-
您在压缩一个 zip 文件吗?
ZipEntry entry = new ZipEntry(filename+".zip"); -
顺便说一句,很好的说明。双拉链? =)
-
其实没有。即使使用不同的扩展名,问题仍然存在
-
test.jpg.zip - O_O 我真的不明白那是什么=)问题可能出在文件的路径上。尝试调试并检查您是否没有传输空值。此外,作为输入流的参数,使用文件,而不是字符串。这将澄清您是否真的拥有该文件。
标签: java io zipoutputstream