ziperu 0.2.2-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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +197 -0
- data/README.md +51 -31
- data/lib/zip.rb +3 -3
- data/lib/ziperu/3.1/ziperu.so +0 -0
- data/lib/ziperu/3.2/ziperu.so +0 -0
- data/lib/ziperu/3.3/ziperu.so +0 -0
- data/lib/ziperu/3.4/ziperu.so +0 -0
- data/lib/ziperu/4.0/ziperu.so +0 -0
- data/lib/ziperu/central_directory.rb +4 -8
- data/lib/ziperu/constants.rb +8 -0
- data/lib/ziperu/crypto.rb +10 -2
- data/lib/ziperu/dos_time.rb +1 -1
- data/lib/ziperu/entry/source.rb +6 -0
- data/lib/ziperu/entry.rb +275 -107
- data/lib/ziperu/entry_set.rb +0 -5
- data/lib/ziperu/errors.rb +4 -2
- data/lib/ziperu/extra_field/zip64.rb +1 -3
- data/lib/ziperu/extra_field.rb +10 -10
- data/lib/ziperu/file/commit.rb +255 -0
- data/lib/ziperu/file/extraction.rb +280 -0
- data/lib/ziperu/file.rb +52 -363
- data/lib/ziperu/file_split.rb +1 -3
- data/lib/ziperu/filesystem/dir.rb +3 -9
- data/lib/ziperu/filesystem/file.rb +2 -6
- data/lib/ziperu/filesystem/zip_file_name_mapper.rb +2 -2
- data/lib/ziperu/input_stream.rb +48 -36
- data/lib/ziperu/ioextras/abstract_input_stream.rb +83 -31
- data/lib/ziperu/ioextras/abstract_output_stream.rb +1 -3
- data/lib/ziperu/ioextras/fake_io.rb +1 -3
- data/lib/ziperu/ioextras.rb +6 -0
- data/lib/ziperu/limits.rb +48 -0
- data/lib/ziperu/null_input_stream.rb +1 -1
- data/lib/ziperu/output_stream.rb +127 -30
- data/lib/ziperu/pass_thru_decompressor.rb +1 -3
- data/lib/ziperu/rails_streaming.rb +5 -5
- data/lib/ziperu/remote_io.rb +181 -32
- data/lib/ziperu/size_estimator.rb +27 -19
- data/lib/ziperu/streamable_stream.rb +110 -10
- data/lib/ziperu/verify_report.rb +62 -0
- data/lib/ziperu/version.rb +2 -2
- data/lib/ziperu.rb +19 -17
- 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 = [
|
|
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
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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
|
|
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; '
|
|
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.
|
|
222
|
-
yield Entry.
|
|
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
|
-
|
|
242
|
+
&continue_on_exists_proc)
|
|
242
243
|
end
|
|
243
244
|
end
|
|
244
245
|
|
|
@@ -288,7 +289,7 @@ module Ziperu
|
|
|
288
289
|
"cannot open stream to directory entry - '#{new_entry}'"
|
|
289
290
|
end
|
|
290
291
|
new_entry.unix_perms = permissions
|
|
291
|
-
zip_streamable_entry = StreamableStream.new(new_entry)
|
|
292
|
+
zip_streamable_entry = StreamableStream.new(new_entry, @spool_budget ||= SpoolIO::Budget.new)
|
|
292
293
|
@cdir << zip_streamable_entry
|
|
293
294
|
@dirty = true
|
|
294
295
|
zip_streamable_entry.get_output_stream(&a_proc)
|
|
@@ -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
|
-
|
|
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
|
-
|
|
414
|
+
result = VerifyReport.new
|
|
521
415
|
entries.each do |entry|
|
|
522
|
-
#
|
|
523
|
-
next if entry.directory?
|
|
524
|
-
|
|
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,111 +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
|
-
|
|
535
|
-
end
|
|
536
|
-
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
|
|
432
|
+
result.add_failed(entry, e)
|
|
551
433
|
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
|
-
else
|
|
560
|
-
on_success_replace do |tmp_file|
|
|
561
|
-
::Ziperu::OutputStream.open(tmp_file, **stream_options) do |zos|
|
|
562
|
-
zos.put_entries(@cdir.writable_entries, threads: @threads)
|
|
563
|
-
zos.comment = comment
|
|
564
|
-
end
|
|
565
|
-
true
|
|
566
|
-
end
|
|
567
|
-
end
|
|
568
|
-
# Only now are the changes on disk; until here the entries keep
|
|
569
|
-
# their dirty state so a failed commit can be retried.
|
|
570
|
-
@cdir.each(&:clean_up)
|
|
571
|
-
if @write_to
|
|
572
|
-
# From here on this object works on the archive just written:
|
|
573
|
-
# what it holds now includes the changes, and a later commit
|
|
574
|
-
# updates it rather than starting again from the original (which
|
|
575
|
-
# would drop what this commit wrote).
|
|
576
|
-
@name = @write_to
|
|
577
|
-
@write_to = nil
|
|
578
|
-
end
|
|
579
|
-
initialize_cdir(@name)
|
|
580
|
-
end
|
|
581
|
-
|
|
582
|
-
# The entries to append, when the pending changes are additions and
|
|
583
|
-
# nothing else: every entry the archive holds is still listed, in
|
|
584
|
-
# place, unchanged, and the comment is as it was. Nil otherwise, and
|
|
585
|
-
# the archive is rewritten.
|
|
586
|
-
def appended_entries
|
|
587
|
-
return nil if @create || @buffer || @native.nil? || @comment_changed || @settings.sort_entries
|
|
588
|
-
return nil unless @settings.append_in_place && appendable_in_place?
|
|
589
|
-
|
|
590
|
-
all = @cdir.entries
|
|
591
|
-
held = @native.entry_count
|
|
592
|
-
return nil if all.size <= held || all.first(held).each_with_index.any? do |e, i|
|
|
593
|
-
!(e.index == i && e.backed_by?(@native) && !e.dirty? && !e.needs_restream? &&
|
|
594
|
-
!e.extra_changed? && !@cdir.shadowed?(e))
|
|
595
|
-
end
|
|
596
|
-
|
|
597
|
-
all.drop(held)
|
|
598
|
-
end
|
|
599
|
-
private :appended_entries
|
|
600
|
-
|
|
601
|
-
# Whether the archive's file can take an append: one that is read-only
|
|
602
|
-
# can still be replaced (the rewrite renames a new file over it), and
|
|
603
|
-
# one with other hard links must be, or the append would change what
|
|
604
|
-
# they hold too.
|
|
605
|
-
def appendable_in_place?
|
|
606
|
-
stat = ::File.stat(@name)
|
|
607
|
-
stat.file? && stat.writable? && stat.nlink <= 1
|
|
608
|
-
rescue SystemCallError
|
|
609
|
-
false
|
|
610
|
-
end
|
|
611
|
-
private :appendable_in_place?
|
|
612
|
-
|
|
613
|
-
# Writes the archive, with any changes, to `io` and returns it. `io`
|
|
614
|
-
# may be a pipe, socket or response body: entries are streamed to it
|
|
615
|
-
# as they are written, unless the archive holds ZipCrypto entries,
|
|
616
|
-
# whose headers are patched at the end; such an archive is built in
|
|
617
|
-
# memory first.
|
|
618
|
-
def write_buffer(io = ::StringIO.new)
|
|
619
|
-
return io unless commit_required?
|
|
620
|
-
|
|
621
|
-
# Rewriting the archive into the very IO it is read from would
|
|
622
|
-
# overwrite entries still to be copied; build it aside first.
|
|
623
|
-
if @buffer && io.equal?(@io) && !io.kind_of?(::StringIO)
|
|
624
|
-
built = write_buffer(::StringIO.new).string
|
|
625
|
-
io.rewind
|
|
626
|
-
io.write(built)
|
|
627
|
-
io.truncate(built.bytesize) if io.respond_to?(:truncate)
|
|
628
|
-
return io
|
|
629
|
-
end
|
|
630
|
-
|
|
631
|
-
# ZipCrypto entries are copied with patched headers, which a stream
|
|
632
|
-
# (a pipe, say) cannot take: such an archive is built in memory first.
|
|
633
|
-
memory = @cdir.writable_entries.any?(&:zipcrypto?)
|
|
634
|
-
::Ziperu::OutputStream.write_buffer(io, memory: memory, **stream_options) do |zos|
|
|
635
|
-
@cdir.each_writable { |e| e.write_to_zip_output_stream(zos) }
|
|
636
|
-
zos.comment = comment
|
|
637
434
|
end
|
|
435
|
+
report ? result.freeze : result.failed
|
|
638
436
|
end
|
|
639
437
|
|
|
640
438
|
# Closes the zip file committing any changes that has been made.
|
|
@@ -654,9 +452,7 @@ module Ziperu
|
|
|
654
452
|
|
|
655
453
|
# The archive's password, if one was given to `open`.
|
|
656
454
|
# Not in rubyzip.
|
|
657
|
-
|
|
658
|
-
@password
|
|
659
|
-
end
|
|
455
|
+
attr_reader :password
|
|
660
456
|
|
|
661
457
|
# Raises when the archive was opened with `write: false`.
|
|
662
458
|
def check_writable!
|
|
@@ -664,18 +460,6 @@ module Ziperu
|
|
|
664
460
|
end
|
|
665
461
|
private :check_writable!
|
|
666
462
|
|
|
667
|
-
# Returns true if any changes has been made to this archive since the
|
|
668
|
-
# previous commit
|
|
669
|
-
def commit_required?
|
|
670
|
-
return true if @create || @dirty
|
|
671
|
-
|
|
672
|
-
@cdir.each do |e|
|
|
673
|
-
return true if e.dirty?
|
|
674
|
-
end
|
|
675
|
-
|
|
676
|
-
false
|
|
677
|
-
end
|
|
678
|
-
|
|
679
463
|
# Searches for entry with the specified name. Returns nil if no entry is
|
|
680
464
|
# found. See also get_entry
|
|
681
465
|
def find_entry(entry_name)
|
|
@@ -712,61 +496,13 @@ module Ziperu
|
|
|
712
496
|
|
|
713
497
|
private
|
|
714
498
|
|
|
715
|
-
#
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
code = Entry.on_exists_code(on_exists)
|
|
722
|
-
slow = []
|
|
723
|
-
specs = []
|
|
724
|
-
by_index = {}
|
|
725
|
-
targets.each do |entry, target|
|
|
726
|
-
if entry.symlink? || entry.encrypted? || !native_extractable?(entry, native)
|
|
727
|
-
slow << [entry, target]
|
|
728
|
-
else
|
|
729
|
-
specs << native_extract_spec(entry, target)
|
|
730
|
-
by_index[entry.index] = entry
|
|
731
|
-
end
|
|
732
|
-
end
|
|
733
|
-
|
|
734
|
-
# A few hundred entries per call, so an interrupt is noticed between
|
|
735
|
-
# calls rather than after the whole archive.
|
|
736
|
-
specs.each_slice(NATIVE_EXTRACT_BATCH) do |batch|
|
|
737
|
-
native.extract_entries(dest, batch, @settings.validate_entry_sizes, threads, code).each do |index, kind, path|
|
|
738
|
-
entry = by_index[index]
|
|
739
|
-
case kind
|
|
740
|
-
when :unsafe
|
|
741
|
-
warn "WARNING: skipped extracting '#{entry.name}' to '#{path}' as unsafe."
|
|
742
|
-
when :oversize
|
|
743
|
-
warn "WARNING: #{EntrySizeError.new(entry).message}"
|
|
744
|
-
end
|
|
745
|
-
end
|
|
746
|
-
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)
|
|
747
505
|
end
|
|
748
|
-
|
|
749
|
-
slow
|
|
750
|
-
end
|
|
751
|
-
|
|
752
|
-
NATIVE_EXTRACT_BATCH = 256 # :nodoc:
|
|
753
|
-
|
|
754
|
-
def native_extractable?(entry, native)
|
|
755
|
-
return false unless entry.instance_of?(::Ziperu::Entry) && entry.index && entry.backed_by?(native)
|
|
756
|
-
# An encrypted entry goes through the Ruby path, which has the password.
|
|
757
|
-
return false if entry.encrypted?
|
|
758
|
-
return false unless entry.file? || entry.directory?
|
|
759
|
-
|
|
760
|
-
name = entry.name
|
|
761
|
-
name.ascii_only? || (name.encoding == ::Encoding::UTF_8 && name.valid_encoding?)
|
|
762
|
-
end
|
|
763
|
-
|
|
764
|
-
# What `Entry#extract` would do for the entry, as plain values:
|
|
765
|
-
# [entry, index, name, mtime seconds, mtime nanoseconds, unix mode, directory?].
|
|
766
|
-
def native_extract_spec(entry, target = entry.name)
|
|
767
|
-
mtime = entry.time if @restore_times
|
|
768
|
-
mode = entry.unix_perms & 0o1777 if @restore_permissions && entry.fstype == FSTYPE_UNIX && entry.unix_perms
|
|
769
|
-
[entry, entry.index, target, mtime&.to_i, mtime&.nsec, mode, entry.directory?]
|
|
770
506
|
end
|
|
771
507
|
|
|
772
508
|
def add_recursive_dir(entry_prefix, src_dir, depth, max_depth, continue_on_exists_proc, report, symlinks)
|
|
@@ -813,7 +549,7 @@ module Ziperu
|
|
|
813
549
|
@create = false
|
|
814
550
|
@file_permissions = ::File.stat(@name).mode
|
|
815
551
|
read_archive(buffer ? path_or_io : @name)
|
|
816
|
-
elsif buffer && path_or_io.size > 0
|
|
552
|
+
elsif buffer && path_or_io.size > 0 # rubocop:disable Style/ZeroLengthPredicate -- a StringIO has no empty?
|
|
817
553
|
# This zip is probably a non-empty StringIO.
|
|
818
554
|
@create = false
|
|
819
555
|
read_archive(path_or_io)
|
|
@@ -830,9 +566,9 @@ module Ziperu
|
|
|
830
566
|
IOExtras.binmode(path_or_io)
|
|
831
567
|
@native = InputStream.open_archive(path_or_io, @settings)
|
|
832
568
|
@comment = @native.comment # the stored bytes, as rubyzip reads them
|
|
833
|
-
@native.
|
|
834
|
-
@cdir.push_record(Entry.
|
|
835
|
-
|
|
569
|
+
@native.each_entry_row do |row|
|
|
570
|
+
@cdir.push_record(Entry.from_row(@native, row, self, metadata_encoding: @metadata_encoding,
|
|
571
|
+
settings: @settings))
|
|
836
572
|
end
|
|
837
573
|
end
|
|
838
574
|
|
|
@@ -841,42 +577,6 @@ module Ziperu
|
|
|
841
577
|
@native = nil
|
|
842
578
|
end
|
|
843
579
|
|
|
844
|
-
# Windows refuses to replace a file that is still open. A stream the
|
|
845
|
-
# caller forgot to close holds its own handle until the GC frees it, so
|
|
846
|
-
# on failure collect garbage once and try again.
|
|
847
|
-
def rename_over_original(tmp_filename)
|
|
848
|
-
::File.rename(tmp_filename, name)
|
|
849
|
-
rescue Errno::EACCES
|
|
850
|
-
raise unless ::Ziperu::RUNNING_ON_WINDOWS
|
|
851
|
-
|
|
852
|
-
GC.start
|
|
853
|
-
begin
|
|
854
|
-
::File.rename(tmp_filename, name)
|
|
855
|
-
rescue Errno::EACCES
|
|
856
|
-
# A directory (or anything but a file) at the path is not ours to
|
|
857
|
-
# move: the commit fails, as rubyzip's does.
|
|
858
|
-
raise unless ::File.file?(name)
|
|
859
|
-
|
|
860
|
-
# Something else (another Ziperu::File, say) still has the archive
|
|
861
|
-
# open. Renaming an open file is allowed where replacing one is
|
|
862
|
-
# not, so move the old archive aside first; that handle keeps
|
|
863
|
-
# reading the old bytes, as it would on Unix.
|
|
864
|
-
aside = "#{name}.#{Process.pid}.#{rand(1 << 32).to_s(36)}.old"
|
|
865
|
-
::File.rename(name, aside)
|
|
866
|
-
::File.rename(tmp_filename, name)
|
|
867
|
-
begin
|
|
868
|
-
::File.delete(aside)
|
|
869
|
-
rescue SystemCallError
|
|
870
|
-
warn "WARNING: could not remove '#{aside}' (still open elsewhere); delete it once it is closed."
|
|
871
|
-
end
|
|
872
|
-
end
|
|
873
|
-
end
|
|
874
|
-
|
|
875
|
-
# What every stream this archive is written through is opened with.
|
|
876
|
-
def stream_options
|
|
877
|
-
{ suppress_extra_fields: @suppress_extra_fields, **Settings.overrides(@settings) }
|
|
878
|
-
end
|
|
879
|
-
|
|
880
580
|
def check_entry_exists(entry_name, continue_on_exists_proc, proc_name)
|
|
881
581
|
return unless @cdir.include?(entry_name)
|
|
882
582
|
|
|
@@ -889,19 +589,8 @@ module Ziperu
|
|
|
889
589
|
def check_file(path)
|
|
890
590
|
raise Errno::ENOENT, path unless ::File.readable?(path)
|
|
891
591
|
end
|
|
892
|
-
|
|
893
|
-
def on_success_replace
|
|
894
|
-
dirname, basename = ::File.split(name)
|
|
895
|
-
::Dir::Tmpname.create(basename, dirname) do |tmp_filename|
|
|
896
|
-
if yield tmp_filename
|
|
897
|
-
# The native handle must be gone before the rename on Windows.
|
|
898
|
-
release_native
|
|
899
|
-
rename_over_original(tmp_filename)
|
|
900
|
-
::File.chmod(@file_permissions, name) unless @create
|
|
901
|
-
end
|
|
902
|
-
ensure
|
|
903
|
-
FileUtils.rm_f(tmp_filename)
|
|
904
|
-
end
|
|
905
|
-
end
|
|
906
592
|
end
|
|
907
593
|
end
|
|
594
|
+
|
|
595
|
+
require_relative 'file/commit'
|
|
596
|
+
require_relative 'file/extraction'
|
data/lib/ziperu/file_split.rb
CHANGED
|
@@ -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
|
|