Skip to main content

Writer

Struct Writer 

Source
pub struct Writer<W> { /* private fields */ }
Expand description

A 7z archive writer.

Implementations§

Source§

impl<W: Write + Seek + Send> Writer<W>

Source

pub fn add_path( &mut self, disk_path: impl AsRef<Path>, archive_path: ArchivePath, ) -> Result<()>

Adds a file from a filesystem path.

§Arguments
  • disk_path - Path to the file on disk
  • archive_path - Path within the archive
§Errors

Returns an error if the file cannot be read or if the writer is in an invalid state.

Source

pub fn add_directory( &mut self, archive_path: ArchivePath, meta: EntryMeta, ) -> Result<()>

Adds a directory entry.

§Arguments
  • archive_path - Path within the archive
  • meta - Entry metadata
§Errors

Returns an error if the writer is in an invalid state.

Source

pub fn add_anti_item(&mut self, archive_path: ArchivePath) -> Result<()>

Adds an anti-item entry (file marked for deletion in incremental backups).

Anti-items are empty entries that indicate a file or directory should be deleted when the incremental archive is applied. This is useful for incremental backup systems.

§Arguments
  • archive_path - Path within the archive to mark for deletion
§Example
let mut writer = Writer::create_file("incremental.7z")?;
writer.add_anti_item(ArchivePath::new("deleted_file.txt")?)?;
writer.finish()?;
Source

pub fn add_anti_directory(&mut self, archive_path: ArchivePath) -> Result<()>

Adds an anti-item directory (directory marked for deletion).

§Arguments
  • archive_path - Directory path within the archive to mark for deletion
Source

pub fn add_stream( &mut self, archive_path: ArchivePath, source: &mut dyn Read, meta: EntryMeta, ) -> Result<()>

Adds data from a stream.

§Arguments
  • archive_path - Path within the archive
  • source - Reader providing the data
  • meta - Entry metadata
§Errors

Returns an error if compression fails or if the writer is in an invalid state.

Source

pub fn add_bytes( &mut self, archive_path: ArchivePath, data: &[u8], ) -> Result<()>

Adds data from a byte slice.

§Arguments
  • archive_path - Path within the archive
  • data - The data to add
§Errors

Returns an error if compression fails or if the writer is in an invalid state.

Source§

impl Writer<BufWriter<File>>

Source

pub fn create_path(path: impl AsRef<Path>) -> Result<Self>

Creates a new archive file at the given path.

The file is created, and an existing one truncated, before a single entry has been written - so a run that fails partway leaves neither the finished archive nor what was at that path before. Where that matters, write to a path of your own beside it and rename onto the destination when Writer::finish returns; that is what this crate’s own command-line tool does.

§Arguments
  • path - Path to the archive file to create
§Errors

Returns an error if the file cannot be created.

Source

pub fn finish(self) -> Result<WriteResult>

Finishes writing the archive.

§Returns

A WriteResult with statistics about the written archive.

§Errors

Returns an error if header writing fails.

Source§

impl Writer<Cursor<Vec<u8>>>

Source

pub fn finish(self) -> Result<WriteResult>

Finishes writing the archive to an owned cursor.

Source§

impl Writer<Cursor<&mut Vec<u8>>>

Source

pub fn finish(self) -> Result<WriteResult>

Finishes writing the archive to a borrowed cursor.

Source§

impl Writer<MultiVolumeWriter>

Source

pub fn create_multivolume(config: VolumeConfig) -> Result<Self>

Creates a new multi-volume archive writer.

The archive will be split across multiple files when each volume reaches the configured size limit.

§Arguments
  • config - Volume configuration specifying size and base path

Each volume is created, and an existing file of that name truncated, as the archive reaches it - so a run that fails partway leaves a set of volumes that is neither complete nor what was there before.

§Errors

Returns an error if the first volume file cannot be created.

§Example
use zesven::{Writer, VolumeConfig, ArchivePath};

let config = VolumeConfig::new("archive.7z", 50 * 1024 * 1024); // 50 MB volumes
let mut writer = Writer::create_multivolume(config)?;
writer.add_bytes(ArchivePath::new("data.bin")?, &large_data)?;
let result = writer.finish()?;
println!("Created {} volumes", result.volume_count);
Source

