Commit Graph

97 Commits

Author SHA1 Message Date
Robert Haines 1c06454985 Update minimum ruby version to 3.0.
All rubies before 3.0 are EOL and this is a major version bump, so it's
the right time to do this.
2024-03-01 22:14:48 +00:00
Robert Haines f460da3afb Prevent unnecessary Zip64 data being stored.
With Zip64 write support enabled by default, it's important that we
only store the extra data when we need to. This commit ensures that
the Zip64 extra data is included for an entry if its size is over
4GB, or if we don't know how big it will be at the point of writing
the local header data.

This commit also removes the need for the Zip64Placeholder extra
data field. Now we just use the Zip64 field itself and ensure it's
filled in correctly.
2023-01-03 20:19:40 +00:00
Robert Haines 7097492dc8 Define the EntryExistsError message within the error class. 2022-08-14 22:23:51 +01:00
Robert Haines 03a9ee6b8a Rename `GPFBit3Error` to `StreamingError`.
`GPFBit3Error` doesn't really mean anything to the general user, and
it's not descriptive of the issue at hand. This error is raised when a
zip file cannot be streamed via `InputStream`, so `StreamingError` makes
more sense.

Also standardize the error message while we're about it.
2022-08-14 22:23:51 +01:00
Robert Haines 708b7f5393 Add a couple more checks in the tests for double `commit`s.
Just ensure that a `commit` really does stick with both new and edited
zip files.
2022-06-25 08:53:35 +01:00
Robert Haines 14ff11ba05 Re-initialize CDir after a `commit`.
Using the factored-out code preserves options set in `File`.

Fixes #529.
2022-06-25 08:51:32 +01:00
Robert Haines 48d6acf9ca Ensure all streams passed to `File.new` are in `binmode`.
Previously, only those streams that were passed to `new` by `open_buffer`
were in the correct mode.
2022-06-18 16:19:52 +01:00
Robert Haines 513ce5e5f7 Remove unnecessary encoding change in tests for `File`. 2022-06-18 12:45:59 +01:00
Robert Haines 05a1739069 Properly test `File#mkdir`. 2022-01-22 08:39:43 +00:00
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