Commit Graph

48 Commits

Author SHA1 Message Date
Robert Haines e83bec471b Update API documentation.
Mostly move things out of the public API by removing them from the docs,
but also add and correct docs where appropriate.
2024-03-02 14:52:24 +00:00
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 2e4dd9e0aa Improve the message for CompressionMethodError.
Convert the compression method number into a meaningful text
representation, e.g., "BZIP2" instead of "12".
2022-08-14 22:23:51 +01: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 8489ab07d1 `OutputStream`: use a `CentralDirectory` object internally.
Now `CentralDirectory` is a bit cleaner it actually makes sense to use
it here instead of an `EntrySet` and comment separately.
2022-01-17 22:32:56 +00:00
Robert Haines e1e1cab39c Fix some non-writable `StringIO`s. 2021-06-27 10:20:11 +01:00
Robert Haines 659db85bff `open` and `write_buffer` in `OutputStream` use named params. 2021-06-27 10:20:11 +01:00
Robert Haines 7ae90be63e Fix Style/OptionalBooleanParameter in `OutputStream`. 2021-06-27 10:20:11 +01:00
Robert Haines e7f0aba5ff Fix Style/OptionalBooleanParameter cop in `Entry`.
Just an internal API so safe, and makes things a lot neater.
2021-06-27 10:20:11 +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 1183607ea1 Flush buffered `OutputStream` on close.
Fixes #265.
2021-06-23 22:24:44 +01:00
Jan-Joost Spanjers cdef4a5187 Prevent directory not empty error when running file_test on Windows
Fixed error:

