我试图通过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);
}
}
}谢谢你的帮助
发布于 2013-11-08 19:51:40
压缩的工作方式是用较短的字节序列对输入中重复和可预测的模式进行编码。一个完全随机的输入,就像这里一样,没有可预测的模式,也不能被压缩。如果您压缩了已经压缩的文件,也会发生同样的情况。
尝试生成随机大写字符、随机英文单词或随机DNA序列(字母A、C、T、G),而不是随机字节,您将看到它们被压缩得有多好。
https://stackoverflow.com/questions/19867344
复制相似问题