tar: Automatically recognize gzip archives

This commit is contained in:
Tim Schumacher 2022-02-17 23:35:11 +01:00 committed by Andreas Kling
parent 4487e515f7
commit b689e8b7f4
Notes: sideshowbarker 2024-07-17 18:36:27 +09:00
2 changed files with 12 additions and 4 deletions

View File

@ -5,7 +5,7 @@ tar - file archiving utility
## Synopsis ## Synopsis
```**sh ```**sh
$ tar [--create] [--extract] [--list] [--verbose] [--gzip] [--directory DIRECTORY] [--file FILE] [PATHS...] $ tar [--create] [--extract] [--list] [--verbose] [--gzip] [--no-auto-compress] [--directory DIRECTORY] [--file FILE] [PATHS...]
``` ```
## Description ## Description
@ -22,6 +22,7 @@ Files may also be compressed and decompressed using GNU Zip (GZIP) compression.
* `-t`, `--list`: List contents * `-t`, `--list`: List contents
* `-v`, `--verbose`: Print paths * `-v`, `--verbose`: Print paths
* `-z`, `--gzip`: Compress or decompress file using gzip * `-z`, `--gzip`: Compress or decompress file using gzip
* `--no-auto-compress`: Do not use the archive suffix to select the compression algorithm
* `-C DIRECTORY`, `--directory DIRECTORY`: Directory to extract to/create from * `-C DIRECTORY`, `--directory DIRECTORY`: Directory to extract to/create from
* `-f FILE`, `--file FILE`: Archive file * `-f FILE`, `--file FILE`: Archive file

View File

@ -28,7 +28,8 @@ int main(int argc, char** argv)
bool list = false; bool list = false;
bool verbose = false; bool verbose = false;
bool gzip = false; bool gzip = false;
const char* archive_file = nullptr; bool no_auto_compress = false;
StringView archive_file;
const char* directory = nullptr; const char* directory = nullptr;
Vector<const char*> paths; Vector<const char*> paths;
@ -38,6 +39,7 @@ int main(int argc, char** argv)
args_parser.add_option(list, "List contents", "list", 't'); args_parser.add_option(list, "List contents", "list", 't');
args_parser.add_option(verbose, "Print paths", "verbose", 'v'); args_parser.add_option(verbose, "Print paths", "verbose", 'v');
args_parser.add_option(gzip, "Compress or decompress file using gzip", "gzip", 'z'); args_parser.add_option(gzip, "Compress or decompress file using gzip", "gzip", 'z');
args_parser.add_option(no_auto_compress, "Do not use the archive suffix to select the compression algorithm", "no-auto-compress", 0);
args_parser.add_option(directory, "Directory to extract to/create from", "directory", 'C', "DIRECTORY"); args_parser.add_option(directory, "Directory to extract to/create from", "directory", 'C', "DIRECTORY");
args_parser.add_option(archive_file, "Archive file", "file", 'f', "FILE"); args_parser.add_option(archive_file, "Archive file", "file", 'f', "FILE");
args_parser.add_positional_argument(paths, "Paths", "PATHS", Core::ArgsParser::Required::No); args_parser.add_positional_argument(paths, "Paths", "PATHS", Core::ArgsParser::Required::No);
@ -48,10 +50,15 @@ int main(int argc, char** argv)
return 1; return 1;
} }
if (!no_auto_compress && !archive_file.is_empty()) {
if (archive_file.ends_with(".gz"sv) || archive_file.ends_with(".tgz"sv))
gzip = true;
}
if (list || extract) { if (list || extract) {
auto file = Core::File::standard_input(); auto file = Core::File::standard_input();
if (archive_file) { if (!archive_file.is_empty()) {
auto maybe_file = Core::File::open(archive_file, Core::OpenMode::ReadOnly); auto maybe_file = Core::File::open(archive_file, Core::OpenMode::ReadOnly);
if (maybe_file.is_error()) { if (maybe_file.is_error()) {
warnln("Core::File::open: {}", maybe_file.error()); warnln("Core::File::open: {}", maybe_file.error());
@ -160,7 +167,7 @@ int main(int argc, char** argv)
auto file = Core::File::standard_output(); auto file = Core::File::standard_output();
if (archive_file) { if (!archive_file.is_empty()) {
auto maybe_file = Core::File::open(archive_file, Core::OpenMode::WriteOnly); auto maybe_file = Core::File::open(archive_file, Core::OpenMode::WriteOnly);
if (maybe_file.is_error()) { if (maybe_file.is_error()) {
warnln("Core::File::open: {}", maybe_file.error()); warnln("Core::File::open: {}", maybe_file.error());