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.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +197 -0
  3. data/README.md +51 -31
  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/source.rb +6 -0
  15. data/lib/ziperu/entry.rb +275 -107
  16. data/lib/ziperu/entry_set.rb +0 -5
  17. data/lib/ziperu/errors.rb +4 -2
  18. data/lib/ziperu/extra_field/zip64.rb +1 -3
  19. data/lib/ziperu/extra_field.rb +10 -10
  20. data/lib/ziperu/file/commit.rb +255 -0
  21. data/lib/ziperu/file/extraction.rb +280 -0
  22. data/lib/ziperu/file.rb +52 -363
  23. data/lib/ziperu/file_split.rb +1 -3
  24. data/lib/ziperu/filesystem/dir.rb +3 -9
  25. data/lib/ziperu/filesystem/file.rb +2 -6
  26. data/lib/ziperu/filesystem/zip_file_name_mapper.rb +2 -2
  27. data/lib/ziperu/input_stream.rb +48 -36
  28. data/lib/ziperu/ioextras/abstract_input_stream.rb +83 -31
  29. data/lib/ziperu/ioextras/abstract_output_stream.rb +1 -3
  30. data/lib/ziperu/ioextras/fake_io.rb +1 -3
  31. data/lib/ziperu/ioextras.rb +6 -0
  32. data/lib/ziperu/limits.rb +48 -0
  33. data/lib/ziperu/null_input_stream.rb +1 -1
  34. data/lib/ziperu/output_stream.rb +127 -30
  35. data/lib/ziperu/pass_thru_decompressor.rb +1 -3
  36. data/lib/ziperu/rails_streaming.rb +5 -5
  37. data/lib/ziperu/remote_io.rb +181 -32
  38. data/lib/ziperu/size_estimator.rb +27 -19
  39. data/lib/ziperu/streamable_stream.rb +110 -10
  40. data/lib/ziperu/verify_report.rb +62 -0
  41. data/lib/ziperu/version.rb +2 -2
  42. data/lib/ziperu.rb +19 -17
  43. metadata +7 -2
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ziperu
4
- class OutputStream
4
+ class OutputStream # rubocop:disable Style/Documentation -- documented in output_stream.rb
5
5
  # Works out, to the byte, how large an archive will be when streamed
6
6
  # (`OutputStream.body`, `write_buffer` to a pipe or socket), from the
7
7
  # names and sizes of its entries, so a download can carry a
@@ -25,12 +25,21 @@ module Ziperu
25
25
  # for a stream: a data descriptor after every file's data, zip64
26
26
  # records only for a 4 GiB entry or 65,535 entries.
27
27
  class SizeEstimator
28
- LOCAL_HEADER = 30
29
- CENTRAL_HEADER = 46
28
+ LOCAL_HEADER = LOCAL_ENTRY_STATIC_HEADER_LENGTH
29
+ CENTRAL_HEADER = CDIR_ENTRY_STATIC_HEADER_LENGTH
30
30
  END_OF_CENTRAL_DIRECTORY = 22
31
31
  ZIP64_END_OF_CENTRAL_DIRECTORY = 56 + 20 # the record and its locator
32
- ZIP64_THRESHOLD = 0xFFFFFFFF
32
+ ZIP64_THRESHOLD = ZIP64_SIZE_LIMIT
33
33
  ZIP64_ENTRY_THRESHOLD = 0xFFFF
34
+ # After a file's data: signature, CRC and two sizes, 32-bit or 64-bit.
35
+ DATA_DESCRIPTOR = 16
36
+ ZIP64_DATA_DESCRIPTOR = 24
37
+ # Every extra field starts with an ID and a length.
38
+ EXTRA_FIELD_HEADER = 4
39
+ # A local header's zip64 field: its header and two 8-byte sizes.
40
+ ZIP64_LOCAL_EXTRA = EXTRA_FIELD_HEADER + 16
41
+ # The extended timestamp field: its header, flags and the mtime.
42
+ EXTENDED_TIMESTAMP = EXTRA_FIELD_HEADER + 1 + 4
34
43
 
35
44
  def initialize(settings = ::Ziperu)
36
45
  @settings = settings
