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

Extension: add support for tgz #85

Merged
merged 2 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ For compiling, check [the wiki guide](https://github.com/ouch-org/ouch/wiki/Comp

## Supported formats

| | .tar | .zip | .bz, .bz2 | .gz | .xz, .lz, .lzma | .zst |
| | .tar, .tgz | .zip | .bz, .bz2 | .gz | .xz, .lz, .lzma | .zst |
|:-------------:|:----:|:----:|:---------:| --- |:---------------:| --- |
| Decompression | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
| Compression | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
Expand Down
34 changes: 28 additions & 6 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,24 @@ fn compress_files(
let file_writer = BufWriter::with_capacity(BUFFER_CAPACITY, output_file);

if formats.len() == 1 {
let build_archive_from_paths = match formats[0] {
Tar => archive::tar::build_archive_from_paths,
Zip => archive::zip::build_archive_from_paths,
match formats[0] {
Tar => {
let mut bufwriter = archive::tar::build_archive_from_paths(&files, file_writer)?;
bufwriter.flush()?;
}
Tgz => {
let mut bufwriter = archive::tar::build_archive_from_paths(
&files,
flate2::write::GzEncoder::new(file_writer, Default::default()),
)?;
bufwriter.flush()?;
}
Zip => {
let mut bufwriter = archive::zip::build_archive_from_paths(&files, file_writer)?;
bufwriter.flush()?;
}
_ => unreachable!(),
};

let mut bufwriter = build_archive_from_paths(&files, file_writer)?;
bufwriter.flush()?;
} else {
let mut writer: Box<dyn Write> = Box::new(file_writer);

Expand Down Expand Up @@ -213,6 +223,13 @@ fn compress_files(
let mut writer = archive::tar::build_archive_from_paths(&files, writer)?;
writer.flush()?;
}
Tgz => {
let mut writer = archive::tar::build_archive_from_paths(
&files,
flate2::write::GzEncoder::new(writer, Default::default()),
)?;
writer.flush()?;
}
Zip => {
eprintln!("{yellow}Warning:{reset}", yellow = colors::yellow(), reset = colors::reset());
eprintln!("\tCompressing .zip entirely in memory.");
Expand Down Expand Up @@ -305,6 +322,11 @@ fn decompress_file(
let _ = crate::archive::tar::unpack_archive(reader, output_folder, flags)?;
info!("Successfully uncompressed archive in '{}'.", to_utf(output_folder));
}
Tgz => {
utils::create_dir_if_non_existent(output_folder)?;
let _ = crate::archive::tar::unpack_archive(chain_reader_decoder(&Gzip, reader)?, output_folder, flags)?;
info!("Successfully uncompressed archive in '{}'.", to_utf(output_folder));
}
Zip => {
utils::create_dir_if_non_existent(output_folder)?;

Expand Down
25 changes: 13 additions & 12 deletions src/extension.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Our representation of all the supported compression formats.

use std::{fmt, path::Path};
use std::{ffi::OsStr, fmt, path::Path};

use self::CompressionFormat::*;

Expand All @@ -11,6 +11,7 @@ pub enum CompressionFormat {
Bzip, // .bz
Lzma, // .lzma
Tar, // .tar (technically not a compression extension, but will do for now)
Tgz, // .tgz
Zstd, // .zst
Zip, // .zip
}
Expand All @@ -26,6 +27,7 @@ impl fmt::Display for CompressionFormat {
Zstd => ".zst",
Lzma => ".lz",
Tar => ".tar",
Tgz => ".tgz",
Zip => ".zip",
}
)
Expand All @@ -44,18 +46,17 @@ pub fn separate_known_extensions_from_name(mut path: &Path) -> (&Path, Vec<Compr
let mut extensions = vec![];

// While there is known extensions at the tail, grab them
while let Some(extension) = path.extension() {
let extension = match () {
_ if extension == "tar" => Tar,
_ if extension == "zip" => Zip,
_ if extension == "bz" || extension == "bz2" => Bzip,
_ if extension == "gz" => Gzip,
_ if extension == "xz" || extension == "lzma" || extension == "lz" => Lzma,
_ if extension == "zst" => Zstd,
while let Some(extension) = path.extension().and_then(OsStr::to_str) {
extensions.push(match extension {
"tar" => Tar,
"tgz" => Tgz,
"zip" => Zip,
"bz" | "bz2" => Bzip,
"gz" => Gzip,
"xz" | "lzma" | "lz" => Lzma,
"zst" => Zstd,
_ => break,
};

extensions.push(extension);
});

// Update for the next iteration
path = if let Some(stem) = path.file_stem() { Path::new(stem) } else { Path::new("") };
Expand Down
4 changes: 3 additions & 1 deletion tests/compress_and_decompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ 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", "tar.bz", "tar.bz2", "tar.lzma", "tar.xz", "tar.zst"];
let formats = ["tar", "zip", "tar.gz", "tgz", "tar.bz", "tar.bz2", "tar.lzma", "tar.xz", "tar.zst"];

let expected_mimes = [
"application/x-tar",
"application/zip",
"application/gzip",
"application/gzip",
"application/x-bzip2",
"application/x-bzip2",
"application/x-xz",
Expand Down Expand Up @@ -67,6 +68,7 @@ fn test_each_format() {
test_compressing_and_decompressing_archive("tar.lz");
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("zip");
test_compressing_and_decompressing_archive("zip.gz");
test_compressing_and_decompressing_archive("zip.bz");
Expand Down