首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Apache commons压缩的文件大小为7z,远大于p7zip压缩

Apache commons压缩的文件大小为7z,远大于p7zip压缩
EN

Stack Overflow用户
提问于 2018-08-11 03:04:10
回答 1查看 431关注 0票数 2

当我压缩500mb的html文件时,p7zip在几秒钟内就完成了,文件大小是7mb (没有任何自定义设置,只有7z a filename.7z /folder)。

因此,我预计apache commons compress也会使用7z压缩到类似的大小。然而,事实并非如此。即使我为apache commons compress 7z启用了最大预设。由此产生的文件大小也很大,接近100mb。

我做错了什么吗?还是我需要调整我的预设?我已经阅读了apache commons compress,但还没有找到答案。

java实现的相关代码:

代码语言:javascript
复制
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");
    }
}
EN

回答 1

Stack Overflow用户

发布于 2018-08-11 03:18:25

您能试着删除这些行吗:

代码语言:javascript
复制
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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51792678

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档