我正在尝试通过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);
}
}
}
感谢您的帮助
答案 0 :(得分:3)
压缩的工作原理是使用较短的字节序列对输入中的重复和可预测模式进行编码。与此处一样,完全随机的输入没有可预测的模式,也无法压缩。如果压缩已经压缩的文件,也会发生同样的情况。
尝试生成随机大写字符,随机英语单词或随机DNA序列(字母A C T G)而不是随机字节,您将看到它们的压缩程度。