Struct ZipArchive

Source
pub struct ZipArchive<'d, 'p, 'r> { /* private fields */ }
Expand description

Structure that holds the current state of ZIP archive creation.

§Lifetimes

Because some of the methods allow supplying borrowed data, the lifetimes are used to indicate that Self borrows them. If you only provide owned data, such as Vec<u8> or PathBuf, you won’t have to worry about lifetimes and can simply use 'static, if you ever need to specify them in your code.

Implementations§

Source§

impl<'d, 'p, 'r> ZipArchive<'d, 'p, 'r>

Source

pub fn new() -> Self

Create an empty ZipArchive

Source

pub fn add_file_from_fs( &mut self, fs_path: impl Into<Cow<'p, Path>>, archived_path: String, ) -> ZipFileBuilder<'_, 'd, 'p, 'r>

Add file from filesystem.

Opens the file and reads data from it when compress is called.

let mut zipper = ZipArchive::new();
zipper
    .add_file_from_fs(Path::new("input.txt"), "input.txt".to_owned())
    .done();
Source

pub fn add_file_from_memory( &mut self, data: impl Into<Cow<'d, [u8]>>, archived_path: String, ) -> ZipFileBuilder<'_, 'd, 'p, 'r>

Add file with data from memory.

The data can be either borrowed or owned by the ZipArchive struct to avoid lifetime hell.

let mut zipper = ZipArchive::new();
let data: &[u8] = "Hello, world!".as_ref();
zipper
    .add_file_from_memory(data, "hello_world.txt".to_owned())
    .done();
Source

pub fn add_file_from_reader<R: Read + Send + Sync + UnwindSafe + RefUnwindSafe + 'r>( &mut self, reader: R, archived_path: String, ) -> ZipFileBuilder<'_, 'd, 'p, 'r>

Add a file with data from a reader.

This method takes any type implementing Read and allows it to have borrowed data ('r)

let mut zipper = ZipArchive::new();
let data_input = std::io::stdin();
zipper
    .add_file_from_reader(data_input, "stdin_file.txt".to_owned())
    .done();
Source

pub fn add_directory( &mut self, archived_path: String, ) -> ZipFileBuilder<'_, 'd, 'p, 'r>

Add a directory entry.

All directories in the tree should be added. This method does not asssociate any filesystem properties to the entry.

let mut zipper = ZipArchive::new();
zipper.add_directory("test_dir/".to_owned()).done();
Source

pub fn compress(&mut self)

Compress contents. Will be done automatically on write call if files were added between last compression and write call. Automatically chooses amount of threads to use based on how much are available.

Source

pub fn compress_with_threads(&mut self, threads: usize)

Compress contents. Will be done automatically on write_with_threads call if files were added between last compression and write. Allows specifying amount of threads that will be used.

Example of getting amount of threads that this library uses in compress:

let threads = std::thread::available_parallelism()
    .map(NonZeroUsize::get)
    .unwrap_or(1);

zipper.compress_with_threads(threads);
Source

pub fn write<W: Write + Seek>(&mut self, writer: &mut W) -> Result<()>

Write compressed data to a writer (usually a file). Executes compress if files were added between last compress call and this call. Automatically chooses the amount of threads cpu has.

Source

pub fn write_with_threads<W: Write + Seek>( &mut self, writer: &mut W, threads: usize, ) -> Result<()>

Write compressed data to a writer (usually a file). Executes compress_with_threads if files were added between last compress call and this call. Allows specifying amount of threads that will be used.

Example of getting amount of threads that this library uses in write:

let threads = std::thread::available_parallelism()
    .map(NonZeroUsize::get)
    .unwrap_or(1);

zipper.compress_with_threads(threads);
Source§

impl<'d, 'p, 'r> ZipArchive<'d, 'p, 'r>

Source

pub fn compress_with_rayon(&mut self)

Compress contents and use rayon for parallelism.

Uses whatever thread pool this function is executed in.

If you want to limit the amount of threads to be used, use rayon::ThreadPoolBuilder::num_threads and either set it as a global pool, or rayon::ThreadPool::install the call to this method in it.

Source

pub fn write_with_rayon<W: Write + Seek + Send>( &mut self, writer: &mut W, ) -> Result<()>

Write the contents to a writer.

This method uses teh same thread logic as Self::compress_with_rayon, refer to its documentation for details on how to control the parallelism and thread allocation.

Trait Implementations§

Source§

impl<'d, 'p, 'r> Debug for ZipArchive<'d, 'p, 'r>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'d, 'p, 'r> Default for ZipArchive<'d, 'p, 'r>

Source§

fn default() -> ZipArchive<'d, 'p, 'r>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'d, 'p, 'r> Freeze for ZipArchive<'d, 'p, 'r>

§

impl<'d, 'p, 'r> RefUnwindSafe for ZipArchive<'d, 'p, 'r>

§

impl<'d, 'p, 'r> Send for ZipArchive<'d, 'p, 'r>

§

impl<'d, 'p, 'r> Sync for ZipArchive<'d, 'p, 'r>

§

impl<'d, 'p, 'r> Unpin for ZipArchive<'d, 'p, 'r>

§

impl<'d, 'p, 'r> UnwindSafe for ZipArchive<'d, 'p, 'r>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.