use crate::builder::Pf8Builder;
use crate::callbacks::ArchiveHandler;
use crate::error::Result;
use crate::reader::Pf8Reader;
use std::ops::{Deref, DerefMut};
use std::path::Path;
pub struct Pf8Archive {
reader: Pf8Reader,
}
impl Pf8Archive {
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
let reader = Pf8Reader::open(path)?;
Ok(Self { reader })
}
pub fn builder() -> Pf8Builder {
Pf8Builder::new()
}
pub fn extract_file<P: AsRef<Path>, Q: AsRef<Path>>(
&mut self,
archive_path: P,
output_path: Q,
) -> Result<()> {
if let Some(parent) = output_path.as_ref().parent() {
std::fs::create_dir_all(parent)?;
}
use std::fs::File;
use std::io::Write;
let mut output_file = File::create(output_path)?;
self.reader.read_file_streaming(archive_path, |chunk| {
output_file.write_all(chunk)?;
Ok(())
})?;
Ok(())
}
pub fn extract_file_with_progress<P: AsRef<Path>, Q: AsRef<Path>, H: ArchiveHandler>(
&mut self,
archive_path: P,
output_path: Q,
handler: &mut H,
) -> Result<()> {
self.reader
.extract_file_with_progress(archive_path, output_path, handler)
}
pub fn reader(&self) -> &Pf8Reader {
&self.reader
}
pub fn reader_mut(&mut self) -> &mut Pf8Reader {
&mut self.reader
}
}
impl Deref for Pf8Archive {
type Target = Pf8Reader;
fn deref(&self) -> &Self::Target {
&self.reader
}
}
impl DerefMut for Pf8Archive {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.reader
}
}
pub fn extract<P: AsRef<Path>, Q: AsRef<Path>>(archive_path: P, output_dir: Q) -> Result<()> {
let mut archive = Pf8Archive::open(archive_path)?;
archive.extract_all(output_dir)
}
pub fn extract_with_progress<P: AsRef<Path>, Q: AsRef<Path>, H: ArchiveHandler>(
archive_path: P,
output_dir: Q,
handler: &mut H,
) -> Result<()> {
let mut archive = Pf8Archive::open(archive_path)?;
archive.extract_all_with_progress(output_dir, handler)
}
pub fn create_from_dir<P: AsRef<Path>, Q: AsRef<Path>>(input_dir: P, output_path: Q) -> Result<()> {
let mut builder = Pf8Builder::new();
builder.add_dir(input_dir)?;
builder.write_to_file(output_path)
}
pub fn create_from_dir_with_progress<P: AsRef<Path>, Q: AsRef<Path>, H: ArchiveHandler>(
input_dir: P,
output_path: Q,
handler: &mut H,
) -> Result<()> {
let mut builder = Pf8Builder::new();
builder.add_dir(input_dir)?;
builder.write_to_file_with_progress(output_path, handler)
}