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.
'd
is the lifetime of borrowed data added viaadd_file_from_memory
'p
is the lifetime of borrowedPath
s used inadd_file_from_fs
'r
is the lifetime of of borrowed data in readers supplied toadd_file_from_reader
Implementations§
Source§impl<'d, 'p, 'r> ZipArchive<'d, 'p, 'r>
impl<'d, 'p, 'r> ZipArchive<'d, 'p, 'r>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create an empty ZipArchive
Sourcepub fn add_file_from_fs(
&mut self,
fs_path: impl Into<Cow<'p, Path>>,
archived_path: String,
) -> ZipFileBuilder<'_, 'd, 'p, 'r>
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();
Sourcepub fn add_file_from_memory(
&mut self,
data: impl Into<Cow<'d, [u8]>>,
archived_path: String,
) -> ZipFileBuilder<'_, 'd, 'p, 'r>
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();
Sourcepub fn add_file_from_reader<R: Read + Send + Sync + UnwindSafe + RefUnwindSafe + 'r>(
&mut self,
reader: R,
archived_path: String,
) -> ZipFileBuilder<'_, 'd, 'p, 'r>
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();
Sourcepub fn add_directory(
&mut self,
archived_path: String,
) -> ZipFileBuilder<'_, 'd, 'p, 'r>
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();
Sourcepub fn compress_with_threads(&mut self, threads: usize)
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);
Sourcepub fn write_with_threads<W: Write + Seek>(
&mut self,
writer: &mut W,
threads: usize,
) -> Result<()>
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>
impl<'d, 'p, 'r> ZipArchive<'d, 'p, 'r>
Sourcepub fn compress_with_rayon(&mut self)
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.
Sourcepub fn write_with_rayon<W: Write + Seek + Send>(
&mut self,
writer: &mut W,
) -> Result<()>
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>
impl<'d, 'p, 'r> Debug for ZipArchive<'d, 'p, 'r>
Source§impl<'d, 'p, 'r> Default for ZipArchive<'d, 'p, 'r>
impl<'d, 'p, 'r> Default for ZipArchive<'d, 'p, 'r>
Source§fn default() -> ZipArchive<'d, 'p, 'r>
fn default() -> ZipArchive<'d, 'p, 'r>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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