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...