rubyzip 2.4.1 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Changelog.md +444 -0
- data/LICENSE.md +24 -0
- data/README.md +136 -39
- data/Rakefile +11 -7
- data/lib/zip/central_directory.rb +169 -123
- data/lib/zip/compressor.rb +3 -1
- data/lib/zip/constants.rb +29 -21
- data/lib/zip/crypto/aes_encryption.rb +119 -0
- data/lib/zip/crypto/decrypted_io.rb +20 -5
- data/lib/zip/crypto/encryption.rb +4 -2
- data/lib/zip/crypto/null_encryption.rb +6 -4
- data/lib/zip/crypto/traditional_encryption.rb +8 -6
- data/lib/zip/decompressor.rb +4 -3
- data/lib/zip/deflater.rb +12 -8
- data/lib/zip/dirtyable.rb +32 -0
- data/lib/zip/dos_time.rb +43 -4
- data/lib/zip/entry.rb +373 -249
- data/lib/zip/entry_set.rb +11 -9
- data/lib/zip/errors.rb +136 -16
- data/lib/zip/extra_field/aes.rb +45 -0
- data/lib/zip/extra_field/generic.rb +6 -13
- data/lib/zip/extra_field/ntfs.rb +6 -4
- data/lib/zip/extra_field/old_unix.rb +3 -1
- data/lib/zip/extra_field/universal_time.rb +3 -1
- data/lib/zip/extra_field/unix.rb +5 -3
- data/lib/zip/extra_field/unknown.rb +33 -0
- data/lib/zip/extra_field/zip64.rb +12 -5
- data/lib/zip/extra_field.rb +17 -22
- data/lib/zip/file.rb +167 -264
- data/lib/zip/file_split.rb +91 -0
- data/lib/zip/filesystem/dir.rb +86 -0
- data/lib/zip/filesystem/directory_iterator.rb +48 -0
- data/lib/zip/filesystem/file.rb +262 -0
- data/lib/zip/filesystem/file_stat.rb +110 -0
- data/lib/zip/filesystem/zip_file_name_mapper.rb +81 -0
- data/lib/zip/filesystem.rb +27 -596
- data/lib/zip/inflater.rb +7 -5
- data/lib/zip/input_stream.rb +65 -52
- data/lib/zip/ioextras/abstract_input_stream.rb +16 -11
- data/lib/zip/ioextras/abstract_output_stream.rb +13 -3
- data/lib/zip/ioextras.rb +7 -7
- data/lib/zip/null_compressor.rb +3 -1
- data/lib/zip/null_decompressor.rb +3 -1
- data/lib/zip/null_input_stream.rb +3 -1
- data/lib/zip/output_stream.rb +55 -56
- data/lib/zip/pass_thru_compressor.rb +3 -1
- data/lib/zip/pass_thru_decompressor.rb +4 -2
- data/lib/zip/streamable_directory.rb +3 -1
- data/lib/zip/streamable_stream.rb +3 -0
- data/lib/zip/version.rb +4 -1
- data/lib/zip.rb +24 -22
- data/rubyzip.gemspec +39 -0
- data/samples/example.rb +8 -3
- data/samples/example_filesystem.rb +3 -2
- data/samples/example_recursive.rb +3 -1
- data/samples/gtk_ruby_zip.rb +4 -2
- data/samples/qtzip.rb +6 -5
- data/samples/write_simple.rb +2 -1
- data/samples/zipfind.rb +1 -0
- metadata +87 -49
- data/TODO +0 -15
- data/lib/zip/extra_field/zip64_placeholder.rb +0 -15
data/README.md
CHANGED
@@ -2,23 +2,21 @@
|
|
2
2
|
|
3
3
|
[](http://badge.fury.io/rb/rubyzip)
|
4
4
|
[](https://github.com/rubyzip/rubyzip/actions/workflows/tests.yml)
|
5
|
+
[](https://github.com/rubyzip/rubyzip/actions/workflows/lint.yml)
|
5
6
|
[](https://codeclimate.com/github/rubyzip/rubyzip)
|
6
7
|
[](https://coveralls.io/r/rubyzip/rubyzip?branch=master)
|
7
8
|
|
8
9
|
Rubyzip is a ruby library for reading and writing zip files.
|
9
10
|
|
10
|
-
## Important
|
11
|
-
|
12
|
-
Rubyzip 2.4 is intended to be the last release in the 2.x series. Please get ready for version 3.0.
|
11
|
+
## Important notes
|
13
12
|
|
14
13
|
### Updating to version 3.0
|
15
14
|
|
16
|
-
The public API of some classes has been modernized to use named parameters for optional arguments.
|
15
|
+
The public API of some classes has been modernized to use named parameters for optional arguments. Please check your usage of the following Rubyzip classes:
|
17
16
|
* `File`
|
18
17
|
* `Entry`
|
19
18
|
* `InputStream`
|
20
19
|
* `OutputStream`
|
21
|
-
* `DOSTime`
|
22
20
|
|
23
21
|
**Please see [Updating to version 3.x](https://github.com/rubyzip/rubyzip/wiki/Updating-to-version-3.x) in the wiki for details.**
|
24
22
|
|
@@ -57,7 +55,7 @@ input_filenames = ['image.jpg', 'description.txt', 'stats.csv']
|
|
57
55
|
|
58
56
|
zipfile_name = "/Users/me/Desktop/archive.zip"
|
59
57
|
|
60
|
-
Zip::File.open(zipfile_name,
|
58
|
+
Zip::File.open(zipfile_name, create: true) do |zipfile|
|
61
59
|
input_filenames.each do |filename|
|
62
60
|
# Two arguments:
|
63
61
|
# - The name of the file as it will appear in the archive
|
@@ -96,7 +94,7 @@ class ZipFileGenerator
|
|
96
94
|
def write
|
97
95
|
entries = Dir.entries(@input_dir) - %w[. ..]
|
98
96
|
|
99
|
-
::Zip::File.open(@output_file,
|
97
|
+
::Zip::File.open(@output_file, create: true) do |zipfile|
|
100
98
|
write_entries entries, '', zipfile
|
101
99
|
end
|
102
100
|
end
|
@@ -129,9 +127,9 @@ class ZipFileGenerator
|
|
129
127
|
end
|
130
128
|
```
|
131
129
|
|
132
|
-
### Save zip archive entries
|
130
|
+
### Save zip archive entries sorted by name
|
133
131
|
|
134
|
-
To save zip archives
|
132
|
+
To save zip archives with their entries sorted by name (see below), set `::Zip.sort_entries` to `true`
|
135
133
|
|
136
134
|
```
|
137
135
|
Vegetable/
|
@@ -145,7 +143,7 @@ fruit/mango
|
|
145
143
|
fruit/orange
|
146
144
|
```
|
147
145
|
|
148
|
-
|
146
|
+
Opening an existing zip file with this option set will not change the order of the entries automatically. Altering the zip file - adding an entry, renaming an entry, adding or changing the archive comment, etc - will cause the ordering to be applied when closing the file.
|
149
147
|
|
150
148
|
### Default permissions of zip archives
|
151
149
|
|
@@ -181,28 +179,79 @@ Zip::File.open('foo.zip') do |zip_file|
|
|
181
179
|
end
|
182
180
|
```
|
183
181
|
|
184
|
-
|
182
|
+
### Notes on `Zip::InputStream`
|
183
|
+
|
184
|
+
`Zip::InputStream` can be used for faster reading of zip file content because it does not read the Central directory up front.
|
185
|
+
|
186
|
+
There is one exception where it can not work however, and this is if the file does not contain enough information in the local entry headers to extract an entry. This is indicated in an entry by the General Purpose Flag bit 3 being set.
|
187
|
+
|
188
|
+
> If bit 3 (0x08) of the general-purpose flags field is set, then the CRC-32 and file sizes are not known when the header is written. The fields in the local header are filled with zero, and the CRC-32 and size are appended in a 12-byte structure (optionally preceded by a 4-byte signature) immediately after the compressed data.
|
189
|
+
|
190
|
+
If `Zip::InputStream` finds such an entry in the zip archive it will raise an exception (`Zip::StreamingError`).
|
191
|
+
|
192
|
+
`Zip::InputStream` is not designed to be used for random access in a zip file. When performing any operations on an entry that you are accessing via `Zip::InputStream.get_next_entry` then you should complete any such operations before the next call to `get_next_entry`.
|
185
193
|
|
186
|
-
|
194
|
+
```ruby
|
195
|
+
zip_stream = Zip::InputStream.new(File.open('file.zip'))
|
187
196
|
|
188
|
-
|
197
|
+
while entry = zip_stream.get_next_entry
|
198
|
+
# All required operations on `entry` go here.
|
199
|
+
end
|
200
|
+
```
|
189
201
|
|
190
|
-
|
202
|
+
Any attempt to move about in a zip file opened with `Zip::InputStream` could result in the incorrect entry being accessed and/or Zlib buffer errors. If you need random access in a zip file, use `Zip::File`.
|
191
203
|
|
192
|
-
|
204
|
+
### Password Protection (experimental)
|
193
205
|
|
194
|
-
|
206
|
+
Rubyzip supports reading zip files with AES encryption (version 3.1 and later), and reading and writing zip files with traditional zip encryption (a.k.a. "ZipCrypto"). Encryption is currently only available with the stream API, with either files or buffers, e.g.:
|
195
207
|
|
196
|
-
|
208
|
+
#### Version 2.x (ZipCrypto only)
|
197
209
|
|
198
210
|
```ruby
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
211
|
+
# Writing.
|
212
|
+
enc = Zip::TraditionalEncrypter.new('password')
|
213
|
+
buffer = Zip::OutputStream.write_buffer(::StringIO.new(''), enc) do |output|
|
214
|
+
output.put_next_entry("my_file.txt")
|
215
|
+
output.write my_data
|
216
|
+
end
|
217
|
+
|
218
|
+
# Reading.
|
219
|
+
dec = Zip::TraditionalDecrypter.new('password')
|
220
|
+
Zip::InputStream.open(buffer, 0, dec) do |input|
|
221
|
+
entry = input.get_next_entry
|
222
|
+
puts "Contents of '#{entry.name}':"
|
223
|
+
puts input.read
|
224
|
+
end
|
203
225
|
```
|
204
226
|
|
205
|
-
|
227
|
+
#### Version 3.x (AES reading and ZipCrypto read/write)
|
228
|
+
|
229
|
+
```ruby
|
230
|
+
# Reading AES, version 3.1 and later.
|
231
|
+
dec = Zip::AESDecrypter.new('password', Zip::AESEncryption::STRENGTH_256_BIT)
|
232
|
+
Zip::InputStream.open('aes-encrypted-file.zip', decrypter: dec) do |input|
|
233
|
+
entry = input.get_next_entry
|
234
|
+
puts "Contents of '#{entry.name}':"
|
235
|
+
puts input.read
|
236
|
+
end
|
237
|
+
|
238
|
+
# Writing.
|
239
|
+
enc = Zip::TraditionalEncrypter.new('password')
|
240
|
+
buffer = Zip::OutputStream.write_buffer(encrypter: enc) do |output|
|
241
|
+
output.put_next_entry("my_file.txt")
|
242
|
+
output.write my_data
|
243
|
+
end
|
244
|
+
|
245
|
+
# Reading.
|
246
|
+
dec = Zip::TraditionalDecrypter.new('password')
|
247
|
+
Zip::InputStream.open(buffer, decrypter: dec) do |input|
|
248
|
+
entry = input.get_next_entry
|
249
|
+
puts "Contents of '#{entry.name}':"
|
250
|
+
puts input.read
|
251
|
+
end
|
252
|
+
```
|
253
|
+
|
254
|
+
_This is an evolving feature and the interface for encryption may change in future versions._
|
206
255
|
|
207
256
|
## Known issues
|
208
257
|
|
@@ -216,7 +265,7 @@ buffer = Zip::OutputStream.write_buffer do |out|
|
|
216
265
|
unless [DOCUMENT_FILE_PATH, RELS_FILE_PATH].include?(e.name)
|
217
266
|
out.put_next_entry(e.name)
|
218
267
|
out.write e.get_input_stream.read
|
219
|
-
|
268
|
+
end
|
220
269
|
end
|
221
270
|
|
222
271
|
out.put_next_entry(DOCUMENT_FILE_PATH)
|
@@ -296,25 +345,37 @@ Zip.validate_entry_sizes = false
|
|
296
345
|
|
297
346
|
Note that if you use the lower level `Zip::InputStream` interface, `rubyzip` does *not* check the entry `size`s. In this case, the caller is responsible for making sure it does not read more data than expected from the input stream.
|
298
347
|
|
299
|
-
###
|
348
|
+
### Compression level
|
349
|
+
|
350
|
+
When adding entries to a zip archive you can set the compression level to trade-off compressed size against compression speed. By default this is set to the same as the underlying Zlib library's default (`Zlib::DEFAULT_COMPRESSION`), which is somewhere in the middle.
|
300
351
|
|
301
|
-
You can
|
352
|
+
You can configure the default compression level with:
|
302
353
|
|
303
354
|
```ruby
|
304
|
-
Zip.default_compression =
|
355
|
+
Zip.default_compression = X
|
305
356
|
```
|
306
357
|
|
307
|
-
|
358
|
+
Where X is an integer between 0 and 9, inclusive. If this option is set to 0 (`Zlib::NO_COMPRESSION`) then entries will be stored in the zip archive uncompressed. A value of 1 (`Zlib::BEST_SPEED`) gives the fastest compression and 9 (`Zlib::BEST_COMPRESSION`) gives the smallest compressed file size.
|
359
|
+
|
360
|
+
This can also be set for each archive as an option to `Zip::File`:
|
361
|
+
|
362
|
+
```ruby
|
363
|
+
Zip::File.open('foo.zip', create:true, compression_level: 9) do |zip|
|
364
|
+
zip.add ...
|
365
|
+
end
|
366
|
+
```
|
308
367
|
|
309
368
|
### Zip64 Support
|
310
369
|
|
311
|
-
|
370
|
+
Since version 3.0, Zip64 support is enabled for writing by default. To disable it do this:
|
312
371
|
|
313
372
|
```ruby
|
314
|
-
Zip.write_zip64_support =
|
373
|
+
Zip.write_zip64_support = false
|
315
374
|
```
|
316
375
|
|
317
|
-
|
376
|
+
Prior to version 3.0, Zip64 support is disabled for writing by default.
|
377
|
+
|
378
|
+
_NOTE_: If Zip64 write support is enabled then any extractor subsequently used may also require Zip64 support to read from the resultant archive.
|
318
379
|
|
319
380
|
### Block Form
|
320
381
|
|
@@ -329,15 +390,50 @@ You can set multiple settings at the same time by using a block:
|
|
329
390
|
end
|
330
391
|
```
|
331
392
|
|
393
|
+
## Compatibility
|
394
|
+
|
395
|
+
Rubyzip is known to run on a number of platforms and under a number of different Ruby versions.
|
396
|
+
|
397
|
+
### Version 2.4.x
|
398
|
+
|
399
|
+
Rubyzip 2.4 is known to work on MRI 2.4 to 3.4 on Linux and Mac, and JRuby and Truffleruby on Linux. There are known issues with Windows which have been fixed on the development branch. Please [let us know](https://github.com/rubyzip/rubyzip/pulls) if you know Rubyzip 2.4 works on a platform/Ruby combination not listed here, or [raise an issue](https://github.com/rubyzip/rubyzip/issues) if you see a failure where we think it should work.
|
400
|
+
|
401
|
+
### Version 3.x
|
402
|
+
|
403
|
+
Please see the table below for what we think the current situation is. Note: an empty cell means "unknown", not "does not work".
|
404
|
+
|
405
|
+
| OS/Ruby | 3.0 | 3.1 | 3.2 | 3.3 | 3.4 | Head | JRuby 10.0.1.0 | JRuby Head | Truffleruby 24.2.1 | Truffleruby Head |
|
406
|
+
|---------|-----|-----|-----|-----|-----|------|---------------|------------|--------------------|------------------|
|
407
|
+
|Ubuntu 24.04| CI | CI | CI | CI | CI | ci | CI | ci | CI | ci |
|
408
|
+
|Mac OS 14.7.6| CI | CI | CI | CI | CI | ci | x | | x | |
|
409
|
+
|Windows Server 2022| CI | | | | CI mswin</br>CI ucrt | | | | | |
|
410
|
+
|
411
|
+
Key: `CI` - tested in CI, should work; `ci` - tested in CI, might fail; `x` - known working; `o` - known failing.
|
412
|
+
|
413
|
+
Rubies 3.1+ are also tested separately with YJIT turned on (Ubuntu and Mac OS).
|
414
|
+
|
415
|
+
See [the Actions tab](https://github.com/rubyzip/rubyzip/actions) in GitHub for full details.
|
416
|
+
|
417
|
+
Please [raise a PR](https://github.com/rubyzip/rubyzip/pulls) if you know Rubyzip works on a platform/Ruby combination not listed here, or [raise an issue](https://github.com/rubyzip/rubyzip/issues) if you see a failure where we think it should work.
|
418
|
+
|
332
419
|
## Developing
|
333
420
|
|
334
|
-
|
421
|
+
Install the dependencies:
|
335
422
|
|
336
|
-
```
|
423
|
+
```shell
|
337
424
|
bundle install
|
425
|
+
```
|
426
|
+
|
427
|
+
Run the tests with `rake`:
|
428
|
+
|
429
|
+
```shell
|
338
430
|
rake
|
339
431
|
```
|
340
432
|
|
433
|
+
Please also run `rubocop` over your changes.
|
434
|
+
|
435
|
+
Our CI runs on [GitHub Actions](https://github.com/rubyzip/rubyzip/actions). Please note that `rubocop` is run as part of the CI configuration and will fail a build if errors are found.
|
436
|
+
|
341
437
|
## Website and Project Home
|
342
438
|
|
343
439
|
http://github.com/rubyzip/rubyzip
|
@@ -346,17 +442,18 @@ http://rdoc.info/github/rubyzip/rubyzip/master/frames
|
|
346
442
|
|
347
443
|
## Authors
|
348
444
|
|
349
|
-
|
445
|
+
See https://github.com/rubyzip/rubyzip/graphs/contributors for a comprehensive list.
|
350
446
|
|
351
|
-
|
447
|
+
### Current maintainers
|
352
448
|
|
353
|
-
|
449
|
+
* Robert Haines (@hainesr)
|
450
|
+
* John Lees-Miller (@jdleesmiller)
|
451
|
+
* Oleksandr Simonov (@simonoff)
|
354
452
|
|
355
|
-
|
453
|
+
### Original author
|
356
454
|
|
357
|
-
|
455
|
+
* Thomas Sondergaard
|
358
456
|
|
359
457
|
## License
|
360
458
|
|
361
|
-
Rubyzip is distributed under the same license as ruby.
|
362
|
-
http://www.ruby-lang.org/en/LICENSE.txt
|
459
|
+
Rubyzip is distributed under the same license as Ruby. In practice this means you can use it under the terms of the Ruby License or the 2-Clause BSD License. See https://www.ruby-lang.org/en/about/license.txt and LICENSE.md for details.
|
data/Rakefile
CHANGED
@@ -1,5 +1,8 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'bundler/gem_tasks'
|
2
4
|
require 'rake/testtask'
|
5
|
+
require 'rdoc/task'
|
3
6
|
require 'rubocop/rake_task'
|
4
7
|
|
5
8
|
task default: :test
|
@@ -11,11 +14,12 @@ Rake::TestTask.new(:test) do |test|
|
|
11
14
|
test.verbose = true
|
12
15
|
end
|
13
16
|
|
14
|
-
|
17
|
+
RDoc::Task.new do |rdoc|
|
18
|
+
rdoc.main = 'README.md'
|
19
|
+
rdoc.rdoc_files.include('README.md', 'lib/**/*.rb')
|
20
|
+
rdoc.options << '--markup=markdown'
|
21
|
+
rdoc.options << '--tab-width=2'
|
22
|
+
rdoc.options << "-t Rubyzip version #{Zip::VERSION}"
|
23
|
+
end
|
15
24
|
|
16
|
-
|
17
|
-
# test.libs << File.join(File.dirname(__FILE__), 'lib')
|
18
|
-
# test.libs << File.join(File.dirname(__FILE__), 'test')
|
19
|
-
# test.pattern = File.join(File.dirname(__FILE__), 'test/zip64_full_test.rb')
|
20
|
-
# test.verbose = true
|
21
|
-
# end
|
25
|
+
RuboCop::RakeTask.new
|