Skip to content
Draft
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
24 changes: 10 additions & 14 deletions rc-zip-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clap::{Parser, Subcommand};
use humansize::{format_size, BINARY};
use indicatif::{ProgressBar, ProgressStyle};
use rc_zip::{Archive, Entry, EntryKind};
use rc_zip_sync::{ArchiveHandle, ReadZip, ReadZipStreaming};
use rc_zip_sync::{HasCursor, ReadZip, ReadZipStreaming};

use std::{
borrow::Cow,
Expand Down Expand Up @@ -81,10 +81,10 @@ fn do_main(cli: Cli) -> Result<(), Box<dyn std::error::Error>> {
}
Commands::Ls { zipfile, verbose } => {
let zipfile = File::open(zipfile)?;
let reader = zipfile.read_zip()?;
let archive = zipfile.read_zip()?;
let mut stdout = io::stdout().lock();
let _ = info(&mut stdout, &reader);
let _ = list(&mut stdout, &reader, verbose);
let _ = info(&mut stdout, &archive);
let _ = list(&mut stdout, &zipfile, &archive, verbose);
}
Commands::Unzip { zipfile, dir } => unzip(&zipfile, dir.as_deref(), false)?,
Commands::UnzipStreaming { zipfile, dir } => {
Expand Down Expand Up @@ -134,11 +134,7 @@ fn info(out: &mut impl io::Write, archive: &Archive) -> io::Result<()> {
Ok(())
}

fn list(
out: &mut impl io::Write,
archive: &ArchiveHandle<'_, File>,
verbose: bool,
) -> io::Result<()> {
fn list(out: &mut impl io::Write, f: &File, archive: &Archive, verbose: bool) -> io::Result<()> {
for entry in archive.entries() {
write!(
out,
Expand Down Expand Up @@ -167,7 +163,7 @@ fn list(

if let EntryKind::Symlink = entry.kind() {
let mut target = String::new();
entry.reader().read_to_string(&mut target).unwrap();
f.reader_at(entry).read_to_string(&mut target).unwrap();
print!("\t{target}", target = target);
}

Expand All @@ -189,10 +185,10 @@ fn unzip(
) -> Result<(), Box<dyn std::error::Error>> {
let zipfile = File::open(zipfile)?;
let dir = dir.unwrap_or_else(|| Path::new("."));
let reader = zipfile.read_zip()?;
let archive = zipfile.read_zip()?;

let mut stats = Stats::default();
let total_uncompressed_size = reader
let total_uncompressed_size = archive
.entries()
.map(|entry| entry.uncompressed_size)
.sum::<u64>();
Expand All @@ -212,10 +208,10 @@ fn unzip(
};

let start_time = Instant::now();
for entry in reader.entries() {
for entry in archive.entries() {
extract_entry(
entry.to_owned(),
&mut entry.reader(),
&mut zipfile.reader_at(entry),
dir,
&pbar,
&mut stats,
Expand Down
2 changes: 1 addition & 1 deletion rc-zip-cli/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ fn list() {
let zip_file = fs::File::open(&zip_path).unwrap();
let archive = zip_file.read_zip().unwrap();
let mut output = Vec::new();
crate::list(&mut output, &archive, verbose).unwrap();
crate::list(&mut output, &zip_file, &archive, verbose).unwrap();
String::from_utf8(output).unwrap()
}

Expand Down
18 changes: 12 additions & 6 deletions rc-zip-sync/examples/byte_count.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
use std::{ffi::OsString, fmt, fs::File, io::Read, sync::Mutex, thread, time};

use rc_zip_sync::{ArchiveHandle, EntryHandle, ReadZip};
use rc_zip::{Archive, Entry};
use rc_zip_sync::{HasCursor, ReadZip};

struct EntryHandle<'e, F> {
entr: &'e Entry,
f: &'e F,
}

/// Display counts for each byte in a zip's entries
fn main() -> Result<(), Box<dyn std::error::Error>> {
Expand All @@ -9,7 +15,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let archive = zip_file.read_zip()?;

let start = time::Instant::now();
let counts = byte_count_multi_threaded(&archive);
let counts = byte_count_multi_threaded(&zip_file, &archive);
print_stats(counts, start.elapsed());

Ok(())
Expand Down Expand Up @@ -60,9 +66,9 @@ impl Counts {
/// The work is split across a pool of worker threads where each worker takes turns fetching an
/// entry from the archive to then read over the entry and reduce it down to counts. Then the final
/// counts are totaled together as each worker finishes.
fn byte_count_multi_threaded(archive: &ArchiveHandle<'_, File>) -> Counts {
fn byte_count_multi_threaded(file: &File, archive: &Archive) -> Counts {
let mut total_counts = Counts::new();
let entries = Mutex::new(archive.entries());
let entries = Mutex::new(archive.entries().map(|entr| EntryHandle { f: file, entr }));
let num_workers = thread::available_parallelism().unwrap();
thread::scope(|s| {
let worker_handles: Vec<_> = (1..num_workers.into())
Expand Down Expand Up @@ -104,9 +110,9 @@ fn byte_count_worker<'zip>(entries: &Mutex<impl Iterator<Item = ZipEntry<'zip>>>
}

fn entry_add_byte_counts(entry: ZipEntry<'_>, counts: &mut Counts) -> rc_zip::Result<()> {
if entry.kind().is_file() {
if entry.entr.kind().is_file() {
let mut buf = [0; 8 * 1024];
let mut entry_reader = entry.reader();
let mut entry_reader = entry.f.reader_at(entry.entr);
while let Ok(num_bytes) = entry_reader.read(&mut buf) {
if num_bytes == 0 {
// finished reading!
Expand Down
13 changes: 7 additions & 6 deletions rc-zip-sync/examples/self_extracting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use rc_zip::{
error::{Error, FormatError},
parse::EntryKind,
};
use rc_zip_sync::{ArchiveHandle, ReadZip};
use rc_zip_sync::rc_zip::Archive;
use rc_zip_sync::{HasCursor, ReadZip};

/// The executable side of a self-extracting zip file
///
Expand Down Expand Up @@ -36,12 +37,12 @@ fn main() -> Result<(), Error> {
}
})?;

extract(&archive)?;
extract(&zip_file, &archive)?;

Ok(())
}

fn extract(archive: &ArchiveHandle<'_, File>) -> Result<(), Error> {
fn extract(file: &File, archive: &Archive) -> Result<(), Error> {
for entry in archive.entries() {
println!("extracting {}", entry.name);
let Some(entry_name) = entry.sanitized_name() else {
Expand All @@ -57,7 +58,7 @@ fn extract(archive: &ArchiveHandle<'_, File>) -> Result<(), Error> {
EntryKind::Directory => fs::create_dir_all(path)?,
EntryKind::File => {
let mut entry_writer = File::create(path)?;
let mut entry_reader = entry.reader();
let mut entry_reader = file.reader_at(entry);
io::copy(&mut entry_reader, &mut entry_writer)?;
}
EntryKind::Symlink => {
Expand All @@ -66,7 +67,7 @@ fn extract(archive: &ArchiveHandle<'_, File>) -> Result<(), Error> {
// creating a symlink on windows is a privileged action, so instead we create a
// regular file
let mut entry_writer = File::create(path)?;
let mut entry_reader = entry.reader();
let mut entry_reader = file.reader_at(entry);
io::copy(&mut entry_reader, &mut entry_writer)?;
}
#[cfg(unix)]
Expand All @@ -81,7 +82,7 @@ fn extract(archive: &ArchiveHandle<'_, File>) -> Result<(), Error> {
}

let mut src = Vec::new();
entry.reader().read_to_end(&mut src)?;
file.reader_at(entry).read_to_end(&mut src)?;
let src = OsString::from_vec(src);

std::os::unix::fs::symlink(&src, path)?;
Expand Down
4 changes: 1 addition & 3 deletions rc-zip-sync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,4 @@ pub use streaming_entry_reader::StreamingEntryReader;
// re-exports
pub use entry_reader::EntryReader;
pub use rc_zip;
pub use read_zip::{
ArchiveHandle, EntryHandle, HasCursor, ReadZip, ReadZipStreaming, ReadZipWithSize,
};
pub use read_zip::{HasCursor, ReadZip, ReadZipStreaming, ReadZipWithSize};
Loading
Loading