rubyzip 2.3.2 → 3.7.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 (65) hide show
  1. checksums.yaml +4 -4
  2. data/Changelog.md +549 -0
  3. data/LICENSE.md +24 -0
  4. data/README.md +263 -49
  5. data/Rakefile +15 -13
  6. data/lib/rubyzip.rb +3 -0
  7. data/lib/zip/central_directory.rb +194 -125
  8. data/lib/zip/compressor.rb +3 -1
  9. data/lib/zip/constants.rb +30 -21
  10. data/lib/zip/crypto/aes_encryption.rb +213 -0
  11. data/lib/zip/crypto/decrypted_io.rb +27 -15
  12. data/lib/zip/crypto/encryption.rb +13 -2
  13. data/lib/zip/crypto/null_encryption.rb +5 -13
  14. data/lib/zip/crypto/traditional_encryption.rb +13 -7
  15. data/lib/zip/decompressor.rb +4 -3
  16. data/lib/zip/deflater.rb +12 -8
  17. data/lib/zip/dirtyable.rb +32 -0
  18. data/lib/zip/dos_time.rb +72 -7
  19. data/lib/zip/entry.rb +445 -244
  20. data/lib/zip/entry_set.rb +11 -9
  21. data/lib/zip/errors.rb +152 -16
  22. data/lib/zip/extra_field/aes.rb +50 -0
  23. data/lib/zip/extra_field/generic.rb +10 -11
  24. data/lib/zip/extra_field/ntfs.rb +6 -4
  25. data/lib/zip/extra_field/old_unix.rb +5 -3
  26. data/lib/zip/extra_field/universal_time.rb +3 -1
  27. data/lib/zip/extra_field/unix.rb +7 -5
  28. data/lib/zip/extra_field/unknown.rb +35 -0
  29. data/lib/zip/extra_field/zip64.rb +19 -5
  30. data/lib/zip/extra_field.rb +25 -23
  31. data/lib/zip/file.rb +272 -221
  32. data/lib/zip/file_split.rb +91 -0
  33. data/lib/zip/filesystem/dir.rb +86 -0
  34. data/lib/zip/filesystem/directory_iterator.rb +48 -0
  35. data/lib/zip/filesystem/file.rb +263 -0
  36. data/lib/zip/filesystem/file_stat.rb +110 -0
  37. data/lib/zip/filesystem/zip_file_name_mapper.rb +81 -0
  38. data/lib/zip/filesystem.rb +27 -596
  39. data/lib/zip/inflater.rb +13 -10
  40. data/lib/zip/input_stream.rb +102 -48
  41. data/lib/zip/ioextras/abstract_input_stream.rb +156 -58
  42. data/lib/zip/ioextras/abstract_output_stream.rb +15 -3
  43. data/lib/zip/ioextras/fake_io.rb +39 -0
  44. data/lib/zip/ioextras.rb +12 -16
  45. data/lib/zip/null_compressor.rb +3 -1
  46. data/lib/zip/null_decompressor.rb +6 -3
  47. data/lib/zip/null_input_stream.rb +3 -1
  48. data/lib/zip/output_stream.rb +74 -55
  49. data/lib/zip/pass_thru_compressor.rb +6 -3
  50. data/lib/zip/pass_thru_decompressor.rb +12 -9
  51. data/lib/zip/streamable_directory.rb +3 -1
  52. data/lib/zip/streamable_stream.rb +4 -1
  53. data/lib/zip/version.rb +4 -1
  54. data/lib/zip.rb +28 -4
  55. data/rubyzip.gemspec +39 -0
  56. data/samples/example.rb +8 -3
  57. data/samples/example_filesystem.rb +3 -2
  58. data/samples/example_recursive.rb +3 -1
  59. data/samples/gtk_ruby_zip.rb +5 -3
  60. data/samples/qtzip.rb +7 -6
  61. data/samples/write_simple.rb +2 -1
  62. data/samples/zipfind.rb +1 -0
  63. metadata +83 -49
  64. data/TODO +0 -15
  65. data/lib/zip/extra_field/zip64_placeholder.rb +0 -15
data/lib/zip/entry.rb CHANGED
@@ -1,21 +1,45 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'pathname'
4
+
5
+ require_relative 'constants'
6
+ require_relative 'dirtyable'
7
+
2
8
  module Zip
9
+ # Zip::Entry represents an entry in a Zip archive.
3
10
  class Entry
4
- STORED = 0
5
- DEFLATED = 8
11
+ include Dirtyable
12
+
13
+ # Constant used to specify that the entry is stored (i.e., not compressed).
14
+ STORED = ::Zip::COMPRESSION_METHOD_STORE
15
+
16
+ # Constant used to specify that the entry is deflated (i.e., compressed).
17
+ DEFLATED = ::Zip::COMPRESSION_METHOD_DEFLATE
18
+
6
19
  # Language encoding flag (EFS) bit
7
- EFS = 0b100000000000
8
-
9
- attr_accessor :comment, :compressed_size, :crc, :extra, :compression_method,
10
- :name, :size, :local_header_offset, :zipfile, :fstype, :external_file_attributes,
11
- :internal_file_attributes,
12
- :gp_flags, :header_signature, :follow_symlinks,
13
- :restore_times, :restore_permissions, :restore_ownership,
14
- :unix_uid, :unix_gid, :unix_perms,
15
- :dirty
16
- attr_reader :ftype, :filepath # :nodoc:
17
-
18
- def set_default_vars_values
20
+ EFS = 0b100000000000 # :nodoc:
21
+
22
+ # Compression level flags (used as part of the gp flags).
23
+ COMPRESSION_LEVEL_SUPERFAST_GPFLAG = 0b110 # :nodoc:
24
+ COMPRESSION_LEVEL_FAST_GPFLAG = 0b100 # :nodoc:
25
+ COMPRESSION_LEVEL_MAX_GPFLAG = 0b010 # :nodoc:
26
+
27
+ attr_accessor :comment, :compressed_size, :follow_symlinks, :name,
28
+ :restore_ownership, :restore_permissions, :restore_times,
29
+ :unix_gid, :unix_perms, :unix_uid
30
+
31
+ attr_accessor :crc, :external_file_attributes, :fstype, :gp_flags,
32
+ :internal_file_attributes, :local_header_offset # :nodoc:
33
+
34
+ attr_reader :extra, :compression_level, :filepath # :nodoc:
35
+
36
+ attr_writer :size # :nodoc:
37
+
38
+ mark_dirty :comment=, :compressed_size=, :external_file_attributes=,
39
+ :fstype=, :gp_flags=, :name=, :size=,
40
+ :unix_gid=, :unix_perms=, :unix_uid=
41
+
42
+ def set_default_vars_values # :nodoc:
19
43
  @local_header_offset = 0
