Commit Graph

88 Commits

Author SHA1 Message Date
Robert Haines e2e0e23763 Remove `File::add_buffer` from the API.
Its functionality is now replicated in `File::open_buffer` but in a more
secure way.
2022-01-22 07:34:00 +00:00
Robert Haines 044759f502 Fix `OutputStream#put_next_entry` to preserve `StreamableStream`s.
When passing an `Entry` type to `File#get_output_stream` the entry is
used to create a `StreamableStream`, which preserves all the info in the
entry, such as timestamp, etc. But then in `put_next_entry` all that is
lost due to the test for `kind_of?(Entry)` which a `StreamableStream` is
not. See #503 for details.

This change tests for `StreamableStream`s in `put_next_entry` and uses
them directly. Some set-up within `Entry` needed to be made more robust
to cope with this, but otherwise it's a low impact change, which does
fix the problem.

The reason this case was being missed before is that the tests weren't
testing `get_output_stream` with an `Entry` object, so I have also added
that test too.

Fixes #503.
2022-01-20 19:29:40 +00:00
Robert Haines 75503df682 Round out the max comment size tests.
Just sanity check the comment size and the number of entries once the
file has been initialized.
2022-01-17 18:03:04 +00:00
Robert Haines 9c3f8254c7 Fix reading zip64 files with max length file comment.
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 Zip64 End of Central Directory Locator 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 this locator, and then use the
information in it to find the Zip64 End of Central Directory Record.

Test added to catch regressions.

Fixes #509.
2022-01-17 18:02:39 +00:00
Robert Haines 14b63f68db Ensure `File.open_buffer` doesn't rewrite unchanged data. 2021-11-30 22:22:37 +00:00
Robert Haines f5e19db273 Add a 100,000 file zip to test `count_entries`. 2021-11-20 20:02:47 +00:00
Robert Haines 22e47641e6 Add `File::count_entries`.
This method provides a short cut to finding out how many entries are in
an archive by reading this number directly from the central directory,
and not iterating through the entire set of entries.
2021-11-20 10:53:00 +00:00
Robert Haines f7cd692e15 Fix reading zip files with max length file comment.
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 #508.
2021-11-19 19:35:36 +00:00
Robert Haines 19e5f4a8ce Detect and raise GPFBit3Error in `InputStream.get_next_entry`.
We were previously trying to work out where the next entry would be,
even with GP bit 3 set, but the logic was flaky and cannot really be
correct given the data available. It's not expected behaviour, so raise
the error instead.

This means that we get rid of the incorrect `Entry.data_descriptor_size`
which was doing more harm than good.
2021-06-27 21:43:03 +01:00
Robert Haines f75eb61578 Use named parameters for `File#get_output_stream`. 2021-06-27 10:20:11 +01:00
Robert Haines f033ae760d Use named parameters for `File::new`.
This is a breaking change, but now is the time to do this as we've
already done the same for `Entry::new`.
2021-06-27 10:20:11 +01:00
Robert Haines e1e1cab39c Fix some non-writable `StringIO`s. 2021-06-27 10:20:11 +01:00
Robert Haines f1e8c2fc9d Fix Style/StringConcatenation cop. 2021-06-18 16:10:57 +01:00
Jan-Joost Spanjers 8a24bff1b2 Disable recover file permissions test on Windows 2021-06-06 14:44:20 +01:00
Jan-Joost Spanjers c3443c06ea Make recover file permissions test better understandable 2021-06-06 14:44:20 +01:00
Jan-Joost Spanjers 0051d5bb1f Read/write test files in binay mode (for Windows compatibility) 2021-06-06 14:44:20 +01:00
Robert Haines dea45613bb Test non-block version of File#get_output_stream. 2021-05-25 22:23:39 +01:00
Robert Haines ed21f9cf17 Fix Performance/StringInclude cop. 2021-05-25 21:24:50 +01:00
Robert Haines e2c16991e5 Fix Style/Dir cop. 2021-05-25 21:24:50 +01:00
Robert Haines deac4fa313 Fix Style/CommentAnnotation cop. 2021-05-25 21:24:50 +01:00
Robert Haines e10badf68e Fix Style/FrozenStringLiteralComment cop. 2021-05-25 21:24:50 +01:00
Taichi Ishitani 0e4dc676a0 fix frozen string literal error 2021-05-25 21:24:50 +01:00
Robert Haines 9da6be98d8 Fix the compression level tests to be relative.
Made little sense to use hardcoded bytes sizes; the tests end up too
brittle.
2021-02-16 13:21:24 +00:00
Robert Haines e4ceedaa58 Use keyword arguments for the `Entry` initializer.
This greatly simplifies the creation of `Entry` objects when only a
couple of fields are not set to their defaults, while at the same time
allowing an `Entry` to be fully configured at creation time if
appropriate.

This fundamentally changes the `Entry` API and  means that some
convenience methods in `OutputStream` and `File` have needed to be
refactored.
2020-08-31 17:48:08 +01:00
Robert Haines d4bc24dcb3 Clean up `OutputStream` internals.
There was some fairly odd stuff going on in `put_next_entry` that
allowed for data within an `Entry` to be overridden and prevented an
`Entry` from being a single point of truth. Fixing this also simplifies
the code within `File` and still passes all tests.

