diff --git a/Base/usr/share/man/man1/unzip.md b/Base/usr/share/man/man1/unzip.md index 261d659ace8..178283231fa 100644 --- a/Base/usr/share/man/man1/unzip.md +++ b/Base/usr/share/man/man1/unzip.md @@ -5,7 +5,7 @@ unzip - extract files from a ZIP archive ## Synopsis ```**sh -$ unzip file.zip +$ unzip file.zip [files...] ``` ## Description @@ -14,6 +14,8 @@ unzip will extract files from a zip archive to the current directory. The program is compatible with the PKZIP file format specification. +The optional [files] argument can be used to only extract specific files within the archive (using wildcards) during the unzip process. A `_` can be used as a single-character wildcard, and `*` can be used as a variable-length wildcard. + ## Examples ```sh @@ -23,3 +25,10 @@ Archive: archive.zip extracting: file1.txt extracting: file2.png ``` + +```sh +# Unzip select files from archive.zip, according to a filter +$ unzip archive.zip "*.tx_" +Archive: archive.unzip + extracting: file1.txt +``` diff --git a/Userland/Utilities/unzip.cpp b/Userland/Utilities/unzip.cpp index c45a4530ebd..3713c1151a1 100644 --- a/Userland/Utilities/unzip.cpp +++ b/Userland/Utilities/unzip.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -78,12 +79,14 @@ ErrorOr serenity_main(Main::Arguments arguments) int map_size_limit = 32 * MiB; bool quiet { false }; String output_directory_path; + Vector file_filters; Core::ArgsParser args_parser; args_parser.add_option(map_size_limit, "Maximum chunk size to map", "map-size-limit", 0, "size"); args_parser.add_option(output_directory_path, "Directory to receive the archive content", "output-directory", 'd', "path"); args_parser.add_option(quiet, "Be less verbose", "quiet", 'q'); args_parser.add_positional_argument(path, "File to unzip", "path", Core::ArgsParser::Required::Yes); + args_parser.add_positional_argument(file_filters, "Files or filters in the archive to extract", "files", Core::ArgsParser::Required::No); args_parser.parse(arguments); String zip_file_path { path }; @@ -124,7 +127,27 @@ ErrorOr serenity_main(Main::Arguments arguments) } auto success = zip_file->for_each_member([&](auto zip_member) { - return unpack_zip_member(zip_member, quiet) ? IterationDecision::Continue : IterationDecision::Break; + bool keep_file = false; + + if (!file_filters.is_empty()) { + for (auto& filter : file_filters) { + // Convert underscore wildcards (usual unzip convention) to question marks (as used by StringUtils) + auto string_filter = filter.replace("_", "?", true); + if (zip_member.name.matches(string_filter, CaseSensitivity::CaseSensitive)) { + keep_file = true; + break; + } + } + } else { + keep_file = true; + } + + if (keep_file) { + if (!unpack_zip_member(zip_member, quiet)) + return IterationDecision::Break; + } + + return IterationDecision::Continue; }); return success ? 0 : 1;