Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benches/read_metadata.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use bencher::{benchmark_group, benchmark_main};

use std::fs;
use std::io::{self, prelude::*, Cursor};
use std::io::{self, Cursor, Write};

use bencher::Bencher;
use tempfile::TempDir;
Expand Down
2 changes: 1 addition & 1 deletion examples/extract_lorem.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::io::prelude::*;
use std::io::Read;

fn main() {
std::process::exit(real_main());
Expand Down
2 changes: 1 addition & 1 deletion examples/write_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![allow(dead_code)]
use anyhow::Context;
use clap::{Parser, ValueEnum};
use std::io::prelude::*;
use std::io::{Read, Seek, Write};
use zip::{result::ZipError, write::SimpleFileOptions};

use std::fs::File;
Expand Down
2 changes: 1 addition & 1 deletion examples/write_sample.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::io::prelude::*;
use std::io::Write;
use zip::write::SimpleFileOptions;
#[cfg(feature = "aes-crypto")]
use zip::{AesMode, CompressionMethod};
Expand Down
14 changes: 8 additions & 6 deletions fuzz_write/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,11 @@ fn do_operation(
"let mut writer = ZipWriter::new_append(writer.finish()?)?;"
)?;
replace_with_or_abort(writer, |old_writer: zip::ZipWriter<Cursor<Vec<u8>>>| {
(|| -> ZipResult<zip::ZipWriter<Cursor<Vec<u8>>>> {
let get_writer = || {
// return a ZipResult<zip::ZipWriter<Cursor<Vec<u8>>>>
zip::ZipWriter::new_append(old_writer.finish()?)
})()
.unwrap_or_else(|_| {
};
get_writer().unwrap_or_else(|_| {
if panic_on_error {
panic!("Failed to create new ZipWriter")
}
Expand All @@ -275,10 +276,11 @@ fn do_operation(
"let mut writer = ZipWriter::new_append(writer.finish()?)?;"
)?;
replace_with_or_abort(writer, |old_writer| {
(|| -> ZipResult<zip::ZipWriter<Cursor<Vec<u8>>>> {
let get_writer = || {
// return a ZipResult<zip::ZipWriter<Cursor<Vec<u8>>>>
zip::ZipWriter::new_append(old_writer.finish()?)
})()
.unwrap_or_else(|_| {
};
get_writer().unwrap_or_else(|_| {
if panic_on_error {
panic!("Failed to create new ZipWriter")
}
Expand Down
2 changes: 1 addition & 1 deletion src/aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use sha1::Sha1;
use std::io::{self, Error, ErrorKind, Read, Write};
use zeroize::{Zeroize, Zeroizing};

/// The length of the password verifcation value in bytes
/// The length of the password verification value in bytes
pub const PWD_VERIFY_LENGTH: usize = 2;
/// The length of the authentication code in bytes
const AUTH_CODE_LENGTH: usize = 10;
Expand Down
4 changes: 2 additions & 2 deletions src/aes_ctr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use crate::unstable::LittleEndianWriteExt;
use aes::cipher::generic_array::GenericArray;
use aes::cipher::{BlockEncrypt, KeyInit};
use std::{any, fmt};
use core::{any, fmt};

/// Internal block size of an AES cipher.
const AES_BLOCK_SIZE: usize = 16;
Expand Down Expand Up @@ -153,7 +153,7 @@ mod tests {
use aes::cipher::{BlockEncrypt, KeyInit};

/// Checks whether `crypt_in_place` produces the correct plaintext after one use and yields the
/// cipertext again after applying it again.
/// ciphertext again after applying it again.
fn roundtrip<Aes>(key: &[u8], ciphertext: &[u8], expected_plaintext: &[u8])
where
Aes: AesKind,
Expand Down
3 changes: 2 additions & 1 deletion src/compression.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Possible ZIP compression methods.

use std::{fmt, io};
use core::fmt;
use std::io;

#[allow(deprecated)]
/// Identifies the storage format used to compress a file within a ZIP archive.
Expand Down
7 changes: 4 additions & 3 deletions src/crc32.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Helper module to compute a CRC32 checksum

use std::io;
use std::io::prelude::*;
use std::io::{self, Read};

use crc32fast::Hasher;

Expand Down Expand Up @@ -85,7 +84,9 @@ impl<R: Read> Read for Crc32Reader<R> {

#[cfg(test)]
mod test {
use super::*;
use std::io::Read;

use super::Crc32Reader;

#[test]
fn test_empty_reader() {
Expand Down
2 changes: 1 addition & 1 deletion src/extra_fields/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mod zipinfo_utf8;

pub use extended_timestamp::*;
pub use ntfs::Ntfs;
pub use zipinfo_utf8::*;
pub use zipinfo_utf8::UnicodeExtraField;

/// contains one extra field
#[derive(Debug, Clone)]
Expand Down
26 changes: 13 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@
//!
//! This is a list of supported features:
//!
//! | | Reading | Writing |
//! | ------- | ------ | ------- |
//! | Stored | ✅ | ✅ |
//! | Deflate | ✅ [->](`crate::ZipArchive::by_name`) | ✅ [->](`crate::write::FileOptions::compression_method`) |
//! | Deflate64 | ✅ | |
//! | Bzip2 | ✅ | ✅ |
//! | ZStandard | ✅ | ✅ |
//! | LZMA | ✅ | |
//! | XZ | ✅ | ✅ |
//! | PPMd | ✅ | ✅ |
//! | AES encryption | ✅ | ✅ |
//! | ZipCrypto deprecated encryption | ✅ | ✅ |
//!
//! | | Reading | Writing |
//! | ------------------------------- | ------------------------------------- | -------------------------------------------------------- |
//! | Stored | ✅ | ✅ |
//! | Deflate | ✅ [->](`crate::ZipArchive::by_name`) | ✅ [->](`crate::write::FileOptions::compression_method`) |
//! | Deflate64 | ✅ | |
//! | Bzip2 | ✅ | ✅ |
//! | ZStandard | ✅ | ✅ |
//! | LZMA | ✅ | |
//! | XZ | ✅ | ✅ |
//! | PPMd | ✅ | ✅ |
//! | AES encryption | ✅ | ✅ |
//! | ZipCrypto deprecated encryption | ✅ | ✅ |
//!

#![cfg_attr(docsrs, feature(doc_cfg))]
#![warn(missing_docs)]
#![allow(unexpected_cfgs)] // Needed for cfg(fuzzing) on nightly as of 2024-05-06
Expand Down
43 changes: 20 additions & 23 deletions src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,27 @@ use crate::compression::{CompressionMethod, Decompressor};
use crate::cp437::FromCp437;
use crate::crc32::Crc32Reader;
use crate::extra_fields::{ExtendedTimestamp, ExtraField, Ntfs};
use crate::read::zip_archive::{Shared, SharedBuilder};
use crate::result::invalid;
use crate::result::{ZipError, ZipResult};
use crate::spec::{self, CentralDirectoryEndInfo, DataAndPosition, FixedSizeBlock, Pod};
use crate::result::{invalid, ZipError, ZipResult};
use crate::spec::{
self, CentralDirectoryEndInfo, DataAndPosition, FixedSizeBlock, Pod, ZIP64_BYTES_THR,
};
use crate::types::{
AesMode, AesVendorVersion, DateTime, System, ZipCentralEntryBlock, ZipFileData,
ZipLocalEntryBlock,
AesMode, AesVendorVersion, DateTime, SimpleFileOptions, System, ZipCentralEntryBlock,
ZipFileData, ZipLocalEntryBlock,
};
use crate::write::SimpleFileOptions;
use crate::zipcrypto::{ZipCryptoReader, ZipCryptoReaderValid, ZipCryptoValidator};
use crate::ZIP64_BYTES_THR;
use core::mem::{replace, size_of};
use core::ops::{Deref, Range};
use indexmap::IndexMap;
use std::borrow::Cow;
use std::ffi::OsStr;
use std::fs::create_dir_all;
use std::io::{self, copy, prelude::*, sink, SeekFrom};
use std::mem;
use std::mem::size_of;
use std::ops::{Deref, Range};
use std::io::{self, copy, sink, Read, Seek, SeekFrom, Write};
use std::path::{Component, Path, PathBuf};
use std::sync::{Arc, OnceLock};

mod config;

pub use config::*;
pub use config::{ArchiveOffset, Config};

/// Provides high level API for reading from a stream.
pub(crate) mod stream;
Expand Down Expand Up @@ -89,7 +85,7 @@ pub(crate) mod zip_archive {
/// change in the future.
///
/// ```no_run
/// use std::io::prelude::*;
/// use std::io::{Read, Seek};
/// fn list_zip_contents(reader: impl Read + Seek) -> zip::result::ZipResult<()> {
/// use zip::HasZipMetadata;
/// let mut zip = zip::ZipArchive::new(reader)?;
Expand Down Expand Up @@ -313,7 +309,8 @@ impl<R: Read> Read for SeekableTake<'_, R> {
}

pub(crate) fn make_writable_dir_all<T: AsRef<Path>>(outpath: T) -> Result<(), ZipError> {
create_dir_all(outpath.as_ref())?;
use std::fs;
fs::create_dir_all(outpath.as_ref())?;
#[cfg(unix)]
{
// Dirs must be writable until all normal files are extracted
Expand Down Expand Up @@ -545,7 +542,7 @@ impl<R> ZipArchive<R> {
Some((_, file)) => file.header_start,
None => central_start,
};
let shared = Arc::new(Shared {
let shared = Arc::new(zip_archive::Shared {
files,
offset: initial_offset,
dir_start: central_start,
Expand Down Expand Up @@ -642,7 +639,7 @@ impl<R: Read + Seek> ZipArchive<R> {

/// Get the directory start offset and number of files. This is done in a
/// separate function to ease the control flow design.
pub(crate) fn get_metadata(config: Config, reader: &mut R) -> ZipResult<Shared> {
pub(crate) fn get_metadata(config: Config, reader: &mut R) -> ZipResult<zip_archive::Shared> {
// End of the probed region, initially set to the end of the file
let file_len = reader.seek(io::SeekFrom::End(0))?;
let mut end_exclusive = file_len;
Expand Down Expand Up @@ -687,7 +684,7 @@ impl<R: Read + Seek> ZipArchive<R> {
dir_info: CentralDirectoryInfo,
config: Config,
reader: &mut R,
) -> Result<SharedBuilder, ZipError> {
) -> Result<zip_archive::SharedBuilder, ZipError> {
// If the parsed number of files is greater than the offset then
// something fishy is going on and we shouldn't trust number_of_files.
let file_capacity = if dir_info.number_of_files > dir_info.directory_start as usize {
Expand All @@ -711,7 +708,7 @@ impl<R: Read + Seek> ZipArchive<R> {
files.push(file);
}

Ok(SharedBuilder {
Ok(zip_archive::SharedBuilder {
files,
offset: dir_info.archive_offset,
dir_start: dir_info.directory_start,
Expand Down Expand Up @@ -865,7 +862,7 @@ impl<R: Read + Seek> ZipArchive<R> {
) -> ZipResult<()> {
use std::fs;

create_dir_all(&directory)?;
fs::create_dir_all(&directory)?;
let directory = directory.as_ref().canonicalize()?;

let root_dir = root_dir_filter
Expand Down Expand Up @@ -1614,7 +1611,7 @@ impl<'a> ZipReadOptions<'a> {
/// Methods for retrieving information on zip files
impl<'a, R: Read> ZipFile<'a, R> {
pub(crate) fn take_raw_reader(&mut self) -> io::Result<io::Take<&'a mut R>> {
mem::replace(&mut self.reader, ZipFileReader::NoReader).into_inner()
replace(&mut self.reader, ZipFileReader::NoReader).into_inner()
}

/// Get the version of the file
Expand Down Expand Up @@ -2123,7 +2120,7 @@ fn generate_chrono_datetime(datetime: &DateTime) -> Option<chrono::NaiveDateTime
mod test {
use crate::read::ZipReadOptions;
use crate::result::ZipResult;
use crate::write::SimpleFileOptions;
use crate::types::SimpleFileOptions;
use crate::CompressionMethod::Stored;
use crate::{ZipArchive, ZipWriter};
use std::io::{Cursor, Read, Write};
Expand Down
28 changes: 14 additions & 14 deletions src/read/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use super::{
};
use crate::spec::FixedSizeBlock;
use indexmap::IndexMap;
use std::fs;
use std::fs::create_dir_all;
use std::io::{self, Read};
use std::path::{Path, PathBuf};

Expand Down Expand Up @@ -58,7 +56,8 @@ impl<R: Read> ZipStreamReader<R> {
/// Extraction is not atomic; If an error is encountered, some of the files
/// may be left on disk.
pub fn extract<P: AsRef<Path>>(self, directory: P) -> ZipResult<()> {
create_dir_all(&directory)?;
use std::fs;
fs::create_dir_all(&directory)?;
let directory = directory.as_ref().canonicalize()?;
struct Extractor(PathBuf, IndexMap<Box<str>, ()>);
impl ZipStreamVisitor for Extractor {
Expand Down Expand Up @@ -121,7 +120,7 @@ pub trait ZipStreamVisitor {
/// - `external_attributes`: `unix_mode()`: will return None
fn visit_file<R: Read>(&mut self, file: &mut ZipFile<'_, R>) -> ZipResult<()>;

/// This function is guranteed to be called after all `visit_file`s.
/// This function is guaranteed to be called after all `visit_file`s.
///
/// * `metadata` - Provides missing metadata in `visit_file`.
fn visit_additional_metadata(&mut self, metadata: &ZipStreamFileMetadata) -> ZipResult<()>;
Expand Down Expand Up @@ -211,11 +210,13 @@ impl ZipStreamFileMetadata {
mod test {
use tempfile::TempDir;

use super::*;
use crate::read::stream::{ZipStreamFileMetadata, ZipStreamReader, ZipStreamVisitor};
use crate::read::ZipFile;
use crate::result::ZipResult;
use crate::write::SimpleFileOptions;
use crate::ZipWriter;
use std::collections::BTreeSet;
use std::io::Cursor;
use std::io::{Cursor, Read};

struct DummyVisitor;
impl ZipStreamVisitor for DummyVisitor {
Expand Down Expand Up @@ -251,7 +252,7 @@ mod test {

#[test]
fn invalid_offset() {
ZipStreamReader::new(io::Cursor::new(include_bytes!(
ZipStreamReader::new(Cursor::new(include_bytes!(
"../../tests/data/invalid_offset.zip"
)))
.visit(&mut DummyVisitor)
Expand All @@ -260,7 +261,7 @@ mod test {

#[test]
fn invalid_offset2() {
ZipStreamReader::new(io::Cursor::new(include_bytes!(
ZipStreamReader::new(Cursor::new(include_bytes!(
"../../tests/data/invalid_offset2.zip"
)))
.visit(&mut DummyVisitor)
Expand All @@ -269,9 +270,8 @@ mod test {

#[test]
fn zip_read_streaming() {
let reader = ZipStreamReader::new(io::Cursor::new(include_bytes!(
"../../tests/data/mimetype.zip"
)));
let reader =
ZipStreamReader::new(Cursor::new(include_bytes!("../../tests/data/mimetype.zip")));

#[derive(Default)]
struct V {
Expand Down Expand Up @@ -306,7 +306,7 @@ mod test {

#[test]
fn file_and_dir_predicates() {
let reader = ZipStreamReader::new(io::Cursor::new(include_bytes!(
let reader = ZipStreamReader::new(Cursor::new(include_bytes!(
"../../tests/data/files_and_dirs.zip"
)));

Expand Down Expand Up @@ -353,7 +353,7 @@ mod test {
/// files declared is more than the alleged offset in the CDE
#[test]
fn invalid_cde_number_of_files_allocation_smaller_offset() {
ZipStreamReader::new(io::Cursor::new(include_bytes!(
ZipStreamReader::new(Cursor::new(include_bytes!(
"../../tests/data/invalid_cde_number_of_files_allocation_smaller_offset.zip"
)))
.visit(&mut DummyVisitor)
Expand All @@ -365,7 +365,7 @@ mod test {
/// files declared is less than the alleged offset in the CDE
#[test]
fn invalid_cde_number_of_files_allocation_greater_offset() {
ZipStreamReader::new(io::Cursor::new(include_bytes!(
ZipStreamReader::new(Cursor::new(include_bytes!(
"../../tests/data/invalid_cde_number_of_files_allocation_greater_offset.zip"
)))
.visit(&mut DummyVisitor)
Expand Down
Loading
Loading