ziperu 0.3.1 → 0.4.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b96255d80b3f9f9f6f5c981f309f396731a632a87807ba036440aff303f04865
4
- data.tar.gz: 555da85ad0dcdea11e2df3aec1eb4ec3b5fd5574d97e1fd98585f2dee944a4bc
3
+ metadata.gz: 8a499056593773ba9423985aac1f94849369c66eb21028beddbe3b3b1923a506
4
+ data.tar.gz: 6a983144dc7bb0ea1323da3428332cdf9008cfb4e7d6a14c0b8c425d01354d53
5
5
  SHA512:
6
- metadata.gz: 1534422e2d2846dadab5576ac3c66b31a76c138cf3f989b0415993e33ab9d7ab3228a6108effb6a2cf27c9ec603bd0ab6d334e45a4a795de86ca2ff787ae05f8
7
- data.tar.gz: f38ec763b5e80e81a6de0deeb27a5457cc95a49920c45e70e78ae631c46344e94fc1e6e0d512f335ffae84cc210b1166860b2e5ddd71a5289a93cbdea5b2543e
6
+ metadata.gz: f40e92f6ade4897d74aa470a01b9ff9d445ce147ed36e4813cc4374d5144a856d3af048dfbe528976b793aca7273374c99df9a25093cf89caade0a7d3e2fb3ca
7
+ data.tar.gz: 1d4d07ae6b193a4d7538c1f688c4d5b97a0c3f6f1c734142fb6bbf2053d7e11e213f699484053816c13bd62f4e832996b9ef531e68d6e91eaeb93e886ab10eb1
data/CHANGELOG.md CHANGED
@@ -1,5 +1,119 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.4.1 (2026-12-06)
4
+
5
+ Fixed:
6
+
7
+ - An archive opened with `write: false` could still be rewritten: a
8
+ setter on one of its entries (`entry.comment = ...`, `name=`, `time=`
9
+ and the rest) marked it changed, and closing the archive wrote it out.
10
+ Those setters now raise `Ziperu::Error` at the call, and a commit of a
11
+ read-only archive writes nothing whatever was changed.
12
+ - `OutputStream#rollback_entry`, refused because the entry's first bytes
13
+ had already gone out on a stream that cannot seek, dropped what was
14
+ still buffered for that entry, so the archive came out with the entry
15
+ short. The data is kept.
16
+ - `File#rename` to a name the entry cannot take (absolute, too long)
17
+ raised only after the entry had been removed, and the next commit
18
+ wrote the archive without it. The name is checked first.
19
+ - An append in place that failed while the archive was being finished
20
+ (an archive comment too long, a disk that filled, a read-back that
21
+ failed) left the archive without a central directory: the saved one
22
+ was let go before the last steps. It is now kept until they are done
23
+ and put back if any fails, and `OutputStream.open(append: true)`
24
+ abandons the append when closing raises.
25
+ - `OutputStream.estimated_size` with `write_zip64_support: :always`
26
+ came out short by the zip64 fields every entry is then written with.
27
+ The estimator and the writer now ask the same question.
28
+ - Committing an append re-read the appended entries in time that grew
29
+ with the square of the archive: 1,000 entries appended to an archive
30
+ of 60,000 took 2.5 s to commit and now take 0.35 s.
31
+ - A file whose name was within about twenty bytes of the 255 a name may
32
+ have could be extracted but not replaced: the replacement is written
33
+ beside it under a longer name, which the file system refused (`File
34
+ name too long`). That stopped a tar archive listing such a name twice,
35
+ an extraction with `on_exists: :overwrite` over such a file, and
36
+ `Ziperu::Tar.create` to an archive so named. The name written beside
37
+ is now cut to fit. Found by the nightly tar fuzzer.
38
+
39
+ ## 0.4.0 (2026-11-21)
40
+
41
+ Added:
42
+
43
+ - `Ziperu::Tar.foreach` and `Ziperu::Tar::Reader` read tar archives:
44
+ plain, or compressed with gzip, zstd, xz or bzip2, told apart by their
45
+ first bytes and not by their name. An archive is read front to back,
46
+ an entry at a time, in constant memory, from a path or from any IO
47
+ with `read` (a pipe, a socket, a response body). An entry gives its
48
+ name, size, type, mode, time (to the nanosecond where the archive has
49
+ it), link target and owner, and its data through `read` and
50
+ `each_chunk`. v7, ustar, GNU and PAX archives are read, long names and
51
+ extended headers included; sparse files and multi-volume archives are
52
+ refused.
53
+ - `Ziperu::Tar.extract_all(path_or_io, dest)` extracts a tar archive
54
+ under the rules a zip is extracted by: a name that resolves outside
55
+ the destination is skipped with a warning, nothing is written through
56
+ a link already on disk, and times and permission bits are restored
57
+ but never setuid, setgid or an owner. Symlinks are skipped unless
58
+ `symlinks: :create`, and then made only when the target stays inside.
59
+ A hard link is made only to a file the same extraction wrote. Devices
60
+ and fifos are skipped. `on_exists:`, `exclude:`, `sanitize:` and
61
+ `progress:` are as for a zip; `strip_components:` drops leading
62
+ directories. Of two entries with one name the last wins, as `tar` has
63
+ it. Links are created after every file.
64
+ - `Ziperu.extract(path_or_io, dest)` extracts a zip or a tar archive,
65
+ told apart by the first bytes, with the options of whichever it is.
66
+ From a pipe a tar archive is streamed; a zip is read into memory
67
+ first, within `max_archive_size`.
68
+ - `Ziperu::Tar.create(path_or_io) { |tar| ... }` writes a tar archive,
69
+ plain or compressed with gzip, zstd, xz or bzip2 (from the file's name,
70
+ or `compression:`, with `level:`). `add` takes a file from disk or any
71
+ IO, `add_data` a String, and there are `mkdir`, `symlink`, `link` and
72
+ `add_recursive`; `Ziperu::Tar.add_recursive(archive, dir)` does a whole
73
+ directory and `Ziperu::Tar.body` gives a Rack body. What is written is
74
+ ustar, with a PAX extended header only for what ustar cannot hold (a
75
+ long or non-ASCII name, a size of 8 GiB or more). It goes to a pipe or
76
+ socket as readily as to a file, and a file is put in place only once
77
+ it is whole. Owners are not recorded, and with `source_date_epoch` the
78
+ same input gives the same bytes.
79
+ - [docs/tar.md](docs/tar.md) describes what is read and written and how
80
+ extraction behaves. CI now exchanges tar archives with GNU tar, bsdtar, Python's
81
+ tarfile and 7-Zip in both directions, and `rake fuzz:tar` fuzzes the
82
+ reader, the extractor and the writer against Python's tarfile.
83
+ - The limits hold for tar as for zip: `max_entries`, `max_entry_size`,
84
+ `max_total_size`, `max_archive_size` for what is read from an IO, and
85
+ `max_compression_ratio`, which is measured over the stream as it is
86
+ read and so stops a compressed bomb a few megabytes in, read or
87
+ skipped.
88
+ - A compressed tar archive is read to the end of its stream, where
89
+ gzip, xz and bzip2 keep their checksum: tar has none for an entry's
90
+ data. A long name or extended header over 1 MiB is refused, and an xz
91
+ stream that asks for more than 256 MiB to decode.
92
+
93
+ Fixed:
94
+
95
+ - On Windows an entry with a colon in its name was written as an
96
+ alternate data stream: `a.txt:x` went into `a.txt`, a file that could
97
+ have been in the destination before and was neither replaced nor
98
+ reported, whatever `on_exists:` said. And `C:x`, a path relative to
99
+ the current directory of drive C, was written there: outside the
100
+ destination. Such an entry is now skipped with a warning, as one that resolves outside the destination is;
101
+ `sanitize: :windows` rewrites the name and extracts it. This was true
102
+ of zip extraction in every release so far.
103
+
104
+ Internal:
105
+
106
+ - The native extractor is in two parts: `placer.rs` decides where an
107
+ entry goes and what is refused on the way (the path check, links on
108
+ disk, what is already there), and `extract.rs` reads zip entries into
109
+ the files it opens. Nothing changes in what is extracted or how.
110
+ - The memory growth check failed a nightly run on code that had passed
111
+ the day before. It sampled resident memory after a full GC, but memory
112
+ glibc had freed and not yet returned swung the samples between 68 and
113
+ 107 MiB. It now calls `malloc_trim` before each sample: the workload
114
+ then sits at 23.8 MiB for all 600 iterations, and the growth allowed
115
+ drops from 8 MiB to 3.
116
+
3
117
  ## 0.3.1 (2026-11-16)
