diff options
author | Karol Kosek <krkk@serenityos.org> | 2022-12-25 16:14:05 +0100 |
---|---|---|
committer | Ali Mohammad Pur <Ali.mpfard@gmail.com> | 2022-12-27 07:24:07 +0330 |
commit | 012645bdf954229fbc4fc9c0b2c6f290cc1330fc (patch) | |
tree | 56334bc25222fe754010777c63e3620b5f0c388b /Userland | |
parent | 8932b28b8a128318a1308bb5bb028d2cba690bd0 (diff) | |
download | serenity-012645bdf954229fbc4fc9c0b2c6f290cc1330fc.zip |
Utilities/zip: Read files using Core::Stream::File
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Utilities/zip.cpp | 26 |
1 files changed, 14 insertions, 12 deletions
diff --git a/Userland/Utilities/zip.cpp b/Userland/Utilities/zip.cpp index 28edc7e471..5caebca2de 100644 --- a/Userland/Utilities/zip.cpp +++ b/Userland/Utilities/zip.cpp @@ -11,6 +11,7 @@ #include <LibCore/DirIterator.h> #include <LibCore/File.h> #include <LibCore/FileStream.h> +#include <LibCore/Stream.h> #include <LibCore/System.h> #include <LibCrypto/Checksum/CRC32.h> @@ -53,15 +54,10 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) Archive::ZipOutputStream zip_stream { file_stream }; - auto add_file = [&](DeprecatedString path) { - auto file = Core::File::construct(path); - if (!file->open(Core::OpenMode::ReadOnly)) { - warnln("Failed to open {}: {}", path, file->error_string()); - return; - } - + auto add_file = [&](DeprecatedString path) -> ErrorOr<void> { auto canonicalized_path = LexicalPath::canonicalized_path(path); - auto file_buffer = file->read_all(); + auto file = TRY(Core::Stream::File::open(path, Core::Stream::OpenMode::Read)); + auto file_buffer = TRY(file->read_until_eof()); Archive::ZipMember member {}; member.name = canonicalized_path; @@ -81,6 +77,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) member.crc32 = checksum.digest(); member.is_directory = false; zip_stream.add_member(member); + return {}; }; auto add_directory = [&](DeprecatedString path, auto handle_directory) -> void { @@ -103,10 +100,13 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) auto child_path = it.next_full_path(); if (Core::File::is_link(child_path)) return; - if (!Core::File::is_directory(child_path)) - add_file(child_path); - else + if (!Core::File::is_directory(child_path)) { + auto result = add_file(child_path); + if (result.is_error()) + warnln("Couldn't add file '{}': {}", child_path, result.error()); + } else { handle_directory(child_path, handle_directory); + } } }; @@ -114,7 +114,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) if (Core::File::is_directory(source_path)) { add_directory(source_path, add_directory); } else { - add_file(source_path); + auto result = add_file(source_path); + if (result.is_error()) + warnln("Couldn't add file '{}': {}", source_path, result.error()); } } |