@@ -52,7 +61,7 @@ module Ziperu
52
61
 
53
62
  # A directory entry (`name` ends with `/`).
54
63
  def add_directory(name)
55
- fields = default_fields(name)
64
+ fields = default_fields
56
65
  name = name.to_s.b
57
66
  @entries += 1
58
67
  @data += LOCAL_HEADER + name.bytesize + fields
@@ -68,8 +77,8 @@ module Ziperu
68
77
 
69
78
  # The archive's size in bytes.
70
79
  def size
71
- central_start = @data
72
- zip64 = @entries > ZIP64_ENTRY_THRESHOLD || central_start > ZIP64_THRESHOLD ||
80
+ # `@data` is also where the central directory starts.
81
+ zip64 = @entries > ZIP64_ENTRY_THRESHOLD || @data > ZIP64_THRESHOLD ||
73
82
  @central > ZIP64_THRESHOLD
74
83
  total = @data + @central + END_OF_CENTRAL_DIRECTORY + @comment
75
84
  total += ZIP64_END_OF_CENTRAL_DIRECTORY if zip64
@@ -79,29 +88,28 @@ module Ziperu
79
88
  private
80
89
 
81
90
  def add_file(name, size, compressed_size)
82
- fields = default_fields(name)
91
+ fields = default_fields
83
92
  name = name.to_s.b
84
93
  large = size >= ZIP64_THRESHOLD || compressed_size >= ZIP64_THRESHOLD
85
94
  offset_large = @data >= ZIP64_THRESHOLD
86
95
  @entries += 1
87
- # The local header, with a zip64 extra (two 8-byte sizes) for a
88
- # large entry; the data; the descriptor, 16 bytes or 24 with
89
- # 64-bit sizes.
90
- @data += LOCAL_HEADER + name.bytesize + fields + (large ? 20 : 0) + compressed_size + (large ? 24 : 16)
91
- # The central record: a zip64 extra holds the sizes for a large
92
- # entry and the offset for one past 4 GiB.
96
+ # The local header, with a zip64 extra for a large entry; the data;
97
+ # the descriptor.
98
+ @data += LOCAL_HEADER + name.bytesize + fields + (large ? ZIP64_LOCAL_EXTRA : 0) +
99
+ compressed_size + (large ? ZIP64_DATA_DESCRIPTOR : DATA_DESCRIPTOR)
100
+ # The central record: a zip64 extra holds the two 8-byte sizes for a
101
+ # large entry and the 8-byte offset for one past 4 GiB.
93
102
  zip64_extra = 0
94
103
  zip64_extra += 16 if large
95
104
  zip64_extra += 8 if offset_large
96
- zip64_extra += 4 unless zip64_extra.zero?
105
+ zip64_extra += EXTRA_FIELD_HEADER unless zip64_extra.zero?
97
106
  @central += CENTRAL_HEADER + name.bytesize + fields + zip64_extra
98
107
  self
99
108
  end
100
109
 
101
- # The extra field the writer adds by default: the extended timestamp
102
- # (4 + 1 + 4 bytes).
103
- def default_fields(_name)
104
- @settings.write_extended_timestamps ? 9 : 0
110
+ # The size of the extra field the writer adds by default.
111
+ def default_fields
112
+ @settings.write_extended_timestamps ? EXTENDED_TIMESTAMP : 0
105
113
  end
106
114
  end
107
115
 
@@ -4,12 +4,14 @@
4
4
 
5
5
  module Ziperu
6
6
  # An entry whose contents are being written through `get_output_stream`:
7
- # the data goes to a tempfile until the archive is committed.
7
+ # the data is held until the archive is committed. It starts in memory
8
+ # and moves to a tempfile once the archive's entries hold more than
9
+ # `SpoolIO::BUDGET` bytes in memory between them; rubyzip opens a
10
+ # tempfile for every entry, which costs more than writing a small one.
8
11
  class StreamableStream < DelegateClass(Entry) # :nodoc:all
9
- def initialize(entry)
10
- super
11
- @temp_file = Tempfile.new(::File.basename(name))
12
- @temp_file.binmode
12
+ def initialize(entry, budget = SpoolIO::Budget.new)
13
+ super(entry)
14
+ @temp_file = SpoolIO.new(::File.basename(name), budget)
13
15
  end