ZipFileTest#test_open_buffer_no_op_does_not_change_file:
Errno::ENOTEMPTY: Directory not empty @ dir_s_rmdir - D:/a/_temp/d20210605-6612-1yi35sp
    C:/hostedtoolcache/windows/Ruby/2.4.10/x64/lib/ruby/2.4.0/fileutils.rb:1335:in `rmdir'
    C:/hostedtoolcache/windows/Ruby/2.4.10/x64/lib/ruby/2.4.0/fileutils.rb:1335:in `block in remove_dir1'
    C:/hostedtoolcache/windows/Ruby/2.4.10/x64/lib/ruby/2.4.0/fileutils.rb:1349:in `platform_support'
    C:/hostedtoolcache/windows/Ruby/2.4.10/x64/lib/ruby/2.4.0/fileutils.rb:1334:in `remove_dir1'
    C:/hostedtoolcache/windows/Ruby/2.4.10/x64/lib/ruby/2.4.0/fileutils.rb:1327:in `remove'
    C:/hostedtoolcache/windows/Ruby/2.4.10/x64/lib/ruby/2.4.0/fileutils.rb:689:in `block in remove_entry'
    C:/hostedtoolcache/windows/Ruby/2.4.10/x64/lib/ruby/2.4.0/fileutils.rb:1384:in `ensure in postorder_traverse'
    C:/hostedtoolcache/windows/Ruby/2.4.10/x64/lib/ruby/2.4.0/fileutils.rb:1384:in `postorder_traverse'
    C:/hostedtoolcache/windows/Ruby/2.4.10/x64/lib/ruby/2.4.0/fileutils.rb:687:in `remove_entry'
    C:/hostedtoolcache/windows/Ruby/2.4.10/x64/lib/ruby/2.4.0/tmpdir.rb:101:in `mktmpdir'
    D:/a/rubyzip/rubyzip/test/file_test.rb:136:in `test_open_buffer_no_op_does_not_change_file'

Rationale:

File#dup does not behave like what you would expect from #dup on Ruby.
File#dup calls dup(2), which has OS dependant behavoir.

On Windows, calling File#dup seems to cause an extra reference
to an open file, which prevents deleting that file later.

With this commit, we leave out the call to File#dup on Windows.
It is not clear to me that removing this call has no undesired
consequences, but all other existing tests still succeed.
2021-06-06 14:44:20 +01:00
Robert Haines e10badf68e Fix Style/FrozenStringLiteralComment cop. 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 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 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 0790695e8c Clean up OutputStream#init_next_entry.
There's no need for this private method to specify a default.
2020-08-31 17:48:08 +01:00
Sebastian Henke b0ee2683b0 Set buffers to binmode by default 2020-02-19 18:48:13 +01: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 d42c66ce2c Fix Style/MultilineWhenThen cop. 2020-02-15 16:26:32 +00:00
Robert Haines cfe4972e71 Fix Layout/EmptyLineAfterGuardClause cop. 2020-02-15 16:26:32 +00:00
Takumasa Ochi cf91112b57 Apply automatic correction by rubocop 2017-06-29 11:57:12 +09:00
Felix Bünemann 7a4b8bb048 Fix high memory usage due to Deflater buffering
When support for ZipCrypto was added, an internal StringIO buffer was
added to Deflater, in order to fix a decryption bug. While this worked,
it caused unlimited memory growth when compressing large files. The
proper fix is to writer the encryption header in init_next_entry instead
of finalize_current_entry, so the headers are written before any
encrypted data. Because of this fix we can remove the buffering in
Deflater, which keeps memory usage low and allows to stream compressed
data while it is written.

This should fix issue #233.
2015-10-17 17:06:41 +02:00
Pavel Lobashov 1e99ff69b9 Revert "fix rubocop cop Style/ClassCheck". It cause regression in tests
This reverts commit 8c13dfc265.
2015-06-08 10:45:23 +03:00
Pavel Lobashov 8c13dfc265 fix rubocop cop Style/ClassCheck 2015-06-08 10:36:41 +03:00
Pavel Lobashov c0177a455b Merge branch 'master' into rubocop_fixes
Conflicts:
	README.md
	samples/example_recursive.rb
2015-06-08 10:14:25 +03:00
Vít Ondruch 32016ab1bf Remove executable bit. 2015-03-31 13:15:46 +02:00
Pavel Lobashov fd864bd7ab fix rubocop Style/CaseEquality cop 2015-03-24 19:44:47 +03:00
Pavel.Lobashov cb143f0cff fix rubocop Style/SpaceAroundEqualsInParameterDefault cop 2015-03-22 19:45:26 +03:00
Pavel.Lobashov 73e5f70bdf fix rubocop Style/SpaceAfterMethodName cop 2015-03-22 19:43:44 +03:00
Pavel Lobashov b93ef1266f fix rubocop Style/StringLiterals cop 2015-03-21 11:27:44 +03:00
Pavel Lobashov b9a757e045 fix rubocop Style/EmptyLinesAroundClassBody cop 2015-03-21 11:21:26 +03:00
Alexander Simonov 721c1f05ed Merge pull request #201 from mcantor/master
Be smarter about handling buffer file modes
2015-01-17 12:21:32 +02:00
Shigeaki Matsumura 85a7bbdf1a add data descriptor for each entries when encrypto 2015-01-08 18:30:32 +09:00
Johnny Shields c2ecafe770 Change method interfaces to allow encrypter/decrypter to be passed into stream methods 2015-01-04 04:07:51 +09:00
Shigeaki Matsumura c70e5836c7 support traditional encryption 2015-01-01 11:09:17 +09:00
Max Cantor 7c7d786f02 Be smarter about handling buffer file modes. 2014-12-17 16:16:02 -05:00
Orien Madgwick 34f5025d76 default arg for write_buffer
Adding the io parameter to OutputStream::write_buffer breaks backward
compatibility with v1.1.0. Adding a default value reinstates backward
compatibility.
2014-04-06 23:45:19 +10:00
Jeremy Stanley 9e144061b9 fix modifying existing zipfile with zip64 enabled
The local header size computed from the central directory entry
is incorrect due to the Zip64Placeholder in the local entry.
This caused us to seek to the wrong location when copying an
unchanged compressed data stream.

(The same problem could occur when modifying any zip file where
the local header and central directory header contain different
variable-sized fields, so it's a good idea not to trust the CD
to tell us the local header size in any case.)
2014-03-12 15:57:52 -06:00
Alexander Simonov 81c4c4face Fix #135 2014-03-01 00:31:10 +02:00
Alexander Simonov ec81c30382 Fix for #126 and #127 2014-01-24 11:37:38 +02:00
René Sprotte 471aa1599e Add config to set the default compression level. 2014-01-20 14:38:22 +01:00
Alexander Simonov 52efd5cd61 Fix tests. Fix duping StringIO for writing 2014-01-19 14:06:54 +02:00
Alexander Simonov c7f0b17abf Real fix for #119 2014-01-19 13:45:58 +02:00
Jeremy Stanley af165f5cbd Add read/write support for zip64 extensions
This commit adds the capability of creating archives larger than
4GB via zip64 extensions. It also fixes bugs reading archives of
this size (specifically, the 64-bit offset of the local file
header was not being read from the central directory entry).

To maximize compatibility, zip64 extensions are used only when
required. Unfortunately, at the time we write a local file header,
we don't know the size of the file and thus whether a Zip64
Extended Information Extra Field will be required. Therefore
this commit writes a 'placeholder' extra field to reserve space
for the zip64 entry, which will be written if necessary when
we update the local entry with the final sizes and CRC. I use
the signature "\x99\x99" for this field, following the example
of DotNetZip which does the same.

This commit also adds a rake task, zip64_full_test, which
fully tests zip64 by actually creating and verifying a 4GB zip
file. Please note, however, that this test requires UnZip
version 6.00 or newer, which may not be supplied by your OS.
This test doesn't run along with the main unit tests because
it takes a few minutes to complete.
2013-09-27 20:41:00 -06:00
Alexander Simonov 73d9e1c8a0 StingIO support #47 #18 2013-08-29 23:50:12 +03:00
Alexander Simonov 61ce5dbc5f Refactoring part #2
Rubyzip interface was changed!
2013-06-03 10:56:24 +03:00