ziperu 0.2.3-x86_64-linux-gnu → 0.3.0-x86_64-linux-gnu

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 (41) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +151 -0
  3. data/README.md +35 -7
  4. data/lib/zip.rb +3 -3
  5. data/lib/ziperu/3.1/ziperu.so +0 -0
  6. data/lib/ziperu/3.2/ziperu.so +0 -0
  7. data/lib/ziperu/3.3/ziperu.so +0 -0
  8. data/lib/ziperu/3.4/ziperu.so +0 -0
  9. data/lib/ziperu/4.0/ziperu.so +0 -0
  10. data/lib/ziperu/central_directory.rb +4 -8
  11. data/lib/ziperu/constants.rb +8 -0
  12. data/lib/ziperu/crypto.rb +10 -2
  13. data/lib/ziperu/dos_time.rb +1 -1
  14. data/lib/ziperu/entry.rb +226 -109
  15. data/lib/ziperu/entry_set.rb +0 -5
  16. data/lib/ziperu/errors.rb +4 -2
  17. data/lib/ziperu/extra_field/zip64.rb +1 -3
  18. data/lib/ziperu/extra_field.rb +10 -10
  19. data/lib/ziperu/file/commit.rb +255 -0
  20. data/lib/ziperu/file/extraction.rb +280 -0
  21. data/lib/ziperu/file.rb +51 -398
  22. data/lib/ziperu/file_split.rb +1 -3
  23. data/lib/ziperu/filesystem/dir.rb +3 -9
  24. data/lib/ziperu/filesystem/file.rb +2 -6
  25. data/lib/ziperu/filesystem/zip_file_name_mapper.rb +2 -2
  26. data/lib/ziperu/input_stream.rb +28 -32
  27. data/lib/ziperu/ioextras/abstract_input_stream.rb +83 -31
  28. data/lib/ziperu/ioextras/abstract_output_stream.rb +1 -3
  29. data/lib/ziperu/ioextras/fake_io.rb +1 -3
  30. data/lib/ziperu/ioextras.rb +6 -0
  31. data/lib/ziperu/limits.rb +48 -0
  32. data/lib/ziperu/null_input_stream.rb +1 -1
  33. data/lib/ziperu/output_stream.rb +118 -31
  34. data/lib/ziperu/pass_thru_decompressor.rb +1 -3
  35. data/lib/ziperu/rails_streaming.rb +5 -5
  36. data/lib/ziperu/remote_io.rb +181 -32
  37. data/lib/ziperu/size_estimator.rb +27 -19
  38. data/lib/ziperu/verify_report.rb +62 -0
  39. data/lib/ziperu/version.rb +2 -2
  40. data/lib/ziperu.rb +19 -17
  41. metadata +7 -2
data/lib/ziperu/file.rb CHANGED
@@ -23,7 +23,7 @@ module Ziperu
23
23
  include Enumerable
24
24
  extend Forwardable
25
25
 
26
- IO_METHODS = [:tell, :seek, :read, :eof, :close].freeze # :nodoc:
26
+ IO_METHODS = %i[tell seek read eof close].freeze # :nodoc:
27
27
 
28
28
  # The name of this zip archive.
29
29
  attr_reader :name
@@ -35,13 +35,13 @@ module Ziperu
35
35
 
36
36
  def_delegators :@cdir, :each, :entries, :glob, :size
37
37
 
38
- # Opens a zip archive. Pass create: true to create a new archive if it
39
- # doesn't exist already.
40
38
  # rubyzip 2.x's `Zip::File.open(path, Zip::File::CREATE)`. Libraries
41
39
  # that branch on `Zip::VERSION >= "3"` (sablon, selenium-webdriver) see
42
40
  # ziperu's own version and take their 2.x path, so both forms work.
43
41
  CREATE = true
44
42
 
