#![forbid(unsafe_code)]
extern crate alloc;
use std::borrow::Cow;
use libarchive_oxide_core::filter::FilterId;
use libarchive_oxide_core::{decode_to_vec, decode_to_vec_capped, Error, Result};
pub mod create;
pub mod extract;
pub mod filter;
pub mod path;
#[cfg(feature = "sevenz")]
pub mod sevenz;
pub mod zip;
pub use create::{build_archive, build_archive_with, build_cpio, build_tar, CreateOptions};
pub use extract::{reader, reader_with_password, Stats};
pub use libarchive_oxide_core;
pub use path::sanitize;
pub use zip::{SaltSource, ZipMethod, ZipOptions};
pub fn decompress(bytes: &[u8]) -> Result<Cow<'_, [u8]>> {
decompress_capped(bytes, usize::MAX)
}
pub fn decompress_capped(bytes: &[u8], max_output: usize) -> Result<Cow<'_, [u8]>> {
match FilterId::sniff(bytes) {
Some(id) => {
let mut decoder =
crate::filter::decoder(id).ok_or(Error::Unsupported("filter not built in"))?;
let plain = decode_to_vec_capped(&mut decoder, bytes, max_output)?;
Ok(Cow::Owned(plain))
},
None => Ok(Cow::Borrowed(bytes)),
}
}
pub fn compress(plain: &[u8], id: FilterId) -> Result<Vec<u8>> {
let mut encoder =
crate::filter::encoder(id).ok_or(Error::Unsupported("filter not built in"))?;
decode_to_vec(&mut encoder, plain)
}
#[must_use]
pub fn filter_for_name(name: &str) -> Option<FilterId> {
let ext = std::path::Path::new(name)
.extension()
.and_then(|e| e.to_str())
.map(str::to_ascii_lowercase);
match ext.as_deref() {
Some("gz" | "tgz") => Some(FilterId::Gzip),
Some("zst") => Some(FilterId::Zstd),
Some("xz") => Some(FilterId::Xz),
Some("lz4") => Some(FilterId::Lz4),
_ => None,
}
}