zip_writer.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2017 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_WRITER_H_
  5. #define THIRD_PARTY_ZLIB_GOOGLE_ZIP_WRITER_H_
  6. #include <memory>
  7. #include <vector>
  8. #include "base/files/file_path.h"
  9. #include "build/build_config.h"
  10. #include "third_party/zlib/google/zip.h"
  11. #if defined(USE_SYSTEM_MINIZIP)
  12. #include <minizip/unzip.h>
  13. #include <minizip/zip.h>
  14. #else
  15. #include "third_party/zlib/contrib/minizip/unzip.h"
  16. #include "third_party/zlib/contrib/minizip/zip.h"
  17. #endif
  18. namespace zip {
  19. namespace internal {
  20. // A class used to write entries to a ZIP file and buffering the reading of
  21. // files to limit the number of calls to the FileAccessor. This is for
  22. // performance reasons as these calls may be expensive when IPC based).
  23. // This class is so far internal and only used by zip.cc, but could be made
  24. // public if needed.
  25. class ZipWriter {
  26. public:
  27. // Creates a writer that will write a ZIP file to |zip_file_fd|/|zip_file|
  28. // and which entries (specifies with AddEntries) are relative to |root_dir|.
  29. // All file reads are performed using |file_accessor|.
  30. #if defined(OS_POSIX)
  31. static std::unique_ptr<ZipWriter> CreateWithFd(int zip_file_fd,
  32. const base::FilePath& root_dir,
  33. FileAccessor* file_accessor);
  34. #endif
  35. static std::unique_ptr<ZipWriter> Create(const base::FilePath& zip_file,
  36. const base::FilePath& root_dir,
  37. FileAccessor* file_accessor);
  38. ~ZipWriter();
  39. // Writes the files at |paths| to the ZIP file and closes this Zip file.
  40. // Note that the the FilePaths must be relative to |root_dir| specified in the
  41. // Create method.
  42. // Returns true if all entries were written successfuly.
  43. bool WriteEntries(const std::vector<base::FilePath>& paths);
  44. private:
  45. ZipWriter(zipFile zip_file,
  46. const base::FilePath& root_dir,
  47. FileAccessor* file_accessor);
  48. // Writes the pending entries to the ZIP file if there are at least
  49. // |kMaxPendingEntriesCount| of them. If |force| is true, all pending entries
  50. // are written regardless of how many there are.
  51. // Returns false if writing an entry fails, true if no entry was written or
  52. // there was no error writing entries.
  53. bool FlushEntriesIfNeeded(bool force);
  54. // Adds the files at |paths| to the ZIP file. These FilePaths must be relative
  55. // to |root_dir| specified in the Create method.
  56. bool AddEntries(const std::vector<base::FilePath>& paths);
  57. // Closes the ZIP file.
  58. // Returns true if successful, false otherwise (typically if an entry failed
  59. // to be written).
  60. bool Close();
  61. // The entries that have been added but not yet written to the ZIP file.
  62. std::vector<base::FilePath> pending_entries_;
  63. // The actual zip file.
  64. zipFile zip_file_;
  65. // Path to the directory entry paths are relative to.
  66. base::FilePath root_dir_;
  67. // Abstraction over file access methods used to read files.
  68. FileAccessor* file_accessor_;
  69. DISALLOW_COPY_AND_ASSIGN(ZipWriter);
  70. };
  71. } // namespace internal
  72. } // namespace zip
  73. #endif // THIRD_PARTY_ZLIB_GOOGLE_ZIP_WRITER_H_