ZipOutputStream
かなり標準的なコードを使用してzipファイルを作成しています。ZipInputStream
何らかの理由で、ZipEntry
hasとして読み返すとsize=-1
. ファイル名はZipEntry
.
(OS ツールを使用して 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...