unzip.cpp 4.5 KB

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