3

I'm creating a zip file with ZipOutputStream using pretty standard code. For some reason when I read it back in as a ZipInputStream the ZipEntry has size=-1. The filename is stored correctly in the ZipEntry.

(When I make a zip file using my OS tools and then read it back in, the size is correct, so I assume the problem is with ZipOutputStream and not the ZipInputStream).

The context is a Spring MVC controller.

What am I doing wrong? Thanks.

Here is the code:

// 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...
bz3x
  • 171
  • 4
  • 15
  • Are you zipping a zip file? `ZipEntry entry = new ZipEntry(filename+".zip");` – jlordo Nov 05 '12 at 08:24
  • btw, a good note. Double-zip? =) – user Nov 05 '12 at 08:26
  • actually no. even with a different extension the problem remains – bz3x Nov 05 '12 at 10:40
  • test.jpg.zip - O_O I dont really get what is that =) The problem could be with the pathes to the files. Try to debug and check that you are not transfering nulls. Also, as a parameter to the input stream use a File, but not a String. This will clarify whether you really have the file. – user Nov 05 '12 at 13:43

2 Answers2

1

You have to get the next entry from the stream, like in this example:

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

When you set the size manually, it sure would give you a result, like you've shown "64527". You better have a look at the zip examples. They will give you a clear image. Also: Create Java-Zip-Archive from existing OutputStream

Try something, like this:

        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();
Community
  • 1
  • 1
user
  • 3,058
  • 23
  • 45
  • I already have zos.putNextEntry(entry); in the code. Are you saying I need it again somewhere? – bz3x Nov 05 '12 at 10:42
  • Not really. It is just a bit different code you need. Look up in th edit. – user Nov 05 '12 at 10:46
  • Thanks, I've modified it to the following. (The data isn't coming from a physical file but from a byte[]). `code` ZipOutputStream zos = new ZipOutputStream(response.getOutputStream()); zos.putNextEntry(new ZipEntry(filename+".foo")); zos.write(myBytes); // debugging here: zos.current.entry.name is correct, but size=-1. myBytes.length however is 64527! zos.closeEntry(); zos.close(); – bz3x Nov 05 '12 at 11:07
  • I updated the example code in the original question using your suggestions. The problem should be clearer now. Thanks – bz3x Nov 05 '12 at 11:47
0

It seems that entry.getSize() is simply unreliable: Why ZipInputStream can't read the output of ZipOutputStream?

The above post provides a suitable workaround.

Community
  • 1
  • 1
bz3x
  • 171
  • 4
  • 15