14
16
 
15
17
  def get_output_stream
@@ -33,16 +35,15 @@ module Ziperu
33
35
  raise StandardError, "cannot open entry for reading while its open for writing - #{name}"
34
36
  end
35
37
 
36
- @temp_file.open # reopens tempfile from top
37
- @temp_file.binmode
38
+ io = @temp_file.reader
38
39
  if block_given?
39
40
  begin
40
- yield(@temp_file)
41
+ yield(io)
41
42
  ensure
42
- @temp_file.close
43
+ io.close
43
44
  end
44
45
  else
45
- @temp_file
46
+ io
46
47
  end
47
48
  end
48
49
 
@@ -56,4 +57,103 @@ module Ziperu
56
57
  @temp_file.unlink
57
58
  end
58
59
  end
60
+
61
+ # What `get_output_stream` hands out: a StringIO that becomes a Tempfile
62
+ # (with what was written so far) when its archive's in-memory budget
63
+ # runs out. Everything else is the IO's own.
64
+ class SpoolIO < SimpleDelegator # :nodoc:all
65
+ # Bytes the pending `get_output_stream` entries of one archive may hold
66
+ # in memory between them.
67
+ BUDGET = 8 * 1024 * 1024
68
+
69
+ # The bytes left to the entries of one archive.
70
+ class Budget
71
+ attr_accessor :left
72
+
73
+ def initialize(left = BUDGET)
74
+ @left = left
75
+ end
76
+ end
77
+
78
+ def initialize(basename, budget)
79
+ @basename = basename
80
+ @budget = budget
81
+ @held = 0
82
+ @file = nil
83
+ super(::StringIO.new(+''.b))
84
+ end
85
+
86
+ # Every call goes to the IO; afterwards, the memory it holds is
87
+ # counted, and past the budget moved to a tempfile.
88
+ def method_missing(name, *args, &block)
89
+ result = super
90
+ account unless @file
91
+ result.equal?(__getobj__) ? self : result
92
+ end
93
+ ruby2_keywords(:method_missing)
94
+
95
+ def respond_to_missing?(name, include_private = false)
96
+ __getobj__.respond_to?(name, include_private)
97
+ end
98
+
99
+ # An IO over what was written, from its start.
100
+ def reader
101
+ if @file
102
+ @file.open
103
+ @file.binmode
104
+ @file
105
+ else
106
+ ::StringIO.new(__getobj__.string, 'rb')
107
+ end
108
+ end
109
+
110
+ def unlink
111
+ if @file
112
+ @file.close
113
+ @file.unlink
114
+ else
115
+ __getobj__.string.clear unless __getobj__.string.frozen?
116
+ @budget.left += @held
117
+ @held = 0
118
+ end
119
+ end
120
+
121
+ def path
122
+ spill
123
+ @file.path
124
+ end
125
+
126
+ private
127
+
128
+ def account
129
+ size = __getobj__.string.bytesize
130
+ grown = size - @held
131
+ return if grown.zero?
132
+
133
+ if grown > @budget.left
134
+ spill
135
+ else
136
+ @budget.left -= grown
137
+ @held = size
138
+ end
139
+ end
140
+
141
+ def spill
142
+ return if @file
143
+
144
+ io = __getobj__
145
+ file = ::Tempfile.new(@basename)
146
+ file.binmode
147
+ file.write(io.string)
148
+ if io.closed?
149
+ file.close
150
+ else
151
+ file.pos = io.pos
152
+ end
153
+ @budget.left += @held
154
+ @held = 0
155
+ @file = file
156
+ __setobj__(file)
157
+ end
158
+ end
59
159
  end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ziperu
