byte[]
メモリ内にzipファイルを書き込んでから、それをディスクに書き込もうとしています。結果の zip ファイルは破損しています。
これは機能します:
try (FileOutputStream fos = new FileOutputStream(Files.createTempFile("works", ".zip").toFile());
ZipOutputStream zos = new ZipOutputStream(fos)) {
zos.putNextEntry(new ZipEntry("test.txt"));
zos.write("hello world".getBytes());
zos.closeEntry();
}
これは壊れており、破損した zip ファイルを作成します。
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(bos)) {
zos.putNextEntry(new ZipEntry("test.txt"));
zos.write("hello world".getBytes());
zos.closeEntry();
Files.write(Files.createTempFile("broken", ".zip"), bos.toByteArray());
}
2番目のものが機能しないのはなぜですか?生で操作する必要があると仮定して、どうすれば修正できますか(他の目的でbyte[]
必要になるため、zipファイルをファイルに直接作成することはできません)。byte[]