pub fn finish(self) -> Result<WriteResult>

Finishes writing the multi-volume archive.

This finalizes all volumes and returns a WriteResult with volume information.

§Returns

A WriteResult with statistics including volume_count and volume_sizes.

§Errors

Returns an error if header writing or volume finalization fails.

Source§

impl<W: Write + Seek> Writer<W>

Source

pub fn create(sink: W) -> Result<Self>

Creates a new archive writer.

§Arguments
  • sink - The writer to output archive data to
§Errors

Returns an error if the initial seek fails.

Source

pub fn progress(self, reporter: impl ProgressReporter + 'static) -> Self

Reports what is being written to reporter as the archive is built.

Writing an archive is mostly silent from the outside: entries are gathered and compressed together, so the call that accepts an entry usually returns before anything has been compressed and the work lands on whichever call fills the batch, or on finish. A caller timing its own calls sees that as one long pause among instant ones, which is a description of the batching rather than of the work.

The reporter is told what actually happens: which entries are being worked on, how far an entry large enough to be compressed on its own has got, and when each one is in.

The three say different things, and each says what it can honestly say:

  • on_entry_start comes before the work, and for a batch it comes for every entry in it at once, because they are compressed at the same time as each other and no one of them is the one being worked on.
  • on_progress reports bytes of archive produced against the size the entry declared, and only for an entry compressed on its own. It is the work rather than the reading: such an entry is taken in far faster than it is compressed, so a count of what has been read would reach the end within a second and leave the rest of the wait silent.
  • on_ratio comes with each completed entry and covers the archive so far on both sides, which is the only total a writer knows: what it will hold in the end depends on calls that have not been made yet.

Entries do not begin and end one at a time. An entry large enough to be compressed on its own is left finishing while the entry after it is read, and a batch is compressed while a large entry is, so a reporter can be told an entry has started before it is told the one before it has finished. What is guaranteed is the order they are finished in, which is the order they were added: an entry is reported finished when its bytes are in the archive, and they go in as they were accepted.

A reporter can call the writing off from either of two places. Answering on_progress with false stops the entry being written where it stands, and is the only way to stop during one; should_cancel is asked before each entry is accepted. Both give the caller Error::Cancelled, but they leave different things behind: an entry stopped partway has put bytes in the sink that belong to no folder, so the archive cannot be finished, while one refused before it started leaves an archive that can.

use zesven::{StatisticsProgress, write::Writer};

let writer = Writer::create_path("archive.7z")?
    .progress(StatisticsProgress::new());
Source

pub fn options(self, options: WriteOptions) -> Self

Sets the write options.

Entries already accepted keep the options they were accepted under. Anything still waiting is written out with those before the next entry is taken, so a change here never reaches back over work already done.

Source

pub fn finish_into_inner(self) -> Result<(WriteResult, W)>

Finishes writing the archive and returns the underlying sink.

This is useful when you need access to the written data, such as when writing to a Cursor<Vec<u8>> and need to retrieve the buffer.

§Returns

A tuple of (WriteResult, W) where W is the underlying sink.

§Errors

Returns an error if header writing fails.

§Example
use zesven::write::Writer;
use std::io::Cursor;

let mut writer = Writer::create(Cursor::new(Vec::new()))?;
writer.add_bytes("test.txt".try_into()?, b"Hello")?;
let (result, cursor) = writer.finish_into_inner()?;
let archive_bytes = cursor.into_inner();

Auto Trait Implementations§

§

impl<W> !RefUnwindSafe for Writer<W>

§

impl<W> !UnwindSafe for Writer<W>

§

impl<W> Freeze for Writer<W>
where W: Freeze,

§

impl<W> Send for Writer<W>
where W: Send,

§

impl<W> Sync for Writer<W>
where W: Sync,

§

impl<W> Unpin for Writer<W>
where W: Unpin,

§

impl<W> UnsafeUnpin for Writer<W>
where W: UnsafeUnpin,

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> Same for T

Source§

type Output = T

Should always be Self
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.