4
118
 
5
119
  Fixed:
data/Cargo.lock CHANGED
@@ -879,17 +879,20 @@ dependencies = [
879
879
 
880
880
  [[package]]
881
881
  name = "ziperu"
882
- version = "0.3.1"
882
+ version = "0.4.1"
883
883
  dependencies = [
884
+ "bzip2",
884
885
  "crc32fast",
885
886
  "flate2",
886
887
  "getrandom",
887
888
  "libc",
889
+ "lzma-rust2",
888
890
  "magnus",
889
891
  "rayon",
890
892
  "rb-sys",
891
893
  "rb-sys-env",
892
894
  "zip",
895
+ "zstd",
893
896
  ]
894
897
 
895
898
  [[package]]
data/README.md CHANGED
@@ -19,8 +19,13 @@ 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
- ziperu 0.2.3 on GitHub's Linux runners (Ruby 3.4, median of 5), against
23
- rubyzip 3.7.0:
22
+ It also reads, extracts and writes tar archives (`.tar`, `.tar.gz`,
23
+ `.tar.zst`, `.tar.xz`, `.tar.bz2`), extracting under the same safety
24
+ rules, so one gem unpacks whatever was uploaded:
25
+ `Ziperu.extract(upload, "out")`.
26
+
27
+ Measured with ziperu 0.2.3 against rubyzip 3.7.0 on GitHub's Linux
28
+ runners (Ruby 3.4, median of 5 runs):
24
29
 
25
30
  | scenario | rubyzip | ziperu | speedup |
26
31
  |---|---:|---:|---:|
@@ -32,15 +37,17 @@ rubyzip 3.7.0:
32
37
  | append one entry to a 10000-entry archive | 1.16 s | 148 ms | 7.8x |
33
38
  | add 1000 files from disk (`threads: 4`) | 348 ms | 89 ms | 3.9x |
34
39
 
35
- macOS and Windows, every scenario, and which calls release the GVL (what
36
- Puma users ask about) are in [docs/benchmarks.md](docs/benchmarks.md).
40
+ Results for macOS and Windows, and the rest of the scenarios, are in
41
+ [docs/benchmarks.md](docs/benchmarks.md). It also lists which calls
42
+ release the GVL, so other threads (other Puma requests, say) keep running.
37
43
 
38
- - **Precompiled** for Linux (glibc and musl), macOS and Windows, Ruby 3.1 to 4.0. No toolchain, no runtime dependencies.
44
+ - **Precompiled** for Linux (glibc and musl), macOS and Windows, on Ruby 3.3 to 4.0, with binaries for 3.1 and 3.2 as well. No toolchain, no runtime dependencies.
39
45
  - **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.
40
46
  - **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.
47
+ - **Tar as well**: `Ziperu::Tar` reads and extracts v7, ustar, GNU and PAX archives, plain or compressed with gzip, zstd, xz or bzip2, from a path or any IO including a pipe, and writes them too. See [docs/tar.md](docs/tar.md).
41
48
  - **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.
42
49
  - **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.
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.
50
+ - **Checked against the field**: rubyzip's own test suite, differential fuzzing against Python's `zipfile` and rubyzip, interoperability runs with Info-ZIP, 7-Zip, Python, GNU tar and bsdtar, and the test suites of gems built on rubyzip (roo, caxlsx, rubyXL, docx, write_xlsx, sablon, gepub) run with ziperu in its place.
44
51
 
45
52
  ## Installation
46
53
 
@@ -56,9 +63,6 @@ Or on its own:
56
63
  gem install ziperu
57
64
  ```
58
65
 
59
- Precompiled gems ship for Linux, macOS and Windows; see
60
- [Platforms](#platforms) for the details.
61
-
62
66
  ## Quick start
63
67
 
64
68
  ```ruby
@@ -74,28 +78,10 @@ Ziperu::File.open("new.zip", create: true) do |zf|
74
78
  zf.add("report.pdf", "tmp/report.pdf")
75
79
  zf.get_output_stream("notes.txt") { |io| io.puts "written at #{Time.now}" }
76
80
  end
77
-
78
- Ziperu::OutputStream.write_buffer(socket) do |zos| # any IO; a pipe, socket or
79
- zos.put_next_entry("hello.txt") # response body is streamed,
80
- zos.write("hello") # nothing buffered to the end
81
- end
82
-
83
- body = Ziperu::OutputStream.body do |zos| # a Rack body: an Enumerator of
84
- zos.put_next_entry("export.csv") # the archive's bytes, produced
85
- zos.write(csv) # as the server consumes them
86
- end
87
- [200, { "content-type" => "application/zip" }, body]
88
-
89
- Ziperu::File.foreach("huge.zip") do |entry| # one entry at a time, constant
90
- puts entry.name if entry.size > 1 << 30 # memory, however many there are
91
- end
92
-
93
- Ziperu::File.open("secret.zip", password: "s3cret") do |zf| # AES or ZipCrypto
94
- zf.extract_all("out/")
95
- zf.add("upload.bin", StringIO.new(bytes)) # any IO, streamed on commit
96
- end
97
81
  ```
98
82
 
83
+ Streaming, passwords, limits and the rest are in the cookbook below.
84
+
99
85
  ## Cookbook
100
86
 
101
87
  Zip a directory, unzip into one:
@@ -109,7 +95,8 @@ Ziperu::File.open("site.zip") do |zf|
109
95
  end
110
96
  ```
111
97
 
112
- Build an archive in memory and hand it out, or take one in:
98
+ Build an archive in memory and hand it out, write one straight to an IO,
99
+ or take one in:
113
100
 
114
101
  ```ruby
115
102
  bytes = Ziperu::OutputStream.write_buffer do |zos|
@@ -117,26 +104,45 @@ bytes = Ziperu::OutputStream.write_buffer do |zos|
117
104
  zos.write(csv)
118
105
  end.string
119
106
 
107
+ Ziperu::OutputStream.write_buffer(socket) do |zos| # a pipe or socket is streamed,
108
+ zos.put_next_entry("report.csv") # nothing buffered to the end
109
+ zos.write(csv)
110
+ end
111
+
120
112
  Ziperu::File.open_buffer(params[:upload].read) do |zf|
121
113
  zf.each { |e| puts "#{e.name}: #{e.size} bytes" }
122
114
  end
123
115
  ```
124
116
 
125
- Stream a zip download without a temp file (Rack; in Rails, write to
126
- `response.stream` inside `ActionController::Live` the same way):
117
+ Stream a zip download without a temp file. `OutputStream.body` returns a
118
+ Rack body: an Enumerator of the archive's bytes, produced as the server
119
+ consumes them.
127
120
 
128
121
  ```ruby
122
+ require "ziperu/rails_streaming"
123
+
129
124
  body = Ziperu::OutputStream.body do |zos|
130
125
  attachments.each do |att|
131
126
  zos.put_next_entry(att.filename)
132
127
  att.download { |chunk| zos.write(chunk) }
133
128
  end
134
129
  end
135
- [200, Ziperu::RailsStreaming.headers("attachments.zip"), body] # require "ziperu/rails_streaming"
130
+ [200, Ziperu::RailsStreaming.headers("attachments.zip"), body]
131
+ ```
136
132
 
137
- class ExportsController < ApplicationController # in Rails: headers, body, no Live
133
+ In a Rails controller, `zip_stream` sets the headers and the body, with
134
+ no need for `ActionController::Live`:
135
+
136
+ ```ruby
137
+ class ExportsController < ApplicationController
138
138
  include Ziperu::RailsStreaming
139
- def show = zip_stream("export.zip") { |zos| zos.put_next_entry("a.csv"); zos.write(csv) }
139
+
140
+ def show
141
+ zip_stream("export.zip") do |zos|
142
+ zos.put_next_entry("a.csv")
143
+ zos.write(csv)
144
+ end
145
+ end
140
146
  end
141
147
  ```
142
148
 
@@ -147,6 +153,7 @@ changed archive elsewhere:
147
153
  ```ruby
148
154
  Ziperu::File.open("backup.zip") do |zf|
149
155
  zf.add("logs/today.log", "/var/log/app/today.log")
156
+ zf.add("upload.bin", StringIO.new(bytes)) # any IO, streamed on commit
150
157
  zf.get_output_stream("manifest.json") { |io| io.write(manifest.to_json) }
151
158
  end
152
159
 
@@ -154,7 +161,9 @@ Ziperu::File.open("backup.zip", write: "backup-2.zip") { |zf| zf.remove("old.log
154
161
  Ziperu::File.open("backup.zip", write: false) { |zf| zf.read("manifest.json") } # read-only
155
162
  ```
156
163
 
157
- Pick one entry out of a large archive, or read it all as a stream:
164
+ Pick one entry out of a large archive, or read it all as a stream.
165
+ `foreach` yields one entry at a time in constant memory, however many
166
+ there are:
158
167
 
159
168
  ```ruby
160
169
  entry = Ziperu::File.foreach("huge.zip").find { |e| e.name.end_with?(".sig") }
@@ -264,6 +273,41 @@ Ziperu::File.open(upload) do |zip|
264
273
  end
265
274
  ```
266
275
 
276
+ Unpack whatever was uploaded, a zip or a tar archive, compressed or not.
277
+ `Ziperu.extract` tells which from the first bytes. A tar archive is read
278
+ front to back, so it can come from a pipe or a response body:
279
+
280
+ ```ruby
281
+ Ziperu.extract(upload, "out", **Ziperu::Limits::UPLOAD)
282
+
283
+ Ziperu::Tar.extract_all("release.tar.gz", "src", strip_components: 1)
284
+
285
+ Ziperu::Tar.foreach("logs.tar.zst") do |entry| # one entry at a time
286
+ process(entry.name, entry.read) if entry.file?
287
+ end
288
+ ```
289
+
290
+ Tar extraction follows the rules zip extraction does, and adds its own
291
+ for hard links and devices; [docs/tar.md](docs/tar.md) has them, and
292
+ what is read.
293
+
294
+ Write a tar archive, to a file or to anything with `write`. The
295
+ compression follows the file's name, or `compression:`:
296
+
297
+ ```ruby
298
+ Ziperu::Tar.add_recursive("site.tar.gz", "public")
299
+
300
+ Ziperu::Tar.create("backup.tar.zst", level: 10) do |tar|
301
+ tar.add("report.pdf", "tmp/report.pdf") # a file from disk
302
+ tar.add("upload.bin", StringIO.new(bytes)) # any IO
303
+ tar.add_data("manifest.json", manifest.to_json)
304
+ tar.add_recursive("var/log/app", prefix: "logs/")
305
+ end
306
+
307
+ [200, { "content-type" => "application/gzip" },
308
+ Ziperu::Tar.body(compression: :gzip) { |tar| tar.add_data("report.csv", csv) }]
309
+ ```
310
+
267
311
  ## Migrating from rubyzip
268
312
 
269
313
  Change the Gemfile line and keep `require "zip"`: the `Zip` constant and
@@ -300,14 +344,6 @@ flagged UTF-8 come back as UTF-8 strings; duplicate names in one
300
344
  methods), is in [docs/migrating.md](docs/migrating.md). What rubyzip's own
