Skip to content

Commit

Permalink
Merge branch 'master' into eocd_rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
RisaI committed Nov 21, 2024
2 parents 50323d4 + 2c03abc commit b39a70e
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 18 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Changelog

## [2.2.1](https://github.com/zip-rs/zip2/compare/v2.2.0...v2.2.1) - 2024-11-20

### <!-- 1 -->🐛 Bug Fixes

- remove executable bit ([#238](https://github.com/zip-rs/zip2/pull/238))
- *(lzma)* fixed panic in case of invalid lzma stream ([#259](https://github.com/zip-rs/zip2/pull/259))
- resolve new clippy warnings on nightly ([#262](https://github.com/zip-rs/zip2/pull/262))
- resolve clippy warning in nightly ([#252](https://github.com/zip-rs/zip2/pull/252))

### <!-- 4 -->⚡ Performance

- Faster cde rejection ([#255](https://github.com/zip-rs/zip2/pull/255))

## [2.2.0](https://github.com/zip-rs/zip2/compare/v2.1.6...v2.2.0) - 2024-08-11

### <!-- 0 -->🚀 Features
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "zip"
version = "2.2.0"
version = "2.2.1"
authors = [
"Mathijs van de Nes <[email protected]>",
"Marli Frost <[email protected]>",
Expand Down Expand Up @@ -39,7 +39,7 @@ memchr = "2.7.4"
pbkdf2 = { version = "0.12.2", optional = true }
rand = { version = "0.8.5", optional = true }
sha1 = { version = "0.10.6", optional = true }
thiserror = "1.0.63"
thiserror = "2.0.3"
time = { workspace = true, optional = true, features = [
"std",
] }
Expand All @@ -62,7 +62,7 @@ walkdir = "2.5.0"
time = { workspace = true, features = ["formatting", "macros"] }
anyhow = "1"
clap = { version = "=4.4.18", features = ["derive"] }
tempdir = "0.3.7"
tempfile = "3"

[features]
aes-crypto = ["aes", "constant_time_eq", "hmac", "pbkdf2", "sha1", "rand", "zeroize"]
Expand Down
20 changes: 18 additions & 2 deletions benches/read_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::io::{self, prelude::*, Cursor};

use bencher::Bencher;
use getrandom::getrandom;
use tempdir::TempDir;
use tempfile::TempDir;
use zip::write::SimpleFileOptions;
use zip::{result::ZipResult, CompressionMethod, ZipArchive, ZipWriter};

Expand Down Expand Up @@ -102,7 +102,7 @@ fn parse_stream_archive(bench: &mut Bencher) {
let bytes = generate_random_archive(STREAM_ZIP_ENTRIES, STREAM_FILE_SIZE).unwrap();

/* Write to a temporary file path to incur some filesystem overhead from repeated reads */
let dir = TempDir::new("stream-bench").unwrap();
let dir = TempDir::with_prefix("stream-bench").unwrap();
let out = dir.path().join("bench-out.zip");
fs::write(&out, &bytes).unwrap();

Expand All @@ -116,11 +116,27 @@ fn parse_stream_archive(bench: &mut Bencher) {
bench.bytes = bytes.len() as u64;
}

fn parse_large_non_zip(bench: &mut Bencher) {
const FILE_SIZE: usize = 17_000_000;

// Create a large file that doesn't have a zip header (generating random data _might_ make a zip magic
// number somewhere which is _not_ what we're trying to test).
let dir = TempDir::with_prefix("large-non-zip-bench").unwrap();
let file = dir.path().join("zeros");
let buf = vec![0u8; FILE_SIZE];
fs::write(&file, &buf).unwrap();

bench.iter(|| {
assert!(zip::ZipArchive::new(std::fs::File::open(&file).unwrap()).is_err());
})
}

benchmark_group!(
benches,
read_metadata,
parse_archive_with_comment,
parse_zip64_archive_with_comment,
parse_stream_archive,
parse_large_non_zip,
);
benchmark_main!(benches);
4 changes: 2 additions & 2 deletions src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1603,7 +1603,7 @@ mod test {
use crate::CompressionMethod::Stored;
use crate::{ZipArchive, ZipWriter};
use std::io::{Cursor, Read, Write};
use tempdir::TempDir;
use tempfile::TempDir;

#[test]
fn invalid_offset() {
Expand Down Expand Up @@ -1805,7 +1805,7 @@ mod test {
v.extend_from_slice(include_bytes!("../tests/data/symlink.zip"));
let mut reader = ZipArchive::new(Cursor::new(v)).unwrap();
assert!(reader.by_index(0).unwrap().is_symlink());
let tempdir = TempDir::new("test_is_symlink")?;
let tempdir = TempDir::with_prefix("test_is_symlink")?;
reader.extract(&tempdir).unwrap();
assert!(tempdir.path().join("bar").is_symlink());
Ok(())
Expand Down
8 changes: 6 additions & 2 deletions src/read/lzma.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use lzma_rs::decompress::{Options, Stream, UnpackedSize};
use std::collections::VecDeque;
use std::io::{BufRead, Read, Result, Write};
use std::io::{BufRead, Error, ErrorKind, Read, Result, Write};

const OPTIONS: Options = Options {
unpacked_size: UnpackedSize::ReadFromHeader,
Expand Down Expand Up @@ -29,7 +29,11 @@ impl<R: Read> LzmaDecoder<R> {

impl<R: BufRead> Read for LzmaDecoder<R> {
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
let mut bytes_read = self.stream.get_output_mut().unwrap().read(buf)?;
let mut bytes_read = self
.stream
.get_output_mut()
.ok_or(Error::new(ErrorKind::InvalidData, "Invalid LZMA stream"))?
.read(buf)?;
while bytes_read < buf.len() {
let compressed_bytes = self.compressed_reader.fill_buf()?;
if compressed_bytes.is_empty() {
Expand Down
Empty file modified src/spec.rs
100755 → 100644
Empty file.
55 changes: 50 additions & 5 deletions src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1292,11 +1292,7 @@ impl<W: Write + Seek> ZipWriter<W> {
/// Ok(())
/// }
/// ```
pub fn raw_copy_file_rename<S: ToString>(
&mut self,
mut file: ZipFile,
name: S,
) -> ZipResult<()> {
pub fn raw_copy_file_rename<S: ToString>(&mut self, file: ZipFile, name: S) -> ZipResult<()> {
let mut options = SimpleFileOptions::default()
.large_file(file.compressed_size().max(file.size()) > spec::ZIP64_BYTES_THR)
.last_modified_time(
Expand All @@ -1308,7 +1304,15 @@ impl<W: Write + Seek> ZipWriter<W> {
options = options.unix_permissions(perms);
}
Self::normalize_options(&mut options);
self.raw_copy_file_rename_internal(file, name, options)
}

fn raw_copy_file_rename_internal<S: ToString>(
&mut self,
mut file: ZipFile,
name: S,
options: SimpleFileOptions,
) -> ZipResult<()> {
let raw_values = ZipRawValues {
crc32: file.crc32(),
compressed_size: file.compressed_size(),
Expand Down Expand Up @@ -1364,6 +1368,47 @@ impl<W: Write + Seek> ZipWriter<W> {
self.raw_copy_file_rename(file, name)
}

/// Add a new file using the already compressed data from a ZIP file being read and set the last
/// modified date and unix mode. This allows faster copies of the `ZipFile` since there is no need
/// to decompress and compress it again. Any `ZipFile` metadata other than the last modified date
/// and the unix mode is copied and not checked, for example the file CRC.
///
/// ```no_run
/// use std::io::{Read, Seek, Write};
/// use zip::{DateTime, ZipArchive, ZipWriter};
///
/// fn copy<R, W>(src: &mut ZipArchive<R>, dst: &mut ZipWriter<W>) -> zip::result::ZipResult<()>
/// where
/// R: Read + Seek,
/// W: Write + Seek,
/// {
/// // Retrieve file entry by name
/// let file = src.by_name("src_file.txt")?;
///
/// // Copy the previously obtained file entry to the destination zip archive
/// dst.raw_copy_file_touch(file, DateTime::default(), Some(0o644))?;
///
/// Ok(())
/// }
/// ```
pub fn raw_copy_file_touch(
&mut self,
file: ZipFile,
last_modified_time: DateTime,
unix_mode: Option<u32>,
) -> ZipResult<()> {
let name = file.name().to_owned();
let mut options = SimpleFileOptions::default()
.large_file(file.compressed_size().max(file.size()) > spec::ZIP64_BYTES_THR)
.last_modified_time(last_modified_time)
.compression_method(file.compression());
if let Some(perms) = unix_mode {
options = options.unix_permissions(perms);
}
Self::normalize_options(&mut options);
self.raw_copy_file_rename_internal(file, name, options)
}

/// Add a directory entry.
///
/// As directories have no content, you must not call [`ZipWriter::write`] before adding a new file.
Expand Down
4 changes: 2 additions & 2 deletions tests/extract_symlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
#[cfg(all(unix, feature = "_deflate-any"))]
fn extract_should_respect_links() {
use std::{fs, io, path::PathBuf, str::FromStr};
use tempdir::TempDir;
use tempfile::TempDir;
use zip::ZipArchive;

let mut v = Vec::new();
v.extend_from_slice(include_bytes!("data/pandoc_soft_links.zip"));
let mut archive = ZipArchive::new(io::Cursor::new(v)).expect("couldn't open test zip file");
let temp_dir = TempDir::new("pandoc_soft_links").unwrap();
let temp_dir = TempDir::with_prefix("pandoc_soft_links").unwrap();
archive.extract(&temp_dir).unwrap();

let symlink_path = temp_dir.path().join("pandoc-3.2-arm64/bin/pandoc-lua");
Expand Down
4 changes: 2 additions & 2 deletions tests/repro_old423.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
#[test]
fn repro_old423() -> zip::result::ZipResult<()> {
use std::io;
use tempdir::TempDir;
use tempfile::TempDir;
use zip::ZipArchive;

let mut v = Vec::new();
v.extend_from_slice(include_bytes!("data/lin-ub_iwd-v11.zip"));
let mut archive = ZipArchive::new(io::Cursor::new(v)).expect("couldn't open test zip file");
archive.extract(TempDir::new("repro_old423")?)
archive.extract(TempDir::with_prefix("repro_old423")?)
}

0 comments on commit b39a70e

Please sign in to comment.