4
+ # What `File#verify(report: true)` found, entry by entry:
5
+ #
6
+ # - `verified`: the entries whose data inflated with the right CRC and
7
+ # size, in archive order;
8
+ # - `failed`: a Hash of the entries that did not to the exception raised;
9
+ # - `skipped`: a Hash of the entries not checked to why: `:encrypted` (no
10
+ # password was given) or `:not_written` (added or replaced since the
11
+ # archive was opened, so not in it yet).
12
+ #
13
+ # Directories have no data and appear in none of them.
14
+ # Not in rubyzip.
15
+ class VerifyReport
16
+ attr_reader :verified, :failed, :skipped
17
+
18
+ def initialize # :nodoc:
19
+ @verified = []
20
+ @failed = {}
21
+ @skipped = {}
22
+ end
23
+
24
+ # True when every entry with data was checked and passed.
25
+ def ok?
26
+ @failed.empty? && @skipped.empty?
27
+ end
28
+
29
+ # True when nothing that was checked failed; skipped entries aside.
30
+ def no_failures?
31
+ @failed.empty?
32
+ end
33
+
34
+ def add_verified(entry) # :nodoc:
35
+ @verified << entry
36
+ end
37
+
38
+ def add_failed(entry, error) # :nodoc:
39
+ @failed[entry] = error
40
+ end
41
+
42
+ def add_skipped(entry, reason) # :nodoc:
43
+ @skipped[entry] = reason
44
+ end
45
+
46
+ def freeze
47
+ @verified.freeze
48
+ @failed.freeze
49
+ @skipped.freeze
50
+ super
51
+ end
52
+
53
+ # One line: how many were verified, failed and skipped.
54
+ def to_s
55
+ "#{@verified.size} verified, #{@failed.size} failed, #{@skipped.size} skipped"
56
+ end
57
+
58
+ def inspect
59
+ "#<#{self.class.name} #{self}>"
60
+ end
61
+ end
62
+ end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ziperu
4
- VERSION = "0.2.2"
4
+ VERSION = '0.3.0'
5
5
  # The rubyzip API this gem tracks.
6
- RUBYZIP_COMPAT_VERSION = "3.7"
6
+ RUBYZIP_COMPAT_VERSION = '3.7'
7
7
  end
data/lib/ziperu.rb CHANGED
@@ -2,17 +2,17 @@
2
2
 
3
3
  # Same standard-library surface rubyzip loads, since code downstream tends
4
4
  # to rely on it being there.
5
- require 'English'
6
- require 'delegate'
7
- require 'digest'
8
- require 'fileutils'
9
- require 'forwardable'
10
- require 'pathname'
11
- require 'rbconfig'
12
- require 'singleton'
13
- require 'stringio'
14
- require 'tempfile'
15
- require 'zlib'
5
+ require "English"
6
+ require "delegate"
7
+ require "digest"
8
+ require "fileutils"
9
+ require "forwardable"
10
+ require "pathname"
11
+ require "rbconfig"
12
+ require "singleton"
13
+ require "stringio"
14
+ require "tempfile"
15
+ require "zlib"
16
16
 
17
17
  require_relative "ziperu/version"
18
18
  require_relative "ziperu/compat"
@@ -34,12 +34,12 @@ rescue LoadError => e
34
34
  " The native extension needs libgcc_s.so.1: on Alpine run `apk add libgcc`."
35
35
  elsif e.message.include?("ld-linux") || e.message.include?("musl")
36
36
  " The installed ziperu gem was built for another libc; on Alpine (musl) use " \
37
- "`gem install ziperu --platform #{RUBY_PLATFORM}` or " \
38
- "`bundle lock --add-platform #{RUBY_PLATFORM}`, or install the source gem with " \
39
- "`gem install ziperu --platform ruby` (needs a Rust toolchain)."
37
+ "`gem install ziperu --platform #{RUBY_PLATFORM}` or " \
38
+ "`bundle lock --add-platform #{RUBY_PLATFORM}`, or install the source gem with " \
39
+ "`gem install ziperu --platform ruby` (needs a Rust toolchain)."
40
40
  else
41
41
  " The native extension for Ruby #{RUBY_VERSION} is missing from the installed gem; " \
42
- "install the source gem with `gem install ziperu --platform ruby` (needs a Rust toolchain)."
42
+ "install the source gem with `gem install ziperu --platform ruby` (needs a Rust toolchain)."
43
43
  end
44
44
  raise LoadError, e.message + hint
45
45
  end