20
44
  @local_header_size = nil # not known until local entry is created or read
21
45
  @internal_file_attributes = 1
@@ -34,178 +58,283 @@ module Zip
34
58
  end
35
59
  @follow_symlinks = false
36
60
 
37
- @restore_times = false
38
- @restore_permissions = false
39
- @restore_ownership = false
61
+ @restore_times = DEFAULT_RESTORE_OPTIONS[:restore_times]
62
+ @restore_permissions = DEFAULT_RESTORE_OPTIONS[:restore_permissions]
63
+ @restore_ownership = DEFAULT_RESTORE_OPTIONS[:restore_ownership]
40
64
  # BUG: need an extra field to support uid/gid's
41
65
  @unix_uid = nil
42
66
  @unix_gid = nil
43
67
  @unix_perms = nil
44
- # @posix_acl = nil
45
- # @ntfs_acl = nil
46
- @dirty = false
47
68
  end
48
69
 
49
- def check_name(name)
50
- return unless name.start_with?('/')
51
-
52
- raise ::Zip::EntryNameError, "Illegal ZipEntry name '#{name}', name must not start with /"
70
+ def check_name(name) # :nodoc:
71
+ raise EntryNameError, name if name.start_with?('/')
72
+ raise EntryNameError if name.length > 65_535
53
73
  end
54
74
 
55
- def initialize(*args)
56
- name = args[1] || ''
57
- check_name(name)
75
+ # Create a new Zip::Entry.
76
+ def initialize(
77
+ zipfile = '', name = '',
78
+ comment: '', size: nil, compressed_size: 0, crc: 0,
79
+ compression_method: DEFLATED,
80
+ compression_level: ::Zip.default_compression,
81
+ time: ::Zip::DOSTime.now, extra: ::Zip::ExtraField.new
82
+ )
83
+ super()
84
+ @name = name
85
+ check_name(@name)
58
86
 
59
87
  set_default_vars_values
60
88
  @fstype = ::Zip::RUNNING_ON_WINDOWS ? ::Zip::FSTYPE_FAT : ::Zip::FSTYPE_UNIX
61
89
 
62
- @zipfile = args[0] || ''
63
- @name = name
64
- @comment = args[2] || ''
65
- @extra = args[3] || ''
66
- @compressed_size = args[4] || 0
67
- @crc = args[5] || 0
68
- @compression_method = args[6] || ::Zip::Entry::DEFLATED
69
- @size = args[7] || 0
70
- @time = args[8] || ::Zip::DOSTime.now
71
-
72
- @ftype = name_is_directory? ? :directory : :file
73
- @extra = ::Zip::ExtraField.new(@extra.to_s) unless @extra.kind_of?(::Zip::ExtraField)
74
- end
75
-
90
+ @zipfile = zipfile
91
+ @comment = comment || ''
92
+ @compression_method = compression_method || DEFLATED
93
+ @compression_level = compression_level || ::Zip.default_compression
94
+ @compressed_size = compressed_size || 0
95
+ @crc = crc || 0
96
+ @size = size
97
+ @time = case time
98
+ when ::Zip::DOSTime
99
+ time
100
+ when Time
101
+ ::Zip::DOSTime.from_time(time)
102
+ else
103
+ ::Zip::DOSTime.now
104
+ end
105
+ @extra =
106
+ extra.kind_of?(ExtraField) ? extra : ExtraField.new(extra.to_s)
107
+
108
+ set_compression_level_flags
109
+ end
110
+
111
+ # Is this entry encrypted?
76
112
  def encrypted?
77
113
  gp_flags & 1 == 1
78
114
  end
79
115
 
80
- def incomplete?
81
- gp_flags & 8 == 8
116
+ def incomplete? # :nodoc:
117
+ (gp_flags & 8 == 8) && (crc == 0 || size == 0 || compressed_size == 0)
82
118
  end
83
119
 
84
- def time
85
- if @extra['UniversalTime']
86
- @extra['UniversalTime'].mtime
87
- elsif @extra['NTFS']
88
- @extra['NTFS'].mtime
89
- else
90
- # Standard time field in central directory has local time
91
- # under archive creator. Then, we can't get timezone.
92
- @time
93
- end
120
+ # The uncompressed size of the entry.
121
+ def size
122
+ @size || 0
123
+ end
124
+
125
+ # Get a timestamp component of this entry.
126
+ #
127
+ # Returns modification time by default.
128
+ def time(component: :mtime)
129
+ time =
130
+ if @extra[:universaltime]
131
+ @extra[:universaltime].send(component)
132
+ elsif @extra[:ntfs]
133
+ @extra[:ntfs].send(component)
134
+ end
135
+
136
+ # Standard time field in central directory has local time
137
+ # under archive creator. Then, we can't get timezone.
138
+ time || (@time if component == :mtime)
94
139
  end
95
140
 
96
141
  alias mtime time
97
142
 
98
- def time=(value)
99
- unless @extra.member?('UniversalTime') || @extra.member?('NTFS')
100
- @extra.create('UniversalTime')
143
+ # Get the last access time of this entry, if available.
144
+ def atime
145
+ time(component: :atime)
146
+ end
147
+
148
+ # Get the creation time of this entry, if available.
149
+ def ctime
150
+ time(component: :ctime)
151
+ end
152
+
153
+ # Set a timestamp component of this entry.
154
+ #
155
+ # Sets modification time by default.
156
+ def time=(value, component: :mtime)
157
+ @dirty = true
158
+ unless @extra.member?(:universaltime) || @extra.member?(:ntfs)
159
+ @extra.create(:universaltime)
101
160
  end
102
- (@extra['UniversalTime'] || @extra['NTFS']).mtime = value
103
- @time = value
161
+
162
+ value = DOSTime.from_time(value)
163
+ comp = "#{component}=" unless component.to_s.end_with?('=')
164
+ (@extra[:universaltime] || @extra[:ntfs]).send(comp, value)
165
+ @time = value if component == :mtime
104
166
  end
105
167
 
106
- def file_type_is?(type)
107
- raise InternalError, "current filetype is unknown: #{inspect}" unless @ftype
168
+ alias mtime= time=
169
+
170
+ # Set the last access time of this entry.
171
+ def atime=(value)
172
+ send(:time=, value, component: :atime)
173
+ end
108
174
 