43
+ # Opens a zip archive. Pass create: true to create a new archive if it
44
+ # doesn't exist already.
45
45
  def initialize(path_or_io, *legacy, create: false, buffer: false,
46
46
  restore_ownership: DEFAULT_RESTORE_OPTIONS[:restore_ownership],
47
47
  restore_permissions: DEFAULT_RESTORE_OPTIONS[:restore_permissions],
@@ -87,13 +87,13 @@ module Ziperu
87
87
  raise TypeError, "no implicit conversion of #{path_or_io.inspect} into String"
88
88
  end
89
89
 
90
- @name = if IOExtras.path_like?(path_or_io)
91
- IOExtras.path_of(path_or_io)
92
- elsif path_or_io.respond_to?(:path)
93
- path_or_io.path
94
- else
95
- path_or_io
96
- end
90
+ @name = if IOExtras.path_like?(path_or_io)
91
+ IOExtras.path_of(path_or_io)
92
+ elsif path_or_io.respond_to?(:path)
93
+ path_or_io.path
94
+ else
95
+ path_or_io
96
+ end
97
97
  # rubyzip 2.x's positional form: `new(path, create, buffer, options)`,
98
98
  # the options being the restore_* and compression_level keywords.
99
99
  given = Compat.positional(legacy, %i[create buffer options], create: create, buffer: buffer)
@@ -105,7 +105,7 @@ module Ziperu
105
105
  restore_times = options.fetch(:restore_times, restore_times)
106
106
  compression_level = options.fetch(:compression_level, compression_level)
107
107
  end
108
- @create = create ? true : false # allow any truthy value to mean true
108
+ @create = create ? true : false # allow any truthy value to mean true
109
109
 
110
110
  initialize_cdir(path_or_io, buffer: buffer)
111
111
 
@@ -186,7 +186,8 @@ module Ziperu
186
186
  # A pipe or socket answers to seek but cannot: reading the central
187
187
  # directory would block forever. `InputStream` reads those.
188
188
  unless io.kind_of?(::StringIO) || IOExtras.seekable?(io)
189
- raise ArgumentError, 'Ziperu::File.open_buffer needs a seekable IO; ' 'use Ziperu::InputStream for a pipe or socket'
189
+ raise ArgumentError, 'Ziperu::File.open_buffer needs a seekable IO; ' \
190
+ 'use Ziperu::InputStream for a pipe or socket'
190
191
  end
191
192
 
192
193
  zf = ::Ziperu::File.new(io, buffer: true, **options)
@@ -218,8 +219,8 @@ module Ziperu
218
219
 
219
220
  native = InputStream.open_archive(path_or_io, settings)
220
221
  begin
221
- native.each_entry do |meta|
222
- yield Entry.from_meta(native, meta, nil, metadata_encoding: metadata_encoding, settings: settings)
222
+ native.each_entry_row do |row|
223
+ yield Entry.from_row(native, row, nil, metadata_encoding: metadata_encoding, settings: settings)
223
224
  end
224
225
  ensure
225
226
  native.close
@@ -238,7 +239,7 @@ module Ziperu
238
239
  &continue_on_exists_proc)
239
240
  Ziperu::File.open(zip_file_name, create: true, threads: threads) do |zf|
240
241
  zf.add_recursive(src_dir, prefix: prefix, max_depth: max_depth, symlinks: symlinks,
241
- &continue_on_exists_proc)
242
+ &continue_on_exists_proc)
242
243
  end
243
244
  end
244
245
 
@@ -307,7 +308,6 @@ module Ziperu
307
308
  # Convenience method for adding the contents of a file to the archive
308
309
  def add(entry, src_path, &continue_on_exists_proc)
309
310
  check_writable!
310
- continue_on_exists_proc ||= proc { @settings.continue_on_exists_proc }
311
311
  check_entry_exists(entry, continue_on_exists_proc, 'add')
312
312
  new_entry = if entry.kind_of?(::Ziperu::Entry)
313
313
  entry
@@ -359,8 +359,7 @@ module Ziperu
359
359
  prefix = prefix.to_s
360
360
  prefix += '/' unless prefix.empty? || prefix.end_with?('/')
361
361
 
362
- done = 0
363
- report = ->(entry) { done += 1; progress&.call(entry, done, nil) }
362
+ report = progress_reporter(progress)
364
363
  add_recursive_dir(prefix, src_dir, 1, max_depth, continue_on_exists_proc, report, symlinks)
365
364
  self
366
365
  end
@@ -395,133 +394,31 @@ module Ziperu
395
394
  add(entry, src_path)
396
395
  end
397
396
 
