Merge pull request #101 from dnaka91/tar-short-ext

Add support for short tar archive extensions
This commit is contained in:
João Marcos Bezerra 2021-10-17 18:28:54 -03:00 committed by GitHub
commit a424a2a017
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 12 deletions

View File

@ -246,10 +246,24 @@ fn compress_files(
writer.flush()?; writer.flush()?;
} }
Tgz => { Tgz => {
// Wrap it into an gz_decoder, and pass to the tar archive builder let encoder = flate2::write::GzEncoder::new(writer, Default::default());
let gz_decoder = flate2::write::GzEncoder::new(writer, Default::default()); let writer = archive::tar::build_archive_from_paths(&files, encoder)?;
let mut writer = archive::tar::build_archive_from_paths(&files, gz_decoder)?; writer.finish()?.flush()?;
writer.flush()?; }
Tbz => {
let encoder = bzip2::write::BzEncoder::new(writer, Default::default());
let writer = archive::tar::build_archive_from_paths(&files, encoder)?;
writer.finish()?.flush()?;
}
Tlzma => {
let encoder = xz2::write::XzEncoder::new(writer, 6);
let writer = archive::tar::build_archive_from_paths(&files, encoder)?;
writer.finish()?.flush()?;
}
Tzst => {
let encoder = zstd::stream::write::Encoder::new(writer, Default::default())?;
let writer = archive::tar::build_archive_from_paths(&files, encoder)?;
writer.finish()?.flush()?;
} }
Zip => { Zip => {
eprintln!("{yellow}Warning:{reset}", yellow = *colors::YELLOW, reset = *colors::RESET); eprintln!("{yellow}Warning:{reset}", yellow = *colors::YELLOW, reset = *colors::RESET);
@ -349,6 +363,21 @@ fn decompress_file(
let _ = crate::archive::tar::unpack_archive(reader, output_folder, flags)?; let _ = crate::archive::tar::unpack_archive(reader, output_folder, flags)?;
info!("Successfully uncompressed archive in '{}'.", to_utf(output_folder)); info!("Successfully uncompressed archive in '{}'.", to_utf(output_folder));
} }
Tbz => {
let reader = chain_reader_decoder(&Bzip, reader)?;
let _ = crate::archive::tar::unpack_archive(reader, output_folder, flags)?;
info!("Successfully uncompressed archive in '{}'.", to_utf(output_folder));
}
Tlzma => {
let reader = chain_reader_decoder(&Lzma, reader)?;
let _ = crate::archive::tar::unpack_archive(reader, output_folder, flags)?;
info!("Successfully uncompressed archive in '{}'.", to_utf(output_folder));
}
Tzst => {
let reader = chain_reader_decoder(&Zstd, reader)?;
let _ = crate::archive::tar::unpack_archive(reader, output_folder, flags)?;
info!("Successfully uncompressed archive in '{}'.", to_utf(output_folder));
}
Zip => { Zip => {
eprintln!("Compressing first into .zip."); eprintln!("Compressing first into .zip.");
eprintln!("Warning: .zip archives with extra extensions have a downside."); eprintln!("Warning: .zip archives with extra extensions have a downside.");

View File

@ -12,6 +12,9 @@ pub enum CompressionFormat {
Lzma, // .lzma Lzma, // .lzma
Tar, // .tar (technically not a compression extension, but will do for now) Tar, // .tar (technically not a compression extension, but will do for now)
Tgz, // .tgz Tgz, // .tgz
Tbz, // .tbz
Tlzma, // .tlzma
Tzst, // .tzst
Zstd, // .zst Zstd, // .zst
Zip, // .zip Zip, // .zip
} }
@ -28,6 +31,9 @@ impl fmt::Display for CompressionFormat {
Lzma => ".lz", Lzma => ".lz",
Tar => ".tar", Tar => ".tar",
Tgz => ".tgz", Tgz => ".tgz",
Tbz => ".tbz",
Tlzma => ".tlz",
Tzst => ".tzst",
Zip => ".zip", Zip => ".zip",
} }
) )
@ -50,6 +56,9 @@ pub fn separate_known_extensions_from_name(mut path: &Path) -> (&Path, Vec<Compr
extensions.push(match extension { extensions.push(match extension {
"tar" => Tar, "tar" => Tar,
"tgz" => Tgz, "tgz" => Tgz,
"tbz" | "tbz2" => Tbz,
"txz" | "tlz" | "tlzma" => Tlzma,
"tzst" => Tzst,
"zip" => Zip, "zip" => Zip,
"bz" | "bz2" => Bzip, "bz" | "bz2" => Bzip,
"gz" => Gzip, "gz" => Gzip,

View File

@ -27,7 +27,10 @@ fn sanity_check_through_mime() {
let bytes = generate_random_file_content(&mut SmallRng::from_entropy()); let bytes = generate_random_file_content(&mut SmallRng::from_entropy());
test_file.write_all(&bytes).expect("to successfully write bytes to the file"); test_file.write_all(&bytes).expect("to successfully write bytes to the file");
let formats = ["tar", "zip", "tar.gz", "tgz", "tar.bz", "tar.bz2", "tar.lzma", "tar.xz", "tar.zst"]; let formats = [
"tar", "zip", "tar.gz", "tgz", "tbz", "tbz2", "txz", "tlz", "tlzma", "tzst", "tar.bz", "tar.bz2", "tar.lzma",
"tar.xz", "tar.zst",
];
let expected_mimes = [ let expected_mimes = [
"application/x-tar", "application/x-tar",
@ -38,6 +41,12 @@ fn sanity_check_through_mime() {
"application/x-bzip2", "application/x-bzip2",
"application/x-xz", "application/x-xz",
"application/x-xz", "application/x-xz",
"application/x-xz",
"application/zstd",
"application/x-bzip2",
"application/x-bzip2",
"application/x-xz",
"application/x-xz",
"application/zstd", "application/zstd",
]; ];
@ -69,6 +78,12 @@ fn test_each_format() {
test_compressing_and_decompressing_archive("tar.lzma"); test_compressing_and_decompressing_archive("tar.lzma");
test_compressing_and_decompressing_archive("tar.zst"); test_compressing_and_decompressing_archive("tar.zst");
test_compressing_and_decompressing_archive("tgz"); test_compressing_and_decompressing_archive("tgz");
test_compressing_and_decompressing_archive("tbz");
test_compressing_and_decompressing_archive("tbz2");
test_compressing_and_decompressing_archive("txz");
test_compressing_and_decompressing_archive("tlz");
test_compressing_and_decompressing_archive("tlzma");
test_compressing_and_decompressing_archive("tzst");
test_compressing_and_decompressing_archive("zip"); test_compressing_and_decompressing_archive("zip");
test_compressing_and_decompressing_archive("zip.gz"); test_compressing_and_decompressing_archive("zip.gz");
test_compressing_and_decompressing_archive("zip.bz"); test_compressing_and_decompressing_archive("zip.bz");