rubyzip 2.3.1 → 3.0.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.
Files changed (61) hide show
  1. checksums.yaml +4 -4
  2. data/Changelog.md +422 -0
  3. data/LICENSE.md +24 -0
  4. data/README.md +138 -41
  5. data/Rakefile +11 -7
  6. data/lib/zip/central_directory.rb +169 -123
  7. data/lib/zip/compressor.rb +3 -1
  8. data/lib/zip/constants.rb +29 -21
  9. data/lib/zip/crypto/decrypted_io.rb +4 -2
  10. data/lib/zip/crypto/encryption.rb +4 -2
  11. data/lib/zip/crypto/null_encryption.rb +6 -4
  12. data/lib/zip/crypto/traditional_encryption.rb +8 -6
  13. data/lib/zip/decompressor.rb +4 -3
  14. data/lib/zip/deflater.rb +10 -8
  15. data/lib/zip/dirtyable.rb +32 -0
  16. data/lib/zip/dos_time.rb +51 -5
  17. data/lib/zip/entry.rb +363 -222
  18. data/lib/zip/entry_set.rb +11 -9
  19. data/lib/zip/errors.rb +136 -16
  20. data/lib/zip/extra_field/generic.rb +6 -13
  21. data/lib/zip/extra_field/ntfs.rb +6 -4
  22. data/lib/zip/extra_field/old_unix.rb +3 -1
  23. data/lib/zip/extra_field/universal_time.rb +3 -1
  24. data/lib/zip/extra_field/unix.rb +5 -3
  25. data/lib/zip/extra_field/unknown.rb +33 -0
  26. data/lib/zip/extra_field/zip64.rb +12 -5
  27. data/lib/zip/extra_field.rb +16 -22
  28. data/lib/zip/file.rb +177 -223
  29. data/lib/zip/file_split.rb +91 -0
  30. data/lib/zip/filesystem/dir.rb +86 -0
  31. data/lib/zip/filesystem/directory_iterator.rb +48 -0
  32. data/lib/zip/filesystem/file.rb +262 -0
  33. data/lib/zip/filesystem/file_stat.rb +110 -0
  34. data/lib/zip/filesystem/zip_file_name_mapper.rb +81 -0
  35. data/lib/zip/filesystem.rb +27 -596
  36. data/lib/zip/inflater.rb +6 -4
  37. data/lib/zip/input_stream.rb +50 -37
  38. data/lib/zip/ioextras/abstract_input_stream.rb +15 -10
  39. data/lib/zip/ioextras/abstract_output_stream.rb +5 -3
  40. data/lib/zip/ioextras.rb +7 -7
  41. data/lib/zip/null_compressor.rb +3 -1
  42. data/lib/zip/null_decompressor.rb +3 -1
  43. data/lib/zip/null_input_stream.rb +3 -1
  44. data/lib/zip/output_stream.rb +53 -47
  45. data/lib/zip/pass_thru_compressor.rb +3 -1
  46. data/lib/zip/pass_thru_decompressor.rb +4 -2
  47. data/lib/zip/streamable_directory.rb +3 -1
  48. data/lib/zip/streamable_stream.rb +3 -0
  49. data/lib/zip/version.rb +3 -1
  50. data/lib/zip.rb +17 -23
  51. data/rubyzip.gemspec +39 -0
  52. data/samples/example.rb +8 -3
  53. data/samples/example_filesystem.rb +3 -2
  54. data/samples/example_recursive.rb +3 -1
  55. data/samples/gtk_ruby_zip.rb +4 -2
  56. data/samples/qtzip.rb +6 -5
  57. data/samples/write_simple.rb +2 -1
  58. data/samples/zipfind.rb +1 -0
  59. metadata +83 -33
  60. data/TODO +0 -15
  61. data/lib/zip/extra_field/zip64_placeholder.rb +0 -15
