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 an 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()?;Reads 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};
use std::fs;
let file = fs::read("foo.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::Normal(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;
use std::fs;
let bytes = fs::read("foo.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<NormalEntry>> + '_
pub fn entries_skip_solid( &mut self, ) -> impl Iterator<Item = Result<NormalEntry>> + '_
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_with_password<'a>(
&'a mut self,
password: Option<&'a str>,
) -> impl Iterator<Item = Result<NormalEntry>> + 'a
pub fn entries_with_password<'a>( &'a mut self, password: Option<&'a str>, ) -> impl Iterator<Item = Result<NormalEntry>> + 'a
Sourcepub 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> Archive<R>
impl<R> Archive<R>
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) => todo!("fill your code"),
ReadEntry::Normal(entry) => todo!("fill your code"),
}
}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.
§Errors
Returns an error if this function failed to seek or contains broken chunk.
§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: impl WriteOption,
f: F,
) -> Result<()>
pub fn write_file<F>( &mut self, name: EntryName, metadata: Metadata, option: impl WriteOption, f: F, ) -> Result<()>
Write a regular file as normal 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()?;§Errors
Returns an error if an I/O error occurs while writing the entry.
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()?;§Errors
Returns an error if an I/O error occurs while writing the entry part.
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()?;§Errors
Returns an error if an I/O error occurs while splitting to the next archive.
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.
§Errors
Returns an error if failed to write archive end marker.
§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: impl WriteOption,
) -> Result<SolidArchive<W>>
pub fn write_solid_header( write: W, option: impl WriteOption, ) -> 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()?;