Also, fixing the above means we can stop passing the compression level
around as a parameter and use the value stored in each `Entry` directly.

Let's keep `compression_level` out of the `Entry` public API though as
it only makes sense when writing an `Entry`: there doesn't seem to be an
obvious way to read what level of compression was used when reading an
`Entry` from a zip file.
2020-08-31 17:48:08 +01:00
Robert Haines 14451e63e7 Add setting a compression level to the File options.
It looks like it needs to be surfaced in `add` and `get_output_stream`.
The compression level defaults to whatever the global default is unless
it is overridden on opening the Zip::File.

Also needed to reorder some of the requires in the top-level module file
now that we are using defaults in the File class.
2020-08-31 17:48:08 +01:00
Robert Haines ef520b4b94 Add `compression_level` to the Entry API.
Allow an Entry to specify a compression level and pass this down to the
underlying OutputStream infrastructure. OutputStream has been able to
specify a compression level for a while but this has, up until now, only
ever been set to the default.

This fundamentally changes the API so will need a major version bump.
2020-08-31 17:48:08 +01:00
Robert Haines e6f414f539 Fix Naming/VariableName cop in the tests. 2020-02-29 18:11:52 +00:00
Robert Haines b09f05d8d3 Fix Naming/MethodParameterName cop in the tests. 2020-02-29 18:11:52 +00:00
Robert Haines 0df6cb3059 Fix Style/SymbolProc cop. 2020-02-15 16:26:32 +00:00
Robert Haines 2e11a88fd2 Fix Style/StringLiterals cop. 2020-02-15 16:26:32 +00:00
Robert Haines b3c4c37882 Fix Style/NonNilCheck cop.
Use the `refute_nil` method for most of these.
2020-02-15 16:26:32 +00:00
Robert Haines cfe4972e71 Fix Layout/EmptyLineAfterGuardClause cop. 2020-02-15 16:26:32 +00:00
Robert Haines cd065d0186 Fix Lint/UnusedBlockArgument cop. 2020-02-15 16:26:32 +00:00
Robert Haines 3a3ac6feb7 Fix Style/Semicolon cop. 2020-02-15 16:26:32 +00:00
Robert Haines f3a2f4a8ec Add tests for File#get/find_entry. 2019-10-31 18:15:17 +00:00
John Lees-Miller 93505ca16f Check expected entry size in add_stored test 2019-09-15 14:58:13 +01:00
Robert Haines ecb2776218 Zip::File.add_stored() to add uncompressed files.
Adding uncompressed files to a zip archive can be overly complex, so
this convenience method makes it easier.
2019-09-14 13:58:30 +01:00
John Lees-Miller 9a41ce65c4 Add more explicit test for #280 2019-09-06 17:58:38 +01:00
John Lees-Miller eeef5073d5 Add test case based on #146 2019-09-05 19:00:34 +01:00
John Lees-Miller 7fbaf1e6c0
Merge pull request #360 from hainesr/fix-open-buffer
Fix #280 - `open_buffer` mangles the content of the buffer it is given.
2019-09-05 18:48:39 +01:00
Oleksandr Simonov 8887b70349
Merge pull request #361 from hainesr/quiet-tests
Turn off all terminal output in all tests.
2018-04-23 22:18:31 +03:00
Robert Haines cd7bb142a4 Turn off all terminal output in all tests.
Makes things a lot easier when trying to track down bugs.
2018-04-04 19:45:54 +01:00
Robert Haines 84c208982f Switch newly created StringIOs to binmode.
StringIO objects created within File.open_buffer were not being switched into
binmode, but those passed in were. Fix this inconsistency and add a test.
2018-04-04 16:02:19 +01:00
Robert Haines 15ccc25da1 Fix File.open_buffer when no changes are made.
Things are now more carefully set up, and if a buffer is passed in which
represents a file that already exists then this is taken into account. All
initialization is now done in File.new, rather than being split between there
and File.open_buffer.

This has also needed a bit of a re-write of Zip::File.initialize. I've tried to
bring some logic to it as a result, and have added comments to explain what is
now happening.
2018-04-04 15:54:12 +01:00
Andrew Meyer c787d94852 Handle stored files with general purpose bit 3 set
Signed-off-by: Sam Coward <scoward@pivotal.io>
2018-04-03 16:07:18 -04:00
Phil 792266dbf3 Added fix for calling 'close' on a StringIO-backed zip file, and specs 2018-02-21 11:48:56 -08:00
Takumasa Ochi cf91112b57 Apply automatic correction by rubocop 2017-06-29 11:57:12 +09:00
mwlang 14b8fed18c allows open_buffer to work without a given block 2017-01-16 13:24:22 -06:00
David Rodríguez f157dfc1b6 Revert "Cleanup after ourselves"
This reverts commit 6597af1257 since
7caab1f already fixed the same thing. It should fix the build.
2016-11-10 08:23:34 -02:00