ziperu 0.3.0 → 0.4.0
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 +117 -0
- data/Cargo.lock +4 -1
- data/README.md +85 -44
- data/ext/ziperu/Cargo.toml +8 -1
- data/ext/ziperu/src/errors.rs +38 -0
- data/ext/ziperu/src/extract.rs +87 -699
- data/ext/ziperu/src/lib.rs +25 -0
- data/ext/ziperu/src/placer.rs +759 -0
- data/ext/ziperu/src/stream.rs +7 -1
- data/ext/ziperu/src/tar.rs +1454 -0
- data/ext/ziperu/src/tar_writer.rs +736 -0
- data/lib/ziperu/entry/source.rb +41 -6
- data/lib/ziperu/entry.rb +34 -46
- data/lib/ziperu/file/extraction.rb +16 -6
- data/lib/ziperu/file.rb +27 -8
- data/lib/ziperu/input_stream.rb +2 -0
- data/lib/ziperu/ioextras.rb +7 -0
- data/lib/ziperu/link_safety.rb +43 -0
- data/lib/ziperu/tar/extraction.rb +399 -0
- data/lib/ziperu/tar/writer.rb +393 -0
- data/lib/ziperu/tar.rb +264 -0
- data/lib/ziperu/version.rb +1 -1
- data/lib/ziperu.rb +2 -0
- metadata +8 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 8ab51a6955845e3c641851257a1ab8a302a1bb2115d11e618be10c1d21d8dd92
|
|
4
|
+
data.tar.gz: 190bb34531982103d5297943ffe52b8a0c43d0e1db4f3758810c57f5a0df8b91
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c641e17c9cd08409256357e437b22f0a0c52d9c02727761ec93c602895ab4036ea90fcee2ead4489d2bdafd130373f4df6fb9c58faadad26656c4e1f39209c24
|
|
7
|
+
data.tar.gz: eaa17172b6f75a66176bbafabca0a746639ae53e5ce71c5ea2288f32f8eeda7d537a775c965c08544571e11d7c9ea587cb2422304831cc3d57dacbee21d51f29
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,122 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.4.0 (2026-11-21)
|
|
4
|
+
|
|
5
|
+
Added:
|
|
6
|
+
|
|
7
|
+
- `Ziperu::Tar.foreach` and `Ziperu::Tar::Reader` read tar archives:
|
|
8
|
+
plain, or compressed with gzip, zstd, xz or bzip2, told apart by their
|
|
9
|
+
first bytes and not by their name. An archive is read front to back,
|
|
10
|
+
an entry at a time, in constant memory, from a path or from any IO
|
|
11
|
+
with `read` (a pipe, a socket, a response body). An entry gives its
|
|
12
|
+
name, size, type, mode, time (to the nanosecond where the archive has
|
|
13
|
+
it), link target and owner, and its data through `read` and
|
|
14
|
+
`each_chunk`. v7, ustar, GNU and PAX archives are read, long names and
|
|
15
|
+
extended headers included; sparse files and multi-volume archives are
|
|
16
|
+
refused.
|
|
17
|
+
- `Ziperu::Tar.extract_all(path_or_io, dest)` extracts a tar archive
|
|
18
|
+
under the rules a zip is extracted by: a name that resolves outside
|
|
19
|
+
the destination is skipped with a warning, nothing is written through
|
|
20
|
+
a link already on disk, and times and permission bits are restored
|
|
21
|
+
but never setuid, setgid or an owner. Symlinks are skipped unless
|
|
22
|
+
`symlinks: :create`, and then made only when the target stays inside.
|
|
23
|
+
A hard link is made only to a file the same extraction wrote. Devices
|
|
24
|
+
and fifos are skipped. `on_exists:`, `exclude:`, `sanitize:` and
|
|
25
|
+
`progress:` are as for a zip; `strip_components:` drops leading
|
|
26
|
+
directories. Of two entries with one name the last wins, as `tar` has
|
|
27
|
+
it. Links are created after every file.
|
|
28
|
+
- `Ziperu.extract(path_or_io, dest)` extracts a zip or a tar archive,
|
|
29
|
+
told apart by the first bytes, with the options of whichever it is.
|
|
30
|
+
From a pipe a tar archive is streamed; a zip is read into memory
|
|
31
|
+
first, within `max_archive_size`.
|
|
32
|
+
- `Ziperu::Tar.create(path_or_io) { |tar| ... }` writes a tar archive,
|
|
33
|
+
plain or compressed with gzip, zstd, xz or bzip2 (from the file's name,
|
|
34
|
+
or `compression:`, with `level:`). `add` takes a file from disk or any
|
|
35
|
+
IO, `add_data` a String, and there are `mkdir`, `symlink`, `link` and
|
|
36
|
+
`add_recursive`; `Ziperu::Tar.add_recursive(archive, dir)` does a whole
|
|
37
|
+
directory and `Ziperu::Tar.body` gives a Rack body. What is written is
|
|
38
|
+
ustar, with a PAX extended header only for what ustar cannot hold (a
|
|
39
|
+
long or non-ASCII name, a size of 8 GiB or more). It goes to a pipe or
|
|
40
|
+
socket as readily as to a file, and a file is put in place only once
|
|
41
|
+
it is whole. Owners are not recorded, and with `source_date_epoch` the
|
|
42
|
+
same input gives the same bytes.
|
|
43
|
+
- [docs/tar.md](docs/tar.md) describes what is read and written and how
|
|
44
|
+
extraction behaves. CI now exchanges tar archives with GNU tar, bsdtar, Python's
|
|
45
|
+
tarfile and 7-Zip in both directions, and `rake fuzz:tar` fuzzes the
|
|
46
|
+
reader, the extractor and the writer against Python's tarfile.
|
|
47
|
+
- The limits hold for tar as for zip: `max_entries`, `max_entry_size`,
|
|
48
|
+
`max_total_size`, `max_archive_size` for what is read from an IO, and
|
|
49
|
+
`max_compression_ratio`, which is measured over the stream as it is
|
|
50
|
+
read and so stops a compressed bomb a few megabytes in, read or
|
|
51
|
+
skipped.
|
|
52
|
+
- A compressed tar archive is read to the end of its stream, where
|
|
53
|
+
gzip, xz and bzip2 keep their checksum: tar has none for an entry's
|
|
54
|
+
data. A long name or extended header over 1 MiB is refused, and an xz
|
|
55
|
+
stream that asks for more than 256 MiB to decode.
|
|
56
|
+
|
|
57
|
+
Fixed:
|
|
58
|
+
|
|
59
|
+
- On Windows an entry with a colon in its name was written as an
|
|
60
|
+
alternate data stream: `a.txt:x` went into `a.txt`, a file that could
|
|
61
|
+
have been in the destination before and was neither replaced nor
|
|
62
|
+
reported, whatever `on_exists:` said. And `C:x`, a path relative to
|
|
63
|
+
the current directory of drive C, was written there: outside the
|
|
64
|
+
destination. Such an entry is now skipped with a warning, as one that resolves outside the destination is;
|
|
65
|
+
`sanitize: :windows` rewrites the name and extracts it. This was true
|
|
66
|
+
of zip extraction in every release so far.
|
|
67
|
+
|
|
68
|
+
Internal:
|
|
69
|
+
|
|
70
|
+
- The native extractor is in two parts: `placer.rs` decides where an
|
|
71
|
+
entry goes and what is refused on the way (the path check, links on
|
|
72
|
+
disk, what is already there), and `extract.rs` reads zip entries into
|
|
73
|
+
the files it opens. Nothing changes in what is extracted or how.
|
|
74
|
+
- The memory growth check failed a nightly run on code that had passed
|
|
75
|
+
the day before. It sampled resident memory after a full GC, but memory
|
|
76
|
+
glibc had freed and not yet returned swung the samples between 68 and
|
|
77
|
+
107 MiB. It now calls `malloc_trim` before each sample: the workload
|
|
78
|
+
then sits at 23.8 MiB for all 600 iterations, and the growth allowed
|
|
79
|
+
drops from 8 MiB to 3.
|
|
80
|
+
|
|
81
|
+
## 0.3.1 (2026-11-16)
|
|
82
|
+
|
|
83
|
+
Fixed:
|
|
84
|
+
|
|
85
|
+
- An entry that `File.foreach` yielded from a path could not be read once
|
|
86
|
+
`foreach` had returned: `get_input_stream` raised `IOError: closed
|
|
87
|
+
archive`. That covered `foreach(path).find { ... }` and entries
|
|
88
|
+
collected in the block. rubyzip allows it, and so does an entry of a
|
|
89
|
+
closed `Ziperu::File`; the archive is now reopened for these entries
|
|
90
|
+
too. Found by write_xlsx's test suite, 974 of whose tests failed on it.
|
|
91
|
+
- `File#extract`, and `extract_all` with a block, left an empty file
|
|
92
|
+
behind when the entry could not be read (a bad CRC, a wrong password).
|
|
93
|
+
The file is removed, as the native path of `extract_all` already did.
|
|
94
|
+
- `File#read` and `extract_all` raised a plain `Zip::Error` for an entry
|
|
95
|
+
in a compression method that cannot be read. They raise
|
|
96
|
+
`Zip::CompressionMethodError`, as rubyzip does and as
|
|
97
|
+
`get_input_stream` did, so one `rescue` covers every way of reading.
|
|
98
|
+
- `File.extract_all(path, dest, password: "...")` raised "unknown
|
|
99
|
+
keyword": the one-liner passed every option to `#extract_all` and none
|
|
100
|
+
to `open`. It now takes both kinds, the limits included.
|
|
101
|
+
- `File.foreach` takes `password:`, and the entries it yields are read
|
|
102
|
+
with it. They could only be read by building a decrypter for each.
|
|
103
|
+
- A `metadata_encoding:` Ruby does not know raises `ArgumentError`. It was
|
|
104
|
+
ignored, and names stayed binary with nothing to say why.
|
|
105
|
+
- The message for an archive cut short named Rust types ("Unexpected end
|
|
106
|
+
of zip::format::blocks::Zip32CDEBlock"). It says what is cut short: the
|
|
107
|
+
end of central directory record, a central directory entry and so on.
|
|
108
|
+
- `File#inspect` is one line (the path, the entry count, read-only or
|
|
109
|
+
uncommitted). The default listed every entry: half a megabyte for an
|
|
110
|
+
archive of 5,000, in irb, a log line or an error message.
|
|
111
|
+
- `InputStream.new(path, offset: n)` closes the archive before raising
|
|
112
|
+
when no entry starts at `n`. It was left for the GC.
|
|
113
|
+
|
|
114
|
+
Internal:
|
|
115
|
+
|
|
116
|
+
- The weekly run of other gems' test suites never got as far as running
|
|
117
|
+
one: installing the built gem with `--local` could not find `rb_sys`,
|
|
118
|
+
and the script had lost its execute bit.
|
|
119
|
+
|
|
3
120
|
## 0.3.0 (2026-11-14)
|
|
4
121
|
|
|
5
122
|
Security:
|
data/Cargo.lock
CHANGED
|
@@ -879,17 +879,20 @@ dependencies = [
|
|
|
879
879
|
|
|
880
880
|
[[package]]
|
|
881
881
|
name = "ziperu"
|
|
882
|
-
version = "0.
|
|
882
|
+
version = "0.4.0"
|
|
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
|
-
|
|
23
|
-
|
|
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,
|
|
36
|
-
|
|
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.
|
|
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
|
|
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,
|
|
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
|
|
126
|
-
|
|
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]
|
|
130
|
+
[200, Ziperu::RailsStreaming.headers("attachments.zip"), body]
|
|
131
|
+
```
|
|
136
132
|
|
|
137
|
-
|
|
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
|
-
|
|
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
|
|
data/ext/ziperu/Cargo.toml
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[package]
|
|
2
2
|
name = "ziperu"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.4.0"
|
|
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]
|
data/ext/ziperu/src/errors.rs
CHANGED
|
@@ -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");
|
|
@@ -95,6 +96,17 @@ pub fn zip_error(ruby: &Ruby, err: ZipError) -> Error {
|
|
|
95
96
|
fn invalid_archive_message(msg: &str) -> String {
|
|
96
97
|
if msg.contains("EOCD") {
|
|
97
98
|
"Zip end of central directory signature not found".to_string()
|
|
99
|
+
} else if let Some(block) = msg.strip_prefix("Unexpected end of ") {
|
|
100
|
+
// The crate names the Rust type it was reading.
|
|
101
|
+
let what = match block.rsplit("::").next().unwrap_or(block) {
|
|
102
|
+
"Zip32CDEBlock" => "the end of central directory record",
|
|
103
|
+
"Zip64CDEBlock" => "the zip64 end of central directory record",
|
|
104
|
+
"Zip64CDELocatorBlock" => "the zip64 end of central directory locator",
|
|
105
|
+
"ZipCentralEntryBlock" => "a central directory entry",
|
|
106
|
+
"ZipLocalEntryBlock" => "a local file header",
|
|
107
|
+
_ => "a record",
|
|
108
|
+
};
|
|
109
|
+
format!("invalid Zip archive: {what} is cut short")
|
|
98
110
|
} else {
|
|
99
111
|
// Some of the crate's messages carry a placeholder it never fills.
|
|
100
112
|
msg.replace(" of {properties_size}", "")
|
|
@@ -254,3 +266,29 @@ fn decompression_error(ruby: &Ruby, msg: &str) -> Error {
|
|
|
254
266
|
Err(e) => e,
|
|
255
267
|
}
|
|
256
268
|
}
|
|
269
|
+
|
|
270
|
+
#[cfg(test)]
|
|
271
|
+
mod tests {
|
|
272
|
+
use super::invalid_archive_message;
|
|
273
|
+
|
|
274
|
+
#[test]
|
|
275
|
+
fn no_rust_type_names_in_messages() {
|
|
276
|
+
for block in [
|
|
277
|
+
"Zip32CDEBlock",
|
|
278
|
+
"Zip64CDEBlock",
|
|
279
|
+
"Zip64CDELocatorBlock",
|
|
280
|
+
"ZipCentralEntryBlock",
|
|
281
|
+
"ZipLocalEntryBlock",
|
|
282
|
+
"SomethingNew",
|
|
283
|
+
] {
|
|
284
|
+
let said =
|
|
285
|
+
invalid_archive_message(&format!("Unexpected end of zip::format::blocks::{block}"));
|
|
286
|
+
assert!(said.ends_with("is cut short"), "{said}");
|
|
287
|
+
assert!(!said.contains("::") && !said.contains("Block"), "{said}");
|
|
288
|
+
}
|
|
289
|
+
assert_eq!(
|
|
290
|
+
invalid_archive_message("Unexpected end of zip::format::blocks::Zip32CDEBlock"),
|
|
291
|
+
"invalid Zip archive: the end of central directory record is cut short"
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
}
|