109
- @ftype == type
175
+ # Set the creation time of this entry.
176
+ def ctime=(value)
177
+ send(:time=, value, component: :ctime)
178
+ end
179
+
180
+ # Does this entry return time fields with accurate timezone information?
181
+ def absolute_time?
182
+ @extra.member?(:universaltime) || @extra.member?(:ntfs)
183
+ end
184
+
185
+ # Return the compression method for this entry.
186
+ #
187
+ # Returns STORED if the entry is a directory or if the compression
188
+ # level is 0.
189
+ def compression_method
190
+ return STORED if ftype == :directory || @compression_level == 0
191
+
192
+ @compression_method
193
+ end
194
+
195
+ # Set the compression method for this entry.
196
+ def compression_method=(method)
197
+ @dirty = true
198
+ @compression_method = (ftype == :directory ? STORED : method)
199
+ end
200
+
201
+ # Does this entry use the ZIP64 extensions?
202
+ def zip64?
203
+ !@extra[:zip64].nil?
204
+ end
205
+
206
+ # Is this entry encrypted with AES encryption?
207
+ def aes?
208
+ !@extra[:aes].nil?
209
+ end
210
+
211
+ # Called by `AESEncrypter#prepare_entry` before the local header is first
212
+ # written, so the extra field is in place before its size is measured.
213
+ # Directories have no content to encrypt, so they are left untouched.
214
+ def prep_aes_extra(vendor_version, strength) # :nodoc:
215
+ return if directory?
216
+
217
+ @version_needed_to_extract =
218
+ [@version_needed_to_extract, Zip::VERSION_NEEDED_TO_EXTRACT_AES].max
219
+ aes = @extra[:aes] || @extra.create(:aes)
220
+ aes.vendor_version = vendor_version
221
+ aes.vendor_id = 'AE'
222
+ aes.encryption_strength = strength
223
+ aes.compression_method = compression_method
224
+ end
225
+
226
+ def file_type_is?(type) # :nodoc:
227
+ ftype == type
228
+ end
229
+
230
+ def ftype # :nodoc:
231
+ @ftype ||= name_is_directory? ? :directory : :file
110
232
  end
111
233
 
112
234
  # Dynamic checkers
113
235
  %w[directory file symlink].each do |k|
114
- define_method "#{k}?" do
236
+ define_method :"#{k}?" do
115
237
  file_type_is?(k.to_sym)
116
238
  end
117
239
  end
118
240
 
119
- def name_is_directory? #:nodoc:all
241
+ def name_is_directory? # :nodoc:
120
242
  @name.end_with?('/')
121
243
  end
122
244
 
123
245
  # Is the name a relative path, free of `..` patterns that could lead to
124
246
  # path traversal attacks? This does NOT handle symlinks; if the path
125
247
  # contains symlinks, this check is NOT enough to guarantee safety.
126
- def name_safe?
248
+ def name_safe? # :nodoc:
127
249
  cleanpath = Pathname.new(@name).cleanpath
128
250
  return false unless cleanpath.relative?
129
251
 
130
252
  root = ::File::SEPARATOR
