Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for short tar archive extensions #101

Merged
merged 1 commit into from
Oct 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add support for short tar archive extensions
  • Loading branch information
dnaka91 committed Oct 17, 2021
commit 06af320595a29a9876921962f5b77dabcec8b074
37 changes: 33 additions & 4 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,24 @@ fn compress_files(
writer.flush()?;
}
Tgz => {
// Wrap it into an gz_decoder, and pass to the tar archive builder
let gz_decoder = flate2::write::GzEncoder::new(writer, Default::default());
let mut writer = archive::tar::build_archive_from_paths(&files, gz_decoder)?;
writer.flush()?;
let encoder = flate2::write::GzEncoder::new(writer, Default::default());
let writer = archive::tar::build_archive_from_paths(&files, encoder)?;
writer.finish()?.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 => {
eprintln!("{yellow}Warning:{reset}", yellow = *colors::YELLOW, reset = *colors::RESET);
Expand Down Expand Up @@ -349,6 +363,21 @@ fn decompress_file(
let _ = crate::archive::tar::unpack_archive(reader, output_folder, flags)?;
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 => {
eprintln!("Compressing first into .zip.");
eprintln!("Warning: .zip archives with extra extensions have a downside.");
Expand Down
23 changes: 16 additions & 7 deletions src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ use self::CompressionFormat::*;
#[derive(Clone, PartialEq, Eq, Debug)]
/// Accepted extensions for input and output
pub enum CompressionFormat {
Gzip, // .gz
Bzip, // .bz
Lzma, // .lzma
Tar, // .tar (technically not a compression extension, but will do for now)
Tgz, // .tgz
Zstd, // .zst
Zip, // .zip
Gzip, // .gz
Bzip, // .bz
Lzma, // .lzma
Tar, // .tar (technically not a compression extension, but will do for now)
Tgz, // .tgz
Tbz, // .tbz
Tlzma, // .tlzma
Tzst, // .tzst
Zstd, // .zst
Zip, // .zip
}

impl fmt::Display for CompressionFormat {
Expand All @@ -28,6 +31,9 @@ impl fmt::Display for CompressionFormat {
Lzma => ".lz",
Tar => ".tar",
Tgz => ".tgz",
Tbz => ".tbz",
Tlzma => ".tlz",
Tzst => ".tzst",
Zip => ".zip",
}
)
Expand All @@ -50,6 +56,9 @@ pub fn separate_known_extensions_from_name(mut path: &Path) -> (&Path, Vec<Compr
extensions.push(match extension {
"tar" => Tar,
"tgz" => Tgz,
"tbz" | "tbz2" => Tbz,
"txz" | "tlz" | "tlzma" => Tlzma,
"tzst" => Tzst,
"zip" => Zip,
"bz" | "bz2" => Bzip,
"gz" => Gzip,
Expand Down
17 changes: 16 additions & 1 deletion tests/compress_and_decompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ fn sanity_check_through_mime() {
let bytes = generate_random_file_content(&mut SmallRng::from_entropy());
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 = [
"application/x-tar",
Expand All @@ -38,6 +41,12 @@ fn sanity_check_through_mime() {
"application/x-bzip2",
"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",
];

Expand Down Expand Up @@ -69,6 +78,12 @@ fn test_each_format() {
test_compressing_and_decompressing_archive("tar.lzma");
test_compressing_and_decompressing_archive("tar.zst");
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.gz");
test_compressing_and_decompressing_archive("zip.bz");
Expand Down