zip.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/LexicalPath.h>
  7. #include <LibArchive/Zip.h>
  8. #include <LibCompress/Deflate.h>
  9. #include <LibCore/ArgsParser.h>
  10. #include <LibCore/DirIterator.h>
  11. #include <LibCore/File.h>
  12. #include <LibCore/Stream.h>
  13. #include <LibCore/System.h>
  14. #include <LibCrypto/Checksum/CRC32.h>
  15. ErrorOr<int> serenity_main(Main::Arguments arguments)
  16. {
  17. StringView zip_path;
  18. Vector<StringView> source_paths;
  19. bool recurse = false;
  20. bool force = false;
  21. Core::ArgsParser args_parser;
  22. args_parser.add_positional_argument(zip_path, "Zip file path", "zipfile", Core::ArgsParser::Required::Yes);
  23. args_parser.add_positional_argument(source_paths, "Input files to be archived", "files", Core::ArgsParser::Required::Yes);
  24. args_parser.add_option(recurse, "Travel the directory structure recursively", "recurse-paths", 'r');
  25. args_parser.add_option(force, "Overwrite existing zip file", "force", 'f');
  26. args_parser.parse(arguments);
  27. TRY(Core::System::pledge("stdio rpath wpath cpath"));
  28. auto cwd = TRY(Core::System::getcwd());
  29. TRY(Core::System::unveil(LexicalPath::absolute_path(cwd, zip_path), "wc"sv));
  30. for (auto const& source_path : source_paths) {
  31. TRY(Core::System::unveil(LexicalPath::absolute_path(cwd, source_path), "r"sv));
  32. }
  33. TRY(Core::System::unveil(nullptr, nullptr));
  34. DeprecatedString zip_file_path { zip_path };
  35. if (Core::File::exists(zip_file_path)) {
  36. if (force) {
  37. outln("{} already exists, overwriting...", zip_file_path);
  38. } else {
  39. warnln("{} already exists, aborting!", zip_file_path);
  40. return 1;
  41. }
  42. }
  43. outln("Archive: {}", zip_file_path);
  44. auto file_stream = TRY(Core::Stream::File::open(zip_file_path, Core::Stream::OpenMode::Write));
  45. Archive::ZipOutputStream zip_stream(move(file_stream));
  46. auto add_file = [&](DeprecatedString path) -> ErrorOr<void> {
  47. auto canonicalized_path = LexicalPath::canonicalized_path(path);
  48. auto file = TRY(Core::Stream::File::open(path, Core::Stream::OpenMode::Read));
  49. auto file_buffer = TRY(file->read_until_eof());
  50. Archive::ZipMember member {};
  51. member.name = TRY(String::from_deprecated_string(canonicalized_path));
  52. auto deflate_buffer = Compress::DeflateCompressor::compress_all(file_buffer);
  53. if (deflate_buffer.has_value() && deflate_buffer.value().size() < file_buffer.size()) {
  54. member.compressed_data = deflate_buffer.value().bytes();
  55. member.compression_method = Archive::ZipCompressionMethod::Deflate;
  56. auto compression_ratio = (double)deflate_buffer.value().size() / file_buffer.size();
  57. outln(" adding: {} (deflated {}%)", canonicalized_path, (int)(compression_ratio * 100));
  58. } else {
  59. member.compressed_data = file_buffer.bytes();
  60. member.compression_method = Archive::ZipCompressionMethod::Store;
  61. outln(" adding: {} (stored 0%)", canonicalized_path);
  62. }
  63. member.uncompressed_size = file_buffer.size();
  64. Crypto::Checksum::CRC32 checksum { file_buffer.bytes() };
  65. member.crc32 = checksum.digest();
  66. member.is_directory = false;
  67. return zip_stream.add_member(member);
  68. };
  69. auto add_directory = [&](DeprecatedString path, auto handle_directory) -> ErrorOr<void> {
  70. auto canonicalized_path = DeprecatedString::formatted("{}/", LexicalPath::canonicalized_path(path));
  71. Archive::ZipMember member {};
  72. member.name = TRY(String::from_deprecated_string(canonicalized_path));
  73. member.compressed_data = {};
  74. member.compression_method = Archive::ZipCompressionMethod::Store;
  75. member.uncompressed_size = 0;
  76. member.crc32 = 0;
  77. member.is_directory = true;
  78. TRY(zip_stream.add_member(member));
  79. outln(" adding: {} (stored 0%)", canonicalized_path);
  80. if (!recurse)
  81. return {};
  82. Core::DirIterator it(path, Core::DirIterator::Flags::SkipParentAndBaseDir);
  83. while (it.has_next()) {
  84. auto child_path = it.next_full_path();
  85. if (Core::File::is_link(child_path))
  86. return {};
  87. if (!Core::File::is_directory(child_path)) {
  88. auto result = add_file(child_path);
  89. if (result.is_error())
  90. warnln("Couldn't add file '{}': {}", child_path, result.error());
  91. } else {
  92. auto result = handle_directory(child_path, handle_directory);
  93. if (result.is_error())
  94. warnln("Couldn't add directory '{}': {}", child_path, result.error());
  95. }
  96. }
  97. return {};
  98. };
  99. for (auto const& source_path : source_paths) {
  100. if (Core::File::is_directory(source_path)) {
  101. auto result = add_directory(source_path, add_directory);
  102. if (result.is_error())
  103. warnln("Couldn't add directory '{}': {}", source_path, result.error());
  104. } else {
  105. auto result = add_file(source_path);
  106. if (result.is_error())
  107. warnln("Couldn't add file '{}': {}", source_path, result.error());
  108. }
  109. }
  110. TRY(zip_stream.finish());
  111. return 0;
  112. }