398
- # Extracts `entry` to a file at `entry_path`, with
399
- # `destination_directory` as the base. The block (or Ziperu.on_exists_proc)
400
- # decides whether an existing file is overwritten.
401
- def extract(entry, entry_path = nil, destination_directory: '.', symlinks: :skip, on_exists: nil,
402
- &block)
403
- found_entry = get_entry(entry)
404
- entry_path ||= found_entry.name
405
- found_entry.extract(entry_path, destination_directory: destination_directory,
406
- symlinks: symlinks, on_exists: on_exists, &block)
407
- end
408
-
409
- # Extracts every entry in the archive into `destination_directory`,
410
- # preserving the archive's directory structure. Symlink entries are
411
- # skipped with a warning; see `Entry#extract` for the path-safety checks
412
- # applied to every other entry.
413
- #
414
- # `symlinks:` is `:skip` (the default, with a warning per link, as in
415
- # rubyzip 3) or `:create`, which creates links whose target stays inside
416
- # `destination_directory`. `progress:` is called with
417
- # `(entry, done, total)` after each entry.
418
- # `threads: n` extracts the files on that many threads (the archive is
419
- # read through one handle per thread); a file or String archive only,
420
- # since an IO is driven from Ruby. Directories are made first, links
421
- # last, as before. An error is raised for the first entry (in archive
422
- # order) that failed, once every thread has stopped.
423
- #
424
- # The policies `unzip` has: `on_exists:` is `:raise` (the default),
425
- # `:overwrite` (`unzip -o`) or `:skip` (`unzip -n`) for a path already
426
- # taken, where rubyzip only offers a block; `exclude:` lists globs (or
427
- # Regexps) of entry names to leave out (`unzip -x`), matched against
428
- # the whole name, so `"*.log"` matches at any depth; `junk_paths: true`
429
- # drops the directories and puts every file in `destination_directory`
430
- # itself (`unzip -j`); `sanitize: :windows` rewrites names that Windows
431
- # refuses (`<>:"|?*`, control characters, a trailing dot or space,
432
- # `CON`, `NUL` and the like) so the archive extracts there at all.
433
- def extract_all(destination_directory = '.', symlinks: :skip, progress: nil, threads: 1,
434
- on_exists: nil, exclude: nil, junk_paths: false, sanitize: nil, &block)
435
- # Directories first, so files have somewhere to go; links last, so
436
- # nothing is written through one. Of several records under one name
437
- # only the last is extracted, which is the one rubyzip would know.
438
- dirs, rest = @cdir.writable_entries.partition(&:directory?)
439
- links, others = rest.partition(&:symlink?)
440
- ordered = dirs + others + links
441
- ordered = ordered.reject { |e| excluded?(e.name, exclude) } if exclude
442
- ordered = ordered.reject(&:directory?) if junk_paths
443
- targets = ordered.map { |e| [e, extract_target(e.name, junk_paths, sanitize)] }
444
- total = targets.size
445
- done = 0
446
- report = ->(entry) { done += 1; progress&.call(entry, done, total) }
447
-
448
- # With no block to consult about paths already taken (the policy
449
- # says, or Ziperu.on_exists_proc), the native core extracts every
450
- # qualifying entry in one call; the rest take the per-entry path.
451
- policy = on_exists || (@settings.on_exists_proc ? :overwrite : :raise)
452
- slow = if block.nil? && !@restore_ownership
453
- extract_all_native(targets, destination_directory, report, threads, policy)
454
- else
455
- targets
456
- end
457
-
458
- slow.each do |entry, target|
459
- if entry.symlink? && symlinks != :create
460
- warn "WARNING: skipped symlink '#{entry.name}' during extract_all."
461
- else
462
- entry.extract(target, destination_directory: destination_directory,
463
- create_parent_directories: true,
464
- symlinks: symlinks,
465
- on_exists: on_exists, &block)
466
- end
467
- report.call(entry)
468
- end
469
-
470
- self
471
- end
472
-
473
- # Whether `name` matches one of `patterns`: a glob (`File.fnmatch`
474
- # with `*` crossing `/`, as `unzip -x` has it) or a Regexp.
475
- def excluded?(name, patterns)
476
- Array(patterns).any? do |pattern|
477
- case pattern
478
- when ::Regexp then pattern.match?(name)
479
- else ::File.fnmatch?(pattern.to_s, name, ::File::FNM_EXTGLOB | ::File::FNM_DOTMATCH)
480
- end
481
- end
482
- end
483
- private :excluded?
484
-
485
- # The path under the destination an entry goes to.
486
- def extract_target(name, junk_paths, sanitize)
487
- name = ::File.basename(name) if junk_paths
488
- case sanitize
489
- when nil, false then name
490
- when :windows then self.class.sanitize_windows_name(name)
491
- else raise ArgumentError, "sanitize: takes :windows, not #{sanitize.inspect}"
492
- end
493
- end
494
- private :extract_target
495
-
496
- WINDOWS_RESERVED_NAMES = /\A(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])(\.|\z)/i # :nodoc:
497
-
498
- # `name` as Windows will take it: in each component the characters it
499
- # refuses (`<>:"|?*` and controls) become `_`, a trailing dot or space
500
- # goes, and a reserved device name gets a leading `_`.
501
- # Not in rubyzip.
502
- def self.sanitize_windows_name(name)
503
- trailing = name.end_with?('/') ? '/' : ''
504
- parts = name.split('/').map do |part|
505
- part = part.gsub(/[<>:"|?*\x00-\x1f]/, '_').sub(/[. ]+\z/, '')
506
- part = "_#{part}" if part.match?(WINDOWS_RESERVED_NAMES)
507
- part.empty? ? '_' : part
508
- end
509
- parts.join('/') + trailing
510
- end
511
-
512
397
  # Inflates every entry (without writing anything) so that the CRC of
513
398
  # each is checked. Returns a Hash of the entries that failed to the
514
399
  # exception raised for them; empty when the archive is sound. Encrypted
515
400
  # entries are checked when a `decrypter` is given and skipped otherwise.
401
+ #
402
+ # With `report: true` it returns a Ziperu::VerifyReport instead, which
403
+ # also says which entries were checked and which were skipped and why,
404
+ # so that an archive with nothing checked does not pass for a sound one:
405
+ #
406
+ # report = zf.verify(report: true)
407
+ # report.ok? # => false when anything failed or was skipped
408
+ # report.skipped # => { entry => :encrypted }
409
+ #
516
410
  # Not in rubyzip.
517
- def verify(decrypter: nil)
411
+ def verify(decrypter: nil, report: false)
518
412
  native = native_archive
519
413
  password = (decrypter&.password || @password)&.b
520
- failures = {}
414
+ result = VerifyReport.new
521
415
  entries.each do |entry|
522
- # An entry replaced from disk has nothing in the archive to check.
523
- next if entry.directory? || !entry.backed_by?(native)
524
- next if entry.encrypted? && password.nil?
416
+ # A directory has no data to check.
417
+ next if entry.directory?
418
+ # An entry added or replaced from disk has nothing in the archive
419
+ # to check yet.
420
+ next result.add_skipped(entry, :not_written) unless entry.backed_by?(native)
421
+ next result.add_skipped(entry, :encrypted) if entry.encrypted? && password.nil?
525
422
 
526
423
  begin
527
424
  if entry.index
@@ -530,147 +427,12 @@ module Ziperu
530
427
  native.verify_entry_at(entry.local_header_offset, entry.size, entry.compressed_size,
531
428
  entry.crc, password)
532
429
  end
430
+ result.add_verified(entry)
533
431
  rescue Error, IOError => e
534
- failures[entry] = e
432
+ result.add_failed(entry, e)
535
433
  end
536
434
  end
537
- failures
538
- end
539
-
540
- # Commits changes made since the previous commit to the zip archive: a
541
- # new archive is written beside the old one and renamed over it.
542
- def commit
543
- return if name.kind_of?(StringIO) || !commit_required?
544
-
545
- if @write_to
546
- # The changes go to a new archive; the one opened is not touched,
547
- # and keeps being what this object reads.
548
- ::Ziperu::OutputStream.open(@write_to, **stream_options) do |zos|
549
- zos.put_entries(@cdir.writable_entries, threads: @threads)
550
- zos.comment = comment
551
- end
552
- elsif (appended = appended_entries)
553
- # Only additions: they go after the entries already there, which
554
- # are left untouched (see Ziperu.append_in_place). A failure puts
555
- # the archive back as it was.
556
- ::Ziperu::OutputStream.open(@name, append: true, **stream_options) do |zos|
557
- zos.put_entries(appended, threads: @threads)
558
- end
559
- was_append = true
560
- else
561
- on_success_replace do |tmp_file|
562
- ::Ziperu::OutputStream.open(tmp_file, **stream_options) do |zos|
563
- zos.put_entries(@cdir.writable_entries, threads: @threads)
564
- zos.comment = comment
565
- end
566
- true
567
- end
568
- end
569
- # Only now are the changes on disk; until here the entries keep
570
- # their dirty state so a failed commit can be retried.
571
- @cdir.each(&:clean_up)
572
- if @write_to
573
- # From here on this object works on the archive just written:
574
- # what it holds now includes the changes, and a later commit
575
- # updates it rather than starting again from the original (which
576
- # would drop what this commit wrote).
577
- @name = @write_to
578
- @write_to = nil
579
- end
580
- initialize_cdir(@name) unless was_append && refresh_after_append(appended)
581
- end
582
-
583
- # After an append: the entries already there are where they were, so
584
- # they are kept (pointed at the archive reopened) and only the new
585
- # records are read, rather than every entry built again. False, with
586
- # nothing changed, when the archive does not hold what was expected;
587
- # `initialize_cdir` then reads it all.
588
- def refresh_after_append(appended)
589
- held = @native.entry_count
590
- native = InputStream.open_archive(@name, @settings)
591
- unless native.entry_count == held + appended.size && !native.duplicate_names?
592
- native.close
593
- return false
594
- end
595
-
596
- fresh = native.entry_metas(held, appended.size).map do |meta|
597
- Entry.from_meta(native, meta, self, metadata_encoding: @metadata_encoding, settings: @settings)
598
- end
599
- unless fresh.map { |e| e.name.b } == appended.map { |e| e.name.b }
600
- native.close
601
- return false
602
- end
603
-
604
- kept = @cdir.entries.first(held)
605
- kept.each { |e| e.rebind_native(native) }
606
- fresh.each { |e| @cdir << e }
607
- @native.close
608
- @native = native
609
- @dirty = false
610
- @file_permissions = ::File.stat(@name).mode
611
- true
612
- end
613
- private :refresh_after_append
614
-
615
- # The entries to append, when the pending changes are additions and
616
- # nothing else: every entry the archive holds is still listed, in
617
- # place, unchanged, and the comment is as it was. Nil otherwise, and
618
- # the archive is rewritten.
619
- def appended_entries
620
- return nil if @create || @buffer || @native.nil? || @comment_changed || @settings.sort_entries
621
- return nil unless @settings.append_in_place && appendable_in_place?
622
-
623
- all = @cdir.entries
624
- held = @native.entry_count
625
- # The local headers there stay as they are; the crate writes the
626
- # central directory again, which loses some fields, so those decide
627
- # (without reading every local header, as `needs_restream?` would).
628
- return nil if all.size <= held || all.first(held).each_with_index.any? do |e, i|
629
- !(e.index == i && e.backed_by?(@native) && !e.dirty? && !e.changed? &&
630
- !e.central_fields_lost_on_append? && !@cdir.shadowed?(e))
631
- end
632
-
633
- all.drop(held)
634
- end
635
- private :appended_entries
636
-
637
- # Whether the archive's file can take an append: one that is read-only
638
- # can still be replaced (the rewrite renames a new file over it), and
639
- # one with other hard links must be, or the append would change what
640
- # they hold too.
641
- def appendable_in_place?
642
- stat = ::File.stat(@name)
643
- stat.file? && stat.writable? && stat.nlink <= 1
644
- rescue SystemCallError
645
- false
646
- end
647
- private :appendable_in_place?
648
-
649
- # Writes the archive, with any changes, to `io` and returns it. `io`
650
- # may be a pipe, socket or response body: entries are streamed to it
651
- # as they are written, unless the archive holds ZipCrypto entries,
652
- # whose headers are patched at the end; such an archive is built in
653
- # memory first.
654
- def write_buffer(io = ::StringIO.new)
655
- return io unless commit_required?
656
-
657
- # Rewriting the archive into the very IO it is read from would
658
- # overwrite entries still to be copied; build it aside first.
659
- if @buffer && io.equal?(@io) && !io.kind_of?(::StringIO)
660
- built = write_buffer(::StringIO.new).string
661
- io.rewind
662
- io.write(built)
663
- io.truncate(built.bytesize) if io.respond_to?(:truncate)
664
- return io
665
- end
666
-
667
- # ZipCrypto entries are copied with patched headers, which a stream
668
- # (a pipe, say) cannot take: such an archive is built in memory first.
669
- memory = @cdir.writable_entries.any?(&:zipcrypto?)
670
- ::Ziperu::OutputStream.write_buffer(io, memory: memory, **stream_options) do |zos|
671
- @cdir.each_writable { |e| e.write_to_zip_output_stream(zos) }
672
- zos.comment = comment
673
- end
435
+ report ? result.freeze : result.failed
674
436
  end
675
437
 
676
438
  # Closes the zip file committing any changes that has been made.
@@ -690,9 +452,7 @@ module Ziperu
690
452
 
691
453
  # The archive's password, if one was given to `open`.
692
454
  # Not in rubyzip.
693
- def password
694
- @password
695
- end
455
+ attr_reader :password
696
456
 
697
457
  # Raises when the archive was opened with `write: false`.
698
458
  def check_writable!
@@ -700,18 +460,6 @@ module Ziperu
700
460
  end
701
461
  private :check_writable!
702
462
 
703
- # Returns true if any changes has been made to this archive since the
704
- # previous commit
705
- def commit_required?
706
- return true if @create || @dirty
707
-
708
- @cdir.each do |e|
709
- return true if e.dirty?
710
- end
711
-
712
- false
713
- end
714
-
715
463
  # Searches for entry with the specified name. Returns nil if no entry is
716
464
  # found. See also get_entry
717
465
  def find_entry(entry_name)
@@ -748,61 +496,13 @@ module Ziperu
748
496
 
749
497
  private
750
498
 
751
- # Entries the native core cannot take are returned for the Ruby loop:
752
- # symlinks (skipped there with a warning), encrypted entries, entries
753
- # not read from this archive, and names it cannot represent.
754
- def extract_all_native(targets, destination_directory, report, threads, on_exists)
755
- native = native_archive
756
- dest = ::File.absolute_path(destination_directory || '.')
757
- code = Entry.on_exists_code(on_exists)
758
- slow = []
759
- specs = []
760
- by_index = {}
761
- targets.each do |entry, target|
762
- if entry.symlink? || entry.encrypted? || !native_extractable?(entry, native)
763
- slow << [entry, target]
764
- else
765
- specs << native_extract_spec(entry, target)
766
- by_index[entry.index] = entry
767
- end
768
- end
769
-
770
- # A few hundred entries per call, so an interrupt is noticed between
771
- # calls rather than after the whole archive.
772
- specs.each_slice(NATIVE_EXTRACT_BATCH) do |batch|
773
- native.extract_entries(dest, batch, @settings.validate_entry_sizes, threads, code).each do |index, kind, path|
774
- entry = by_index[index]
775
- case kind
776
- when :unsafe
777
- warn "WARNING: skipped extracting '#{entry.name}' to '#{path}' as unsafe."
778
- when :oversize
779
- warn "WARNING: #{EntrySizeError.new(entry).message}"
780
- end
781
- end
782
- batch.each { |spec| report.call(spec[0]) }
499
+ # Counts entries as they are done and tells `progress`, if there is one.
500
+ def progress_reporter(progress, total = nil)
501
+ done = 0
502
+ lambda do |entry|
503
+ done += 1
504
+ progress&.call(entry, done, total)
783
505
  end
784
-
785
- slow
786
- end
787
-
788
- NATIVE_EXTRACT_BATCH = 256 # :nodoc:
789
-
790
- def native_extractable?(entry, native)
791
- return false unless entry.instance_of?(::Ziperu::Entry) && entry.index && entry.backed_by?(native)
792
- # An encrypted entry goes through the Ruby path, which has the password.
793
- return false if entry.encrypted?
794
- return false unless entry.file? || entry.directory?
795
-
796
- name = entry.name
797
- name.ascii_only? || (name.encoding == ::Encoding::UTF_8 && name.valid_encoding?)
798
- end
799
-
800
- # What `Entry#extract` would do for the entry, as plain values:
801
- # [entry, index, name, mtime seconds, mtime nanoseconds, unix mode, directory?].
802
- def native_extract_spec(entry, target = entry.name)
803
- mtime = entry.time if @restore_times
804
- mode = entry.unix_perms & 0o1777 if @restore_permissions && entry.fstype == FSTYPE_UNIX && entry.unix_perms
805
- [entry, entry.index, target, mtime&.to_i, mtime&.nsec, mode, entry.directory?]
806
506
  end
807
507
 
808
508
  def add_recursive_dir(entry_prefix, src_dir, depth, max_depth, continue_on_exists_proc, report, symlinks)
@@ -849,7 +549,7 @@ module Ziperu
849
549
  @create = false
850
550
  @file_permissions = ::File.stat(@name).mode
851
551
  read_archive(buffer ? path_or_io : @name)
852
- elsif buffer && path_or_io.size > 0
552
+ elsif buffer && path_or_io.size > 0 # rubocop:disable Style/ZeroLengthPredicate -- a StringIO has no empty?
853
553
  # This zip is probably a non-empty StringIO.
854
554
  @create = false
855
555
  read_archive(path_or_io)
@@ -866,9 +566,9 @@ module Ziperu
866
566
  IOExtras.binmode(path_or_io)
867
567
  @native = InputStream.open_archive(path_or_io, @settings)
868
568
  @comment = @native.comment # the stored bytes, as rubyzip reads them
869
- @native.each_entry do |meta|
870
- @cdir.push_record(Entry.from_meta(@native, meta, self, metadata_encoding: @metadata_encoding,
871
- settings: @settings))
569
+ @native.each_entry_row do |row|
570
+ @cdir.push_record(Entry.from_row(@native, row, self, metadata_encoding: @metadata_encoding,
571
+ settings: @settings))
872
572
  end
873
573
  end
874
574
 
@@ -877,42 +577,6 @@ module Ziperu
877
577
  @native = nil
878
578
  end
879
579
 
880
- # Windows refuses to replace a file that is still open. A stream the
881
- # caller forgot to close holds its own handle until the GC frees it, so
882
- # on failure collect garbage once and try again.
883
- def rename_over_original(tmp_filename)
884
- ::File.rename(tmp_filename, name)
885
- rescue Errno::EACCES
886
- raise unless ::Ziperu::RUNNING_ON_WINDOWS
887
-
888
- GC.start
889
- begin
890
- ::File.rename(tmp_filename, name)
891
- rescue Errno::EACCES
892
- # A directory (or anything but a file) at the path is not ours to
893
- # move: the commit fails, as rubyzip's does.
894
- raise unless ::File.file?(name)
895
-
896
- # Something else (another Ziperu::File, say) still has the archive
897
- # open. Renaming an open file is allowed where replacing one is
898
- # not, so move the old archive aside first; that handle keeps
899
- # reading the old bytes, as it would on Unix.
900
- aside = "#{name}.#{Process.pid}.#{rand(1 << 32).to_s(36)}.old"
901
- ::File.rename(name, aside)
902
- ::File.rename(tmp_filename, name)
903
- begin
904
- ::File.delete(aside)
905
- rescue SystemCallError
906
- warn "WARNING: could not remove '#{aside}' (still open elsewhere); delete it once it is closed."
907
- end
908
- end
909
- end
910
-
911
- # What every stream this archive is written through is opened with.
912
- def stream_options
913
- { suppress_extra_fields: @suppress_extra_fields, **Settings.overrides(@settings) }
914
- end
915
-
916
580
  def check_entry_exists(entry_name, continue_on_exists_proc, proc_name)
917
581
  return unless @cdir.include?(entry_name)
918
582
 
@@ -925,19 +589,8 @@ module Ziperu
925
589
  def check_file(path)
926
590
  raise Errno::ENOENT, path unless ::File.readable?(path)
927
591
  end
928
-
929
- def on_success_replace
930
- dirname, basename = ::File.split(name)
931
- ::Dir::Tmpname.create(basename, dirname) do |tmp_filename|
932
- if yield tmp_filename
933
- # The native handle must be gone before the rename on Windows.
934
- release_native
935
- rename_over_original(tmp_filename)
936
- ::File.chmod(@file_permissions, name) unless @create
937
- end
938
- ensure
939
- FileUtils.rm_f(tmp_filename)
940
- end
941
- end
942
592
  end
943
593
  end
594
+
595
+ require_relative 'file/commit'
596
+ require_relative 'file/extraction'
@@ -45,9 +45,7 @@ module Ziperu
45
45
  ssegment_size = segment_size if ssegment_size > segment_size
46
46
  szip_file_name = "#{partial_zip_file_name}.#{format('%03d', szip_file_index)}"
47
47
  ::File.open(szip_file_name, 'wb') do |szip_file|
48
- if szip_file_index == 1
49
- ssegment_size = put_split_signature(szip_file, segment_size)
50
- end
48
+ ssegment_size = put_split_signature(szip_file, segment_size) if szip_file_index == 1
51
49
  chunk_bytes = 0
52
50
  until ssegment_size == chunk_bytes || zip_file.eof?
53
51
  segment_bytes_left = ssegment_size - chunk_bytes
@@ -34,9 +34,7 @@ module Ziperu
34
34
  alias getwd pwd
35
35
 
36
36
  def chdir(directory_name)
37
- unless @file.stat(directory_name).directory?
38
- raise Errno::EINVAL, "Invalid argument - #{directory_name}"
39
- end
37
+ raise Errno::EINVAL, "Invalid argument - #{directory_name}" unless @file.stat(directory_name).directory?
40
38
 
41
39
  @mapped_zip.pwd = @file.expand_path(directory_name)
42
40
  end
@@ -52,9 +50,7 @@ module Ziperu
52
50
  end
53
51
 
54
52
  def foreach(directory_name)
55
- unless @file.stat(directory_name).directory?
56
- raise Errno::ENOTDIR, directory_name
57
- end
53
+ raise Errno::ENOTDIR, directory_name unless @file.stat(directory_name).directory?
58
54
 
59
55
  path = @file.expand_path(directory_name)
60
56
  path << '/' unless path.end_with?('/')
@@ -67,9 +63,7 @@ module Ziperu
67
63
  end
68
64
 
69
65
  def delete(entry_name)
70
- unless @file.stat(entry_name).directory?
71
- raise Errno::EINVAL, "Invalid argument - #{entry_name}"
72
- end
66
+ raise Errno::EINVAL, "Invalid argument - #{entry_name}" unless @file.stat(entry_name).directory?
73
67
 
74
68
  @mapped_zip.remove(entry_name)
75
69
  end
@@ -20,9 +20,7 @@ module Ziperu
20
20
  end
21
21
 
22
22
  def find_entry(filename)
23
- unless exists?(filename)
24
- raise Errno::ENOENT, "No such file or directory - #{filename}"
25
- end
23
+ raise Errno::ENOENT, "No such file or directory - #{filename}" unless exists?(filename)
26
24
 
27
25
  @mapped_zip.find_entry(filename)
28
26
  end
@@ -243,9 +241,7 @@ module Ziperu
243
241
 
244
242
  def delete(*args)
245
243
  args.each do |filename|
246
- if directory?(filename)
247
- raise Errno::EISDIR, "Is a directory - \"#{filename}\""
248
- end
244
+ raise Errno::EISDIR, "Is a directory - \"#{filename}\"" if directory?(filename)
249
245
 
250
246
  @mapped_zip.remove(filename)
251
247
  end
@@ -68,8 +68,8 @@ module Ziperu
68
68
 
69
69
  def expand_path(path)
70
70
  expanded = path.start_with?('/') ? path.dup : ::File.join(@pwd, path)
71
- expanded.gsub!(/\/\.(\/|$)/, '')
72
- expanded.gsub!(/[^\/]+\/\.\.(\/|$)/, '')
71
+ expanded.gsub!(%r{/\.(/|$)}, '')
72
+ expanded.gsub!(%r{[^/]+/\.\.(/|$)}, '')
73
73
  expanded.empty? ? '/' : expanded
74
74
  end
75
75