【问题标题】:ZipOutputStream leaving size=-1ZipOutputStream 离开 size=-1
【发布时间】: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


【解决方案1】:

您必须从流中获取下一个条目,如下例所示:

http://www.roseindia.net/tutorial/java/corejava/zip/zipentry.html

当您手动设置大小时,它肯定会给您一个结果,就像您显示的“64527”一样。 你最好看看 zip 示例。他们会给你一个清晰的形象。 另外:Create Java-Zip-Archive from existing OutputStream

尝试一下,像这样:

        String inputFileName = "test.txt";
        String zipFileName = "compressed.zip";

        //Create input and output streams
        FileInputStream inStream = new FileInputStream(inputFileName);
        ZipOutputStream outStream = new ZipOutputStream(new FileOutputStream(zipFileName));

        // Add a zip entry to the output stream
        outStream.putNextEntry(new ZipEntry(inputFileName));

        byte[] buffer = new byte[1024];
        int bytesRead;

        //Each chunk of data read from the input stream 
        //is written to the output stream
        while ((bytesRead = inStream.read(buffer)) > 0) {
            outStream.write(buffer, 0, bytesRead);
        }

        //Close zip entry and file streams
        outStream.closeEntry();

        outStream.close();
        inStream.close();

【讨论】:

  • 我已经有了 zos.putNextEntry(entry);在代码中。你是说我在某个地方再次需要它吗?
  • 并非如此。这只是您需要的一些不同的代码。在编辑中查找。
  • 谢谢,我已将其修改为以下内容。 (数据不是来自物理文件,而是来自字节[])。 code ZipOutputStream zos = new ZipOutputStream(response.getOutputStream()); zos.putNextEntry(new ZipEntry(filename+".foo")); zos.write(myBytes); // 在这里调试:zos.current.entry.name 是正确的,但是 size=-1。 myBytes.length 但是是 64527! zos.closeEntry(); zos.close();
  • 我使用您的建议更新了原始问题中的示例代码。现在问题应该更清楚了。谢谢
【解决方案2】:

似乎 entry.getSize() 根本不可靠:Why ZipInputStream can't read the output of ZipOutputStream?

上面的帖子提供了一个合适的解决方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-23
    • 2014-12-17
    • 2014-03-20
    • 1970-01-01
    • 1970-01-01
    • 2020-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多