Java apachecommons压缩7z文件大小比p7zip压缩大
当我压缩500mb的html文件时,p7zip只需几秒钟,文件大小为7mb(没有任何自定义设置,只需Java apachecommons压缩7z文件大小比p7zip压缩大,java,zip,compression,apache-commons,7zip,Java,Zip,Compression,Apache Commons,7zip,当我压缩500mb的html文件时,p7zip只需几秒钟,文件大小为7mb(没有任何自定义设置,只需7z a filename.7z/folder) 因此,我希望ApacheCommonsCompress也能使用7z压缩到相当的大小。但事实并非如此。尽管我为apache commons compress 7z启用了最大预设。由此产生的文件大小也很大,接近100mb 我是做错了什么,还是需要调整预设?我已经阅读了ApacheCommonsCompressWiki,但还没有找到我的答案 java实现
7z a filename.7z/folder
)
因此,我希望ApacheCommonsCompress也能使用7z压缩到相当的大小。但事实并非如此。尽管我为apache commons compress 7z启用了最大预设。由此产生的文件大小也很大,接近100mb
我是做错了什么,还是需要调整预设?我已经阅读了ApacheCommonsCompressWiki,但还没有找到我的答案
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");
}
}
公共静态路径压缩(字符串名称、列表文件)引发IOException{
try(SevenZOutputFile out=newsevenzoutputfile(新文件(名称))){
列表方法=新的ArrayList();
LZMA2Options LZMA2Options=新的LZMA2Options();
lzma2Options.setPreset(lzma2Options.PRESET_MAX);
SevenZMethodConfiguration lzmaConfig=
新的SevenZMethodConfiguration(SevenZMethod.LZMA、lzma2Options);
方法:添加(lzmaConfig);
out.setContentMethods(方法);
用于(路径文件:文件){
添加到ArchiveCompression(输出,文件“.”);
}
}
返回路径get(name);
}
私有静态void addToArchiveCompression(SevenZOutputFile输出、路径文件、,
字符串目录)抛出IOException{
String name=dir+File.separator+File.getFileName();
if(Files.isRegularFile(file)){
SevenZArchiveEntry=out.createArchiveEntry(file.toFile(),name);
出档案室(入口);
FileInputStream in=newfileinputstream(file.toFile());
字节[]b=新字节[1024];
整数计数=0;
而((计数=in.read(b))>0){
输出。写入(b,0,计数);
}
out.closeArchiveEntry();
}else if(Files.isDirectory(file)){
File[]children=File.toFile().listFiles();
如果(子项!=null){
用于(文件子项:子项){
addToArchiveCompression(out,path.get(child.toURI()),name);
}
}
}否则{
System.out.println(不支持file.getFileName()+);
}
}
请尝试删除以下行:
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);
List methods=newarraylist();
LZMA2Options LZMA2Options=新的LZMA2Options();
lzma2Options.setPreset(lzma2Options.PRESET_MAX);
SevenZMethodConfiguration lzmaConfig=
新的SevenZMethodConfiguration(SevenZMethod.LZMA、lzma2Options);
方法:添加(lzmaConfig);
out.setContentMethods(方法);
与其深入研究apache,不如看看p7zip源代码,看看它在默认情况下使用了什么。它在sourceforge上。我没有检查,但可能您单独压缩每个文件,而p7zip压缩组合文件(稳定压缩)?