mholt / archiver

DEPRECATED. Please use mholt/archives instead.
https://github.com/mholt/archives
MIT License
4.45k stars 392 forks source link

Zip{} does not compress files without SelectiveCompression enabled #418

Closed hevav closed 3 months ago

hevav commented 3 months ago

What version of the package or command are you using?

https://github.com/mholt/archiver/commit/743ede3881bb34d43b9ca063232367cbc237daea

What are you trying to do?

I'm trying to make a zip archive with compression.

What steps did you take?

format := archiver.Zip{
    Compression:          zip.Deflate,
}
format.Archive(context.Background(), writer, file)

What did you expect to happen, and what actually happened instead?

Expected: the files should be compressed. Actual: the files are not compressed at all.

How do you think this should be fixed?

We should append the else-clause in zip.go (last 3 lines in the code snippet below):

    if file.IsDir() {
        if !strings.HasSuffix(hdr.Name, "/") {
            hdr.Name += "/" // required
        }
        hdr.Method = zip.Store
    } else if z.SelectiveCompression {
        // only enable compression on compressable files
        ext := strings.ToLower(path.Ext(hdr.Name))
        if _, ok := compressedFormats[ext]; ok {
            hdr.Method = zip.Store
        } else {
            hdr.Method = z.Compression
        }
    } else {
            hdr.Method = z.Compression
    }

Workaround

It works as expected when I enable SelectiveCompression:

format := archiver.Zip{
    SelectiveCompression: true,
    Compression:          zip.Deflate,
}
format.Archive(context.Background(), writer, file)
mholt commented 3 months ago

What are the files (filenames at least)? Enabling SelectiveCompression turns compression off for certain files.

hevav commented 3 months ago

What are the files (filenames at least)?

*.mca (minecraft chunk files).

Enabling SelectiveCompression turns compression off for certain files.

This issue is a bug report - all files are not compressed without SelectiveCompression enabled. I believe this happens because hdr.Method = z.Compression is only set in "if z.SelectiveCompression {}" clause. I think adding

else {
     hdr.Method = z.Compression
}

should help

mholt commented 3 months ago

Ah I see!

Would you like to submit a pull request and get credited for the commit?

hevav commented 3 months ago

See #419

mholt commented 3 months ago

Thanks for the patch!