ziperu 0.2.3-x86_64-linux-gnu → 0.3.1-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 +190 -0
- data/README.md +35 -7
- 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 +41 -6
- data/lib/ziperu/entry.rb +252 -112
- 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 +285 -0
- data/lib/ziperu/file.rb +77 -405
- 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 +30 -32
- 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 +118 -31
- 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/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],
|
|
@@ -61,6 +61,7 @@ module Ziperu
|
|
|
61
61
|
# encoding flag (an archive made on Windows in Japan, say:
|
|
62
62
|
# "Shift_JIS"); they are transcoded to UTF-8. Without it such names
|
|
63
63
|
# are binary Strings, as in rubyzip.
|
|
64
|
+
Entry.check_metadata_encoding!(metadata_encoding)
|
|
64
65
|
@metadata_encoding = metadata_encoding
|
|
65
66
|
|
|
66
67
|
# On commit, runs of files added from disk are compressed on this
|
|
@@ -87,13 +88,13 @@ module Ziperu
|
|
|
87
88
|
raise TypeError, "no implicit conversion of #{path_or_io.inspect} into String"
|
|
88
89
|
end
|
|
89
90
|
|
|
90
|
-
@name
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
91
|
+
@name = if IOExtras.path_like?(path_or_io)
|
|
92
|
+
IOExtras.path_of(path_or_io)
|
|
93
|
+
elsif path_or_io.respond_to?(:path)
|
|
94
|
+
path_or_io.path
|
|
95
|
+
else
|
|
96
|
+
path_or_io
|
|
97
|
+
end
|
|
97
98
|
# rubyzip 2.x's positional form: `new(path, create, buffer, options)`,
|
|
98
99
|
# the options being the restore_* and compression_level keywords.
|
|
99
100
|
given = Compat.positional(legacy, %i[create buffer options], create: create, buffer: buffer)
|
|
@@ -105,7 +106,7 @@ module Ziperu
|
|
|
105
106
|
restore_times = options.fetch(:restore_times, restore_times)
|
|
106
107
|
compression_level = options.fetch(:compression_level, compression_level)
|
|
107
108
|
end
|
|
108
|
-
@create
|
|
109
|
+
@create = create ? true : false # allow any truthy value to mean true
|
|
109
110
|
|
|
110
111
|
initialize_cdir(path_or_io, buffer: buffer)
|
|
111
112
|
|
|
@@ -186,7 +187,8 @@ module Ziperu
|
|
|
186
187
|
# A pipe or socket answers to seek but cannot: reading the central
|
|
187
188
|
# directory would block forever. `InputStream` reads those.
|
|
188
189
|
unless io.kind_of?(::StringIO) || IOExtras.seekable?(io)
|
|
189
|
-
raise ArgumentError, 'Ziperu::File.open_buffer needs a seekable IO; '
|
|
190
|
+
raise ArgumentError, 'Ziperu::File.open_buffer needs a seekable IO; ' \
|
|
191
|
+
'use Ziperu::InputStream for a pipe or socket'
|
|
190
192
|
end
|
|
191
193
|
|
|
192
194
|
zf = ::Ziperu::File.new(io, buffer: true, **options)
|
|
@@ -207,19 +209,27 @@ module Ziperu
|
|
|
207
209
|
# building the whole listing first: the way to walk an archive with
|
|
208
210
|
# hundreds of thousands of entries, or to stop at the first match.
|
|
209
211
|
# Each entry can be read (`get_input_stream`, `extract`) while the
|
|
210
|
-
# block runs; the archive is closed when `foreach` returns.
|
|
211
|
-
#
|
|
212
|
-
|
|
213
|
-
|
|
212
|
+
# block runs; the archive is closed when `foreach` returns. An entry
|
|
213
|
+
# of an archive at a path can still be read afterwards, as rubyzip
|
|
214
|
+
# allows: the archive is reopened for it. `password:` is the one the
|
|
215
|
+
# entries are read with, as with `open`. Without a block, returns an
|
|
216
|
+
# Enumerator.
|
|
217
|
+
def foreach(path_or_io, metadata_encoding: nil, password: nil, **settings, &block)
|
|
218
|
+
unless block
|
|
219
|
+
return enum_for(:foreach, path_or_io, metadata_encoding: metadata_encoding, password: password, **settings)
|
|
220
|
+
end
|
|
214
221
|
|
|
222
|
+
Entry.check_metadata_encoding!(metadata_encoding)
|
|
215
223
|
unknown = settings.dup
|
|
216
224
|
settings = Settings.take!(unknown)
|
|
217
225
|
raise ArgumentError, "unknown keyword: #{unknown.keys.map(&:inspect).join(', ')}" unless unknown.empty?
|
|
218
226
|
|
|
219
227
|
native = InputStream.open_archive(path_or_io, settings)
|
|
228
|
+
owner = Entry::Source::Reopener.new((path_or_io if InputStream.path_like?(path_or_io)), settings, password)
|
|
220
229
|
begin
|
|
221
|
-
native.
|
|
222
|
-
yield Entry.
|
|
230
|
+
native.each_entry_row do |row|
|
|
231
|
+
yield Entry.from_row(native, row, nil, metadata_encoding: metadata_encoding, settings: settings,
|
|
232
|
+
owner: owner)
|
|
223
233
|
end
|
|
224
234
|
ensure
|
|
225
235
|
native.close
|
|
@@ -238,15 +248,18 @@ module Ziperu
|
|
|
238
248
|
&continue_on_exists_proc)
|
|
239
249
|
Ziperu::File.open(zip_file_name, create: true, threads: threads) do |zf|
|
|
240
250
|
zf.add_recursive(src_dir, prefix: prefix, max_depth: max_depth, symlinks: symlinks,
|
|
241
|
-
|
|
251
|
+
&continue_on_exists_proc)
|
|
242
252
|
end
|
|
243
253
|
end
|
|
244
254
|
|
|
245
255
|
# Extracts every entry of the archive at `zip_file_name` into
|
|
246
|
-
# `destination_directory`.
|
|
256
|
+
# `destination_directory`. Takes the options of `#extract_all`; any
|
|
257
|
+
# other is for opening the archive (`password:`, the limits and the
|
|
258
|
+
# rest of what `open` takes).
|
|
247
259
|
def extract_all(zip_file_name, destination_directory = '.', **options, &block)
|
|
248
|
-
|
|
249
|
-
|
|
260
|
+
how, open_options = options.partition { |name, _| EXTRACT_ALL_OPTIONS.include?(name) }.map(&:to_h)
|
|
261
|
+
Ziperu::File.open(zip_file_name, **open_options) do |zf|
|
|
262
|
+
zf.extract_all(destination_directory, **how, &block)
|
|
250
263
|
end
|
|
251
264
|
end
|
|
252
265
|
end
|
|
@@ -299,6 +312,13 @@ module Ziperu
|
|
|
299
312
|
@name
|
|
300
313
|
end
|
|
301
314
|
|
|
315
|
+
# Short, where the default lists every entry: half a megabyte for an
|
|
316
|
+
# archive of 5,000, in irb, a log line or an error message.
|
|
317
|
+
def inspect
|
|
318
|
+
state = [('read-only' if @read_only), ('uncommitted changes' if @dirty)].compact
|
|
319
|
+
"#<#{self.class.name}:#{@name} entries=#{@cdir.size}#{state.map { |s| " (#{s})" }.join}>"
|
|
320
|
+
end
|
|
321
|
+
|
|
302
322
|
# Returns a string containing the contents of the specified entry
|
|
303
323
|
def read(entry)
|
|
304
324
|
get_entry(entry).read_all
|
|
@@ -307,7 +327,6 @@ module Ziperu
|
|
|
307
327
|
# Convenience method for adding the contents of a file to the archive
|
|
308
328
|
def add(entry, src_path, &continue_on_exists_proc)
|
|
309
329
|
check_writable!
|
|
310
|
-
continue_on_exists_proc ||= proc { @settings.continue_on_exists_proc }
|
|
311
330
|
check_entry_exists(entry, continue_on_exists_proc, 'add')
|
|
312
331
|
new_entry = if entry.kind_of?(::Ziperu::Entry)
|
|
313
332
|
entry
|
|
@@ -359,8 +378,7 @@ module Ziperu
|
|
|
359
378
|
prefix = prefix.to_s
|
|
360
379
|
prefix += '/' unless prefix.empty? || prefix.end_with?('/')
|
|
361
380
|
|
|
362
|
-
|
|
363
|
-
report = ->(entry) { done += 1; progress&.call(entry, done, nil) }
|
|
381
|
+
report = progress_reporter(progress)
|
|
364
382
|
add_recursive_dir(prefix, src_dir, 1, max_depth, continue_on_exists_proc, report, symlinks)
|
|
365
383
|
self
|
|
366
384
|
end
|
|
@@ -395,133 +413,31 @@ module Ziperu
|
|
|
395
413
|
add(entry, src_path)
|
|
396
414
|
end
|
|
397
415
|
|
|
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
416
|
# Inflates every entry (without writing anything) so that the CRC of
|
|
513
417
|
# each is checked. Returns a Hash of the entries that failed to the
|
|
514
418
|
# exception raised for them; empty when the archive is sound. Encrypted
|
|
515
419
|
# entries are checked when a `decrypter` is given and skipped otherwise.
|
|
420
|
+
#
|
|
421
|
+
# With `report: true` it returns a Ziperu::VerifyReport instead, which
|
|
422
|
+
# also says which entries were checked and which were skipped and why,
|
|
423
|
+
# so that an archive with nothing checked does not pass for a sound one:
|
|
424
|
+
#
|
|
425
|
+
# report = zf.verify(report: true)
|
|
426
|
+
# report.ok? # => false when anything failed or was skipped
|
|
427
|
+
# report.skipped # => { entry => :encrypted }
|
|
428
|
+
#
|
|
516
429
|
# Not in rubyzip.
|
|
517
|
-
def verify(decrypter: nil)
|
|
430
|
+
def verify(decrypter: nil, report: false)
|
|
518
431
|
native = native_archive
|
|
519
432
|
password = (decrypter&.password || @password)&.b
|
|
520
|
-
|
|
433
|
+
result = VerifyReport.new
|
|
521
434
|
entries.each do |entry|
|
|
522
|
-
#
|
|
523
|
-
next if entry.directory?
|
|
524
|
-
|
|
435
|
+
# A directory has no data to check.
|
|
436
|
+
next if entry.directory?
|
|
437
|
+
# An entry added or replaced from disk has nothing in the archive
|
|
438
|
+
# to check yet.
|
|
439
|
+
next result.add_skipped(entry, :not_written) unless entry.backed_by?(native)
|
|
440
|
+
next result.add_skipped(entry, :encrypted) if entry.encrypted? && password.nil?
|
|
525
441
|
|
|
526
442
|
begin
|
|
527
443
|
if entry.index
|
|
@@ -530,147 +446,12 @@ module Ziperu
|
|
|
530
446
|
native.verify_entry_at(entry.local_header_offset, entry.size, entry.compressed_size,
|
|
531
447
|
entry.crc, password)
|
|
532
448
|
end
|
|
449
|
+
result.add_verified(entry)
|
|
533
450
|
rescue Error, IOError => e
|
|
534
|
-
|
|
451
|
+
result.add_failed(entry, e)
|
|
535
452
|
end
|
|
536
453
|
end
|
|
537
|
-
|
|
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
|
|
454
|
+
report ? result.freeze : result.failed
|
|
674
455
|
end
|
|
675
456
|
|
|
676
457
|
# Closes the zip file committing any changes that has been made.
|
|
@@ -690,9 +471,7 @@ module Ziperu
|
|
|
690
471
|
|
|
691
472
|
# The archive's password, if one was given to `open`.
|
|
692
473
|
# Not in rubyzip.
|
|
693
|
-
|
|
694
|
-
@password
|
|
695
|
-
end
|
|
474
|
+
attr_reader :password
|
|
696
475
|
|
|
697
476
|
# Raises when the archive was opened with `write: false`.
|
|
698
477
|
def check_writable!
|
|
@@ -700,18 +479,6 @@ module Ziperu
|
|
|
700
479
|
end
|
|
701
480
|
private :check_writable!
|
|
702
481
|
|
|
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
482
|
# Searches for entry with the specified name. Returns nil if no entry is
|
|
716
483
|
# found. See also get_entry
|
|
717
484
|
def find_entry(entry_name)
|
|
@@ -748,61 +515,13 @@ module Ziperu
|
|
|
748
515
|
|
|
749
516
|
private
|
|
750
517
|
|
|
751
|
-
#
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
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]) }
|
|
518
|
+
# Counts entries as they are done and tells `progress`, if there is one.
|
|
519
|
+
def progress_reporter(progress, total = nil)
|
|
520
|
+
done = 0
|
|
521
|
+
lambda do |entry|
|
|
522
|
+
done += 1
|
|
523
|
+
progress&.call(entry, done, total)
|
|
783
524
|
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
525
|
end
|
|
807
526
|
|
|
808
527
|
def add_recursive_dir(entry_prefix, src_dir, depth, max_depth, continue_on_exists_proc, report, symlinks)
|
|
@@ -849,7 +568,7 @@ module Ziperu
|
|
|
849
568
|
@create = false
|
|
850
569
|
@file_permissions = ::File.stat(@name).mode
|
|
851
570
|
read_archive(buffer ? path_or_io : @name)
|
|
852
|
-
elsif buffer && path_or_io.size > 0
|
|
571
|
+
elsif buffer && path_or_io.size > 0 # rubocop:disable Style/ZeroLengthPredicate -- a StringIO has no empty?
|
|
853
572
|
# This zip is probably a non-empty StringIO.
|
|
854
573
|
@create = false
|
|
855
574
|
read_archive(path_or_io)
|
|
@@ -866,9 +585,9 @@ module Ziperu
|
|
|
866
585
|
IOExtras.binmode(path_or_io)
|
|
867
586
|
@native = InputStream.open_archive(path_or_io, @settings)
|
|
868
587
|
@comment = @native.comment # the stored bytes, as rubyzip reads them
|
|
869
|
-
@native.
|
|
870
|
-
@cdir.push_record(Entry.
|
|
871
|
-
|
|
588
|
+
@native.each_entry_row do |row|
|
|
589
|
+
@cdir.push_record(Entry.from_row(@native, row, self, metadata_encoding: @metadata_encoding,
|
|
590
|
+
settings: @settings))
|
|
872
591
|
end
|
|
873
592
|
end
|
|
874
593
|
|
|
@@ -877,42 +596,6 @@ module Ziperu
|
|
|
877
596
|
@native = nil
|
|
878
597
|
end
|
|
879
598
|
|
|
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
599
|
def check_entry_exists(entry_name, continue_on_exists_proc, proc_name)
|
|
917
600
|
return unless @cdir.include?(entry_name)
|
|
918
601
|
|
|
@@ -925,19 +608,8 @@ module Ziperu
|
|
|
925
608
|
def check_file(path)
|
|
926
609
|
raise Errno::ENOENT, path unless ::File.readable?(path)
|
|
927
610
|
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
611
|
end
|
|
943
612
|
end
|
|
613
|
+
|
|
614
|
+
require_relative 'file/commit'
|
|
615
|
+
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
|