我正在尝试通过 ZipOutputStream 输出数据,但生成的文件未压缩。这是在 Windows 7 下。这是一个示例:
import java.io.*;
import java.nio.file.*;
import java.util.Random;
import java.util.zip.*;
public class Testy {
public static void main(String[] args) {
byte[] data = new byte[100];
Random rnd = new Random(System.currentTimeMillis());
try {
BufferedOutputStream out = new BufferedOutputStream(Files.newOutputStream(
Paths.get("record.zip"), StandardOpenOption.CREATE, StandardOpenOption.APPEND), 200000);
ZipOutputStream zout = new ZipOutputStream(out);
zout.setLevel(4);
zout.putNextEntry(new ZipEntry("record.dat"));
for (int i = 0; i < 10000; i++) {
rnd.nextBytes(data);
zout.write(data, 0, data.length);
Thread.sleep(1L);
}
zout.closeEntry();
zout.finish();
zout.close();
} catch (IOException | InterruptedException e) {
e.printStackTrace(System.out);
}
}
}
谢谢你的帮助