ziperu 0.2.3 → 0.3.1

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 (54) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +190 -0
  3. data/Cargo.lock +2 -1
  4. data/README.md +35 -7
  5. data/ext/ziperu/Cargo.toml +9 -1
  6. data/ext/ziperu/src/archive.rs +246 -144
  7. data/ext/ziperu/src/entry_options.rs +1 -1
  8. data/ext/ziperu/src/errors.rs +39 -11
  9. data/ext/ziperu/src/extract.rs +332 -104
  10. data/ext/ziperu/src/gvl.rs +45 -26
  11. data/ext/ziperu/src/lib.rs +17 -0
  12. data/ext/ziperu/src/place.rs +379 -0
  13. data/ext/ziperu/src/rb.rs +14 -1
  14. data/ext/ziperu/src/reader.rs +100 -29
  15. data/ext/ziperu/src/sink.rs +2 -7
  16. data/ext/ziperu/src/source.rs +4 -7
  17. data/ext/ziperu/src/stream.rs +89 -11
  18. data/ext/ziperu/src/test_rng.rs +37 -0
  19. data/ext/ziperu/src/writer.rs +71 -25
  20. data/ext/ziperu/src/zipcrypto.rs +40 -12
  21. data/lib/zip.rb +3 -3
  22. data/lib/ziperu/central_directory.rb +4 -8
  23. data/lib/ziperu/constants.rb +8 -0
  24. data/lib/ziperu/crypto.rb +10 -2
  25. data/lib/ziperu/dos_time.rb +1 -1
  26. data/lib/ziperu/entry/source.rb +41 -6
  27. data/lib/ziperu/entry.rb +252 -112
  28. data/lib/ziperu/entry_set.rb +0 -5
  29. data/lib/ziperu/errors.rb +4 -2
  30. data/lib/ziperu/extra_field/zip64.rb +1 -3
  31. data/lib/ziperu/extra_field.rb +10 -10
  32. data/lib/ziperu/file/commit.rb +255 -0
  33. data/lib/ziperu/file/extraction.rb +285 -0
  34. data/lib/ziperu/file.rb +77 -405
  35. data/lib/ziperu/file_split.rb +1 -3
  36. data/lib/ziperu/filesystem/dir.rb +3 -9
  37. data/lib/ziperu/filesystem/file.rb +2 -6
  38. data/lib/ziperu/filesystem/zip_file_name_mapper.rb +2 -2
  39. data/lib/ziperu/input_stream.rb +30 -32
  40. data/lib/ziperu/ioextras/abstract_input_stream.rb +83 -31
  41. data/lib/ziperu/ioextras/abstract_output_stream.rb +1 -3
  42. data/lib/ziperu/ioextras/fake_io.rb +1 -3
  43. data/lib/ziperu/ioextras.rb +6 -0
  44. data/lib/ziperu/limits.rb +48 -0
  45. data/lib/ziperu/null_input_stream.rb +1 -1
  46. data/lib/ziperu/output_stream.rb +118 -31
  47. data/lib/ziperu/pass_thru_decompressor.rb +1 -3
  48. data/lib/ziperu/rails_streaming.rb +5 -5
  49. data/lib/ziperu/remote_io.rb +181 -32
  50. data/lib/ziperu/size_estimator.rb +27 -19
  51. data/lib/ziperu/verify_report.rb +62 -0
  52. data/lib/ziperu/version.rb +2 -2
  53. data/lib/ziperu.rb +19 -17
  54. metadata +8 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b5e977083967283ed8594aa131fccbbea2b8b766458c6e6fa8a3006356af57e1
4
- data.tar.gz: 62d85e136fc36d7618c8b58aa2fb8ed910f403a994c9a6f1e06a1e812cd13310
3
+ metadata.gz: b96255d80b3f9f9f6f5c981f309f396731a632a87807ba036440aff303f04865
4
+ data.tar.gz: 555da85ad0dcdea11e2df3aec1eb4ec3b5fd5574d97e1fd98585f2dee944a4bc
5
5
  SHA512:
6
- metadata.gz: d57553d22be136d860dda97bade7f0a8bffe3ea28ab0f0befc2b7d2c14ef674dc219522bd02884bb62e556f5c77f93374ed3a25dd7cb2af8b54136616a2f96e1
7
- data.tar.gz: 293a8363e37ac330bdf5bfef408d7b572f35d06254188a7e6795ce5da1cf64a01f18b45cc5434cf9a9c524b91d249599c3220fe67adbe06afafd80939cca6763
6
+ metadata.gz: 1534422e2d2846dadab5576ac3c66b31a76c138cf3f989b0415993e33ab9d7ab3228a6108effb6a2cf27c9ec603bd0ab6d334e45a4a795de86ca2ff787ae05f8
7
+ data.tar.gz: f38ec763b5e80e81a6de0deeb27a5457cc95a49920c45e70e78ae631c46344e94fc1e6e0d512f335ffae84cc210b1166860b2e5ddd71a5289a93cbdea5b2543e
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/Cargo.lock CHANGED
@@ -879,11 +879,12 @@ dependencies = [
879
879
 
880
880
  [[package]]
881
881
  name = "ziperu"
882
- version = "0.2.3"
882
+ version = "0.3.1"
883
883
  dependencies = [
884
884
  "crc32fast",
885
885
  "flate2",
886
886
  "getrandom",
887
+ "libc",
887
888
  "magnus",
888
889
  "rayon",
889
890
  "rb-sys",
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
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "ziperu"
3
- version = "0.2.3"
3
+ version = "0.3.1"
4
4
  edition = "2021"
5
5
  publish = false
6
6
  rust-version = "1.88"
@@ -34,5 +34,13 @@ flate2 = { version = "1", default-features = false, features = ["zlib-rs"] }
34
34
  crc32fast = "1"
35
35
  getrandom = { version = "0.4", features = ["std"] }
36
36
 
37
+ # O_NOFOLLOW for extraction (already in the tree through the zip crate's
38
+ # dependencies).
39
+ [target.'cfg(unix)'.dependencies]
40
+ libc = "0.2"
41
+
42
+ [lints.clippy]
43
+ undocumented_unsafe_blocks = "warn"
44
+
37
45
  [build-dependencies]
38
46
  rb-sys-env = "0.2"