Skip to content

Commit

Permalink
Make sure that compression method is STORE for level 0.
Browse files Browse the repository at this point in the history
Whatever the compression method that is set by the user, if the
compression level is set to 0 (no compression), then the entry should be
STORED. This mimics commandline tool behaviour and matches user
expectations.
  • Loading branch information
hainesr committed Jun 20, 2020
1 parent d457600 commit 9ea7201
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/zip/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ def time=(value)
end

def compression_method
@ftype == :directory ? STORED : @compression_method
return STORED if @ftype == :directory || @compression_level == 0

@compression_method
end

def compression_method=(method)
Expand Down
21 changes: 21 additions & 0 deletions test/entry_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,25 @@ def test_compression_level_flags
e_dir = Zip::Entry.new('', 'd/', '', Zip::Entry::DEFLATED, 1)
assert_equal(0, e_dir.gp_flags & 0b110)
end

def test_compression_method_reader
[
[Zip.default_compression, Zip::Entry::DEFLATED],
[0, Zip::Entry::STORED],
[1, Zip::Entry::DEFLATED],
[9, Zip::Entry::DEFLATED]
].each do |level, method|
# Check that the correct method is returned when DEFLATED is specified.
entry = Zip::Entry.new('', '', '', Zip::Entry::DEFLATED, level)
assert_equal(method, entry.compression_method)
end

# Check that the correct method is returned when STORED is specified.
entry = Zip::Entry.new('', '', '', Zip::Entry::STORED, 1)
assert_equal(Zip::Entry::STORED, entry.compression_method)

# Check that directories are always STORED, whatever level is specified.
entry = Zip::Entry.new('', 'd/', '', Zip::Entry::DEFLATED, 1)
assert_equal(Zip::Entry::STORED, entry.compression_method)
end
end

0 comments on commit 9ea7201

Please sign in to comment.