feat(libarchive): support adding any reader

main
Jef Roosens 2023-07-14 13:04:03 +02:00
parent 00a0a62ab0
commit 443a03c8f6
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
1 changed files with 11 additions and 7 deletions

View File

@ -31,11 +31,7 @@ impl FileWriter {
} }
} }
pub fn append_path<P: AsRef<Path>>( pub fn append_data<R: Read>(&mut self, entry: &mut WriteEntry, r: &mut R) -> crate::Result<()> {
&mut self,
entry: &mut WriteEntry,
path: P,
) -> crate::Result<()> {
unsafe { unsafe {
match ffi::archive_write_header(self.handle_mut(), entry.entry_mut()) { match ffi::archive_write_header(self.handle_mut(), entry.entry_mut()) {
ffi::ARCHIVE_OK => (), ffi::ARCHIVE_OK => (),
@ -43,11 +39,10 @@ impl FileWriter {
} }
} }
let mut f = fs::File::open(path)?;
let mut buf = [0; 8192]; let mut buf = [0; 8192];
loop { loop {
match f.read(&mut buf) { match r.read(&mut buf) {
Ok(0) => return Ok(()), Ok(0) => return Ok(()),
Ok(written) => unsafe { Ok(written) => unsafe {
match ffi::archive_write_data( match ffi::archive_write_data(
@ -68,6 +63,15 @@ impl FileWriter {
} }
} }
pub fn append_path<P: AsRef<Path>>(
&mut self,
entry: &mut WriteEntry,
path: P,
) -> crate::Result<()> {
let mut f = fs::File::open(path)?;
self.append_data(entry, &mut f)
}
pub fn close(&mut self) -> crate::Result<()> { pub fn close(&mut self) -> crate::Result<()> {
unsafe { unsafe {
match ffi::archive_write_close(self.handle_mut()) { match ffi::archive_write_close(self.handle_mut()) {