Struct libpna::Archive

source ·
pub struct Archive<T> { /* private fields */ }
Expand description

An object providing access to a PNA file. An instance of an Archive can be read and/or written.

§Examples

Creates a new PNA file and adds entry to it.


let file = File::create("foo.pna")?;
let mut archive = Archive::write_header(file)?;
let mut entry_builder =
    EntryBuilder::new_file("bar.txt".into(), WriteOptions::builder().build())?;
entry_builder.write_all(b"content")?;
let entry = entry_builder.build()?;
archive.add_entry(entry)?;
archive.finalize()?;

Read the entries of a pna file.


let file = File::open("foo.pna")?;
let mut archive = Archive::read_header(file)?;
for entry in archive.entries_skip_solid() {
    let entry = entry?;
    let mut file = File::create(entry.header().path().as_path())?;
    let mut reader = entry.reader(ReadOptions::builder().build())?;
    copy(&mut reader, &mut file)?;
}

Implementations§

source§

impl<'d> Archive<&'d [u8]>

source

pub fn read_header_from_slice(bytes: &'d [u8]) -> Result<Self>

Reads the archive header from the provided reader and returns a new Archive.

§Arguments
  • bytes - The [&[u8]] object to read the header from.
§Returns

A new [io::Result<Archive<&[u8]>>].

§Errors

Returns an error if an I/O error occurs while reading header from the bytes.

source

pub fn entries_slice<'a>(&'a mut self) -> Entries<'a, 'd>

Returns an iterator over the entries in the archive.

§Returns

An iterator over the entries in the archive.

§Example
use libpna::{Archive, ReadEntry};

let file = include_bytes!("../../../../resources/test/zstd.pna");
let mut archive = Archive::read_header_from_slice(&file[..])?;
for entry in archive.entries_slice() {
    match entry? {
        ReadEntry::Solid(solid_entry) => {
            // fill your code
        }
        ReadEntry::Regular(entry) => {
            // fill your code
        }
    }
}
source

pub fn raw_entries_slice<'s>( &'s mut self, ) -> impl Iterator<Item = Result<impl Entry + Sized + 'd>> + 's

Returns an iterator over raw entries in the archive.

§Returns

An iterator over raw entries in the archive.

§Examples
use libpna::Archive;

let bytes = include_bytes!("../../../../resources/test/zstd.pna");
let mut src = Archive::read_header_from_slice(&bytes[..])?;
let mut dist = Archive::write_header(Vec::new())?;
for entry in src.raw_entries_slice() {
    dist.add_entry(entry?)?;
}
dist.finalize()?;
source

pub fn read_next_archive_from_slice( self, bytes: &[u8], ) -> Result<Archive<&[u8]>>

Reads the next archive from the provided reader and returns a new Archive.

§Arguments
  • bytes - The [&[u8]] to read from.
§Returns

A new Archive.

§Errors

Returns an error if an I/O error occurs while reading from the reader.

source§

impl<R: Read> Archive<R>

source

pub fn read_header(reader: R) -> Result<Self>

Reads the archive header from the provided reader and returns a new Archive.

§Arguments
  • reader - The Read object to read the header from.
§Returns

A new io::Result<Archive<W>>.

§Errors

Returns an error if an I/O error occurs while reading header from the reader.

source

pub fn raw_entries( &mut self, ) -> impl Iterator<Item = Result<impl Entry + Sized>> + '_

Returns an iterator over raw entries in the archive.

§Returns

An iterator over raw entries in the archive.

§Examples
use libpna::Archive;
use std::fs::File;

let mut src = Archive::read_header(File::open("foo.pna")?)?;
let mut dist = Archive::write_header(File::create("bar.pna")?)?;
for entry in src.raw_entries() {
    dist.add_entry(entry?)?;
}
dist.finalize()?;
source

pub fn entries_skip_solid( &mut self, ) -> impl Iterator<Item = Result<RegularEntry>> + '_

Returns an iterator over the entries in the archive, excluding entries in solid mode.

§Returns

An iterator over the entries in the archive.

source

pub fn entries(&mut self) -> Entries<'_, R>

Returns an iterator over the entries in the archive.

§Returns

An iterator over the entries in the archive.

§Example
use libpna::{Archive, ReadEntry};
use std::fs;

let file = fs::File::open("foo.pna")?;
let mut archive = Archive::read_header(file)?;
for entry in archive.entries() {
    match entry? {
        ReadEntry::Solid(solid_entry) => {
            // fill your code
        }
        ReadEntry::Regular(entry) => {
            // fill your code
        }
    }
}
source

pub fn entries_with_password<'a>( &'a mut self, password: Option<&'a str>, ) -> impl Iterator<Item = Result<RegularEntry>> + 'a

Returns an iterator over the entries in the archive, including entries in solid mode.

§Arguments
  • password - a password for solid mode entry.
§Returns

An iterator over the entries in the archive.

source

pub const fn next_archive(&self) -> bool

👎Deprecated since 0.16.0: Renamed to Archive::has_next_archive

Returns true if ANXT chunk is appeared before call this method calling.

§Returns

true if the next archive in the series is available, otherwise false.

source

pub fn read_next_archive<OR: Read>(self, reader: OR) -> Result<Archive<OR>>

Reads the next archive from the provided reader and returns a new Archive.

§Arguments
  • reader - The reader to read from.
§Returns

A new Archive.

