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

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

Closed
hevav opened this issue Aug 21, 2024 · 5 comments
Closed

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

hevav opened this issue Aug 21, 2024 · 5 comments

Comments

@hevav
Copy link
Contributor

hevav commented Aug 21, 2024

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)
@hevav hevav changed the title Zip{} does not compress files without SelectiveCompression set Zip{} does not compress files without SelectiveCompression enabled Aug 21, 2024
@mholt
Copy link
Owner

mholt commented Aug 21, 2024

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

@hevav
Copy link
Contributor Author

hevav commented Aug 21, 2024

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
Copy link
Owner

mholt commented Aug 21, 2024

Ah I see!

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

@hevav
Copy link
Contributor Author

hevav commented Aug 21, 2024

See #419

@mholt
Copy link
Owner

mholt commented Aug 21, 2024

Thanks for the patch!

@mholt mholt closed this as completed Aug 21, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants