简体   繁体   中英

Apache commons compress 7z file size way bigger than p7zip compression

When I zip 500mb of html files, p7zip does it in a couple of seconds and the filesize is 7mb (Without any custom settings, just 7z a filename.7z /folder ).

Thus I expected apache commons compress to also compress, using 7z, to a comparable size. This is however, not the case. Even though I enabled the max presets for apache commons compress 7z. The resulting file size is also huge, close to 100mb.

Do I do something wrong or do I need to tune my presets? I have read the apache commons compress wiki but have not found my answers.

Relevant code for the java implementation :

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");
    }
}

Could you please try to remove these lines:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM