| 1 |
# zip [](https://crates.io/crates/zip)  [](https://docs.rs/zip) [](https://github.com/zip-rs/zip2/actions?query=branch%3Amaster+workflow%3ACI) [](https://www.bestpractices.dev/projects/11847) |
| 2 |
|
| 3 |
A zip library for rust which supports reading and writing of simple ZIP files. Currently hosted at |
| 4 |
<https://github.com/zip-rs/zip2>. |
| 5 |
|
| 6 |
The current implementation is based on [PKWARE's APPNOTE.TXT v6.3.9](https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT) |
| 7 |
|
| 8 |
Supported compression formats: |
| 9 |
|
| 10 |
| Formats | Reading | Writing | |
| 11 |
| ----------------------------------- | ------- | ------- | |
| 12 |
| Stored (i.e. none) | ✅ | ✅ | |
| 13 |
| Implode, Shrink, Reduce | ✅ | | |
| 14 |
| Deflate | ✅ | ✅ | |
| 15 |
| `Deflate64` | ✅ | | |
| 16 |
| `Bzip2` | ✅ | ✅ | |
| 17 |
| `ZStandard` | ✅ | ✅ | |
| 18 |
| `LZMA` | ✅ | | |
| 19 |
| `XZ` | ✅ | ✅ | |
| 20 |
| `PPMd` | ✅ | ✅ | |
| 21 |
| AES encryption | ✅ | ✅ | |
| 22 |
| `ZipCrypto` (deprecated encryption) | ✅ | ✅ | |
| 23 |
|
| 24 |
Currently unsupported zip extensions: |
| 25 |
|
| 26 |
- Multi-disk |
| 27 |
|
| 28 |
## Features |
| 29 |
|
| 30 |
The features available are: |
| 31 |
|
| 32 |
- `aes-crypto`: Enables decryption of files which were encrypted with AES. Supports AE-1 and AE-2 methods. |
| 33 |
- `deflate`: Enables compressing and decompressing an unspecified implementation (that may change in future versions) of |
| 34 |
the deflate compression algorithm, which is the default for zip files. Supports compression quality 1..=264. |
| 35 |
- `deflate-flate2`: Combine this with any `flate2` feature flag that enables a back-end, to support deflate compression |
| 36 |
at quality 1..=9. |
| 37 |
- `deflate-zopfli`: Enables deflating files with the `zopfli` library (used when compression quality is 10..=264). This |
| 38 |
is the most effective `deflate` implementation available, but also among the slowest. If `flate2` isn't also enabled, |
| 39 |
only compression will be supported and not decompression. |
| 40 |
- `deflate64`: Enables the deflate64 compression algorithm. Only decompression is supported. |
| 41 |
- `lzma`: Enables the `LZMA` compression algorithm. Only decompression is supported. |
| 42 |
- `bzip2`: Enables the `BZip2` compression algorithm. |
| 43 |
- `ppmd`: Enables the `PPMd` compression algorithm. |
| 44 |
- `time`: Enables features using the [time](https://github.com/time-rs/time) crate. |
| 45 |
- `chrono`: Enables converting last-modified [`zip::DateTime`](https://docs.rs/zip/latest/zip/struct.DateTime.html) to and from `chrono::NaiveDateTime`. |
| 46 |
- `jiff-02`: Enables converting last-modified [`zip::DateTime`](https://docs.rs/zip/latest/zip/struct.DateTime.html) to and from `jiff::civil::DateTime`. |
| 47 |
- `nt-time`: Enables returning timestamps stored in the NTFS extra field as `nt_time::FileTime`. |
| 48 |
- `xz`: Enables the `XZ` compression algorithm. |
| 49 |
- `zstd`: Enables the `Zstandard` compression algorithm. |
| 50 |
|
| 51 |
By default `aes-crypto`, `bzip2`, `deflate`, `deflate64`, `lzma`, `ppmd`, `time`, `xz` and `zstd` are enabled. |
| 52 |
|
| 53 |
## Library usage |
| 54 |
|
| 55 |
Reading: |
| 56 |
- [`ZipArchive::new()`](https://docs.rs/zip/latest/zip/read/struct.ZipArchive.html) |
| 57 |
- [`read_zipfile_from_stream_*`](https://docs.rs/zip/latest/zip/read/) |
| 58 |
|
| 59 |
Writing: |
| 60 |
- [`ZipWriter::new()`](https://docs.rs/zip/latest/zip/write/struct.ZipWriter.html) - to create a new archive |
| 61 |
- [`ZipWriter::new_stream()`](https://docs.rs/zip/latest/zip/write/struct.ZipWriter.html#method.new_stream) - to write in stream mode |
| 62 |
|
| 63 |
## Examples |
| 64 |
|
| 65 |
See the [examples directory](https://github.com/zip-rs/zip2/tree/master/examples) for: |
| 66 |
|
| 67 |
- How to [write a file to a zip](https://github.com/zip-rs/zip2/tree/master/examples/write_sample.rs). |
| 68 |
- How to [write a directory of files to a zip](https://github.com/zip-rs/zip2/tree/master/examples/write_dir.rs) (using [walkdir](https://github.com/BurntSushi/walkdir)). |
| 69 |
- How to [extract a zip file](https://github.com/zip-rs/zip2/tree/master/examples/extract.rs). |
| 70 |
- How to [extract a single file from a zip](https://github.com/zip-rs/zip2/tree/master/examples/extract_lorem.rs). |
| 71 |
- How to [read a zip from the standard input](https://github.com/zip-rs/zip2/tree/master/examples/stdin_info.rs). |
| 72 |
- How to [append a directory to an existing archive](https://github.com/zip-rs/zip2/tree/master/examples/append.rs) |
| 73 |
|
| 74 |
## Wasm |
| 75 |
|
| 76 |
This library can work in a Wasm environment but you may need to disable certain features (which are using non-rust library). Here is an example below |
| 77 |
|
| 78 |
```toml |
| 79 |
# change to latest version |
| 80 |
zip = { version = "latest", default-features = false, features = [ |
| 81 |
# "aes-crypto", |
| 82 |
# "bzip2", |
| 83 |
# "xz", |
| 84 |
"deflate64", |
| 85 |
"deflate", |
| 86 |
"lzma", |
| 87 |
"time", |
| 88 |
"zstd", |
| 89 |
] } |
| 90 |
``` |
| 91 |
|
| 92 |
## MSRV |
| 93 |
|
| 94 |
Our current Minimum Supported Rust Version is **1.88**. When adding features, |
| 95 |
we will follow these guidelines: |
| 96 |
|
| 97 |
- We will always support a minor Rust version that has been stable for at least 6 months. |
| 98 |
- Any change to the MSRV will be accompanied with a **minor** version bump. |
| 99 |
|
| 100 |
## License |
| 101 |
|
| 102 |
Licensed under the [MIT License](./LICENSE). Some files in the "tests/data" subdirectory of this repository are under other |
| 103 |
licenses; see files named `LICENSE.*.txt` for details. |
| 104 |
|
| 105 |
## Fuzzing |
| 106 |
|
| 107 |
Fuzzing support is through [`cargo afl`](https://rust-fuzz.github.io/book/afl/tutorial.html). To install `cargo afl`: |
| 108 |
|
| 109 |
```sh |
| 110 |
cargo install cargo-afl |
| 111 |
``` |
| 112 |
|
| 113 |
To start fuzzing zip extraction: |
| 114 |
|
| 115 |
```sh |
| 116 |
mkdir -vp fuzz-read-out |
| 117 |
cargo afl build --manifest-path=fuzz/Cargo.toml --all-features -p fuzz_read |
| 118 |
# Curated input corpus: |
| 119 |
cargo afl fuzz -i fuzz/read/in -o fuzz-read-out fuzz/target/debug/fuzz_read |
| 120 |
# Test data files: |
| 121 |
cargo afl fuzz -i tests/data -e zip -o fuzz-read-out fuzz/target/debug/fuzz_read |
| 122 |
``` |
| 123 |
|
| 124 |
To start fuzzing zip creation: |
| 125 |
|
| 126 |
```sh |
| 127 |
mkdir -vp fuzz-write-out |
| 128 |
cargo afl build --manifest-path=fuzz/Cargo.toml --all-features -p fuzz_write |
| 129 |
# Curated input corpus and dictionary schema: |
| 130 |
cargo afl fuzz -x fuzz/write/fuzz.dict -i fuzz/write/in -o fuzz-write-out fuzz/target/debug/fuzz_write |
| 131 |
``` |
| 132 |
|
| 133 |
### Fuzzing stdio |
| 134 |
|
| 135 |
The read and write fuzzers can also receive input over stdin for one-off validation. Note here that the fuzzers can be configured to build in support for DEFLATE, or not: |
| 136 |
|
| 137 |
```sh |
| 138 |
# Success, no output: |
| 139 |
cargo run --manifest-path=fuzz/Cargo.toml --quiet --all-features -p fuzz_read <tests/data/deflate64.zip |
| 140 |
# Error, without deflate64 support: |
| 141 |
cargo run --manifest-path=fuzz/Cargo.toml --quiet -p fuzz_read <tests/data/deflate64.zip |
| 142 |
|
| 143 |
thread 'main' (537304) panicked at fuzz_read/src/main.rs:40:36: |
| 144 |
called `Result::unwrap()` on an `Err` value: UnsupportedArchive("Compression method not supported") |
| 145 |
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace |
| 146 |
``` |
| 147 |
|
| 148 |
The zip creation fuzzer will try to print out a description of the kind of input it translated the input bytes into: |
| 149 |
|
| 150 |
```sh |
| 151 |
# This is an empty input case: |
| 152 |
<fuzz/write/in/id-000000,time-0,execs-0,orig-0011743621118ab6c5278ffbb8fd14bddd8369ee.min \ |
| 153 |
cargo run --manifest-path=fuzz/Cargo.toml --quiet --all-features -p fuzz_write |
| 154 |
# This input was translated into one or more test cases: |
| 155 |
<fuzz/write/in/id-000000,time-0,execs-0,orig-0011743621118ab6c5278ffbb8fd14bddd8369ee.min \ |
| 156 |
cargo run --manifest-path=fuzz/Cargo.toml --quiet -p fuzz_write |
| 157 |
writer.start_file_from_path("", FileOptions { compression_method: Stored, compression_level: None, last_modified_time: DateTime::from_date_and_time(2048, 1, 1, 0, 0, 0)?, permissions: None, large_file: false, encrypt_with: None, extended_options: ExtendedFileOptions {extra_data: vec![].into(), central_extra_data: vec![].into()}, alignment: 0 })?; |
| 158 |
writer.write_all(&[])?; |
| 159 |
writer |
| 160 |
let _ = writer.finish_into_readable()?; |
| 161 |
``` |
| 162 |
|
| 163 |
The zip creation fuzzer uses [`arbitrary::Unstructured`](https://docs.rs/arbitrary/latest/arbitrary/struct.Unstructured.html) to convert bytes over stdin to random inputs, so it can be triggered with other sources of random input: |
| 164 |
|
| 165 |
```sh |
| 166 |
# Usually, the random input is translated into zero test cases: |
| 167 |
head -c50 /dev/random | cargo run --manifest-path=fuzz/Cargo.toml --quiet --all-features -p fuzz_write |
| 168 |
# Sometimes, one or more test cases are generated and successfully evaluated: |
| 169 |
head -c50 /dev/random | cargo run --manifest-path=fuzz/Cargo.toml --quiet --all-features -p fuzz_write |
| 170 |
writer.set_raw_comment([20, 202])?; |
| 171 |
let mut writer = ZipWriter::new_append(writer.finish()?)?; |
| 172 |
let sub_writer = { |
| 173 |
let mut initial_junk = Cursor::new(vec![106]); |
| 174 |
initial_junk.seek(SeekFrom::End(0))?; |
| 175 |
let mut writer = ZipWriter::new(initial_junk); |
| 176 |
writer |
| 177 |
}; |
| 178 |
writer.merge_archive(sub_writer.finish_into_readable()?)?; |
| 179 |
let mut writer = ZipWriter::new_append(writer.finish()?)?; |
| 180 |
``` |