java - Apache commons 压缩 7z 文件的大小比 p7zip 压缩要大
问题描述
当我压缩 500mb 的 html 文件时,p7zip 在几秒钟内完成,文件大小为 7mb(没有任何自定义设置,只是7z a filename.7z /folder
)。
因此,我希望 apache commons compress 也可以使用 7z 压缩到相当的大小。然而,事实并非如此。即使我启用了 apache commons compress 7z 的最大预设。生成的文件大小也很大,接近 100mb。
我做错了什么还是需要调整我的预设?我已经阅读了 apache commons compress wiki,但没有找到我的答案。
java实现的相关代码:
public static Path compress(String name, List<Path> files) throws IOException {
try (SevenZOutputFile out = new SevenZOutputFile(new File(name))) {
List<SevenZMethodConfiguration> methods = new ArrayList<>();
LZMA2Options lzma2Options = new LZMA2Options();
lzma2Options.setPreset(LZMA2Options.PRESET_MAX);
SevenZMethodConfiguration lzmaConfig =
new SevenZMethodConfiguration(SevenZMethod.LZMA, lzma2Options);
methods.add(lzmaConfig);
out.setContentMethods(methods);
for (Path file : files) {
addToArchiveCompression(out, file, ".");
}
}
return Paths.get(name);
}
private static void addToArchiveCompression(SevenZOutputFile out, Path file,
String dir) throws IOException {
String name = dir + File.separator + file.getFileName();
if (Files.isRegularFile(file)) {
SevenZArchiveEntry entry = out.createArchiveEntry(file.toFile(), name);
out.putArchiveEntry(entry);
FileInputStream in = new FileInputStream(file.toFile());
byte[] b = new byte[1024];
int count = 0;
while ((count = in.read(b)) > 0) {
out.write(b, 0, count);
}
out.closeArchiveEntry();
} else if (Files.isDirectory(file)) {
File[] children = file.toFile().listFiles();
if (children != null) {
for (File child : children) {
addToArchiveCompression(out, Paths.get(child.toURI()), name);
}
}
} else {
System.out.println(file.getFileName() + " is not supported");
}
}
解决方案
您能否尝试删除这些行:
List<SevenZMethodConfiguration> methods = new ArrayList<>();
LZMA2Options lzma2Options = new LZMA2Options();
lzma2Options.setPreset(LZMA2Options.PRESET_MAX);
SevenZMethodConfiguration lzmaConfig =
new SevenZMethodConfiguration(SevenZMethod.LZMA, lzma2Options);
methods.add(lzmaConfig);
out.setContentMethods(methods);
推荐阅读
- android - PhoneGap - 调用了电池状态但状态未定义
- php - 如何使用 php 或 javascript 将流转换为 pdf 或以 pdf 格式显示流?
- java - 关于 Leetcode 算法部分加两个数(求几行解释)
- c# - Hololens:Visual Studio 2017 中没有调试输出
- java - 将数据写入具有许多公式、样式和颜色的 Excel 模板需要太多时间
- java - 在Java中用字符分割单词
- angular - 使用 Angular 供电的 Bootstrap 的单选按钮衣柜故障
- sql - 根据具体情况选择
- java - Apache Ignite IGFS 不使用非堆空间
- java - hibernate 在调用 query.list 方法时抛出 NullPointerException