rubyzip 1.0.0 → 2.4.1

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 (52) hide show
  1. checksums.yaml +6 -14
  2. data/README.md +231 -46
  3. data/Rakefile +13 -5
  4. data/TODO +0 -1
  5. data/lib/zip/central_directory.rb +64 -29
  6. data/lib/zip/compressor.rb +1 -2
  7. data/lib/zip/constants.rb +59 -5
  8. data/lib/zip/crypto/decrypted_io.rb +40 -0
  9. data/lib/zip/crypto/encryption.rb +11 -0
  10. data/lib/zip/crypto/null_encryption.rb +43 -0
  11. data/lib/zip/crypto/traditional_encryption.rb +99 -0
  12. data/lib/zip/decompressor.rb +22 -4
  13. data/lib/zip/deflater.rb +11 -6
  14. data/lib/zip/dos_time.rb +27 -16
  15. data/lib/zip/entry.rb +299 -163
  16. data/lib/zip/entry_set.rb +22 -20
  17. data/lib/zip/errors.rb +17 -6
  18. data/lib/zip/extra_field/generic.rb +15 -14
  19. data/lib/zip/extra_field/ntfs.rb +94 -0
  20. data/lib/zip/extra_field/old_unix.rb +46 -0
  21. data/lib/zip/extra_field/universal_time.rb +46 -16
  22. data/lib/zip/extra_field/unix.rb +10 -9
  23. data/lib/zip/extra_field/zip64.rb +46 -6
  24. data/lib/zip/extra_field/zip64_placeholder.rb +15 -0
  25. data/lib/zip/extra_field.rb +32 -18
  26. data/lib/zip/file.rb +260 -160
  27. data/lib/zip/filesystem.rb +297 -276
  28. data/lib/zip/inflater.rb +23 -34
  29. data/lib/zip/input_stream.rb +130 -82
  30. data/lib/zip/ioextras/abstract_input_stream.rb +36 -22
  31. data/lib/zip/ioextras/abstract_output_stream.rb +4 -6
  32. data/lib/zip/ioextras.rb +4 -6
  33. data/lib/zip/null_compressor.rb +2 -2
  34. data/lib/zip/null_decompressor.rb +4 -12
  35. data/lib/zip/null_input_stream.rb +2 -1
  36. data/lib/zip/output_stream.rb +75 -45
  37. data/lib/zip/pass_thru_compressor.rb +6 -6
  38. data/lib/zip/pass_thru_decompressor.rb +14 -24
  39. data/lib/zip/streamable_directory.rb +3 -3
  40. data/lib/zip/streamable_stream.rb +21 -16
  41. data/lib/zip/version.rb +1 -1
  42. data/lib/zip.rb +42 -3
  43. data/samples/example.rb +30 -40
  44. data/samples/example_filesystem.rb +16 -18
  45. data/samples/example_recursive.rb +33 -28
  46. data/samples/gtk_ruby_zip.rb +84 -0
  47. data/samples/qtzip.rb +25 -34
  48. data/samples/write_simple.rb +10 -13
  49. data/samples/zipfind.rb +38 -45
  50. metadata +129 -28
  51. data/NEWS +0 -182
  52. data/samples/gtkRubyzip.rb +0 -86
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- NjQ3YjE3YmQ0MDUyYzViOThmNDBjNGVhYTRmNDdiNzAzZjYzNjZkOA==
5
- data.tar.gz: !binary |-
6
- OGI5ZjFlYTk3NmQwMGY2NmM2NmVkYWI3ZjczMTVjNDAxYzg0YzY3Zg==
7
- !binary "U0hBNTEy":
8
- metadata.gz: !binary |-
9
- ODI3NzAwZDg5NzYyZDdjZTNlNDUyOWJjYmY0YTU0MGEzMjUwOTE5MjdkOTM3
10
- ZjkzMTFjNzQzZmRlZDA4MTllZmU2N2NjMDQyMDM5NTRjMjg5ODVmMzUyODMw
11
- ZGExYjk3MDkwNzRmYTJkYjhhYjNmMmY4YTIyYTQ2ODA4MDdiNzk=
12
- data.tar.gz: !binary |-
13
- MWM5ODRlNzdmNDNiOGE4ZjkzMWYzZTIxZTE4OTU1ZTVhZmQ4YzhhZGQ0ZWNi
14
- N2Q4N2NhZmE1ZmJiZjFjMzcxMWI3YzgzNmRiNDVlZGYwZmYyZTJiNTljY2Yx
15
- MTY4NGM1YjNhOGJiZmE2MGUyZTBjYTc2YzdjMzc0ZTE3NTIyZDc=
2
+ SHA256:
3
+ metadata.gz: 2d59784cc9a5e884c6537a896b30b1a18206c7a97a0f74e37bd58769f0700497
4
+ data.tar.gz: 7e4bf8643b179bb0e5415b0f6aa7e92ce4786ed734cf23b902ea9739d5c46bb9
5
+ SHA512:
6
+ metadata.gz: c65346fc54aa6e931f8975a8e98212323a597583523a251d2d88184971a9469a549e9cd967c2d6015002f07fe8c19b13d96261781131b9bd7a0b099da1c04fe2
7
+ data.tar.gz: b397b12e1bef39a1cd4ade2698756f3194ed75504fb15ea5494dc26189be4a8d66af1a2411e827b660f67044ef4ee207c3d951d190886912079ce8ffcbf19bac
data/README.md CHANGED
@@ -1,16 +1,38 @@
1
1
  # rubyzip
