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.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +190 -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/source.rb +41 -6
  15. data/lib/ziperu/entry.rb +252 -112
  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 +285 -0
  22. data/lib/ziperu/file.rb +77 -405
  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 +30 -32
  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 +118 -31
  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/verify_report.rb +62 -0
  40. data/lib/ziperu/version.rb +2 -2
  41. data/lib/ziperu.rb +19 -17
  42. metadata +7 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5ef9b8e631e00dc12ec631be6c767e1866218d91e195894689e61341b05bb0b4
4
- data.tar.gz: c257e8423a9f3a34eb08085fd817118ac80eb3a7b8f641a3422c9c61afccbc96
3
+ metadata.gz: 7c1c83b3f365169e23e5deb2f17fd91ed0899495ff0a1effc78610c06369df03
4
+ data.tar.gz: 3a5ae29c4d163eedbe64067be5960a64ec6f9383231d5742d6a75524abf907a3
5
5
  SHA512:
6
- metadata.gz: e36ad9b2aed39cf2eb2646f3b88e3c2b28686ecc4bd5752b83de5f2835ebd8f11710315ade0a2de9ea2a38d86bb12b6bdf595e513190b8ece45ebc0b2a39c740
7
- data.tar.gz: 195de33b521aceacb49a331afdb988304eb016c864ab5175f99b54616f87497d0981bb37d9c9006859f647562f15547c0e0b647fe83b68095a3a84af48e9caa1
6
+ metadata.gz: 1bfc68f3c4c19659a6ce49e4494dad8ee5629337830bb11d4b0b53662560aa88cdf58af00798bf87317a0f6151ad5aa8985e6fc197dac04e0c8ff81dfebc0ccd
7
+ data.tar.gz: ba6760aff9da62e8d575045e7ea00df5916152cd9e735d0b0966e48924bf9660ce36a39d75f8a4ac5f98d88172df9695851eba85e3f3e087981f2f46d68d3778
data/CHANGELOG.md CHANGED
@@ -1,5 +1,195 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.3.1 (2026-11-16)
4
+
5
+ Fixed:
6
+
7
+ - An entry that `File.foreach` yielded from a path could not be read once
8
+ `foreach` had returned: `get_input_stream` raised `IOError: closed
9
+ archive`. That covered `foreach(path).find { ... }` and entries
10
+ collected in the block. rubyzip allows it, and so does an entry of a
11
+ closed `Ziperu::File`; the archive is now reopened for these entries
12
+ too. Found by write_xlsx's test suite, 974 of whose tests failed on it.
13
+ - `File#extract`, and `extract_all` with a block, left an empty file
14
+ behind when the entry could not be read (a bad CRC, a wrong password).
15
+ The file is removed, as the native path of `extract_all` already did.
16
+ - `File#read` and `extract_all` raised a plain `Zip::Error` for an entry
17
+ in a compression method that cannot be read. They raise
18
+ `Zip::CompressionMethodError`, as rubyzip does and as
19
+ `get_input_stream` did, so one `rescue` covers every way of reading.
20
+ - `File.extract_all(path, dest, password: "...")` raised "unknown
21
+ keyword": the one-liner passed every option to `#extract_all` and none
22
+ to `open`. It now takes both kinds, the limits included.
23
+ - `File.foreach` takes `password:`, and the entries it yields are read
24
+ with it. They could only be read by building a decrypter for each.
25
+ - A `metadata_encoding:` Ruby does not know raises `ArgumentError`. It was
26
+ ignored, and names stayed binary with nothing to say why.
27
+ - The message for an archive cut short named Rust types ("Unexpected end
28
+ of zip::format::blocks::Zip32CDEBlock"). It says what is cut short: the
29
+ end of central directory record, a central directory entry and so on.
30
+ - `File#inspect` is one line (the path, the entry count, read-only or
31
+ uncommitted). The default listed every entry: half a megabyte for an
32
+ archive of 5,000, in irb, a log line or an error message.
33
+ - `InputStream.new(path, offset: n)` closes the archive before raising
34
+ when no entry starts at `n`. It was left for the GC.
35
+
36
+ Internal:
37
+
38
+ - The weekly run of other gems' test suites never got as far as running
39
+ one: installing the built gem with `--local` could not find `rb_sys`,
40
+ and the script had lost its execute bit.
41
+
42
+ ## 0.3.0 (2026-11-14)
43
+
44
+ Security:
45
+
46
+ - Extraction wrote through a link already on disk below the
47
+ destination: with a junction `dest/linked` pointing elsewhere (no
48
+ privilege needed on Windows) or a symlink, the entry `linked/x` was
49
+ written outside the destination, and with `on_exists: :overwrite` a
50
+ file symlink at an entry's path had its target overwritten. Both
51
+ `extract_all` (native and Ruby paths) and `Entry#extract` now skip,
52
+ with a warning, an entry whose path has a link on the way or is one.
53
+ The destination itself may still be a link. On Linux 5.6 and later the
54
+ native extractor also opens each file's directory with `openat2`
55
+ (`RESOLVE_BENEATH | RESOLVE_NO_SYMLINKS`), so a link swapped in after
56
+ the check is refused by the kernel; other systems, and the Ruby path,
57
+ check just before each write (see SECURITY.md).
58
+ - `symlinks: :create` accepted a chain of links that each looked inside
59
+ the destination (`b -> .`, then `c -> b/..`, which is really the
60
+ destination's parent). A link target with `..` after a name is now
61
+ refused; leading `..`s are still allowed.
62
+ - `RemoteIO` sent its `headers:` (credentials, typically) to wherever a
63
+ redirect led, and followed a redirect from https to http. Headers now
64
+ go only to the origin of the URL given, unless `forward_headers: true`;
65
+ an https-to-http redirect raises, and only http and https URLs are
66
+ accepted.
67
+ - `RemoteIO` kept a whole file in memory, however large, when the server
68
+ ignored Range; past `max_full_download:` (64 MB by default, nil for no
69
+ limit) that now raises. Ranged answers must carry the Content-Range
70
+ asked for, and a file whose ETag (or Last-Modified) changes between
71
+ requests raises instead of being read as a mix of two versions; a
72
+ strong ETag is sent as If-Match.
73
+
74
+ Fixed, failures that destroyed data:
75
+
76
+ - An overwrite during extraction (`on_exists: :overwrite`, or a block
77
+ that says so) truncated the existing file, then deleted it if the
78
+ entry failed to extract (a bad CRC, a size limit, a full disk). The
79
+ new file is now written beside it and renamed over it once complete,
80
+ so the original survives a failure. The replacement is a new file:
81
+ it no longer keeps the old one's permissions or hard links.
82
+ - `File.open(path, write: other)` wrote straight over `other`, so a
83
+ failed commit left a partial archive there. It is now written beside
84
+ it and renamed into place.
85
+ - On Windows, a commit over an archive still open elsewhere moves the
86
+ old one aside first; if the new one then could not be renamed into
87
+ place, neither was left at the path. The old one is now put back.
88
+
89
+ Added:
90
+
91
+ - `File#verify(report: true)` returns a `Ziperu::VerifyReport` listing
92
+ the entries verified, the ones that failed (with the exception) and
93
+ the ones skipped and why (`:encrypted` without a password,
94
+ `:not_written` for an entry added since opening). `ok?` is false when
95
+ anything was skipped, so an archive with nothing checked no longer
96
+ passes for a sound one. Without `report:`, `verify` returns the
97
+ failures as before.
98
+ - `File#extraction_plan(dest, **options)` says what `extract_all` would
99
+ do, entry by entry, without writing anything: the path each entry goes
100
+ to, whether it would be extracted or why not (`:unsafe`,
101
+ `:through_link`, `:symlink_skipped`, `:symlink_refused`), whether
102
+ something is already there, which entries land on the same path once
103
+ sanitized, and the declared size (`declared_size`). It is built from the same plan
104
+ `extract_all` follows.
105
+ - `Ziperu::Limits::UPLOAD`, `STRICT` and `NONE`: ready-made sets of the
106
+ limit settings, as keywords for one archive or through
107
+ `Ziperu::Limits.apply` for all.
108
+
109
+ Faster (Windows, against 0.2.3):
110
+
111
+ - Opening an archive hands each entry's facts from the native core as
112
+ one small Array, yielded directly, rather than a Hash of fifteen keys:
113
+ 10,000 entries open in 36 ms instead of 44, with a fifth fewer objects.
114
+ - `gets` and `each_line` read from an offset into the buffer instead of
115
+ cutting each line off its front: a 15 MB CSV of 400,000 lines, 2.0 s to
116
+ 0.19 s (rubyzip: 1.6 s); 1000 small text entries, 140 to 64 ms.
117
+ - Small writes (`puts`, `<<` of a field) are gathered and handed to the
118
+ native writer 64 KB at a time: 80,000 writes of 100 bytes, 142 to
119
+ 73 ms.
120
+ - A commit that rewrites the archive (a remove, a rename) asks the native
121
+ core in one pass which entries carry fields a raw copy would lose, and
122
+ copies runs of untouched entries in one call. Removing one entry of
123
+ 10,000: 329 to 178 ms.
124
+
125
+ Fixed:
126
+
127
+ - An append (`OutputStream.open(path, append: true)`, or a commit that
128
+ only adds) that failed before its first entry was added damaged the
129
+ last entry already in the archive; the archive is now left as it was.
130
+ - `InputStream#read(n)` on a stream (a pipe or socket) took time in
131
+ proportion to `n` for every 64 KB read, so a very large `n` was slow or
132
+ failed to allocate.
133
+ - `RemoteIO`: a read spanning the oldest cached block and one not yet
134
+ fetched raised `NoMethodError` once the cache was full.
135
+ - `Zip::Deflater` and `Zip::PassThruCompressor`, kept for code that names
136
+ them, raised `NoMethodError` on the first write.
137
+ - A `Timeout` (or `Thread#raise`, or Ctrl-C) could be lost for good. When
138
+ it was delivered during the native core's own check for a pending
139
+ exception, that check dropped it. A `Timeout.timeout` around a loop of
140
+ reads then never fired, and the loop ran forever. The exception is now
141
+ raised. Seen as a CI test that hung about one run in five.
142
+ - A `Timeout` on a stream's read of a whole large entry
143
+ (`get_input_stream(name, &:read)`) took effect late. The interrupted
144
+ read first copied everything it had read so far, to keep it for the
145
+ next read: for a 768 MiB entry, longer than reading the entry on macOS.
146
+ It now keeps the String it was filling and copies only if another read
147
+ follows.
148
+
149
+ Changed:
150
+
151
+ - `InputStream#ungetc` returns nil, as `IO#ungetc` does.
152
+
153
+ Internal:
154
+
155
+ - RuboCop (`rake rubocop`, and a CI job) with a configuration that keeps
156
+ rubyzip's style in the ported files; clippy now requires a `SAFETY:`
157
+ comment on every unsafe block.
158
+ - `rake compat` gates test by test against COMPAT.md, not on the total:
159
+ a test that newly fails, a file that no longer loads or a file with
160
+ fewer passes fails the task, and COMPAT.md is only rewritten by a full
161
+ run without a regression (or with `COMPAT_ACCEPT=1`). COMPAT.md is
162
+ regenerated: 419 of 476 rubyzip tests pass.
163
+ - Seeded property tests in the Rust crate (`cargo test`) for the
164
+ extraction path check, the zip64 extra field parser and the ZipCrypto
165
+ round trip, 5,000 cases each.
166
+ - The native core exports the layout of its entry rows
167
+ (`Native::Archive::ROW_FIELDS`), built from one list on the Rust side
168
+ for both the rows and the Hashes; a test checks it against
169
+ `Entry::ROW_FIELDS`.
170
+ - `File` is split by job: committing lives in `file/commit.rb`,
171
+ extraction in `file/extraction.rb`, and which entries `extract_all`
172
+ writes, in what order and under which names, is an `ExtractionPlan`.
173
+ An unknown `sanitize:` now raises even for an empty archive.
174
+ - The regression tests that were filed by release (0.2.1, 0.2.2, the code
175
+ review, the quality pass) are regrouped by behaviour: failure
176
+ recovery, IO semantics, names and metadata, archive options.
177
+ - The nightly CI run of the whole suite under GC stress overran GitHub's
178
+ six hours every night. GC stress is now on only from each test's setup
179
+ to its teardown, not while Bundler, minitest and the gem load. The run
180
+ is split into four parallel jobs, and the seven scale tests, which take
181
+ more than a quarter of an hour each under stress, are left out. Every
182
+ push still runs the native-boundary tests under stress.
183
+ - The test that a `Timeout` interrupts native work no longer assumes a
184
+ 768 MiB read takes over 0.1 s, which failed on a fast Windows runner.
185
+ It times the work warm and repeats it under a fraction of that. For a
186
+ stream and for `extract_all` it then checks what was left behind (how
187
+ far the reader got, no finished file), not the clock: tidying up after
188
+ an interrupt takes time that varies with the machine. Under valgrind
189
+ it runs the interrupted paths without judging how promptly they stop:
190
+ valgrind runs one thread at a time, and the one raising the timeout can
191
+ wait a whole read for its turn.
192
+
3
193
  ## 0.2.3 (2026-11-11)
4
194
 
5
195
  Faster (Windows, against 0.2.2):
data/README.md CHANGED
@@ -19,15 +19,18 @@ the [`zip`](https://github.com/zip-rs/zip2) crate through
19
19
  [Magnus](https://github.com/matsadler/magnus). `require "zip"` aliases
20
20
  `Zip = Ziperu`, so existing code runs unchanged.
21
21
 
22
- On GitHub's Linux runners (Ruby 3.4, median of 5), against rubyzip 3.7.0:
22
+ ziperu 0.2.3 on GitHub's Linux runners (Ruby 3.4, median of 5), against
23
+ rubyzip 3.7.0:
23
24
 
24
25
  | scenario | rubyzip | ziperu | speedup |
25
26
  |---|---:|---:|---:|
26
- | read 1000 x 8 KB deflated entries | 80 ms | 14 ms | 5.8x |
27
- | open and list 10000 entries | 135 ms | 58 ms | 2.3x |
28
- | deflate one 64 MB entry | 188 ms | 77 ms | 2.4x |
29
- | extract_all 1000 entries | 169 ms | 80 ms | 2.1x |
30
- | add 1000 files from disk (`threads: 4`) | 214 ms | 60 ms | 3.6x |
27
+ | read 1000 x 8 KB deflated entries | 110 ms | 18 ms | 6.1x |
28
+ | inflate one 64 MB entry | 112 ms | 38 ms | 3.0x |
29
+ | open and list 10000 entries | 184 ms | 75 ms | 2.5x |
30
+ | deflate one 64 MB entry | 323 ms | 101 ms | 3.2x |
31
+ | `get_output_stream` 1000 entries and commit | 310 ms | 84 ms | 3.7x |
32
+ | append one entry to a 10000-entry archive | 1.16 s | 148 ms | 7.8x |
33
+ | add 1000 files from disk (`threads: 4`) | 348 ms | 89 ms | 3.9x |
31
34
 
32
35
  macOS and Windows, every scenario, and which calls release the GVL (what
33
36
  Puma users ask about) are in [docs/benchmarks.md](docs/benchmarks.md).
@@ -35,7 +38,7 @@ Puma users ask about) are in [docs/benchmarks.md](docs/benchmarks.md).
35
38
  - **Precompiled** for Linux (glibc and musl), macOS and Windows, Ruby 3.1 to 4.0. No toolchain, no runtime dependencies.
36
39
  - **Fast**: entries inflate and deflate in Rust with the GVL released, the central directory is parsed once, unchanged entries are copied byte for byte on commit, and a commit that only adds entries appends them in place.
37
40
  - **Reads everything**: Stored, Deflate, Deflate64, bzip2, zstd, LZMA, XZ, PPMd and the PKZIP 1.x methods; AES and ZipCrypto. Writes Deflate, Stored, bzip2, zstd, XZ, PPMd, AES and ZipCrypto.
38
- - **Safe by default**: no entry inflates past its declared size, extraction never escapes the destination, symlinks are opt-in, and limits on entry count, entry size, total size and compression ratio can be set up front. [SECURITY.md](SECURITY.md) has the details.
41
+ - **Safe by default**: no entry inflates past its declared size, extraction skips names that resolve outside the destination and never writes through a link already on disk, symlinks are opt-in, and limits on entry count, entry size, total size and compression ratio can be set up front. [SECURITY.md](SECURITY.md) has the details.
39
42
  - **Friendlier than rubyzip where it costs nothing**: a password on the archive (`File.open(path, password: ...)`), `add` from an IO, Pathname arguments, `File#verify`, progress callbacks, and errors that fail at the call site.
40
43
  - **Checked against the field**: rubyzip's own test suite, differential fuzzing against Python's `zipfile` and rubyzip, interoperability runs with Info-ZIP, 7-Zip and Python, and the test suites of gems built on rubyzip (roo, caxlsx, rubyXL, docx, write_xlsx, sablon, gepub) run with ziperu in its place.
41
44
 
@@ -236,6 +239,31 @@ Ziperu::File.open(upload, max_entries: 1_000, max_total_size: 1 << 30) { |zip| .
236
239
  Ziperu::OutputStream.open("out.zip", write_extended_timestamps: true) { |zos| ... }
237
240
  ```
238
241
 
242
+ `Ziperu::Limits` has ready-made sets of these: `UPLOAD` for archives
243
+ users upload, `STRICT` for small documents, `NONE` to clear them.
244
+
245
+ ```ruby
246
+ Ziperu::File.open(upload, **Ziperu::Limits::UPLOAD) { |zip| ... }
247
+ Ziperu::Limits.apply(Ziperu::Limits::STRICT) # the globals
248
+ ```
249
+
250
+ Look before you extract. `extraction_plan` says what `extract_all` would
251
+ do with each entry, without writing anything. `verify(report: true)` says
252
+ which entries were checked, which failed and which were skipped:
253
+
254
+ ```ruby
255
+ Ziperu::File.open(upload) do |zip|
256
+ plan = zip.extraction_plan("out", sanitize: :windows)
257
+ plan.reject(&:extract?).each { |item| warn "#{item.entry.name}: #{item.status}" }
258
+ plan.select(&:collides_with) # entries that land on the same path
259
+ plan.sum(&:declared_size) # declared bytes to be written
260
+
261
+ report = zip.verify(report: true)
262
+ report.ok? # false if anything failed or was skipped
263
+ report.skipped # { entry => :encrypted }, say
264
+ end
265
+ ```
266
+
239
267
  ## Migrating from rubyzip
240
268
 
241
269
  Change the Gemfile line and keep `require "zip"`: the `Zip` constant and
data/lib/zip.rb CHANGED
@@ -5,14 +5,14 @@
5
5
  # runs unchanged. If another library already defines `Zip` (rubyzip itself,
6
6
  # most likely), the two cannot share the name: load ziperu with
7
7
  # `require "ziperu"` and use `Ziperu::` directly instead.
8
- if defined?(::Zip) && !(defined?(::Ziperu) && ::Zip.equal?(::Ziperu))
8
+ if defined?(Zip) && !(defined?(Ziperu) && Zip.equal?(Ziperu))
9
9
  raise LoadError, "ziperu cannot alias Zip = Ziperu: a Zip constant is already defined " \
10
- "(by #{Object.const_source_location(:Zip)&.first || 'another library'}). " \
10
+ "(by #{Object.const_source_location(:Zip)&.first || "another library"}). " \
11
11
  "Remove rubyzip, or require \"ziperu\" and use Ziperu:: instead."
12
12
  end
13
13
 
14
14
  require "ziperu"
15
- Zip = Ziperu unless defined?(::Zip)
15
+ Zip = Ziperu unless defined?(Zip)
16
16
 
17
17
  # The `zip/...` paths rubyzip's files live at (`require "zip/filesystem"`
18
18
  # is the common one) are kept under lib/zip/ as one-line shims onto the
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -4,7 +4,8 @@
4
4
  # (Copyright (C) 2002-2026 the rubyzip authors).
5
5
 
6
6
  module Ziperu
7
- # Reads just the end-of-central-directory records of an archive, in Ruby.
7
+ # Reads the end-of-central-directory records of an archive, in Ruby, and
8
+ # lists an archive held in an IO (`read_from_stream`).
8
9
  #
9
10
  # The native core refuses archives whose EOCD declares more entries than
10
11
  # the central directory holds, so `Ziperu::File.open` raises on them
@@ -37,7 +38,6 @@ module Ziperu
37
38
  @entry_set = EntrySet.new
38
39
  @native.entries.each { |meta| @entry_set.push_record(Entry.from_meta(@native, meta)) }
39
40
  @size = @entry_set.size
40
- @cdir_offset = @native.central_directory_start if @native.respond_to?(:central_directory_start)
41
41
  self
42
42
  end
43
43
 
@@ -59,9 +59,7 @@ module Ziperu
59
59
  end
60
60
 
61
61
  def count_entries(io)
62
- if IOExtras.path_like?(io)
63
- return ::File.open(IOExtras.path_of(io), 'rb') { |f| count_entries(f) }
64
- end
62
+ return ::File.open(IOExtras.path_of(io), 'rb') { |f| count_entries(f) } if IOExtras.path_like?(io)
65
63
 
66
64
  read_eocds(io)
67
65
  @size
@@ -119,9 +117,7 @@ module Ziperu
119
117
  return if unpack_e_o_c_d(data.slice(eocd_location..-1))
120
118
 
121
119
  zip64_eocd_locator = data.rindex([ZIP64_EOCD_LOCATOR_SIG].pack('V'), eocd_location)
122
- unless zip64_eocd_locator
123
- raise Error, 'Zip64 end of central directory locator signature expected but not found'
124
- end
120
+ raise Error, 'Zip64 end of central directory locator signature expected but not found' unless zip64_eocd_locator
125
121
 
126
122
  zip64_eocd_location = data.rindex([ZIP64_END_OF_CD_SIG].pack('V'), zip64_eocd_locator)
127
123
 
@@ -20,6 +20,14 @@ module Ziperu
20
20
 
21
21
  SPLIT_FILE_SIGNATURE = 0x08074b50
22
22
 
23
+ # The longest a name, comment or extra field can be: each length is a
24
+ # 16-bit field.
25
+ MAX_FIELD_LENGTH = 0xFFFF
26
+
27
+ # A size or offset from here up does not fit its 32-bit field and needs
28
+ # the zip64 form.
29
+ ZIP64_SIZE_LIMIT = 0xFFFFFFFF
30
+
23
31
  FILE_TYPE_FILE = 0o10
24
32
  FILE_TYPE_DIR = 0o04
25
33
  FILE_TYPE_SYMLINK = 0o12
data/lib/ziperu/crypto.rb CHANGED
@@ -12,9 +12,9 @@ module Ziperu
12
12
  end
13
13
  end
14
14
 
15
- class TraditionalDecrypter < Decrypter; end
15
+ class TraditionalDecrypter < Decrypter; end # :nodoc:
16
16
 
17
- class AESDecrypter < Decrypter
17
+ class AESDecrypter < Decrypter # :nodoc:
18
18
  def initialize(password, _strength = nil)
19
19
  super(password)
20
20
  end
@@ -39,10 +39,17 @@ module Ziperu
39
39
  end
40
40
  end
41
41
 
42
+ # No encryption.
42
43
  class NullEncrypter < Encrypter
43
44
  def initialize
44
45
  super(nil)
45
46
  end
47
+
48
+ # rubyzip's compressors (Deflater, PassThruCompressor) pass their
49
+ # output through the encrypter.
50
+ def encrypt(data) # :nodoc:
51
+ data
52
+ end
46
53
  end
47
54
 
48
55
  # ZipCrypto, the weak legacy cipher that Windows Explorer and other
@@ -55,6 +62,7 @@ module Ziperu
55
62
  end
56
63
  end
57
64
 
65
+ # WinZip AES; 256-bit unless another strength is given.
58
66
  class AESEncrypter < Encrypter
59
67
  include AESEncryption
60
68
 
@@ -77,7 +77,7 @@ module Ziperu
77
77
  hour = (0b1111100000000000 & bin_dos_time) >> 11
78
78
  day = (0b11111 & bin_dos_date)
79
79
  month = (0b111100000 & bin_dos_date) >> 5
80
- year = ((0b1111111000000000 & bin_dos_date) >> 9) + 1980
80
+ year = ((0b1111111000000000 & bin_dos_date) >> 9) + DOS_EPOCH_YEAR
81
81
 
82
82
  time = local(year, month, day, hour, minute, second)
83
83
  time.absolute_time = false
@@ -52,6 +52,11 @@ module Ziperu
52
52
  stored&.native(offset)
53
53
  end
54
54
 
55
+ # The password its owner was given for the archive, or nil.
56
+ def password
57
+ nil
58
+ end
59
+
55
60
  # The record's local-header extra fields, as stored.
56
61
  def local_extra(offset)
57
62
  stored.local_extra(offset)
@@ -69,8 +74,9 @@ module Ziperu
69
74
 
70
75
  NONE = None.new.freeze
71
76
 
72
- # A record of `native`, the archive `owner` (a Ziperu::File, or nil
73
- # for an entry read through an InputStream) has open. `index` is nil
77
+ # A record of `native`, the archive `owner` has open: a Ziperu::File,
78
+ # a Reopener for an entry `File.foreach` yielded from a path, or nil
79
+ # for an entry read through an InputStream. `index` is nil
74
80
  # for a record the native core cannot address by index (one of
75
81
  # several under the same name), which is found by its offset.
76
82
  class Archive
@@ -89,11 +95,14 @@ module Ziperu
89
95
  self
90
96
  end
91
97
 
92
- # The native archive, reopened if the owning Ziperu::File released
93
- # it on close: rubyzip lets entries be read after `close`.
98
+ def password
99
+ @owner.password if @owner.respond_to?(:password)
100
+ end
101
+
102
+ # The native archive, reopened if its owner released it on close:
103
+ # rubyzip lets entries be read after `close`.
94
104
  def native(offset)
95
- if @native.closed? && @owner.kind_of?(::Ziperu::File)
96
- reopened = @owner.native_archive
105
+ if @native.closed? && (reopened = (@owner.native_archive if @owner.respond_to?(:native_archive)))
97
106
  unless still_at_place?(reopened, offset)
98
107
  where = @index ? "at index #{@index} of" : 'in'
99
108
  raise EntryMovedError, "entry '#{@raw_name}' is no longer #{where} #{@owner.name}"
@@ -132,6 +141,32 @@ module Ziperu
132
141
  end
133
142
  end
134
143
 
144
+ # Stands in for a Ziperu::File as the owner of the entries that
145
+ # `File.foreach` yields: it has the password given for the archive,
146
+ # and for an archive at a path it reopens it, so the entries can be
147
+ # read once the walk is over (`foreach(path).find { ... }`, or
148
+ # entries collected in the block). One is shared by all the entries
149
+ # of a walk, and so is the archive it reopens.
150
+ class Reopener
151
+ attr_reader :name, :password
152
+
153
+ # `name` is the archive's path, or nil for one read from an IO,
154
+ # which cannot be opened again.
155
+ def initialize(name, settings, password = nil)
156
+ @name = name
157
+ @settings = settings
158
+ @password = password
159
+ @native = nil
160
+ end
161
+
162
+ def native_archive
163
+ return if @name.nil?
164
+
165
+ @native = InputStream.open_archive(@name, @settings) if @native.nil? || @native.closed?
166
+ @native
167
+ end
168
+ end
169
+
135
170
  # Data still outside the archive.
136
171
  class Pending
137
172
  include Defaults