§Errors

Returns an error if an I/O error occurs while reading from the reader.

source§

impl<R: Read + Seek> Archive<R>

source

pub fn seek_to_end(&mut self) -> Result<()>

Seek the cursor to the end of the archive marker.

§Examples

For appending entry to the existing archive.


let file = File::open("foo.pna")?;
let mut archive = Archive::read_header(file)?;
archive.seek_to_end()?;
archive.add_entry({
    let entry = EntryBuilder::new_dir("dir_entry".into());
    entry.build()?
})?;
archive.finalize()?;
source§

impl<W: Write> Archive<W>

source

pub fn write_header(write: W) -> Result<Self>

Writes the archive header to the given Write object and return a new Archive.

§Arguments
  • write - The Write object to write the header to.
§Returns

A new io::Result<Archive<W>>

§Errors

Returns an error if an I/O error occurs while writing header to the writer.

§Examples
use libpna::Archive;
use std::fs;

let file = fs::File::create("example.pna")?;
let mut archive = Archive::write_header(file)?;
archive.finalize()?;
source

pub fn write_file<F>( &mut self, name: EntryName, metadata: Metadata, option: WriteOptions, f: F, ) -> Result<()>
where F: FnMut(&mut EntryDataWriter<&mut W>) -> Result<()>,

Write regular file entry into archive.

§Example
use libpna::{Archive, Metadata, WriteOptions};
use std::fs;
use std::io::{self, prelude::*};

let file = fs::File::create("foo.pna")?;
let mut archive = Archive::write_header(file)?;
archive.write_file(
    "bar.txt".into(),
    Metadata::new(),
    WriteOptions::builder().build(),
    |writer| writer.write_all(b"text"),
)?;
archive.finalize()?;
source

pub fn add_entry(&mut self, entry: impl Entry) -> Result<usize>

Adds a new entry to the archive.

§Arguments
  • entry - The entry to add to the archive.
§Examples
use libpna::{Archive, EntryBuilder, WriteOptions};
use std::fs;

let file = fs::File::create("example.pna")?;
let mut archive = Archive::write_header(file)?;
archive.add_entry(
    EntryBuilder::new_file("example.txt".into(), WriteOptions::builder().build())?.build()?,
)?;
archive.finalize()?;
source

pub fn add_entry_part<T>(&mut self, entry_part: EntryPart<T>) -> Result<usize>
where RawChunk<T>: Chunk,

Adds a part of an entry to the archive.

§Arguments
  • entry_part - The part of an entry to add to the archive.
§Examples

let part1_file = File::create("example.part1.pna")?;
let mut archive_part1 = Archive::write_header(part1_file)?;
let entry =
    EntryBuilder::new_file("example.txt".into(), WriteOptions::builder().build())?.build()?;
archive_part1.add_entry_part(EntryPart::from(entry))?;

let part2_file = File::create("example.part2.pna")?;
let archive_part2 = archive_part1.split_to_next_archive(part2_file)?;
archive_part2.finalize()?;
source

pub fn split_to_next_archive<OW: Write>(self, writer: OW) -> Result<Archive<OW>>

Split to the next archive.

§Examples

let part1_file = File::create("example.part1.pna")?;
let mut archive_part1 = Archive::write_header(part1_file)?;
let entry =
    EntryBuilder::new_file("example.txt".into(), WriteOptions::builder().build())?.build()?;
archive_part1.add_entry_part(EntryPart::from(entry))?;

let part2_file = File::create("example.part2.pna")?;
let archive_part2 = archive_part1.split_to_next_archive(part2_file)?;
archive_part2.finalize()?;
source

pub fn finalize(self) -> Result<W>

Write an end marker to finalize the archive.

Marks that the PNA archive contains no more entries. Normally, a PNA archive reader will continue reading entries in the hope that the entry exists until it encounters this end marker. This end marker should always be recorded at the end of the file unless there is a special reason to do so.

§Examples

Create an empty archive.


let file = File::create("foo.pna")?;
let mut archive = Archive::write_header(file)?;
archive.finalize()?;
source§

impl<W: Write> Archive<W>

source

pub fn write_solid_header( write: W, option: WriteOptions, ) -> Result<SolidArchive<W>>

Writes the archive header to the given Write object and return a new SolidArchive.

§Arguments
  • write - The Write object to write the header to.
  • option - The WriteOptions object of a solid mode option.
§Returns

A new io::Result<SolidArchive<W>>

§Errors

Returns an error if an I/O error occurs while writing header to the writer.

§Examples
use libpna::{Archive, WriteOptions};
use std::fs::File;

let option = WriteOptions::builder().build();
let file = File::create("example.pna")?;
let mut archive = Archive::write_solid_header(file, option)?;
archive.finalize()?;
source§

impl<T> Archive<T>

source

pub const fn has_next_archive(&self) -> bool

Returns true if ANXT chunk is appeared before call this method calling.

§Returns

true if the next archive in the series is available, otherwise false.

Auto Trait Implementations§

§

impl<T> Freeze for Archive<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Archive<T>
where T: RefUnwindSafe,

§

impl<T> Send for Archive<T>
where T: Send,

§

impl<T> Sync for Archive<T>
where T: Sync,

§

impl<T> Unpin for Archive<T>
where T: Unpin,

§

impl<T> UnwindSafe for Archive<T>
where T: UnwindSafe,

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> 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.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V