Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

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

Closed

Description

@hevav

What version of the package or command are you using?

743ede3

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)

Activity

  1. changed the title [-]Zip{} does not compress files without SelectiveCompression set [/-] [+]Zip{} does not compress files without SelectiveCompression enabled [/+] on Aug 21, 2024
  2. mholt commented on Aug 21, 2024

    @mholt
    Owner

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

  3. hevav commented on Aug 21, 2024

    @hevav
    ContributorAuthor

    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

  4. mholt commented on Aug 21, 2024

    @mholt
    Owner

    Ah I see!

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

  5. hevav commented on Aug 21, 2024

    @hevav
    ContributorAuthor

    See #419

  6. mholt commented on Aug 21, 2024

    @mholt
    Owner

    Thanks for the patch!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ 换网址