unzip.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (c) 2020, Andrés Vieira <anvieiravazquez@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Assertions.h>
  7. #include <AK/MappedFile.h>
  8. #include <AK/NumberFormat.h>
  9. #include <LibArchive/Zip.h>
  10. #include <LibCompress/Deflate.h>
  11. #include <LibCore/ArgsParser.h>
  12. #include <LibCore/File.h>
  13. #include <sys/stat.h>
  14. #include <unistd.h>
  15. static bool unpack_zip_member(Archive::ZipMember zip_member)
  16. {
  17. if (zip_member.is_directory) {
  18. if (mkdir(zip_member.name.characters(), 0755) < 0) {
  19. perror("mkdir");
  20. return false;
  21. }
  22. outln(" extracting: {}", zip_member.name);
  23. return true;
  24. }
  25. auto new_file = Core::File::construct(zip_member.name);
  26. if (!new_file->open(Core::OpenMode::WriteOnly)) {
  27. warnln("Can't write file {}: {}", zip_member.name, new_file->error_string());
  28. return false;
  29. }
  30. outln(" extracting: {}", zip_member.name);
  31. // TODO: verify CRC32s match!
  32. switch (zip_member.compression_method) {
  33. case Archive::ZipCompressionMethod::Store: {
  34. if (!new_file->write(zip_member.compressed_data.data(), zip_member.compressed_data.size())) {
  35. warnln("Can't write file contents in {}: {}", zip_member.name, new_file->error_string());
  36. return false;
  37. }
  38. break;
  39. }
  40. case Archive::ZipCompressionMethod::Deflate: {
  41. auto decompressed_data = Compress::DeflateDecompressor::decompress_all(zip_member.compressed_data);
  42. if (!decompressed_data.has_value()) {
  43. warnln("Failed decompressing file {}", zip_member.name);
  44. return false;
  45. }
  46. if (decompressed_data.value().size() != zip_member.uncompressed_size) {
  47. warnln("Failed decompressing file {}", zip_member.name);
  48. return false;
  49. }
  50. if (!new_file->write(decompressed_data.value().data(), decompressed_data.value().size())) {
  51. warnln("Can't write file contents in {}: {}", zip_member.name, new_file->error_string());
  52. return false;
  53. }
  54. break;
  55. }
  56. default:
  57. VERIFY_NOT_REACHED();
  58. }
  59. if (!new_file->close()) {
  60. warnln("Can't close file {}: {}", zip_member.name, new_file->error_string());
  61. return false;
  62. }
  63. return true;
  64. }
  65. int main(int argc, char** argv)
  66. {
  67. const char* path;
  68. int map_size_limit = 32 * MiB;
  69. String output_directory_path;
  70. Core::ArgsParser args_parser;
  71. args_parser.add_option(map_size_limit, "Maximum chunk size to map", "map-size-limit", 0, "size");
  72. args_parser.add_option(output_directory_path, "Directory to receive the archive content", "output-directory", 'o', "path");
  73. args_parser.add_positional_argument(path, "File to unzip", "path", Core::ArgsParser::Required::Yes);
  74. args_parser.parse(argc, argv);
  75. String zip_file_path { path };
  76. struct stat st;
  77. int rc = stat(zip_file_path.characters(), &st);
  78. if (rc < 0) {
  79. perror("stat");
  80. return 1;
  81. }
  82. // FIXME: Map file chunk-by-chunk once we have mmap() with offset.
  83. // This will require mapping some parts then unmapping them repeatedly,
  84. // but it would be significantly faster and less syscall heavy than seek()/read() at every read.
  85. if (st.st_size >= map_size_limit) {
  86. warnln("unzip warning: Refusing to map file since it is larger than {}, pass '--map-size-limit {}' to get around this",
  87. human_readable_size(map_size_limit).characters(),
  88. round_up_to_power_of_two(st.st_size, 16));
  89. return 1;
  90. }
  91. auto file_or_error = MappedFile::map(zip_file_path);
  92. if (file_or_error.is_error()) {
  93. warnln("Failed to open {}: {}", zip_file_path, file_or_error.error());
  94. return 1;
  95. }
  96. auto& mapped_file = *file_or_error.value();
  97. warnln("Archive: {}", zip_file_path);
  98. auto zip_file = Archive::Zip::try_create(mapped_file.bytes());
  99. if (!zip_file.has_value()) {
  100. warnln("Invalid zip file {}", zip_file_path);
  101. return 1;
  102. }
  103. if (!output_directory_path.is_null()) {
  104. rc = mkdir(output_directory_path.characters(), 0755);
  105. if (rc < 0 && errno != EEXIST) {
  106. perror("mkdir");
  107. return 1;
  108. }
  109. rc = chdir(output_directory_path.characters());
  110. if (rc < 0) {
  111. perror("chdir");
  112. return 1;
  113. }
  114. }
  115. auto success = zip_file->for_each_member([&](auto zip_member) {
  116. return unpack_zip_member(zip_member) ? IterationDecision::Continue : IterationDecision::Break;
  117. });
  118. return success ? 0 : 1;
  119. }