@@ -70,6 +70,8 @@ require_relative "ziperu/deflater"
70
70
  require_relative "ziperu/streamable_directory"
71
71
  require_relative "ziperu/file_split"
72
72
  require_relative "ziperu/file"
73
+ require_relative "ziperu/verify_report"
74
+ require_relative "ziperu/limits"
73
75
  require_relative "ziperu/size_estimator"
74
76
 
75
77
  # Drop-in replacement for rubyzip, backed by a Rust core.
@@ -84,7 +86,7 @@ require_relative "ziperu/size_estimator"
84
86
  module Ziperu
85
87
  # Loaded on first use, so net/http is only required by those who read
86
88
  # archives over HTTP.
87
- autoload :RemoteIO, ::File.expand_path('ziperu/remote_io', __dir__)
89
+ autoload :RemoteIO, ::File.expand_path("ziperu/remote_io", __dir__)
88
90
 
89
91
  # Used by the ported Deflater only; kept because rubyzip exposes it.
90
92
  ZLIB_FLUSHING_STRATEGY = defined?(JRUBY_VERSION) ? Zlib::SYNC_FLUSH : Zlib::NO_FLUSH
@@ -179,7 +181,7 @@ module Ziperu
179
181
  # every run and machine. Read from the SOURCE_DATE_EPOCH environment
180
182
  # variable, the convention build tools follow; set it here to apply
181
183
  # it without the variable, or to nil to ignore the variable.
182
- @source_date_epoch = ENV['SOURCE_DATE_EPOCH']&.then { |v| Integer(v, exception: false) }
184
+ @source_date_epoch = ENV["SOURCE_DATE_EPOCH"]&.then { |v| Integer(v, exception: false) }
183
185
  end
184
186
 
185
187
  # The time a new entry gets by default: `source_date_epoch` when set,
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ziperu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: x86_64-linux-gnu
6
6
  authors:
7
7
  - Maor Caplan
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2026-11-11 00:00:00.000000000 Z
10
+ date: 2026-11-14 00:00:00.000000000 Z
11
11
  dependencies: []
12
12
  description: Zip reading and writing for Ruby with rubyzip's API under the Ziperu
13
13
  namespace, implemented on the Rust `zip` crate via Magnus. `require "zip"` aliases
@@ -103,6 +103,8 @@ files:
103
103
  - lib/ziperu/extra_field/unknown.rb
104
104
  - lib/ziperu/extra_field/zip64.rb
105
105
  - lib/ziperu/file.rb
106
+ - lib/ziperu/file/commit.rb
107
+ - lib/ziperu/file/extraction.rb
106
108
  - lib/ziperu/file_split.rb
107
109
  - lib/ziperu/filesystem.rb
108
110
  - lib/ziperu/filesystem/dir.rb
@@ -116,6 +118,7 @@ files:
116
118
  - lib/ziperu/ioextras/abstract_input_stream.rb
117
119
  - lib/ziperu/ioextras/abstract_output_stream.rb
118
120
  - lib/ziperu/ioextras/fake_io.rb
121
+ - lib/ziperu/limits.rb
119
122
  - lib/ziperu/null_compressor.rb
120
123
  - lib/ziperu/null_decompressor.rb
121
124
  - lib/ziperu/null_input_stream.rb
@@ -129,6 +132,7 @@ files:
129
132
  - lib/ziperu/size_estimator.rb
130
133
  - lib/ziperu/streamable_directory.rb
131
134
  - lib/ziperu/streamable_stream.rb
135
+ - lib/ziperu/verify_report.rb
132
136
  - lib/ziperu/version.rb
133
137
  homepage: https://github.com/caplanmaor/ziperu
134
138
  licenses:
@@ -136,6 +140,7 @@ licenses:
136
140
  metadata:
137
141
  homepage_uri: https://github.com/caplanmaor/ziperu
138
142
  source_code_uri: https://github.com/caplanmaor/ziperu
143
+ bug_tracker_uri: https://github.com/caplanmaor/ziperu/issues
139
144
  changelog_uri: https://github.com/caplanmaor/ziperu/blob/main/CHANGELOG.md
140
145
  rubygems_mfa_required: 'true'
141
146
  rdoc_options: []