Skip to content

Commit

Permalink
Implement shallow copy from within the file being written
Browse files Browse the repository at this point in the history
  • Loading branch information
Pr0methean committed Apr 23, 2023
1 parent e32db51 commit cde5d5e
Show file tree
Hide file tree
Showing 22 changed files with 148 additions and 85 deletions.
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[package]
name = "zip"
version = "0.6.4"
authors = ["Mathijs van de Nes <git@mathijs.vd-nes.nl>", "Marli Frost <marli@frost.red>", "Ryan Levick <ryan.levick@gmail.com>"]
name = "zip_next"
version = "0.6.5"
authors = ["Mathijs van de Nes <git@mathijs.vd-nes.nl>", "Marli Frost <marli@frost.red>", "Ryan Levick <ryan.levick@gmail.com>",
"Chris Hennick <hennickc@amazon.com>"]
license = "MIT"
repository = "https://github.com/zip-rs/zip.git"
keywords = ["zip", "archive"]
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ With all default features:

```toml
[dependencies]
zip = "0.6.4"
zip-next = "0.6.5"
```

Without the default features:

```toml
[dependencies]
zip = { version = "0.6.4", default-features = false }
zip-next = { version = "0.6.5", default-features = false }
```

The features available are:
Expand Down
4 changes: 2 additions & 2 deletions benches/read_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use std::io::{Cursor, Read, Write};

use bencher::Bencher;
use getrandom::getrandom;
use zip::{ZipArchive, ZipWriter};
use zip_next::{ZipArchive, ZipWriter};

