zip.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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/FileStream.h>
  13. #include <LibCore/System.h>
  14. #include <LibCrypto/Checksum/CRC32.h>
  15. ErrorOr<int> serenity_main(Main::Arguments arguments)
  16. {
  17. const char* 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"));
  30. for (auto const& source_path : source_paths) {
  31. TRY(Core::System::unveil(LexicalPath::absolute_path(cwd, source_path), "r"));
  32. }
  33. TRY(Core::System::unveil(nullptr, nullptr));
  34. String 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. auto file_stream = TRY(Core::OutputFileStream::open(zip_file_path));
  44. outln("Archive: {}", zip_file_path);
  45. Archive::ZipOutputStream zip_stream { file_stream };
  46. auto add_file = [&](String path) {
  47. auto file = Core::File::construct(path);
  48. if (!file->open(Core::OpenMode::ReadOnly)) {
  49. warnln("Failed to open {}: {}", path, file->error_string());
  50. return;
  51. }
  52. auto canonicalized_path = LexicalPath::canonicalized_path(path);
  53. auto file_buffer = file->read_all();
  54. Archive::ZipMember member {};
  55. member.name = canonicalized_path;
  56. auto deflate_buffer = Compress::DeflateCompressor::compress_all(file_buffer);
  57. if (deflate_buffer.has_value() && deflate_buffer.value().size() < file_buffer.size()) {
  58. member.compressed_data = deflate_buffer.value().bytes();
  59. member.compression_method = Archive::ZipCompressionMethod::Deflate;
  60. auto compression_ratio = (double)deflate_buffer.value().size() / file_buffer.size();
  61. outln(" adding: {} (deflated {}%)", canonicalized_path, (int)(compression_ratio * 100));
  62. } else {
  63. member.compressed_data = file_buffer.bytes();
  64. member.compression_method = Archive::ZipCompressionMethod::Store;
  65. outln(" adding: {} (stored 0%)", canonicalized_path);
  66. }
  67. member.uncompressed_size = file_buffer.size();
  68. Crypto::Checksum::CRC32 checksum { file_buffer.bytes() };
  69. member.crc32 = checksum.digest();
  70. member.is_directory = false;
  71. zip_stream.add_member(member);
  72. };
  73. auto add_directory = [&](String path, auto handle_directory) -> void {
  74. auto canonicalized_path = String::formatted("{}/", LexicalPath::canonicalized_path(path));
  75. Archive::ZipMember member {};
  76. member.name = canonicalized_path;
  77. member.compressed_data = {};
  78. member.compression_method = Archive::ZipCompressionMethod::Store;
  79. member.uncompressed_size = 0;
  80. member.crc32 = 0;
  81. member.is_directory = true;
  82. zip_stream.add_member(member);
  83. outln(" adding: {} (stored 0%)", canonicalized_path);
  84. if (!recurse)
  85. return;
  86. Core::DirIterator it(path, Core::DirIterator::Flags::SkipParentAndBaseDir);
  87. while (it.has_next()) {
  88. auto child_path = it.next_full_path();
  89. if (Core::File::is_link(child_path))
  90. return;
  91. if (!Core::File::is_directory(child_path))
  92. add_file(child_path);
  93. else
  94. handle_directory(child_path, handle_directory);
  95. }
  96. };
  97. for (auto const& source_path : source_paths) {
  98. if (Core::File::is_directory(source_path)) {
  99. add_directory(source_path, add_directory);
  100. } else {
  101. add_file(source_path);
  102. }
  103. }
  104. zip_stream.finish();
  105. return 0;
  106. }