301
345
  test suite makes of ziperu is in [COMPAT.md](COMPAT.md).
302
346
 
303
- ## Why
304
-
305
- rubyzip is pure Ruby and does per-entry work in Ruby objects. ziperu
306
- parses the central directory once in Rust and hands Ruby a flat metadata
307
- dump, reads entries in large chunks, copies unchanged entries byte for
308
- byte on `commit`, and releases the GVL while inflating from a file or
309
- String buffer and while deflating to a file, so other Ruby threads keep running.
310
-
311
347
  ## Platforms
312
348
 
313
349
  - **Ruby 3.3 to 4.0** are supported and tested on Linux, macOS and Windows.
@@ -324,6 +360,7 @@ bundle install
324
360
  bundle exec rake compile test # build the extension, run the suite
325
361
  bundle exec rake compat # rubyzip's own test suite against ziperu
326
362
  bundle exec rake fuzz # differential fuzzing (TIME=180 for three minutes)
363
+ bundle exec rake fuzz:tar # the same for tar, against Python's tarfile
327
364
  bundle exec rake bench # the table above
328
365
  ```
329
366
 
@@ -337,6 +374,10 @@ run, the release pipeline and the code layout are described in
337
374
  - [Changelog](CHANGELOG.md)
338
375
  - [Migrating from rubyzip](docs/migrating.md): every deliberate difference, and what ziperu adds
339
376
  - [rubyzip compatibility report](COMPAT.md)
377
+ - [Tar archives](docs/tar.md): what is read and written, how extraction behaves, the limits
378
+ - [Benchmarks](docs/benchmarks.md): all three platforms, and which calls release the GVL
379
+ - [Security](SECURITY.md): what extraction and reading guard against
380
+ - [Development](docs/development.md): building, the fuzzer, the release pipeline
340
381
 
341
382
  ## Contributing
342
383
 
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "ziperu"
3
- version = "0.3.1"
3
+ version = "0.4.1"
4
4
  edition = "2021"
5
5
  publish = false
6
6
  rust-version = "1.88"
@@ -34,6 +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
+ # For reading compressed tar archives: the codecs the zip crate already
38
+ # brings in for its own entries, at the versions and with the backends it
39
+ # picks, so nothing more is compiled.
40
+ bzip2 = { version = "0.6", default-features = false }
41
+ zstd = { version = "0.14", default-features = false }
42
+ lzma-rust2 = { version = "0.23", default-features = false, features = ["std", "xz"] }
43
+
37
44
  # O_NOFOLLOW for extraction (already in the tree through the zip crate's
38
45
  # dependencies).
39
46
  [target.'cfg(unix)'.dependencies]
@@ -30,6 +30,7 @@ exception!(NATIVE_PASSWORD_ERROR, "Ziperu::Native::PasswordError");
30
30
  exception!(pub NATIVE_UNSUPPORTED_ERROR, "Ziperu::Native::UnsupportedError");
31
31
  exception!(pub DESTINATION_EXISTS_ERROR, "Ziperu::DestinationExistsError");
32
32
  exception!(pub ENTRY_SIZE_ERROR, "Ziperu::EntrySizeError");
33
+ exception!(pub LIMIT_ERROR, "Ziperu::LimitError");
33
34
  exception!(pub NATIVE_DUPLICATE_ENTRY_ERROR, "Ziperu::Native::DuplicateEntryError");
34
35
  exception!(ENOENT, "Errno::ENOENT");
35
36
  exception!(EACCES, "Errno::EACCES");