Commit Graph

106 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 2ffbfebb88 Document or hide classes from the docs. 2023-04-14 11:25:22 +01:00
Robert Haines 84087e5774 Ensure that entries can be extracted safely without path traversal.
This commit adds a parameter to the `File#extract` and `Entry#extract` methods
so that a base destination directory can be specified for extracting archives
in bulk to somewhere in the filesystem that isn't the current working
directory. This directory is `.` by default. It is combined with the entry
path - which shouldn't but could have relative directories (e.g. `..`) in it -
and tested for safety before extracting.

Resolves #540.
2023-04-14 11:15:24 +01:00
Robert Haines 7097492dc8 Define the EntryExistsError message within the error class. 2022-08-14 22:23:51 +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 c243b4429a Factor out the code in `File` to init the CDir.
This allows us to reuse it without overwriting any options passed to `File`.
2022-06-25 08:48:23 +01:00
Robert Haines 5cd1ef2910 Use new dirty statuses to detect zip file changes.
This also means that we no longer need to keep a copy of the original
set of `Entry`s or the central directory comment to test for changes.

For situations where a zip file has a lot of entries (e.g. #506) this
means we save a lot of memory, and a lot of time constructing the zip
file in memory.
2022-06-20 17:18:20 +01:00
Robert Haines e0e754ae65 Switch how the `Entry::dirty` flag is used.
Set it to true by default - because a new `Entry` is dirty by
definition, having not been written yet. Then make sure that an `Entry`
that is created by reading from a zip file is set as not dirty.
2022-06-20 17:18:19 +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 31e6688528 Remove unused private method `File#directory?`.
This was a fairly horrible method anyway, for a number of reasons. It
looked like a method that tested whether a name was a 'directory' name
or not, and it did, but it also had some side effects where it would
convert it *to* a directory name in some cases as well. Thankfully,
nothing was using it any more, and as it was private we can lose it
safely. Gone.
2022-01-22 07:38:18 +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 1d6bfb7e69 Expose the `EntrySet` more cleanly through `CentralDirectory`.
There is now no direct access to the set of entries in a central
directory. This makes the interface cleaner because we now, for example,
add/delete things directly to/from the central directory, rather than
to/from the entry set contained within the central directory.
2022-01-16 11:53:40 +00:00
Robert Haines 34731b1885 `Zip::File` no longer subclasses `Zip::CentralDirectory`.
It has bothered me for years that the central directory is exposed in
this way. A zip file should *have* a central directory, but it should
not *be* one.

This commit starts us down the path of properly separating the two.
2022-01-15 13:10:54 +00:00
Robert Haines cf258bbb71 Move to ruby 2.5 as the earliest supported version.
2.4 is nearly two years beyond EOL now.

Closes #484.
2022-01-11 22:26:09 +00:00
Robert Haines 14b63f68db Ensure `File.open_buffer` doesn't rewrite unchanged data. 2021-11-30 22:22:37 +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 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 8699e356d4 Improve documentation for `File.glob`.
Closes #338.
2021-06-26 20:04:17 +01:00
Robert Haines 193507b15a Adjust Layout/LineLength cop to 100 characters.
We'll get the line length down in stages...
2021-06-25 22:31:34 +01:00
Robert Haines bd2f15e4bb Extract the `Zip::File::split` code into its own module.
This code is rarely used and may not even be correct according to the
standard. Also this de-clutters the `File` class.
2021-06-12 16:29:06 +01:00
Robert Haines a6c6345084 Set restoring permissions and times as the default. 2021-06-06 16:17:22 +01:00
Robert Haines 684b69f330 Move the restore options to the top level.
This will ensure consistency between `File` and `Entry`.
2021-06-06 16:17:22 +01:00
Robert Haines ce08405c1a Fix (most) Style/MutableConstant cop errors.
The last one, in `ExtraField` needs a sizeable refactor to fix.
2021-05-25 21:50:06 +01:00
Robert Haines 606b5ffbb2 Fix Lint/EmptyBlock 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 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 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 f9b161eb32 Fix Naming/VariableName cop in the library code. 2020-02-29 18:11:52 +00:00
Robert Haines aa6ea05d45 Fix Naming/MethodParameterName cop in the library code. 2020-02-29 18:11:52 +00:00
Robert Haines 846e704048 Fix Naming/BlockParameterName cop. 2020-02-17 22:35:08 +00:00
Robert Haines 5ce4e13ddd Configure and fix Style/ClassCheck cop. 2020-02-15 16:26:32 +00:00
Robert Haines 6cab5922bc Configure and fix Metrics/LineLength cop.
Set a workable line length for now, and fix a couple of particularly bad
examples.

Also, turn off for the tests.
2020-02-15 16:26:32 +00:00
Robert Haines 0df6cb3059 Fix Style/SymbolProc cop. 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 98c6969c18 Fix Layout/SpaceAroundOperators cop. 2020-02-15 16:26:32 +00:00
Robert Haines 73e405acef Fix Security/Open cop errors. 2020-02-15 15:56:17 +00:00
Jan-Joost Spanjers 4e28f7286c Untangle encryption and decompression 2020-01-26 15:34:17 +01:00
Robert Haines 2d6b6e024b Ensure File#get/find_entry work consistently.
I have switched around the logic somewhat so that `get_entry` calls
`find_entry` and raises an exception if it gets `nil` back.
2019-10-31 18:12:18 +00:00
Robert Haines 378293539d Make the attr_accessors in File more readable.
Note what the default is and that a couple of them will change at some
point soon.
2019-10-19 20:46:21 +01:00
Robert Haines 253457545e Correctly set/default options in the File class.
Fixes #395. Set the options to false for now for consistency.
2019-10-19 20:45:29 +01:00
Robert Haines 3fea9958c2 Update comment for `Zip::File.open`. 2019-10-19 07:15:51 +01:00
Robert Haines f58e38012c Add the `options` parameter to `Zip::File.open`. 2019-10-19 07:04:04 +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
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
Robert Haines 03633933eb No need to require stringio in Zip::File.open_buffer.
It's already required in zip.rb.
2018-04-04 14:31:34 +01:00