2
- [![Build Status](https://secure.travis-ci.org/rubyzip/rubyzip.png)](http://travis-ci.org/rubyzip/rubyzip)
3
- [![Code Climate](https://codeclimate.com/github/rubyzip/rubyzip.png)](https://codeclimate.com/github/rubyzip/rubyzip)
4
- [![Coverage Status](https://coveralls.io/repos/rubyzip/rubyzip/badge.png?branch=master)](https://coveralls.io/r/rubyzip/rubyzip?branch=master)
5
2
 
6
- rubyzip is a ruby library for reading and writing zip files.
3
+ [![Gem Version](https://badge.fury.io/rb/rubyzip.svg)](http://badge.fury.io/rb/rubyzip)
4
+ [![Tests](https://github.com/rubyzip/rubyzip/actions/workflows/tests.yml/badge.svg)](https://github.com/rubyzip/rubyzip/actions/workflows/tests.yml)
5
+ [![Code Climate](https://codeclimate.com/github/rubyzip/rubyzip.svg)](https://codeclimate.com/github/rubyzip/rubyzip)
6
+ [![Coverage Status](https://img.shields.io/coveralls/rubyzip/rubyzip.svg)](https://coveralls.io/r/rubyzip/rubyzip?branch=master)
7
+
8
+ Rubyzip is a ruby library for reading and writing zip files.
7
9
 
8
10
  ## Important note
9
11
 
10
- Rubyzip interface changed!!! No need to do `require "zip/zip"` and `Zip` prefix in class names removed.
12
+ Rubyzip 2.4 is intended to be the last release in the 2.x series. Please get ready for version 3.0.
13
+
14
+ ### Updating to version 3.0
15
+
16
+ The public API of some classes has been modernized to use named parameters for optional arguments. Also some methods have been changed or removed. Please check your usage of the following Rubyzip classes:
17
+ * `File`
18
+ * `Entry`
19
+ * `InputStream`
20
+ * `OutputStream`
21
+ * `DOSTime`
22
+
23
+ **Please see [Updating to version 3.x](https://github.com/rubyzip/rubyzip/wiki/Updating-to-version-3.x) in the wiki for details.**
24
+
25
+ ## Requirements
26
+
27
+ Version 3.x requires at least Ruby 3.0.
28
+
29
+ Version 2.x requires at least Ruby 2.4, and is known to work on Ruby 3.x.
30
+
31
+ It is not recommended to use any versions of Rubyzip earlier than 2.3 due to security issues.
11
32
 
12
33
  ## Installation
13
- rubyzip is available on RubyGems, so:
34
+
35
+ Rubyzip is available on RubyGems:
14
36
 
15
37
  ```
16
38
  gem install rubyzip
@@ -40,27 +62,148 @@ Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile|
40
62
  # Two arguments:
41
63
  # - The name of the file as it will appear in the archive
42
64
  # - The original file, including the path to find it
43
- zipfile.add(filename, folder + '/' + filename)
65
+ zipfile.add(filename, File.join(folder, filename))
44
66
  end
67
+ zipfile.get_output_stream("myFile") { |f| f.write "myFile contains just this" }
45
68
  end
46
69
  ```
47
70
 
48
71
  ### Zipping a directory recursively
49
72
 
73
+ Copy from [here](https://github.com/rubyzip/rubyzip/blob/9d891f7353e66052283562d3e252fe380bb4b199/samples/example_recursive.rb)
74
+
50
75
  ```ruby
51
- require 'rubygems'
52
76
  require 'zip'
53
77
 
54
- directory = '/Users/me/Desktop/directory_to_zip/'
55
- zipfile_name = '/Users/me/Desktop/recursive_directory.zip'
78
+ # This is a simple example which uses rubyzip to
79
+ # recursively generate a zip file from the contents of
80
+ # a specified directory. The directory itself is not
81
+ # included in the archive, rather just its contents.
82
+ #
83
+ # Usage:
84
+ # directory_to_zip = "/tmp/input"
85
+ # output_file = "/tmp/out.zip"
86
+ # zf = ZipFileGenerator.new(directory_to_zip, output_file)
87
+ # zf.write()
88
+ class ZipFileGenerator
89
+ # Initialize with the directory to zip and the location of the output archive.
90
+ def initialize(input_dir, output_file)
91
+ @input_dir = input_dir
92
+ @output_file = output_file
93
+ end
56
94
 
57
- Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile|
58
- Dir[File.join(directory, '**', '**')].each do |file|
59
- zipfile.add(file.sub(directory, ''), file)
60
- end
95
+ # Zip the input directory.
96
+ def write
97
+ entries = Dir.entries(@input_dir) - %w[. ..]
98
+
99
+ ::Zip::File.open(@output_file, ::Zip::File::CREATE) do |zipfile|
100
+ write_entries entries, '', zipfile
101
+ end
102
+ end
103
+
104
+ private
105
+
106
+ # A helper method to make the recursion work.
107
+ def write_entries(entries, path, zipfile)
108
+ entries.each do |e|
109
+ zipfile_path = path == '' ? e : File.join(path, e)
110
+ disk_file_path = File.join(@input_dir, zipfile_path)
111
+
112
+ if File.directory? disk_file_path
113
+ recursively_deflate_directory(disk_file_path, zipfile, zipfile_path)
114
+ else
115
+ put_into_archive(disk_file_path, zipfile, zipfile_path)
116
+ end
117
+ end
118
+ end
119
+
120
+ def recursively_deflate_directory(disk_file_path, zipfile, zipfile_path)
121
+ zipfile.mkdir zipfile_path
122
+ subdir = Dir.entries(disk_file_path) - %w[. ..]
123
+ write_entries subdir, zipfile_path, zipfile
124
+ end
125
+
126
+ def put_into_archive(disk_file_path, zipfile, zipfile_path)
127
+ zipfile.add(zipfile_path, disk_file_path)
128
+ end
61
129
  end
62
130
  ```
63
131
 
132
+ ### Save zip archive entries in sorted by name state
133
+
134
+ To save zip archives in sorted order like below, you need to set `::Zip.sort_entries` to `true`
135
+
136
+ ```
137
+ Vegetable/
138
+ Vegetable/bean
139
+ Vegetable/carrot
140
+ Vegetable/celery
141
+ fruit/
142
+ fruit/apple
143
+ fruit/kiwi
144
+ fruit/mango
145
+ fruit/orange
146
+ ```
147
+
148
+ After this, entries in the zip archive will be saved in ordered state.
149
+
150
+ ### Default permissions of zip archives
151
+
152
+ On Posix file systems the default file permissions applied to a new archive
153
+ are (0666 - umask), which mimics the behavior of standard tools such as `touch`.
154
+
155
+ On Windows the default file permissions are set to 0644 as suggested by the
156
+ [Ruby File documentation](http://ruby-doc.org/core-2.2.2/File.html).
157
+
158
+ When modifying a zip archive the file permissions of the archive are preserved.
159
+
160
+ ### Reading a Zip file
161
+
162
+ ```ruby
163
+ MAX_SIZE = 1024**2 # 1MiB (but of course you can increase this)
164
+ Zip::File.open('foo.zip') do |zip_file|
165
+ # Handle entries one by one
166
+ zip_file.each do |entry|
167
+ puts "Extracting #{entry.name}"
168
+ raise 'File too large when extracted' if entry.size > MAX_SIZE
169
+
170
+ # Extract to file or directory based on name in the archive
171
+ entry.extract
172
+
173
+ # Read into memory
174
+ content = entry.get_input_stream.read
175
+ end
176
+
177
+ # Find specific entry
178
+ entry = zip_file.glob('*.csv').first
179
+ raise 'File too large when extracted' if entry.size > MAX_SIZE
180
+ puts entry.get_input_stream.read
181
+ end
182
+ ```
183
+
184
+ #### Notice about ::Zip::InputStream
185
+
186
+ `::Zip::InputStream` usable for fast reading zip file content because it not read Central directory.
187
+
188
+ But there is one exception when it is not working - General Purpose Flag Bit 3.
189
+
190
+ > 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
191
+
192
+ If `::Zip::InputStream` finds such entry in the zip archive it will raise an exception.
193
+
194
+ ### Password Protection (Experimental)
195
+
196
+ 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.:
197
+
198
+ ```ruby
199
+ Zip::OutputStream.write_buffer(::StringIO.new, Zip::TraditionalEncrypter.new('password')) do |out|
200
+ out.put_next_entry("my_file.txt")
201
+ out.write my_data
202
+ end.string
203
+ ```
204
+
205
+ This is an experimental feature and the interface for encryption may change in future versions.
206
+
64
207
  ## Known issues
65
208
 
66
209
  ### Modify docx file with rubyzip
@@ -83,38 +226,14 @@ buffer = Zip::OutputStream.write_buffer do |out|
83
226
  out.write rels.to_xml(:indent => 0).gsub("\n","")
84
227
  end
85
228
 
86
- File.open(new_path, "w") {|f| f.write(buffer.string) }
229
+ File.open(new_path, "wb") {|f| f.write(buffer.string) }
87
230
  ```
88
231
 
89
- ## Further Documentation
90
-
91
- There is more than one way to access or create a zip archive with
92
- rubyzip. The basic API is modeled after the classes in
93
- java.util.zip from the Java SDK. This means there are classes such
94
- as Zip::InputStream, Zip::OutputStream and
95
- Zip::File. Zip::InputStream provides a basic interface for
96
- iterating through the entries in a zip archive and reading from the
97
- entries in the same way as from a regular File or IO
98
- object. OutputStream is the corresponding basic output
99
- facility. Zip::File provides a mean for accessing the archives
100
- central directory and provides means for accessing any entry without
101
- having to iterate through the archive. Unlike Java's
102
- java.util.zip.ZipFile rubyzip's Zip::File is mutable, which means
103
- it can be used to change zip files as well.
104
-
105
- Another way to access a zip archive with rubyzip is to use rubyzip's
106
- Zip::FileSystem API. Using this API files can be read from and
107
- written to the archive in much the same manner as ruby's builtin
108
- classes allows files to be read from and written to the file system.
109
-
110
- For details about the specific behaviour of classes and methods refer
111
- to the test suite. Finally you can generate the rdoc documentation or
112
- visit http://rubyzip.sourceforge.net.
113
-
114
-
115
232
  ## Configuration
116
233
 
117
- By default, rubyzip will not overwrite files if they already exist inside of the extracted path. To change this behavior, you may specify a configuration option like so:
234
+ ### Existing Files
235
+
236
+ By default, rubyzip will not overwrite files if they already exist inside of the extracted path. To change this behavior, you may specify a configuration option like so:
118
237
 
119
238
  ```ruby
120
239
  Zip.on_exists_proc = true
@@ -128,25 +247,91 @@ Additionally, if you want to configure rubyzip to overwrite existing files while
128
247
  Zip.continue_on_exists_proc = true
129
248
  ```
130
249
 
131
- If you want to store non english names and want to open properly file on Windows(pre 7) you need to set next option:
250
+ ### Non-ASCII Names
251
+
252
+ If you want to store non-english names and want to open them on Windows(pre 7) you need to set this option:
132
253
 
133
254
  ```ruby
134
255
  Zip.unicode_names = true
135
256
  ```
136
257
 
137
- All settings in same time
258
+ Sometimes file names inside zip contain non-ASCII characters. If you can assume which encoding was used for such names and want to be able to find such entries using `find_entry` then you can force assumed encoding like so:
259
+
260
+ ```ruby
261
+ Zip.force_entry_names_encoding = 'UTF-8'
262
+ ```
263
+
264
+ Allowed encoding names are the same as accepted by `String#force_encoding`
265
+
266
+ ### Date Validation
267
+
268
+ Some zip files might have an invalid date format, which will raise a warning. You can hide this warning with the following setting:
269
+
270
+ ```ruby
271
+ Zip.warn_invalid_date = false
272
+ ```
273
+
274
+ ### Size Validation
275
+
276
+ By default (in rubyzip >= 2.0), rubyzip's `extract` method checks that an entry's reported uncompressed size is not (significantly) smaller than its actual size. This is to help you protect your application against [zip bombs](https://en.wikipedia.org/wiki/Zip_bomb). Before `extract`ing an entry, you should check that its size is in the range you expect. For example, if your application supports processing up to 100 files at once, each up to 10MiB, your zip extraction code might look like:
277
+
278
+ ```ruby
279
+ MAX_FILE_SIZE = 10 * 1024**2 # 10MiB
280
+ MAX_FILES = 100
281
+ Zip::File.open('foo.zip') do |zip_file|
282
+ num_files = 0
283
+ zip_file.each do |entry|
284
+ num_files += 1 if entry.file?
285
+ raise 'Too many extracted files' if num_files > MAX_FILES
286
+ raise 'File too large when extracted' if entry.size > MAX_FILE_SIZE
287
+ entry.extract
288
+ end
289
+ end
290
+ ```
291
+
292
+ If you need to extract zip files that report incorrect uncompressed sizes and you really trust them not too be too large, you can disable this setting with
293
+ ```ruby
294
+ Zip.validate_entry_sizes = false
295
+ ```
296
+
297
+ 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
+
299
+ ### Default Compression
300
+
301
+ You can set the default compression level like so:
302
+
303
+ ```ruby
304
+ Zip.default_compression = Zlib::DEFAULT_COMPRESSION
305
+ ```
306
+
307
+ It defaults to `Zlib::DEFAULT_COMPRESSION`. Possible values are `Zlib::BEST_COMPRESSION`, `Zlib::DEFAULT_COMPRESSION` and `Zlib::NO_COMPRESSION`
308
+
309
+ ### Zip64 Support
310
+
311
+ By default, Zip64 support is disabled for writing. To enable it do this:
312
+
313
+ ```ruby
314
+ Zip.write_zip64_support = true
315
+ ```
316
+
317
+ _NOTE_: If you will enable Zip64 writing then you will need zip extractor with Zip64 support to extract archive.
318
+
319
+ ### Block Form
320
+
321
+ You can set multiple settings at the same time by using a block:
138
322
 
139
323
  ```ruby
140
324
  Zip.setup do |c|
141
325
  c.on_exists_proc = true
142
326
  c.continue_on_exists_proc = true
143
327
  c.unicode_names = true
328
+ c.default_compression = Zlib::BEST_COMPRESSION
144
329
  end
145
330
  ```
146
331
 
147
332
  ## Developing
148
333
 
149
- To run tests you need run next commands:
334
+ To run the test you need to do this:
150
335
 
151
336
  ```
152
337
  bundle install
@@ -173,5 +358,5 @@ extra-field support contributed by Tatsuki Sugiura (sugi at nemui.org)
173
358
 
174
359
  ## License
175
360
 
176
- rubyzip is distributed under the same license as ruby. See
361
+ Rubyzip is distributed under the same license as ruby. See
177
362
  http://www.ruby-lang.org/en/LICENSE.txt
data/Rakefile CHANGED
@@ -1,13 +1,21 @@
1
1
  require 'bundler/gem_tasks'
2
2
  require 'rake/testtask'
3
+ require 'rubocop/rake_task'
3
4
 
4
- task :default => :test
5
+ task default: :test
5
6
 
6
7
  Rake::TestTask.new(:test) do |test|
7
- test.libs << File.join(File.dirname(__FILE__), 'lib')
8
- test.libs << File.join(File.dirname(__FILE__), 'test')
9
- test.pattern = File.join(File.dirname(__FILE__), 'test/alltests.rb')
8
+ test.libs << 'lib'
9
+ test.libs << 'test'
10
+ test.pattern = 'test/**/*_test.rb'
10
11
  test.verbose = true
11
-
12
12
  end
13
13
 
14
+ RuboCop::RakeTask.new
15
+
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
data/TODO CHANGED
@@ -4,7 +4,6 @@
4
4
  * Suggestion: Add ZipFile/ZipInputStream example that demonstrates extracting all entries.
5
5
  * Suggestion: ZipFile#extract destination should default to "."
6
6
  * Suggestion: ZipEntry should have extract(), get_input_stream() methods etc
7
- * Suggestion: ZipInputStream/ZipOutputStream should accept an IO object in addition to a filename.
8
7
  * (is buffering used anywhere with write?)
9
8
  * Inflater.sysread should pass the buffer to produce_input.
10
9
  * Implement ZipFsDir.glob
@@ -5,7 +5,7 @@ module Zip
5
5
  END_OF_CDS = 0x06054b50
6
6
  ZIP64_END_OF_CDS = 0x06064b50
7
7
  ZIP64_EOCD_LOCATOR = 0x07064b50
8
- MAX_END_OF_CDS_SIZE = 65536 + 18
8
+ MAX_END_OF_CDS_SIZE = 65_536 + 18
9
9
  STATIC_EOCD_SIZE = 22
10
10
 
11
11
  attr_reader :comment
@@ -22,21 +22,31 @@ module Zip
22
22
  end
23
23
 
24
24
  def write_to_stream(io) #:nodoc:
25
- offset = io.tell
25
+ cdir_offset = io.tell
26
26
  @entry_set.each { |entry| entry.write_c_dir_entry(io) }
27
- write_e_o_c_d(io, offset)
27
+ eocd_offset = io.tell
28
+ cdir_size = eocd_offset - cdir_offset
29
+ if ::Zip.write_zip64_support
30
+ need_zip64_eocd = cdir_offset > 0xFFFFFFFF || cdir_size > 0xFFFFFFFF || @entry_set.size > 0xFFFF
31
+ need_zip64_eocd ||= @entry_set.any? { |entry| entry.extra['Zip64'] }
32
+ if need_zip64_eocd
33
+ write_64_e_o_c_d(io, cdir_offset, cdir_size)
34
+ write_64_eocd_locator(io, eocd_offset)
35
+ end
36
+ end
37
+ write_e_o_c_d(io, cdir_offset, cdir_size)
28
38
  end
29
39
 
30
- def write_e_o_c_d(io, offset) #:nodoc:
40
+ def write_e_o_c_d(io, offset, cdir_size) #:nodoc:
31
41
  tmp = [
32
42
  END_OF_CDS,
33
43
  0, # @numberOfThisDisk
34
44
  0, # @numberOfDiskWithStartOfCDir
35
- @entry_set ? @entry_set.size : 0,
36
- @entry_set ? @entry_set.size : 0,
37
- cdir_size,
38
- offset,
39
- @comment ? @comment.length : 0
45
+ @entry_set ? [@entry_set.size, 0xFFFF].min : 0,
46
+ @entry_set ? [@entry_set.size, 0xFFFF].min : 0,
47
+ [cdir_size, 0xFFFFFFFF].min,
48
+ [offset, 0xFFFFFFFF].min,
49
+ @comment ? @comment.bytesize : 0
40
50
  ]
41
51
  io << tmp.pack('VvvvvVVv')
42
52
  io << @comment
@@ -44,14 +54,35 @@ module Zip
44
54
 
45
55
  private :write_e_o_c_d
46
56
 
47
- def cdir_size #:nodoc:
48
- # does not include eocd
49
- @entry_set.inject(0) do |value, entry|
50
- entry.cdir_header_size + value
51
- end
57
+ def write_64_e_o_c_d(io, offset, cdir_size) #:nodoc:
58
+ tmp = [
59
+ ZIP64_END_OF_CDS,
60
+ 44, # size of zip64 end of central directory record (excludes signature and field itself)
61
+ VERSION_MADE_BY,
62
+ VERSION_NEEDED_TO_EXTRACT_ZIP64,
63
+ 0, # @numberOfThisDisk
64
+ 0, # @numberOfDiskWithStartOfCDir
65
+ @entry_set ? @entry_set.size : 0, # number of entries on this disk
66
+ @entry_set ? @entry_set.size : 0, # number of entries total
67
+ cdir_size, # size of central directory
68
+ offset # offset of start of central directory in its disk
69
+ ]
70
+ io << tmp.pack('VQ<vvVVQ<Q<Q<Q<')
52
71
  end
53
72
 
54
- private :cdir_size
73
+ private :write_64_e_o_c_d
74
+
75
+ def write_64_eocd_locator(io, zip64_eocd_offset)
76
+ tmp = [
77
+ ZIP64_EOCD_LOCATOR,
78
+ 0, # number of disk containing the start of zip64 eocd record
79
+ zip64_eocd_offset, # offset of the start of zip64 eocd record in its disk
80
+ 1 # total number of disks
81
+ ]
82
+ io << tmp.pack('VVQ<V')
83
+ end
84
+
85
+ private :write_64_eocd_locator
55
86
 
56
87
  def read_64_e_o_c_d(buf) #:nodoc:
57
88
  buf = get_64_e_o_c_d(buf)
@@ -65,7 +96,7 @@ module Zip
65
96
  @size_in_bytes = Entry.read_zip_64_long(buf)
66
97
  @cdir_offset = Entry.read_zip_64_long(buf)
67
98
  @zip_64_extensible = buf.slice!(0, buf.bytesize)
68
- raise ZipError, "Zip consistency problem while reading eocd structure" unless buf.size == 0
99
+ raise Error, 'Zip consistency problem while reading eocd structure' unless buf.empty?
69
100
  end
70
101
 
71
102
  def read_e_o_c_d(buf) #:nodoc:
@@ -77,19 +108,19 @@ module Zip
77
108
  @size_in_bytes = Entry.read_zip_long(buf)
78
109
  @cdir_offset = Entry.read_zip_long(buf)
79
110
  comment_length = Entry.read_zip_short(buf)
80
- @comment = if comment_length <= 0
111
+ @comment = if comment_length.to_i <= 0
81
112
  buf.slice!(0, buf.size)
82
113
  else
83
114
  buf.read(comment_length)
84
115
  end
85
- raise ZipError, "Zip consistency problem while reading eocd structure" unless buf.size == 0
116
+ raise Error, 'Zip consistency problem while reading eocd structure' unless buf.empty?
86
117
  end
87
118
 
88
119
  def read_central_directory_entries(io) #:nodoc:
89
120
  begin
90
121
  io.seek(@cdir_offset, IO::SEEK_SET)
91
122
  rescue Errno::EINVAL
92
- raise ZipError, "Zip consistency problem while reading central directory entry"
123
+ raise Error, 'Zip consistency problem while reading central directory entry'
93
124
  end
94
125
  @entry_set = EntrySet.new
95
126
  @size.times do
@@ -99,7 +130,7 @@ module Zip
99
130
 
100
131
  def read_from_stream(io) #:nodoc:
101
132
  buf = start_buf(io)
102
- if self.zip64_file?(buf)
133
+ if zip64_file?(buf)
103
134
  read_64_e_o_c_d(buf)
104
135
  else
105
136
  read_e_o_c_d(buf)
@@ -109,7 +140,8 @@ module Zip
109
140
 
110
141
  def get_e_o_c_d(buf) #:nodoc:
111
142
  sig_index = buf.rindex([END_OF_CDS].pack('V'))
112
- raise ZipError, "Zip end of central directory signature not found" unless sig_index
143
+ raise Error, 'Zip end of central directory signature not found' unless sig_index
144
+
113
145
  buf = buf.slice!((sig_index + 4)..(buf.bytesize))
114
146
 
115
147
  def buf.read(count)
@@ -134,9 +166,11 @@ module Zip
134
166
 
135
167
  def get_64_e_o_c_d(buf) #:nodoc:
136
168
  zip_64_start = buf.rindex([ZIP64_END_OF_CDS].pack('V'))
137
- raise ZipError, "Zip64 end of central directory signature not found" unless zip_64_start
169
+ raise Error, 'Zip64 end of central directory signature not found' unless zip_64_start
170
+
138
171
  zip_64_locator = buf.rindex([ZIP64_EOCD_LOCATOR].pack('V'))
139
- raise ZipError, "Zip64 end of central directory signature locator not found" unless zip_64_locator
172
+ raise Error, 'Zip64 end of central directory signature locator not found' unless zip_64_locator
173
+
140
174
  buf = buf.slice!((zip_64_start + 4)..zip_64_locator)
141
175
 
142
176
  def buf.read(count)
@@ -147,11 +181,11 @@ module Zip
147
181
  end
148
182
 
149
183
  # For iterating over the entries.
150
- def each(&proc)
151
- @entry_set.each(&proc)
184
+ def each(&a_proc)
185
+ @entry_set.each(&a_proc)
152
186
  end
153
187
 
154
- # Returns the number of entries in the central directory (and
188
+ # Returns the number of entries in the central directory (and
155
189
  # consequently in the zip archive).
156
190
  def size
157
191
  @entry_set.size
@@ -160,13 +194,14 @@ module Zip
160
194
  def self.read_from_stream(io) #:nodoc:
161
195
  cdir = new
162
196
  cdir.read_from_stream(io)
163
- return cdir
164
- rescue ZipError
165
- return nil
197
+ cdir
198
+ rescue Error
199
+ nil
166
200
  end
167
201
 
168
202
  def ==(other) #:nodoc:
169
203
  return false unless other.kind_of?(CentralDirectory)
204
+
170
205
  @entry_set.entries.sort == other.entries.sort && comment == other.comment
171
206
  end
172
207
  end
@@ -1,7 +1,6 @@
1
1
  module Zip
2
2
  class Compressor #:nodoc:all
3
- def finish
4
- end
3
+ def finish; end
5
4
  end
6
5
  end
7
6
 
data/lib/zip/constants.rb CHANGED
@@ -6,12 +6,14 @@ module Zip
6
6
 
7
7
  LOCAL_ENTRY_SIGNATURE = 0x04034b50
8
8
  LOCAL_ENTRY_STATIC_HEADER_LENGTH = 30
9
- LOCAL_ENTRY_TRAILING_DESCRIPTOR_LENGTH = 4+4+4
9
+ LOCAL_ENTRY_TRAILING_DESCRIPTOR_LENGTH = 4 + 4 + 4
10
+ VERSION_MADE_BY = 52 # this library's version
10
11
  VERSION_NEEDED_TO_EXTRACT = 20
12
+ VERSION_NEEDED_TO_EXTRACT_ZIP64 = 45
11
13
 
12
- FILE_TYPE_FILE = 010
13
- FILE_TYPE_DIR = 004
14
- FILE_TYPE_SYMLINK = 012
14
+ FILE_TYPE_FILE = 0o10
15
+ FILE_TYPE_DIR = 0o04
16
+ FILE_TYPE_SYMLINK = 0o12
15
17
 
16
18
  FSTYPE_FAT = 0
17
19
  FSTYPE_AMIGA = 1
@@ -56,6 +58,58 @@ module Zip
56
58
  FSTYPE_TANDEM => 'Tandem NSK'.freeze,
57
59
  FSTYPE_THEOS => 'Theos'.freeze,
58
60
  FSTYPE_MAC_OSX => 'Mac OS/X (Darwin)'.freeze,
59
- FSTYPE_ATHEOS => 'AtheOS'.freeze,
61
+ FSTYPE_ATHEOS => 'AtheOS'.freeze
62
+ }.freeze
63
+
64
+ COMPRESSION_METHOD_STORE = 0
65
+ COMPRESSION_METHOD_SHRINK = 1
66
+ COMPRESSION_METHOD_REDUCE_1 = 2
67
+ COMPRESSION_METHOD_REDUCE_2 = 3
68
+ COMPRESSION_METHOD_REDUCE_3 = 4
69
+ COMPRESSION_METHOD_REDUCE_4 = 5
70
+ COMPRESSION_METHOD_IMPLODE = 6
71
+ # RESERVED = 7
72
+ COMPRESSION_METHOD_DEFLATE = 8
73
+ COMPRESSION_METHOD_DEFLATE_64 = 9
74
+ COMPRESSION_METHOD_PKWARE_DCLI = 10
75
+ # RESERVED = 11
76
+ COMPRESSION_METHOD_BZIP2 = 12
77
+ # RESERVED = 13
78
+ COMPRESSION_METHOD_LZMA = 14
79
+ # RESERVED = 15
80
+ COMPRESSION_METHOD_IBM_CMPSC = 16
81
+ # RESERVED = 17
82
+ COMPRESSION_METHOD_IBM_TERSE = 18
83
+ COMPRESSION_METHOD_IBM_LZ77 = 19
84
+ COMPRESSION_METHOD_JPEG = 96
85
+ COMPRESSION_METHOD_WAVPACK = 97
86
+ COMPRESSION_METHOD_PPMD = 98
87
+ COMPRESSION_METHOD_AES = 99
88
+
89
+ COMPRESSION_METHODS = {
90
+ COMPRESSION_METHOD_STORE => 'Store (no compression)',
91
+ COMPRESSION_METHOD_SHRINK => 'Shrink',
92
+ COMPRESSION_METHOD_REDUCE_1 => 'Reduce with compression factor 1',
93
+ COMPRESSION_METHOD_REDUCE_2 => 'Reduce with compression factor 2',
94
+ COMPRESSION_METHOD_REDUCE_3 => 'Reduce with compression factor 3',
95
+ COMPRESSION_METHOD_REDUCE_4 => 'Reduce with compression factor 4',
96
+ COMPRESSION_METHOD_IMPLODE => 'Implode',
97
+ # RESERVED = 7
98
+ COMPRESSION_METHOD_DEFLATE => 'Deflate',
99
+ COMPRESSION_METHOD_DEFLATE_64 => 'Deflate64(tm)',
100
+ COMPRESSION_METHOD_PKWARE_DCLI => 'PKWARE Data Compression Library Imploding (old IBM TERSE)',
101
+ # RESERVED = 11
102
+ COMPRESSION_METHOD_BZIP2 => 'BZIP2',
103
+ # RESERVED = 13
104
+ COMPRESSION_METHOD_LZMA => 'LZMA',
105
+ # RESERVED = 15
106
+ COMPRESSION_METHOD_IBM_CMPSC => 'IBM z/OS CMPSC Compression',
107
+ # RESERVED = 17
108
+ COMPRESSION_METHOD_IBM_TERSE => 'IBM TERSE (new)',
109
+ COMPRESSION_METHOD_IBM_LZ77 => 'IBM LZ77 z Architecture (PFS)',
110
+ COMPRESSION_METHOD_JPEG => 'JPEG variant',
111
+ COMPRESSION_METHOD_WAVPACK => 'WavPack compressed data',
112
+ COMPRESSION_METHOD_PPMD => 'PPMd version I, Rev 1',
113
+ COMPRESSION_METHOD_AES => 'AES encryption'
60
114
  }.freeze
61
115
  end