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]>
impl<'d> Archive<&'d [u8]>
sourcepub fn read_header_from_slice(bytes: &'d [u8]) -> Result<Self>
pub fn read_header_from_slice(bytes: &'d [u8]) -> Result<Self>
sourcepub fn entries_slice<'a>(&'a mut self) -> Entries<'a, 'd>
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
}
}
}sourcepub fn raw_entries_slice<'s>(
&'s mut self,
) -> impl Iterator<Item = Result<impl Entry + Sized + 'd>> + 's
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§impl<R: Read> Archive<R>
impl<R: Read> Archive<R>
sourcepub fn read_header(reader: R) -> Result<Self>
pub fn read_header(reader: R) -> Result<Self>
sourcepub fn raw_entries(
&mut self,
) -> impl Iterator<Item = Result<impl Entry + Sized>> + '_
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()?;sourcepub fn entries_skip_solid(
&mut self,
) -> impl Iterator<Item = Result<RegularEntry>> + '_
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.
sourcepub fn entries(&mut self) -> Entries<'_, R>
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
}
}
}sourcepub fn entries_with_password<'a>(
&'a mut self,
password: Option<&'a str>,
) -> impl Iterator<Item = Result<RegularEntry>> + 'a
pub fn entries_with_password<'a>( &'a mut self, password: Option<&'a str>, ) -> impl Iterator<Item = Result<RegularEntry>> + 'a
sourcepub const fn next_archive(&self) -> bool
👎Deprecated since 0.16.0: Renamed to Archive::has_next_archive
pub const fn next_archive(&self) -> bool
Archive::has_next_archivesourcepub fn read_next_archive<OR: Read>(self, reader: OR) -> Result<Archive<OR>>
pub fn read_next_archive<OR: Read>(self, reader: OR) -> Result<Archive<OR>>
source§impl<R: Read + Seek> Archive<R>
impl<R: Read + Seek> Archive<R>
sourcepub fn seek_to_end(&mut self) -> Result<()>
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>
impl<W: Write> Archive<W>
sourcepub fn write_header(write: W) -> Result<Self>
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()?;sourcepub fn write_file<F>(
&mut self,
name: EntryName,
metadata: Metadata,
option: WriteOptions,
f: F,
) -> Result<()>
pub fn write_file<F>( &mut self, name: EntryName, metadata: Metadata, option: WriteOptions, f: F, ) -> 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()?;sourcepub fn add_entry(&mut self, entry: impl Entry) -> Result<usize>
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()?;sourcepub fn add_entry_part<T>(&mut self, entry_part: EntryPart<T>) -> Result<usize>
pub fn add_entry_part<T>(&mut self, entry_part: EntryPart<T>) -> Result<usize>
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()?;sourcepub fn split_to_next_archive<OW: Write>(self, writer: OW) -> Result<Archive<OW>>
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()?;sourcepub fn finalize(self) -> Result<W>
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>
impl<W: Write> Archive<W>
sourcepub fn write_solid_header(
write: W,
option: WriteOptions,
) -> Result<SolidArchive<W>>
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()?;