fn generate_random_archive(size: usize) -> Vec<u8> {
let data = Vec::new();
let mut writer = ZipWriter::new(Cursor::new(data));
let options =
zip::write::FileOptions::default().compression_method(zip::CompressionMethod::Stored);
zip_next::write::FileOptions::default().compression_method(zip_next::CompressionMethod::Stored);

writer.start_file("random.dat", options).unwrap();
let mut bytes = vec![0u8; size];
Expand Down
4 changes: 2 additions & 2 deletions benches/read_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use bencher::{benchmark_group, benchmark_main};
use std::io::{Cursor, Write};

use bencher::Bencher;
use zip::{ZipArchive, ZipWriter};
use zip_next::{ZipArchive, ZipWriter};

const FILE_COUNT: usize = 15_000;
const FILE_SIZE: usize = 1024;
Expand All @@ -12,7 +12,7 @@ fn generate_random_archive(count_files: usize, file_size: usize) -> Vec<u8> {
let data = Vec::new();
let mut writer = ZipWriter::new(Cursor::new(data));
let options =
zip::write::FileOptions::default().compression_method(zip::CompressionMethod::Stored);
zip_next::write::FileOptions::default().compression_method(zip_next::CompressionMethod::Stored);

let bytes = vec![0u8; file_size];

Expand Down
2 changes: 1 addition & 1 deletion examples/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn real_main() -> i32 {
let fname = std::path::Path::new(&*args[1]);
let file = fs::File::open(fname).unwrap();

let mut archive = zip::ZipArchive::new(file).unwrap();
let mut archive = zip_next::ZipArchive::new(file).unwrap();

for i in 0..archive.len() {
let mut file = archive.by_index(i).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion examples/extract_lorem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn real_main() -> i32 {
let fname = std::path::Path::new(&*args[1]);
let zipfile = std::fs::File::open(fname).unwrap();

let mut archive = zip::ZipArchive::new(zipfile).unwrap();
let mut archive = zip_next::ZipArchive::new(zipfile).unwrap();

let mut file = match archive.by_name("test/lorem_ipsum.txt") {
Ok(file) => file,
Expand Down
2 changes: 1 addition & 1 deletion examples/file_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn real_main() -> i32 {
let file = fs::File::open(fname).unwrap();
let reader = BufReader::new(file);

let mut archive = zip::ZipArchive::new(reader).unwrap();
let mut archive = zip_next::ZipArchive::new(reader).unwrap();

for i in 0..archive.len() {
let file = archive.by_index(i).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion examples/stdin_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn real_main() -> i32 {
let mut buf = [0u8; 16];

loop {
match zip::read::read_zipfile_from_stream(&mut stdin_handle) {
match zip_next::read::read_zipfile_from_stream(&mut stdin_handle) {
Ok(Some(mut file)) => {
println!(
"{}: {} bytes ({} bytes packed)",
Expand Down
28 changes: 14 additions & 14 deletions examples/write_dir.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::io::prelude::*;
use std::io::{Seek, Write};
use std::iter::Iterator;
use zip::result::ZipError;
use zip::write::FileOptions;
use zip_next::result::ZipError;
use zip_next::write::FileOptions;

use std::fs::File;
use std::path::Path;
Expand All @@ -12,30 +12,30 @@ fn main() {
std::process::exit(real_main());
}

const METHOD_STORED: Option<zip::CompressionMethod> = Some(zip::CompressionMethod::Stored);
const METHOD_STORED: Option<zip_next::CompressionMethod> = Some(zip_next::CompressionMethod::Stored);

#[cfg(any(
feature = "deflate",
feature = "deflate-miniz",
feature = "deflate-zlib"
))]
const METHOD_DEFLATED: Option<zip::CompressionMethod> = Some(zip::CompressionMethod::Deflated);
const METHOD_DEFLATED: Option<zip_next::CompressionMethod> = Some(zip_next::CompressionMethod::Deflated);
#[cfg(not(any(
feature = "deflate",
feature = "deflate-miniz",
feature = "deflate-zlib"
)))]
const METHOD_DEFLATED: Option<zip::CompressionMethod> = None;
const METHOD_DEFLATED: Option<zip_next::CompressionMethod> = None;

#[cfg(feature = "bzip2")]
const METHOD_BZIP2: Option<zip::CompressionMethod> = Some(zip::CompressionMethod::Bzip2);
const METHOD_BZIP2: Option<zip_next::CompressionMethod> = Some(zip_next::CompressionMethod::Bzip2);
#[cfg(not(feature = "bzip2"))]
const METHOD_BZIP2: Option<zip::CompressionMethod> = None;
const METHOD_BZIP2: Option<zip_next::CompressionMethod> = None;

#[cfg(feature = "zstd")]
const METHOD_ZSTD: Option<zip::CompressionMethod> = Some(zip::CompressionMethod::Zstd);
const METHOD_ZSTD: Option<zip_next::CompressionMethod> = Some(zip_next::CompressionMethod::Zstd);
#[cfg(not(feature = "zstd"))]
const METHOD_ZSTD: Option<zip::CompressionMethod> = None;
const METHOD_ZSTD: Option<zip_next::CompressionMethod> = None;

fn real_main() -> i32 {
let args: Vec<_> = std::env::args().collect();
Expand Down Expand Up @@ -66,12 +66,12 @@ fn zip_dir<T>(
it: &mut dyn Iterator<Item = DirEntry>,
prefix: &str,
writer: T,
method: zip::CompressionMethod,
) -> zip::result::ZipResult<()>
method: zip_next::CompressionMethod,
) -> zip_next::result::ZipResult<()>
where
T: Write + Seek,
{
let mut zip = zip::ZipWriter::new(writer);
let mut zip = zip_next::ZipWriter::new(writer);
let options = FileOptions::default()
.compression_method(method)
.unix_permissions(0o755);
Expand Down Expand Up @@ -107,8 +107,8 @@ where
fn doit(
src_dir: &str,
dst_file: &str,
method: zip::CompressionMethod,
) -> zip::result::ZipResult<()> {
method: zip_next::CompressionMethod,
) -> zip_next::result::ZipResult<()> {
if !Path::new(src_dir).is_dir() {
return Err(ZipError::FileNotFound);
}
Expand Down
8 changes: 4 additions & 4 deletions examples/write_sample.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::io::prelude::*;
use zip::write::FileOptions;
use zip_next::write::FileOptions;

fn main() {
std::process::exit(real_main());
Expand All @@ -21,16 +21,16 @@ fn real_main() -> i32 {
0
}

fn doit(filename: &str) -> zip::result::ZipResult<()> {
fn doit(filename: &str) -> zip_next::result::ZipResult<()> {
let path = std::path::Path::new(filename);
let file = std::fs::File::create(path).unwrap();

let mut zip = zip::ZipWriter::new(file);
let mut zip = zip_next::ZipWriter::new(file);

zip.add_directory("test/", Default::default())?;

let options = FileOptions::default()
.compression_method(zip::CompressionMethod::Stored)
.compression_method(zip_next::CompressionMethod::Stored)
.unix_permissions(0o755);
zip.start_file("test/☃.txt", options)?;
zip.write_all(b"Hello, World!\n")?;
Expand Down
2 changes: 1 addition & 1 deletion fuzz/fuzz_targets/fuzz_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use libfuzzer_sys::fuzz_target;

fn decompress_all(data: &[u8]) -> Result<(), Box<dyn std::error::Error>> {
let reader = std::io::Cursor::new(data);
let mut zip = zip::ZipArchive::new(reader)?;
let mut zip = zip_next::ZipArchive::new(reader)?;

for i in 0..zip.len() {
let mut file = zip.by_index(i)?;
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
//!
//!
//!

#![feature(read_buf)]
#![warn(missing_docs)]

pub use crate::compression::{CompressionMethod, SUPPORTED_COMPRESSION_METHODS};
Expand Down
22 changes: 11 additions & 11 deletions src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ pub(crate) mod zip_archive {
///
/// ```no_run
/// use std::io::prelude::*;
/// fn list_zip_contents(reader: impl Read + Seek) -> zip::result::ZipResult<()> {
/// let mut zip = zip::ZipArchive::new(reader)?;
/// fn list_zip_contents(reader: impl Read + Seek) -> zip_next::result::ZipResult<()> {
/// let mut zip = zip_next::ZipArchive::new(reader)?;
///
/// for i in 0..zip.len() {
/// let mut file = zip.by_index(i)?;
Expand All @@ -72,7 +72,7 @@ pub(crate) mod zip_archive {

pub use zip_archive::ZipArchive;
#[allow(clippy::large_enum_variant)]
enum CryptoReader<'a> {
pub(crate) enum CryptoReader<'a> {
Plaintext(io::Take<&'a mut dyn Read>),
ZipCrypto(ZipCryptoReaderValid<io::Take<&'a mut dyn Read>>),
#[cfg(feature = "aes-crypto")]
Expand Down Expand Up @@ -119,7 +119,7 @@ impl<'a> CryptoReader<'a> {
}
}

enum ZipFileReader<'a> {
pub(crate) enum ZipFileReader<'a> {
NoReader,
Raw(io::Take<&'a mut dyn io::Read>),
Stored(Crc32Reader<CryptoReader<'a>>),
Expand Down Expand Up @@ -178,12 +178,12 @@ impl<'a> ZipFileReader<'a> {

/// A struct for reading a zip file
pub struct ZipFile<'a> {
data: Cow<'a, ZipFileData>,
crypto_reader: Option<CryptoReader<'a>>,
reader: ZipFileReader<'a>,
pub(crate) data: Cow<'a, ZipFileData>,
pub(crate) crypto_reader: Option<CryptoReader<'a>>,
pub(crate) reader: ZipFileReader<'a>,
}

fn find_content<'a>(
pub(crate) fn find_content<'a>(
data: &ZipFileData,
reader: &'a mut (impl Read + Seek),
) -> ZipResult<io::Take<&'a mut dyn Read>> {
Expand All @@ -206,7 +206,7 @@ fn find_content<'a>(
}

#[allow(clippy::too_many_arguments)]
fn make_crypto_reader<'a>(
pub(crate) fn make_crypto_reader<'a>(
compression_method: crate::compression::CompressionMethod,
crc32: u32,
last_modified_time: DateTime,
Expand Down Expand Up @@ -257,7 +257,7 @@ fn make_crypto_reader<'a>(
Ok(Ok(reader))
}

fn make_reader(
pub(crate) fn make_reader(
compression_method: CompressionMethod,
crc32: u32,
reader: CryptoReader,
Expand Down Expand Up @@ -991,7 +991,7 @@ impl<'a> Drop for ZipFile<'a> {
// Get the inner `Take` reader so all decryption, decompression and CRC calculation is skipped.
let mut reader: std::io::Take<&mut dyn std::io::Read> = match &mut self.reader {
ZipFileReader::NoReader => {
let innerreader = ::std::mem::replace(&mut self.crypto_reader, None);
let innerreader = self.crypto_reader.take();
innerreader.expect("Invalid reader state").into_inner()
}
reader => {
Expand Down
4 changes: 2 additions & 2 deletions src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ impl ZipError {
/// The text used as an error when a password is required and not supplied
///
/// ```rust,no_run
/// # use zip::result::ZipError;
/// # let mut archive = zip::ZipArchive::new(std::io::Cursor::new(&[])).unwrap();
/// # use zip_next::result::ZipError;
/// # let mut archive = zip_next::ZipArchive::new(std::io::Cursor::new(&[])).unwrap();
/// match archive.by_index(1) {
/// Err(ZipError::UnsupportedArchive(ZipError::PASSWORD_REQUIRED)) => eprintln!("a password is needed to unzip this file"),
/// _ => (),
Expand Down
Loading

0 comments on commit cde5d5e

Please sign in to comment.