131
- naive_expanded_path = ::File.join(root, cleanpath.to_s)
132
- ::File.absolute_path(cleanpath.to_s, root) == naive_expanded_path
253
+ naive = Regexp.escape(::File.join(root, cleanpath.to_s))
254
+ # Allow for Windows drive mappings at the root.
255
+ ::File.absolute_path(cleanpath.to_s, root).match?(/([A-Z]:)?#{naive}/i)
133
256
  end
134
257
 
135
- def local_entry_offset #:nodoc:all
258
+ def local_entry_offset # :nodoc:
136
259
  local_header_offset + @local_header_size
137
260
  end
138
261
 
139
- def name_size
262
+ def name_size # :nodoc:
140
263
  @name ? @name.bytesize : 0
141
264
  end
142
265
 
143
- def extra_size
266
+ def extra_size # :nodoc:
144
267
  @extra ? @extra.local_size : 0
145
268
  end
146
269
 
147
- def comment_size
270
+ def comment_size # :nodoc:
148
271
  @comment ? @comment.bytesize : 0
149
272
  end
150
273
 
151
- def calculate_local_header_size #:nodoc:all
274
+ def calculate_local_header_size # :nodoc:
152
275
  LOCAL_ENTRY_STATIC_HEADER_LENGTH + name_size + extra_size
153
276
  end
154
277
 
155
278
  # check before rewriting an entry (after file sizes are known)
156
279
  # that we didn't change the header size (and thus clobber file data or something)
157
- def verify_local_header_size!
280
+ def verify_local_header_size! # :nodoc:
158
281
  return if @local_header_size.nil?
159
282
 
160
283
  new_size = calculate_local_header_size
161
- raise Error, "local header size changed (#{@local_header_size} -> #{new_size})" if @local_header_size != new_size
284
+ return unless @local_header_size != new_size
285
+
286
+ raise Error,
287
+ "Local header size changed (#{@local_header_size} -> #{new_size})"
162
288
  end
163
289
 
164
- def cdir_header_size #:nodoc:all
290
+ def cdir_header_size # :nodoc:
165
291
  CDIR_ENTRY_STATIC_HEADER_LENGTH + name_size +
166
292
  (@extra ? @extra.c_dir_size : 0) + comment_size
167
293
  end
168
294
 
169
- def next_header_offset #:nodoc:all
170
- local_entry_offset + compressed_size + data_descriptor_size
171
- end
172
-
173
- # Extracts entry to file dest_path (defaults to @name).
174
- # NB: The caller is responsible for making sure dest_path is safe, if it
175
- # is passed.
176
- def extract(dest_path = nil, &block)
177
- if dest_path.nil? && !name_safe?
178
- warn "WARNING: skipped '#{@name}' as unsafe."
295
+ def next_header_offset # :nodoc:
296
+ local_entry_offset + compressed_size
297
+ end
298
+
299
+ # Extracts this entry to a file at `entry_path`, with
300
+ # `destination_directory` as the base location in the filesystem.
301
+ #
302
+ # NB: The caller is responsible for making sure `destination_directory` is
303
+ # safe, if it is passed.
304
+ #
305
+ # `create_parent_directories`, if true, creates any missing intermediate
306
+ # directories for a file entry before writing it. This defaults to
307
+ # false: a missing intermediate directory usually means an earlier
308
+ # entry (e.g. a symlink) was deliberately skipped as unsafe, and letting
309
+ # a later entry silently succeed by re-creating that path as a plain
310
+ # directory would mask that. Bulk operations such as `extract_all`,
311
+ # which have no such earlier-entry to skip past, opt into this.
312
+ def extract(entry_path = @name, destination_directory: '.',
313
+ create_parent_directories: false, &block)
314
+ dest_dir = ::File.absolute_path(destination_directory || '.')
315
+ extract_path = ::File.absolute_path(::File.join(dest_dir, entry_path))
316
+
317
+ unless extract_path.start_with?(dest_dir + ::File::SEPARATOR) || dest_dir == ::File::SEPARATOR
318
+ warn "WARNING: skipped extracting '#{@name}' to '#{extract_path}' as unsafe."
179
319
  return self
180
320
  end
181
321
 
182
- dest_path ||= @name
183
322
  block ||= proc { ::Zip.on_exists_proc }
184
323
 
185
324
  raise "unknown file type #{inspect}" unless directory? || file? || symlink?
186
325
 
187
- __send__("create_#{@ftype}", dest_path, &block)
326
+ ::FileUtils.mkdir_p(::File.dirname(extract_path)) if create_parent_directories && file?
327
+
328
+ __send__(:"create_#{ftype}", extract_path, &block)
188
329
  self
189
330
  end
190
331
 
191
- def to_s
332
+ def to_s # :nodoc:
192
333
  @name
193
334
  end
194
335
 
195
336
  class << self
196
- def read_zip_short(io) # :nodoc:
197
- io.read(2).unpack1('v')
198
- end
199
-
200
- def read_zip_long(io) # :nodoc:
201
- io.read(4).unpack1('V')
202
- end
203
-
204
- def read_zip_64_long(io) # :nodoc:
205
- io.read(8).unpack1('Q<')
206
- end
207
-
208
- def read_c_dir_entry(io) #:nodoc:all
337
+ def read_c_dir_entry(io) # :nodoc:
209
338
  path = if io.respond_to?(:path)
210
339
  io.path
211
340
  else
@@ -218,16 +347,18 @@ module Zip
218
347
  nil
219
348
  end
220
349
 
221
- def read_local_entry(io)
350
+ def read_local_entry(io) # :nodoc:
222
351
  entry = new(io)
223
352
  entry.read_local_entry(io)
224
353
  entry
354
+ rescue SplitArchiveError
355
+ raise
225
356
  rescue Error
226
357
  nil
227
358
  end
228
359
  end
229
360
 
230
- def unpack_local_entry(buf)
361
+ def unpack_local_entry(buf) # :nodoc:
231
362
  @header_signature,
232
363
  @version,
233
364
  @fstype,
@@ -242,63 +373,76 @@ module Zip
242
373
  @extra_length = buf.unpack('VCCvvvvVVVvv')
243
374
  end
244
375
 
245
- def read_local_entry(io) #:nodoc:all
246
- @local_header_offset = io.tell
376
+ def read_local_entry(io) # :nodoc:
377
+ @dirty = false # No changes at this point.
378
+ current_offset = io.tell
247
379
 
248
- static_sized_fields_buf = io.read(::Zip::LOCAL_ENTRY_STATIC_HEADER_LENGTH) || ''
380
+ read_local_header_fields(io)
249
381
 
250
- unless static_sized_fields_buf.bytesize == ::Zip::LOCAL_ENTRY_STATIC_HEADER_LENGTH
251
- raise Error, 'Premature end of file. Not enough data for zip entry local header'
252
- end
382
+ if @header_signature == SPLIT_FILE_SIGNATURE
383
+ raise SplitArchiveError if current_offset.zero?
253
384
 
254
- unpack_local_entry(static_sized_fields_buf)
385
+ # Rewind, skipping the data descriptor, then try to read the local header again.
386
+ current_offset += 16
387
+ io.seek(current_offset)
388
+ read_local_header_fields(io)
389
+ end
255
390
 
256
- unless @header_signature == ::Zip::LOCAL_ENTRY_SIGNATURE
257
- raise ::Zip::Error, "Zip local header magic not found at location '#{local_header_offset}'"
391
+ unless @header_signature == LOCAL_ENTRY_SIGNATURE
392
+ raise Error, "Zip local header magic not found at location '#{current_offset}'"
258
393
  end
259
394
 
395
+ @local_header_offset = current_offset
396
+
260
397
  set_time(@last_mod_date, @last_mod_time)
261
398
 
262
399
  @name = io.read(@name_length)
263
- extra = io.read(@extra_length)
264
-
265
- @name.tr!('\\', '/')
266
400
  if ::Zip.force_entry_names_encoding
267
401
  @name.force_encoding(::Zip.force_entry_names_encoding)
268
402
  end
403
+ @name.tr!('\\', '/') # Normalise filepath separators after encoding set.
404
+
405
+ # We need to do this here because `initialize` has so many side-effects.
406
+ # :-(
407
+ @ftype = name_is_directory? ? :directory : :file
269
408
 
409
+ extra = io.read(@extra_length)
270
410
  if extra && extra.bytesize != @extra_length
271
411
  raise ::Zip::Error, 'Truncated local zip entry header'
272
412
  end
273
413
 
274
- if @extra.kind_of?(::Zip::ExtraField)
275
- @extra.merge(extra) if extra
276
- else
277
- @extra = ::Zip::ExtraField.new(extra)
278
- end
279
-
414
+ read_extra_field(extra, local: true)
280
415
  parse_zip64_extra(true)
416
+ parse_aes_extra
281
417
  @local_header_size = calculate_local_header_size
282
418
  end
283
419
 
284
- def pack_local_entry
285
- zip64 = @extra['Zip64']
420
+ def pack_local_entry # :nodoc:
421
+ zip64 = @extra[:zip64]
286
422
  [::Zip::LOCAL_ENTRY_SIGNATURE,
287
423
  @version_needed_to_extract, # version needed to extract
288
424
  @gp_flags, # @gp_flags
289
- @compression_method,
425
+ stored_compression_method,
290
426
  @time.to_binary_dos_time, # @last_mod_time
291
427
  @time.to_binary_dos_date, # @last_mod_date
292
428
  @crc,
293
429
  zip64 && zip64.compressed_size ? 0xFFFFFFFF : @compressed_size,
294
- zip64 && zip64.original_size ? 0xFFFFFFFF : @size,
430
+ zip64 && zip64.original_size ? 0xFFFFFFFF : (@size || 0),
295
431
  name_size,
296
432
  @extra ? @extra.local_size : 0].pack('VvvvvvVVVvv')
297
433
  end
298
434
 
299
- def write_local_entry(io, rewrite = false) #:nodoc:all
300
- prep_zip64_extra(true)
301
- verify_local_header_size! if rewrite
435
+ def write_local_entry(io, suppress_extra_fields: false, rewrite: false) # :nodoc:
436
+ prep_local_zip64_extra
437
+
438
+ # If we are rewriting the local header, then we verify that we haven't changed
439
+ # its size. At this point we have to keep extra fields if they are present.
440
+ if rewrite
441
+ verify_local_header_size!
442
+ elsif suppress_extra_fields
443
+ @extra.suppress_fields!(suppress_extra_fields)
444
+ end
445
+
302
446
  @local_header_offset = io.tell
303
447
 
304
448
  io << pack_local_entry
@@ -308,7 +452,7 @@ module Zip
308
452
  @local_header_size = io.tell - @local_header_offset
309
453
  end
310
454
 
311
- def unpack_c_dir_entry(buf)
455
+ def unpack_c_dir_entry(buf) # :nodoc:
312
456
  @header_signature,
313
457
  @version, # version of encoding software
314
458
  @fstype, # filesystem type
@@ -326,13 +470,10 @@ module Zip
326
470
  _, # diskNumberStart
327
471
  @internal_file_attributes,
328
472
  @external_file_attributes,
329
- @local_header_offset,
330
- @name,
331
- @extra,
332
- @comment = buf.unpack('VCCvvvvvVVVvvvvvVV')
473
+ @local_header_offset = buf.unpack('VCCvvvvvVVVvvvvvVV')
333
474
  end
334
475
 
335
- def set_ftype_from_c_dir_entry
476
+ def set_ftype_from_c_dir_entry # :nodoc:
336
477
  @ftype = case @fstype
337
478
  when ::Zip::FSTYPE_UNIX
338
479
  @unix_perms = (@external_file_attributes >> 16) & 0o7777
@@ -344,8 +485,9 @@ module Zip
344
485
  when ::Zip::FILE_TYPE_SYMLINK
345
486
  :symlink
346
487
  else
347
- # best case guess for whether it is a file or not
348
- # Otherwise this would be set to unknown and that entry would never be able to extracted
488
+ # Best case guess for whether it is a file or not.
489
+ # Otherwise this would be set to unknown and that
490
+ # entry would never be able to be extracted.
349
491
  if name_is_directory?
350
492
  :directory
351
493
  else
@@ -361,47 +503,52 @@ module Zip
361
503
  end
362
504
  end
363
505
 
364
- def check_c_dir_entry_static_header_length(buf)
365
- return if buf.bytesize == ::Zip::CDIR_ENTRY_STATIC_HEADER_LENGTH
506
+ def check_c_dir_entry_static_header_length(buf) # :nodoc:
507
+ return unless buf.nil? || buf.bytesize != ::Zip::CDIR_ENTRY_STATIC_HEADER_LENGTH
366
508
 
367
509
  raise Error, 'Premature end of file. Not enough data for zip cdir entry header'
368
510
  end
369
511
 
370
- def check_c_dir_entry_signature
371
- return if header_signature == ::Zip::CENTRAL_DIRECTORY_ENTRY_SIGNATURE
512
+ def check_c_dir_entry_signature # :nodoc:
513
+ return if @header_signature == ::Zip::CENTRAL_DIRECTORY_ENTRY_SIGNATURE
372
514
 
373
515
  raise Error, "Zip local header magic not found at location '#{local_header_offset}'"
374
516
  end
375
517
 
376
- def check_c_dir_entry_comment_size
518
+ def check_c_dir_entry_comment_size # :nodoc:
377
519
  return if @comment && @comment.bytesize == @comment_length
378
520
 
379
521
  raise ::Zip::Error, 'Truncated cdir zip entry header'
380
522
  end
381
523
 
382
- def read_c_dir_extra_field(io)
524
+ def read_extra_field(buf, local: false) # :nodoc:
383
525
  if @extra.kind_of?(::Zip::ExtraField)
384
- @extra.merge(io.read(@extra_length))
526
+ @extra.merge(buf, local: local) if buf
385
527
  else
386
- @extra = ::Zip::ExtraField.new(io.read(@extra_length))
528
+ @extra = ::Zip::ExtraField.new(buf, local: local)
387
529
  end
388
530
  end
389
531
 
390
- def read_c_dir_entry(io) #:nodoc:all
532
+ def read_c_dir_entry(io) # :nodoc:
533
+ @dirty = false # No changes at this point.
391
534
  static_sized_fields_buf = io.read(::Zip::CDIR_ENTRY_STATIC_HEADER_LENGTH)
392
535
  check_c_dir_entry_static_header_length(static_sized_fields_buf)
393
536
  unpack_c_dir_entry(static_sized_fields_buf)
394
537
  check_c_dir_entry_signature
395
538
  set_time(@last_mod_date, @last_mod_time)
539
+
396
540
  @name = io.read(@name_length)
397
541
  if ::Zip.force_entry_names_encoding
398
542
  @name.force_encoding(::Zip.force_entry_names_encoding)
399
543
  end
400
- read_c_dir_extra_field(io)
544
+ @name.tr!('\\', '/') # Normalise filepath separators after encoding set.
545
+
546
+ read_extra_field(io.read(@extra_length))
401
547
  @comment = io.read(@comment_length)
402
548
  check_c_dir_entry_comment_size
403
549
  set_ftype_from_c_dir_entry
404
550
  parse_zip64_extra(false)
551
+ parse_aes_extra
405
552
  end
406
553
 
407
554
  def file_stat(path) # :nodoc:
@@ -413,27 +560,27 @@ module Zip
413
560
  end
414
561
 
415
562
  def get_extra_attributes_from_path(path) # :nodoc:
416
- return if Zip::RUNNING_ON_WINDOWS
563
+ stat = file_stat(path)
564
+ @time = DOSTime.from_time(stat.mtime)
565
+ return if ::Zip::RUNNING_ON_WINDOWS
417
566
 
418
- stat = file_stat(path)
419
567
  @unix_uid = stat.uid
420
568
  @unix_gid = stat.gid
421
569
  @unix_perms = stat.mode & 0o7777
422
- @time = ::Zip::DOSTime.from_time(stat.mtime)
423
570
  end
424
571
 
425
- def set_unix_attributes_on_path(dest_path)
426
- # ignore setuid/setgid bits by default. honor if @restore_ownership
427
- unix_perms_mask = 0o1777
428
- unix_perms_mask = 0o7777 if @restore_ownership
429
- ::FileUtils.chmod(@unix_perms & unix_perms_mask, dest_path) if @restore_permissions && @unix_perms
430
- ::FileUtils.chown(@unix_uid, @unix_gid, dest_path) if @restore_ownership && @unix_uid && @unix_gid && ::Process.egid == 0
431
-
432
- # Restore the timestamp on a file. This will either have come from the
433
- # original source file that was copied into the archive, or from the
434
- # creation date of the archive if there was no original source file.
435
- ::FileUtils.touch(dest_path, mtime: time) if @restore_times
572
+ # rubocop:disable Style/GuardClause
573
+ def set_unix_attributes_on_path(dest_path) # :nodoc:
574
+ # Ignore setuid/setgid bits by default. Honour if @restore_ownership.
575
+ unix_perms_mask = (@restore_ownership ? 0o7777 : 0o1777)
576
+ if @restore_permissions && @unix_perms
577
+ ::FileUtils.chmod(@unix_perms & unix_perms_mask, dest_path)
578
+ end
579
+ if @restore_ownership && @unix_uid && @unix_gid && ::Process.egid == 0
580
+ ::FileUtils.chown(@unix_uid, @unix_gid, dest_path)
581
+ end
436
582
  end
583
+ # rubocop:enable Style/GuardClause
437
584
 
438
585
  def set_extra_attributes_on_path(dest_path) # :nodoc:
439
586
  return unless file? || directory?
@@ -442,40 +589,43 @@ module Zip
442
589
  when ::Zip::FSTYPE_UNIX
443
590
  set_unix_attributes_on_path(dest_path)
444
591
  end
592
+
593
+ # Restore the timestamp on a file. This will either have come from the
594
+ # original source file that was copied into the archive, or from the
595
+ # creation date of the archive if there was no original source file.
596
+ ::FileUtils.touch(dest_path, mtime: time) if @restore_times
445
597
  end
446
598
 
447
- def pack_c_dir_entry
448
- zip64 = @extra['Zip64']
599
+ def pack_c_dir_entry # :nodoc:
600
+ zip64 = @extra[:zip64]
449
601
  [
450
602
  @header_signature,
451
603
  @version, # version of encoding software
452
604
  @fstype, # filesystem type
453
605
  @version_needed_to_extract, # @versionNeededToExtract
454
606
  @gp_flags, # @gp_flags
455
- @compression_method,
607
+ stored_compression_method,
456
608
  @time.to_binary_dos_time, # @last_mod_time
457
609
  @time.to_binary_dos_date, # @last_mod_date
458
610
  @crc,
459
611
  zip64 && zip64.compressed_size ? 0xFFFFFFFF : @compressed_size,
460
- zip64 && zip64.original_size ? 0xFFFFFFFF : @size,
612
+ zip64 && zip64.original_size ? 0xFFFFFFFF : (@size || 0),
461
613
  name_size,
462
614
  @extra ? @extra.c_dir_size : 0,
463
615
  comment_size,
464
616
  zip64 && zip64.disk_start_number ? 0xFFFF : 0, # disk number start
465
617
  @internal_file_attributes, # file type (binary=0, text=1)
466
618
  @external_file_attributes, # native filesystem attributes
467
- zip64 && zip64.relative_header_offset ? 0xFFFFFFFF : @local_header_offset,
468
- @name,
469
- @extra,
470
- @comment
619
+ zip64 && zip64.relative_header_offset ? 0xFFFFFFFF : @local_header_offset
471
620
  ].pack('VCCvvvvvVVVvvvvvVV')
472
621
  end
473
622
 
474
- def write_c_dir_entry(io) #:nodoc:all
475
- prep_zip64_extra(false)
623
+ def write_c_dir_entry(io, suppress_extra_fields: false) # :nodoc:
624
+ prep_cdir_zip64_extra
625
+
476
626
  case @fstype
477
627
  when ::Zip::FSTYPE_UNIX
478
- ft = case @ftype
628
+ ft = case ftype
479
629
  when :file
480
630
  @unix_perms ||= 0o644
481
631
  ::Zip::FILE_TYPE_FILE
@@ -488,10 +638,11 @@ module Zip
488
638
  end
489
639
 
490
640
  unless ft.nil?
491
- @external_file_attributes = (ft << 12 | (@unix_perms & 0o7777)) << 16
641
+ @external_file_attributes = ((ft << 12) | (@unix_perms & 0o7777)) << 16
492
642
  end
493
643
  end
494
644
 
645
+ @extra.suppress_fields!(suppress_extra_fields) if suppress_extra_fields
495
646
  io << pack_c_dir_entry
496
647
 
497
648
  io << @name
@@ -499,43 +650,43 @@ module Zip
499
650
  io << @comment
500
651
  end
501
652
 
502
- def ==(other)
653
+ def ==(other) # :nodoc:
503
654
  return false unless other.class == self.class
504
655
 
505
656
  # Compares contents of local entry and exposed fields
506
- keys_equal = %w[compression_method crc compressed_size size name extra filepath].all? do |k|
657
+ %w[compression_method crc compressed_size size name extra filepath time].all? do |k|
507
658
  other.__send__(k.to_sym) == __send__(k.to_sym)
508
659
  end
509
- keys_equal && time.dos_equals(other.time)
510
660
  end
511
661
 
512
- def <=>(other)
662
+ def <=>(other) # :nodoc:
513
663
  to_s <=> other.to_s
514
664
  end
515
665
 
516
666
  # Returns an IO like object for the given ZipEntry.
667
+ # Optional decrypter can be provided for an encrypted entry
517
668
  # Warning: may behave weird with symlinks.
518
- def get_input_stream(&block)
519
- if @ftype == :directory
520
- yield ::Zip::NullInputStream if block_given?
669
+ def get_input_stream(decrypter: nil, &block)
670
+ if ftype == :directory
671
+ yield ::Zip::NullInputStream if block
521
672
  ::Zip::NullInputStream
522
673
  elsif @filepath
523
- case @ftype
674
+ case ftype
524
675
  when :file
525
676
  ::File.open(@filepath, 'rb', &block)
526
677
  when :symlink
527
678
  linkpath = ::File.readlink(@filepath)
528
679
  stringio = ::StringIO.new(linkpath)
529
- yield(stringio) if block_given?
680
+ yield(stringio) if block
530
681
  stringio
531
682
  else
532
- raise "unknown @file_type #{@ftype}"
683
+ raise "unknown @file_type #{ftype}"
533
684
  end
534
685
  else
535
- zis = ::Zip::InputStream.new(@zipfile, local_header_offset)
686
+ zis = ::Zip::InputStream.new(@zipfile, offset: local_header_offset, decrypter: decrypter)
536
687
  zis.instance_variable_set(:@complete_entry, self)
537
688
  zis.get_next_entry
538
- if block_given?
689
+ if block
539
690
  begin
540
691
  yield(zis)
541
692
  ensure
@@ -553,8 +704,8 @@ module Zip
553
704
  when 'file'
554
705
  if name_is_directory?
555
706
  raise ArgumentError,
556
- "entry name '#{newEntry}' indicates directory entry, but " \
557
- "'#{src_path}' is not a directory"
707
+ "entry name '#{@name}' indicates a directory entry, but " \
708
+ "'#{src_path}' is not a directory"
558
709
  end
559
710
  :file
560
711
  when 'directory'
@@ -563,8 +714,8 @@ module Zip
563
714
  when 'link'
564
715
  if name_is_directory?
565
716
  raise ArgumentError,
566
- "entry name '#{newEntry}' indicates directory entry, but " \
567
- "'#{src_path}' is not a directory"
717
+ "entry name '#{@name}' indicates a directory entry, but " \
718
+ "'#{src_path}' is not a directory"
568
719
  end
569
720
  :symlink
570
721
  else
@@ -572,27 +723,30 @@ module Zip
572
723
  end
573
724
 
574
725
  @filepath = src_path
726
+ @size = stat.size
575
727
  get_extra_attributes_from_path(@filepath)
576
728
  end
577
729
 
578
- def write_to_zip_output_stream(zip_output_stream) #:nodoc:all
579
- if @ftype == :directory
580
- zip_output_stream.put_next_entry(self, nil, nil, ::Zip::Entry::STORED)
730
+ def write_to_zip_output_stream(zip_output_stream) # :nodoc:
731
+ if ftype == :directory
732
+ zip_output_stream.put_next_entry(self)
581
733
  elsif @filepath
582
- zip_output_stream.put_next_entry(self, nil, nil, compression_method || ::Zip::Entry::DEFLATED)
583
- get_input_stream { |is| ::Zip::IOExtras.copy_stream(zip_output_stream, is) }
734
+ zip_output_stream.put_next_entry(self)
735
+ get_input_stream do |is|
736
+ ::Zip::IOExtras.copy_stream(zip_output_stream, is)
737
+ end
584
738
  else
585
739
  zip_output_stream.copy_raw_entry(self)
586
740
  end
587
741
  end
588
742
 
589
- def parent_as_string
743
+ def parent_as_string # :nodoc:
590
744
  entry_name = name.chomp('/')
591
745
  slash_index = entry_name.rindex('/')
592
746
  slash_index ? entry_name.slice(0, slash_index + 1) : nil
593
747
  end
594
748
 
595
- def get_raw_input_stream(&block)
749
+ def get_raw_input_stream(&block) # :nodoc:
596
750
  if @zipfile.respond_to?(:seek) && @zipfile.respond_to?(:read)
597
751
  yield @zipfile
598
752
  else
@@ -600,12 +754,22 @@ module Zip
600
754
  end
601
755
  end
602
756
 
603
- def clean_up
604
- # By default, do nothing
757
+ def clean_up # :nodoc:
758
+ @dirty = false # Any changes are written at this point.
605
759
  end
606
760
 
607
761
  private
608
762
 
763
+ def read_local_header_fields(io) # :nodoc:
764
+ static_sized_fields_buf = io.read(::Zip::LOCAL_ENTRY_STATIC_HEADER_LENGTH) || ''
765
+
766
+ unless static_sized_fields_buf.bytesize == ::Zip::LOCAL_ENTRY_STATIC_HEADER_LENGTH
767
+ raise Error, 'Premature end of file. Not enough data for zip entry local header'
768
+ end
769
+
770
+ unpack_local_entry(static_sized_fields_buf)
771
+ end
772
+
609
773
  def set_time(binary_dos_date, binary_dos_time)
610
774
  @time = ::Zip::DOSTime.parse_binary_dos_format(binary_dos_date, binary_dos_time)
611
775
  rescue ArgumentError
@@ -614,23 +778,22 @@ module Zip
614
778
 
615
779
  def create_file(dest_path, _continue_on_exists_proc = proc { Zip.continue_on_exists_proc })
616
780
  if ::File.exist?(dest_path) && !yield(self, dest_path)
617
- raise ::Zip::DestinationFileExistsError,
618
- "Destination '#{dest_path}' already exists"
781
+ raise DestinationExistsError, dest_path
619
782
  end
783
+
620
784
  ::File.open(dest_path, 'wb') do |os|
621
785
  get_input_stream do |is|
622
786
  bytes_written = 0
623
787
  warned = false
624
- buf = +''
625
- while (buf = is.sysread(::Zip::Decompressor::CHUNK_SIZE, buf))
788
+ while (buf = is.sysread(Decompressor::CHUNK_SIZE))
626
789
  os << buf
627
790
  bytes_written += buf.bytesize
628
791
  next unless bytes_written > size && !warned
629
792
 
630
- message = "entry '#{name}' should be #{size}B, but is larger when inflated."
631
- raise ::Zip::EntrySizeError, message if ::Zip.validate_entry_sizes
793
+ error = EntrySizeError.new(self)
794
+ raise error if Zip.validate_entry_sizes
632
795
 
633
- warn "WARNING: #{message}"
796
+ warn "WARNING: #{error.message}"
634
797
  warned = true
635
798
  end
636
799
  end
@@ -643,14 +806,11 @@ module Zip
643
806
  return if ::File.directory?(dest_path)
644
807
 
645
808
  if ::File.exist?(dest_path)
646
- if block_given? && yield(self, dest_path)
647
- ::FileUtils.rm_f dest_path
648
- else
649
- raise ::Zip::DestinationFileExistsError,
650
- "Cannot create directory '#{dest_path}'. " \
651
- 'A file already exists with that name'
652
- end
809
+ raise ::Zip::DestinationExistsError, dest_path unless block_given? && yield(self, dest_path)
810
+
811
+ ::FileUtils.rm_f dest_path
653
812
  end
813
+
654
814
  ::FileUtils.mkdir_p(dest_path)
655
815
  set_extra_attributes_on_path(dest_path)
656
816
  end
@@ -664,53 +824,94 @@ module Zip
664
824
 
665
825
  # apply missing data from the zip64 extra information field, if present
666
826
  # (required when file sizes exceed 2**32, but can be used for all files)
667
- def parse_zip64_extra(for_local_header) #:nodoc:all
668
- return if @extra['Zip64'].nil?
827
+ def parse_zip64_extra(for_local_header) # :nodoc:
828
+ return unless zip64?
669
829
 
670
830
  if for_local_header
671
- @size, @compressed_size = @extra['Zip64'].parse(@size, @compressed_size)
831
+ @size, @compressed_size = @extra[:zip64].parse(@size, @compressed_size)
672
832
  else
673
- @size, @compressed_size, @local_header_offset = @extra['Zip64'].parse(@size, @compressed_size, @local_header_offset)
833
+ @size, @compressed_size, @local_header_offset = @extra[:zip64].parse(
834
+ @size, @compressed_size, @local_header_offset
835
+ )
674
836
  end
675
837
  end
676
838
 
677
- def data_descriptor_size
678
- (@gp_flags & 0x0008) > 0 ? 16 : 0
839
+ def parse_aes_extra # :nodoc:
840
+ return unless aes?
841
+
842
+ if @extra[:aes].vendor_id != 'AE'
843
+ raise Error, "Unsupported encryption method #{@extra[:aes].vendor_id}"
844
+ end
845
+
846
+ unless ::Zip::AESEncryption::VERSIONS.include? @extra[:aes].vendor_version
847
+ raise Error, "Unsupported encryption style #{@extra[:aes].vendor_version}"
848
+ end
849
+
850
+ @compression_method = @extra[:aes].compression_method if ftype != :directory
851
+ end
852
+
853
+ # AES-encrypted entries always store `COMPRESSION_METHOD_AES` (99) on the
854
+ # wire; the real compression method lives in the AES extra field instead
855
+ # (see `parse_aes_extra`, which reverses this on read).
856
+ def stored_compression_method # :nodoc:
857
+ aes? ? COMPRESSION_METHOD_AES : compression_method
858
+ end
859
+
860
+ # For DEFLATED compression *only*: set the general purpose flags 1 and 2 to
861
+ # indicate compression level. This seems to be mainly cosmetic but they are
862
+ # generally set by other tools - including in docx files. It is these flags
863
+ # that are used by commandline tools (and elsewhere) to give an indication
864
+ # of how compressed a file is. See the PKWARE APPNOTE for more information:
865
+ # https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT
866
+ #
867
+ # It's safe to simply OR these flags here as compression_level is read only.
868
+ def set_compression_level_flags
869
+ return unless compression_method == DEFLATED
870
+
871
+ case @compression_level
872
+ when 1
873
+ @gp_flags |= COMPRESSION_LEVEL_SUPERFAST_GPFLAG
874
+ when 2
875
+ @gp_flags |= COMPRESSION_LEVEL_FAST_GPFLAG
876
+ when 8, 9
877
+ @gp_flags |= COMPRESSION_LEVEL_MAX_GPFLAG
878
+ end
679
879
  end
680
880
 
681
- # create a zip64 extra information field if we need one
682
- def prep_zip64_extra(for_local_header) #:nodoc:all
881
+ # rubocop:disable Style/GuardClause
882
+ def prep_local_zip64_extra
683
883
  return unless ::Zip.write_zip64_support
884
+ return if (!zip64? && @size && @size < 0xFFFFFFFF) || !file?
885
+
886
+ # Might not know size here, so need ZIP64 just in case.
887
+ # If we already have a ZIP64 extra (placeholder) then we must fill it in.
888
+ if zip64? || @size.nil? || @size >= 0xFFFFFFFF || @compressed_size >= 0xFFFFFFFF
889
+ @version_needed_to_extract =
890
+ [@version_needed_to_extract, VERSION_NEEDED_TO_EXTRACT_ZIP64].max
891
+ zip64 = @extra[:zip64] || @extra.create(:zip64)
892
+
893
+ # Local header always includes size and compressed size.
894
+ zip64.original_size = @size || 0
895
+ zip64.compressed_size = @compressed_size
896
+ end
897
+ end
684
898
 
685
- need_zip64 = @size >= 0xFFFFFFFF || @compressed_size >= 0xFFFFFFFF
686
- need_zip64 ||= @local_header_offset >= 0xFFFFFFFF unless for_local_header
687
- if need_zip64
688
- @version_needed_to_extract = VERSION_NEEDED_TO_EXTRACT_ZIP64
689
- @extra.delete('Zip64Placeholder')
690
- zip64 = @extra.create('Zip64')
691
- if for_local_header
692
- # local header always includes size and compressed size
693
- zip64.original_size = @size
694
- zip64.compressed_size = @compressed_size
695
- else
696
- # central directory entry entries include whichever fields are necessary
697
- zip64.original_size = @size if @size >= 0xFFFFFFFF
698
- zip64.compressed_size = @compressed_size if @compressed_size >= 0xFFFFFFFF
699
- zip64.relative_header_offset = @local_header_offset if @local_header_offset >= 0xFFFFFFFF
700
- end
701
- else
702
- @extra.delete('Zip64')
899
+ def prep_cdir_zip64_extra
900
+ return unless ::Zip.write_zip64_support
703
901
 
704
- # if this is a local header entry, create a placeholder
705
- # so we have room to write a zip64 extra field afterward
706
- # (we won't know if it's needed until the file data is written)
707
- if for_local_header
708
- @extra.create('Zip64Placeholder')
709
- else
710
- @extra.delete('Zip64Placeholder')
711
- end
902
+ if (@size && @size >= 0xFFFFFFFF) || @compressed_size >= 0xFFFFFFFF ||
903
+ @local_header_offset >= 0xFFFFFFFF
904
+ @version_needed_to_extract =
905
+ [@version_needed_to_extract, VERSION_NEEDED_TO_EXTRACT_ZIP64].max
906
+ zip64 = @extra[:zip64] || @extra.create(:zip64)
907
+
908
+ # Central directory entry entries include whichever fields are necessary.
909
+ zip64.original_size = @size if @size && @size >= 0xFFFFFFFF
910
+ zip64.compressed_size = @compressed_size if @compressed_size >= 0xFFFFFFFF
911
+ zip64.relative_header_offset = @local_header_offset if @local_header_offset >= 0xFFFFFFFF
712
912
  end
713
913
  end
914
+ # rubocop:enable Style/GuardClause
714
915
  end
715
916
  end
716
917