zip.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef THIRD_PARTY_ZLIB_GOOGLE_ZIP_H_
  5. #define THIRD_PARTY_ZLIB_GOOGLE_ZIP_H_
  6. #include <vector>
  7. #include "base/callback.h"
  8. #include "base/files/file_path.h"
  9. #include "base/files/platform_file.h"
  10. #include "base/time/time.h"
  11. #include "build/build_config.h"
  12. namespace base {
  13. class File;
  14. }
  15. namespace zip {
  16. class WriterDelegate;
  17. // Abstraction for file access operation required by Zip().
  18. // Can be passed to the ZipParams for providing custom access to the files,
  19. // for example over IPC.
  20. // If none is provided, the files are accessed directly.
  21. // All parameters paths are expected to be absolute.
  22. class FileAccessor {
  23. public:
  24. virtual ~FileAccessor() = default;
  25. struct DirectoryContentEntry {
  26. DirectoryContentEntry(const base::FilePath& path, bool is_directory)
  27. : path(path), is_directory(is_directory) {}
  28. base::FilePath path;
  29. bool is_directory = false;
  30. };
  31. // Opens files specified in |paths|.
  32. // Directories should be mapped to invalid files.
  33. virtual std::vector<base::File> OpenFilesForReading(
  34. const std::vector<base::FilePath>& paths) = 0;
  35. virtual bool DirectoryExists(const base::FilePath& path) = 0;
  36. virtual std::vector<DirectoryContentEntry> ListDirectoryContent(
  37. const base::FilePath& dir_path) = 0;
  38. virtual base::Time GetLastModifiedTime(const base::FilePath& path) = 0;
  39. };
  40. class ZipParams {
  41. public:
  42. ZipParams(const base::FilePath& src_dir, const base::FilePath& dest_file);
  43. #if defined(OS_POSIX)
  44. // Does not take ownership of |dest_fd|.
  45. ZipParams(const base::FilePath& src_dir, int dest_fd);
  46. int dest_fd() const { return dest_fd_; }
  47. #endif
  48. const base::FilePath& src_dir() const { return src_dir_; }
  49. const base::FilePath& dest_file() const { return dest_file_; }
  50. // Restricts the files actually zipped to the paths listed in
  51. // |src_relative_paths|. They must be relative to the |src_dir| passed in the
  52. // constructor and will be used as the file names in the created zip file. All
  53. // source paths must be under |src_dir| in the file system hierarchy.
  54. void set_files_to_zip(const std::vector<base::FilePath>& src_relative_paths) {
  55. src_files_ = src_relative_paths;
  56. }
  57. const std::vector<base::FilePath>& files_to_zip() const { return src_files_; }
  58. using FilterCallback = base::RepeatingCallback<bool(const base::FilePath&)>;
  59. void set_filter_callback(FilterCallback filter_callback) {
  60. filter_callback_ = filter_callback;
  61. }
  62. const FilterCallback& filter_callback() const { return filter_callback_; }
  63. void set_include_hidden_files(bool include_hidden_files) {
  64. include_hidden_files_ = include_hidden_files;
  65. }
  66. bool include_hidden_files() const { return include_hidden_files_; }
  67. // Sets a custom file accessor for file operations. Default is to directly
  68. // access the files (with fopen and the rest).
  69. // Useful in cases where running in a sandbox process and file access has to
  70. // go through IPC, for example.
  71. void set_file_accessor(std::unique_ptr<FileAccessor> file_accessor) {
  72. file_accessor_ = std::move(file_accessor);
  73. }
  74. FileAccessor* file_accessor() const { return file_accessor_.get(); }
  75. private:
  76. base::FilePath src_dir_;
  77. base::FilePath dest_file_;
  78. #if defined(OS_POSIX)
  79. int dest_fd_ = base::kInvalidPlatformFile;
  80. #endif
  81. // The relative paths to the files that should be included in the zip file. If
  82. // this is empty, all files in |src_dir_| are included.
  83. std::vector<base::FilePath> src_files_;
  84. // Filter used to exclude files from the ZIP file. Only effective when
  85. // |src_files_| is empty.
  86. FilterCallback filter_callback_;
  87. // Whether hidden files should be included in the ZIP file. Only effective
  88. // when |src_files_| is empty.
  89. bool include_hidden_files_ = true;
  90. // Abstraction around file system access used to read files. An implementation
  91. // that accesses files directly is provided by default.
  92. std::unique_ptr<FileAccessor> file_accessor_;
  93. };
  94. // Zip files specified into a ZIP archives. The source files and ZIP destination
  95. // files (as well as other settings) are specified in |params|.
  96. bool Zip(const ZipParams& params);
  97. // Zip the contents of src_dir into dest_file. src_path must be a directory.
  98. // An entry will *not* be created in the zip for the root folder -- children
  99. // of src_dir will be at the root level of the created zip. For each file in
  100. // src_dir, include it only if the callback |filter_cb| returns true. Otherwise
  101. // omit it.
  102. using FilterCallback = base::RepeatingCallback<bool(const base::FilePath&)>;
  103. bool ZipWithFilterCallback(const base::FilePath& src_dir,
  104. const base::FilePath& dest_file,
  105. const FilterCallback& filter_cb);
  106. // Convenience method for callers who don't need to set up the filter callback.
  107. // If |include_hidden_files| is true, files starting with "." are included.
  108. // Otherwise they are omitted.
  109. bool Zip(const base::FilePath& src_dir, const base::FilePath& dest_file,
  110. bool include_hidden_files);
  111. #if defined(OS_POSIX)
  112. // Zips files listed in |src_relative_paths| to destination specified by file
  113. // descriptor |dest_fd|, without taking ownership of |dest_fd|. The paths listed
  114. // in |src_relative_paths| are relative to the |src_dir| and will be used as the
  115. // file names in the created zip file. All source paths must be under |src_dir|
  116. // in the file system hierarchy.
  117. bool ZipFiles(const base::FilePath& src_dir,
  118. const std::vector<base::FilePath>& src_relative_paths,
  119. int dest_fd);
  120. #endif // defined(OS_POSIX)
  121. // Unzip the contents of zip_file into dest_dir.
  122. // For each file in zip_file, include it only if the callback |filter_cb|
  123. // returns true. Otherwise omit it.
  124. // If |log_skipped_files| is true, files skipped during extraction are printed
  125. // to debug log.
  126. using FilterCallback = base::RepeatingCallback<bool(const base::FilePath&)>;
  127. bool UnzipWithFilterCallback(const base::FilePath& zip_file,
  128. const base::FilePath& dest_dir,
  129. const FilterCallback& filter_cb,
  130. bool log_skipped_files);
  131. // Unzip the contents of zip_file, using the writers provided by writer_factory.
  132. // For each file in zip_file, include it only if the callback |filter_cb|
  133. // returns true. Otherwise omit it.
  134. // If |log_skipped_files| is true, files skipped during extraction are printed
  135. // to debug log.
  136. typedef base::RepeatingCallback<std::unique_ptr<WriterDelegate>(
  137. const base::FilePath&)>
  138. WriterFactory;
  139. typedef base::RepeatingCallback<bool(const base::FilePath&)> DirectoryCreator;
  140. bool UnzipWithFilterAndWriters(const base::PlatformFile& zip_file,
  141. const WriterFactory& writer_factory,
  142. const DirectoryCreator& directory_creator,
  143. const FilterCallback& filter_cb,
  144. bool log_skipped_files);
  145. // Unzip the contents of zip_file into dest_dir.
  146. bool Unzip(const base::FilePath& zip_file, const base::FilePath& dest_dir);
  147. } // namespace zip
  148. #endif // THIRD_PARTY_ZLIB_GOOGLE_ZIP_H_