Skip to content

Commit

Permalink
Fix reading zip files with max length file comment.
Browse files Browse the repository at this point in the history
If a zip file has a comment that is 65,535 characters long - which is a
valid length and the maximum allowable length - the initial read of the
archive fails to find the End of Central Directory Record and therefore
cannot read the rest of the file.

This commit fixes this by making sure that we look far enough back into
the file from the end to find the EoCDR. Test added to catch
regressions.

Fixes rubyzip#508.
  • Loading branch information
hainesr committed Nov 19, 2021
1 parent bc6523e commit f7cd692
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/zip/central_directory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ class CentralDirectory
END_OF_CDS = 0x06054b50
ZIP64_END_OF_CDS = 0x06064b50
ZIP64_EOCD_LOCATOR = 0x07064b50
MAX_END_OF_CDS_SIZE = 65_536 + 18
STATIC_EOCD_SIZE = 22
ZIP64_STATIC_EOCD_SIZE = 56
MAX_END_OF_CDS_SIZE = 65_535 + STATIC_EOCD_SIZE

attr_reader :comment

Expand Down
Binary file added test/data/max_length_file_comment.zip
Binary file not shown.
5 changes: 5 additions & 0 deletions test/file_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ def test_open_buffer_without_block
assert zf.entries.map(&:name).include?('zippedruby1.rb')
end

def test_open_file_with_max_length_comment
# Should not raise any errors.
Zip::File.open('test/data/max_length_file_comment.zip')
end

def test_cleans_up_tempfiles_after_close
zf = ::Zip::File.new(EMPTY_FILENAME, create: true)
zf.get_output_stream('myFile') do |os|
Expand Down

0 comments on commit f7cd692

Please sign in to comment.