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