data/README.md CHANGED
@@ -1,26 +1,32 @@
1
1
  # rubyzip
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/rubyzip.svg)](http://badge.fury.io/rb/rubyzip)
4
- [![Build Status](https://secure.travis-ci.org/rubyzip/rubyzip.svg)](http://travis-ci.org/rubyzip/rubyzip)
4
+ [![Tests](https://github.com/rubyzip/rubyzip/actions/workflows/tests.yml/badge.svg)](https://github.com/rubyzip/rubyzip/actions/workflows/tests.yml)
5
+ [![Linter](https://github.com/rubyzip/rubyzip/actions/workflows/lint.yml/badge.svg)](https://github.com/rubyzip/rubyzip/actions/workflows/lint.yml)
5
6
  [![Code Climate](https://codeclimate.com/github/rubyzip/rubyzip.svg)](https://codeclimate.com/github/rubyzip/rubyzip)
6
7
  [![Coverage Status](https://img.shields.io/coveralls/rubyzip/rubyzip.svg)](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 note
11
+ ## Important notes
11
12
 
12
- The Rubyzip interface has changed!!! No need to do `require "zip/zip"` and `Zip` prefix in class names removed.
13
+ ### Updating to version 3.0
13
14
 
14
- If you have issues with any third-party gems that require an old version of rubyzip, you can use this workaround:
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:
16
+ * `File`
17
+ * `Entry`
18
+ * `InputStream`
19
+ * `OutputStream`
15
20
 
16
- ```ruby
17
- gem 'rubyzip', '>= 1.0.0' # will load new rubyzip version
18
- gem 'zip-zip' # will load compatibility for old rubyzip API.
19
- ```
21
+ **Please see [Updating to version 3.x](https://github.com/rubyzip/rubyzip/wiki/Updating-to-version-3.x) in the wiki for details.**
20
22
 
21
23
  ## Requirements
22
24
 
23
- - Ruby 2.4 or greater (for rubyzip 2.0; use 1.x for older rubies)
25
+ Version 3.x requires at least Ruby 3.0.
26
+
27
+ Version 2.x requires at least Ruby 2.4, and is known to work on Ruby 3.x.
28
+
29
+ It is not recommended to use any versions of Rubyzip earlier than 2.3 due to security issues.
24
30
 
25
31
  ## Installation
26
32
 
@@ -49,7 +55,7 @@ input_filenames = ['image.jpg', 'description.txt', 'stats.csv']
49
55
 
50
56
  zipfile_name = "/Users/me/Desktop/archive.zip"
51
57
 
52
- Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile|
58
+ Zip::File.open(zipfile_name, create: true) do |zipfile|
53
59
  input_filenames.each do |filename|
54
60
  # Two arguments:
55
61
  # - The name of the file as it will appear in the archive
@@ -88,7 +94,7 @@ class ZipFileGenerator
88
94
  def write
89
95
  entries = Dir.entries(@input_dir) - %w[. ..]
90
96
 
91
- ::Zip::File.open(@output_file, ::Zip::File::CREATE) do |zipfile|
97
+ ::Zip::File.open(@output_file, create: true) do |zipfile|
92
98
  write_entries entries, '', zipfile
93
99
  end
94
100
  end
@@ -121,9 +127,9 @@ class ZipFileGenerator
121
127
  end
122
128
  ```
123
129
 
124
- ### Save zip archive entries in sorted by name state
130
+ ### Save zip archive entries sorted by name
125
131
 
126
- To save zip archives in sorted order like below, you need to set `::Zip.sort_entries` to `true`
132
+ To save zip archives with their entries sorted by name (see below), set `::Zip.sort_entries` to `true`
127
133
 
128
134
  ```
129
135
  Vegetable/
@@ -137,7 +143,7 @@ fruit/mango
137
143
  fruit/orange
138
144
  ```
139
145
 
140
- After this, entries in the zip archive will be saved in ordered state.
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.
141
147
 
142
148
  ### Default permissions of zip archives
143
149
 
@@ -173,28 +179,71 @@ Zip::File.open('foo.zip') do |zip_file|
173
179
  end
174
180
  ```
175
181
 
176
- #### Notice about ::Zip::InputStream
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.
177
187
 
178
- `::Zip::InputStream` usable for fast reading zip file content because it not read Central directory.
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.
179
189
 
180
- But there is one exception when it is not working - General Purpose Flag Bit 3.
190
+ If `Zip::InputStream` finds such an entry in the zip archive it will raise an exception (`Zip::StreamingError`).
181
191
 
182
- > 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
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`.
183
193
 
184
- If `::Zip::InputStream` finds such entry in the zip archive it will raise an exception.
194
+ ```ruby
195
+ zip_stream = Zip::InputStream.new(File.open('file.zip'))
196
+
197
+ while entry = zip_stream.get_next_entry
198
+ # All required operations on `entry` go here.
199
+ end
200
+ ```
201
+
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`.
185
203
 
186
204
  ### Password Protection (Experimental)
187
205
 
188
206
  Rubyzip supports reading/writing zip files with traditional zip encryption (a.k.a. "ZipCrypto"). AES encryption is not yet supported. It can be used with buffer streams, e.g.:
189
207
 
208
+ #### Version 2.x
209
+
190
210
  ```ruby
191
- Zip::OutputStream.write_buffer(::StringIO.new(''), Zip::TraditionalEncrypter.new('password')) do |out|
192
- out.put_next_entry("my_file.txt")
193
- out.write my_data
194
- end.string
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
225
+ ```
226
+
227
+ #### Version 3.x
228
+
229
+ ```ruby
230
+ # Writing.
231
+ enc = Zip::TraditionalEncrypter.new('password')
232
+ buffer = Zip::OutputStream.write_buffer(encrypter: enc) do |output|
233
+ output.put_next_entry("my_file.txt")
234
+ output.write my_data
235
+ end
236
+
237
+ # Reading.
238
+ dec = Zip::TraditionalDecrypter.new('password')
239
+ Zip::InputStream.open(buffer, decrypter: dec) do |input|
240
+ entry = input.get_next_entry
241
+ puts "Contents of '#{entry.name}':"
242
+ puts input.read
243
+ end
195
244
  ```
196
245
 
197
- This is an experimental feature and the interface for encryption may change in future versions.
246
+ _This is an experimental feature and the interface for encryption may change in future versions._
198
247
 
199
248
  ## Known issues
200
249
 
@@ -208,7 +257,7 @@ buffer = Zip::OutputStream.write_buffer do |out|
208
257
  unless [DOCUMENT_FILE_PATH, RELS_FILE_PATH].include?(e.name)
209
258
  out.put_next_entry(e.name)
210
259
  out.write e.get_input_stream.read
211
- end
260
+ end
212
261
  end
213
262
 
214
263
  out.put_next_entry(DOCUMENT_FILE_PATH)
@@ -288,25 +337,37 @@ Zip.validate_entry_sizes = false
288
337
 
289
338
  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.
290
339
 
291
- ### Default Compression
340
+ ### Compression level
341
+
342
+ 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.
292
343
 
293
- You can set the default compression level like so:
344
+ You can configure the default compression level with:
294
345
 
295
346
  ```ruby
296
- Zip.default_compression = Zlib::DEFAULT_COMPRESSION
347
+ Zip.default_compression = X
297
348
  ```
298
349
 
299
- It defaults to `Zlib::DEFAULT_COMPRESSION`. Possible values are `Zlib::BEST_COMPRESSION`, `Zlib::DEFAULT_COMPRESSION` and `Zlib::NO_COMPRESSION`
350
+ 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.
351
+
352
+ This can also be set for each archive as an option to `Zip::File`:
353
+
354
+ ```ruby
355
+ Zip::File.open('foo.zip', create:true, compression_level: 9) do |zip|
356
+ zip.add ...
357
+ end
358
+ ```
300
359
 
301
360
  ### Zip64 Support
302
361
 
303
- By default, Zip64 support is disabled for writing. To enable it do this:
362
+ Since version 3.0, Zip64 support is enabled for writing by default. To disable it do this:
304
363
 
305
364
  ```ruby
306
- Zip.write_zip64_support = true
365
+ Zip.write_zip64_support = false
307
366
  ```
308
367
 
309
- _NOTE_: If you will enable Zip64 writing then you will need zip extractor with Zip64 support to extract archive.
368
+ Prior to version 3.0, Zip64 support is disabled for writing by default.
369
+
370
+ _NOTE_: If Zip64 write support is enabled then any extractor subsequently used may also require Zip64 support to read from the resultant archive.
310
371
 
311
372
  ### Block Form
312
373
 
@@ -321,15 +382,50 @@ You can set multiple settings at the same time by using a block:
321
382
  end
322
383
  ```
323
384
 
385
+ ## Compatibility
386
+
387
+ Rubyzip is known to run on a number of platforms and under a number of different Ruby versions.
388
+
389
+ ### Version 2.3.x
390
+
391
+ Rubyzip 2.3 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.3 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.
392
+
393
+ ### Next (version 3.0.0)
394
+
395
+ Please see the table below for what we think the current situation is. Note: an empty cell means "unknown", not "does not work".
396
+
397
+ | 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 |
398
+ |---------|-----|-----|-----|-----|-----|------|---------------|------------|--------------------|------------------|
399
+ |Ubuntu 24.04| CI | CI | CI | CI | CI | ci | CI | ci | CI | ci |
400
+ |Mac OS 14.7.6| CI | CI | CI | CI | CI | ci | x | | x | |
401
+ |Windows Server 2022| CI | | | | CI&nbsp;mswin</br>CI&nbsp;ucrt | | | | | |
402
+
403
+ Key: `CI` - tested in CI, should work; `ci` - tested in CI, might fail; `x` - known working; `o` - known failing.
404
+
405
+ Rubies 3.1+ are also tested separately with YJIT turned on (Ubuntu and Mac OS).
406
+
407
+ See [the Actions tab](https://github.com/rubyzip/rubyzip/actions) in GitHub for full details.
408
+
409
+ 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.
410
+
324
411
  ## Developing
325
412
 
326
- To run the test you need to do this:
413
+ Install the dependencies:
327
414
 
328
- ```
415
+ ```shell
329
416
  bundle install
417
+ ```
418
+
419
+ Run the tests with `rake`:
420
+
421
+ ```shell
330
422
  rake
331
423
  ```
332
424
 
425
+ Please also run `rubocop` over your changes.
426
+
427
+ 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.
428
+
333
429
  ## Website and Project Home
334
430
 
335
431
  http://github.com/rubyzip/rubyzip
@@ -338,17 +434,18 @@ http://rdoc.info/github/rubyzip/rubyzip/master/frames
338
434
 
339
435
  ## Authors
340
436
 
341
- Alexander Simonov ( alex at simonov.me)
437
+ See https://github.com/rubyzip/rubyzip/graphs/contributors for a comprehensive list.
342
438
 
343
- Alan Harper ( alan at aussiegeek.net)
439
+ ### Current maintainers
344
440
 
345
- Thomas Sondergaard (thomas at sondergaard.cc)
441
+ * Robert Haines (@hainesr)
442
+ * John Lees-Miller (@jdleesmiller)
443
+ * Oleksandr Simonov (@simonoff)
346
444
 
347
- Technorama Ltd. (oss-ruby-zip at technorama.net)
445
+ ### Original author
348
446
 
349
- extra-field support contributed by Tatsuki Sugiura (sugi at nemui.org)
447
+ * Thomas Sondergaard
350
448
 
351
449
  ## License
352
450
 
353
- Rubyzip is distributed under the same license as ruby. See
354
- http://www.ruby-lang.org/en/LICENSE.txt
451
+ 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
- RuboCop::RakeTask.new
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
- # Rake::TestTask.new(:zip64_full_test) do |test|
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