blob: 25ec655caf314b5290effb4eebd07797628c409a [file] [log] [blame]
Martin Stjernholmc15e7e42020-12-02 22:50:53 +00001// 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
5#ifndef THIRD_PARTY_ZLIB_GOOGLE_ZIP_H_
6#define THIRD_PARTY_ZLIB_GOOGLE_ZIP_H_
7
android-t13d2c5b22022-10-12 13:43:18 +08008#include <cstdint>
9#include <ostream>
10#include <utility>
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000011#include <vector>
12
13#include "base/callback.h"
android-t13d2c5b22022-10-12 13:43:18 +080014#include "base/containers/span.h"
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000015#include "base/files/file_path.h"
16#include "base/files/platform_file.h"
17#include "base/time/time.h"
18#include "build/build_config.h"
19
20namespace base {
21class File;
22}
23
24namespace zip {
25
26class WriterDelegate;
27
android-t13d2c5b22022-10-12 13:43:18 +080028// Paths passed as span to avoid copying them.
29using Paths = base::span<const base::FilePath>;
30
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000031// Abstraction for file access operation required by Zip().
android-t13d2c5b22022-10-12 13:43:18 +080032//
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000033// Can be passed to the ZipParams for providing custom access to the files,
34// for example over IPC.
android-t13d2c5b22022-10-12 13:43:18 +080035//
36// All parameters paths are expected to be relative to the source directory.
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000037class FileAccessor {
38 public:
39 virtual ~FileAccessor() = default;
40
android-t13d2c5b22022-10-12 13:43:18 +080041 struct Info {
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000042 bool is_directory = false;
android-t13d2c5b22022-10-12 13:43:18 +080043 base::Time last_modified;
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000044 };
45
46 // Opens files specified in |paths|.
47 // Directories should be mapped to invalid files.
android-t13d2c5b22022-10-12 13:43:18 +080048 virtual bool Open(Paths paths, std::vector<base::File>* files) = 0;
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000049
android-t13d2c5b22022-10-12 13:43:18 +080050 // Lists contents of a directory at |path|.
51 virtual bool List(const base::FilePath& path,
52 std::vector<base::FilePath>* files,
53 std::vector<base::FilePath>* subdirs) = 0;
54
55 // Gets info about a file or directory.
56 virtual bool GetInfo(const base::FilePath& path, Info* info) = 0;
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000057};
58
android-t13d2c5b22022-10-12 13:43:18 +080059// Progress of a ZIP creation operation.
60struct Progress {
61 // Total number of bytes read from files getting zipped so far.
62 std::int64_t bytes = 0;
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000063
android-t13d2c5b22022-10-12 13:43:18 +080064 // Number of file entries added to the ZIP so far.
65 // A file entry is added after its bytes have been processed.
66 int files = 0;
67
68 // Number of directory entries added to the ZIP so far.
69 // A directory entry is added before items in it.
70 int directories = 0;
71
72 // Number of errors encountered so far (files that cannot be opened,
73 // directories that cannot be listed).
74 int errors = 0;
75};
76
77// Prints Progress to output stream.
78std::ostream& operator<<(std::ostream& out, const Progress& progress);
79
80// Callback reporting the progress of a ZIP creation operation.
81//
82// This callback returns a boolean indicating whether the ZIP creation operation
83// should continue. If it returns false once, then the ZIP creation operation is
84// immediately cancelled and the callback won't be called again.
85using ProgressCallback = base::RepeatingCallback<bool(const Progress&)>;
86
87using FilterCallback = base::RepeatingCallback<bool(const base::FilePath&)>;
88
89// ZIP creation parameters and options.
90struct ZipParams {
91 // Source directory. Ignored if |file_accessor| is set.
92 base::FilePath src_dir;
93
94 // Abstraction around file system access used to read files.
95 // If left null, an implementation that accesses files directly is used.
96 FileAccessor* file_accessor = nullptr; // Not owned
97
98 // Destination file path.
99 // Either dest_file or dest_fd should be set, but not both.
100 base::FilePath dest_file;
101
102#if defined(OS_POSIX) || defined(OS_FUCHSIA)
103 // Destination file passed a file descriptor.
104 // Either dest_file or dest_fd should be set, but not both.
105 int dest_fd = base::kInvalidPlatformFile;
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000106#endif
107
android-t13d2c5b22022-10-12 13:43:18 +0800108 // The relative paths to the files and directories that should be included in
109 // the ZIP file. If this is empty, the whole contents of |src_dir| are
110 // included.
111 //
112 // These paths must be relative to |src_dir| and will be used as the file
113 // names in the created ZIP file. All files must be under |src_dir| in the
114 // file system hierarchy.
115 //
116 // All the paths in |src_files| are included in the created ZIP file,
117 // irrespective of |include_hidden_files| and |filter_callback|.
118 Paths src_files;
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000119
android-t13d2c5b22022-10-12 13:43:18 +0800120 // Filter used to exclude files from the ZIP file. This is only taken in
121 // account when recursively adding subdirectory contents.
122 FilterCallback filter_callback;
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000123
android-t13d2c5b22022-10-12 13:43:18 +0800124 // Optional progress reporting callback.
125 ProgressCallback progress_callback;
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000126
android-t13d2c5b22022-10-12 13:43:18 +0800127 // Progress reporting period. The final callback is always called when the ZIP
128 // creation operation completes.
129 base::TimeDelta progress_period;
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000130
android-t13d2c5b22022-10-12 13:43:18 +0800131 // Should add hidden files? This is only taken in account when recursively
132 // adding subdirectory contents.
133 bool include_hidden_files = true;
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000134
android-t13d2c5b22022-10-12 13:43:18 +0800135 // Should recursively add subdirectory contents?
136 bool recursive = false;
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000137
android-t13d2c5b22022-10-12 13:43:18 +0800138 // Should ignore errors when discovering files and zipping them?
139 bool continue_on_error = false;
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000140};
141
142// Zip files specified into a ZIP archives. The source files and ZIP destination
143// files (as well as other settings) are specified in |params|.
144bool Zip(const ZipParams& params);
145
146// Zip the contents of src_dir into dest_file. src_path must be a directory.
147// An entry will *not* be created in the zip for the root folder -- children
148// of src_dir will be at the root level of the created zip. For each file in
149// src_dir, include it only if the callback |filter_cb| returns true. Otherwise
150// omit it.
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000151bool ZipWithFilterCallback(const base::FilePath& src_dir,
152 const base::FilePath& dest_file,
android-t13d2c5b22022-10-12 13:43:18 +0800153 FilterCallback filter_cb);
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000154
155// Convenience method for callers who don't need to set up the filter callback.
156// If |include_hidden_files| is true, files starting with "." are included.
157// Otherwise they are omitted.
android-t13d2c5b22022-10-12 13:43:18 +0800158bool Zip(const base::FilePath& src_dir,
159 const base::FilePath& dest_file,
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000160 bool include_hidden_files);
161
android-t13d2c5b22022-10-12 13:43:18 +0800162#if defined(OS_POSIX) || defined(OS_FUCHSIA)
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000163// Zips files listed in |src_relative_paths| to destination specified by file
164// descriptor |dest_fd|, without taking ownership of |dest_fd|. The paths listed
165// in |src_relative_paths| are relative to the |src_dir| and will be used as the
166// file names in the created zip file. All source paths must be under |src_dir|
167// in the file system hierarchy.
168bool ZipFiles(const base::FilePath& src_dir,
android-t13d2c5b22022-10-12 13:43:18 +0800169 Paths src_relative_paths,
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000170 int dest_fd);
android-t13d2c5b22022-10-12 13:43:18 +0800171#endif // defined(OS_POSIX) || defined(OS_FUCHSIA)
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000172
android-t13d2c5b22022-10-12 13:43:18 +0800173// Options of the Unzip function, with valid default values.
174struct UnzipOptions {
175 // Encoding of entry paths in the ZIP archive. By default, paths are assumed
176 // to be in UTF-8.
177 std::string encoding;
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000178
android-t13d2c5b22022-10-12 13:43:18 +0800179 // Only extract the entries for which |filter_cb| returns true. By default,
180 // everything gets extracted.
181 FilterCallback filter;
182
183 // Password to decrypt the encrypted files.
184 std::string password;
185
186 // Should ignore errors when extracting files?
187 bool continue_on_error = false;
188};
189
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000190typedef base::RepeatingCallback<std::unique_ptr<WriterDelegate>(
191 const base::FilePath&)>
192 WriterFactory;
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000193
android-t13d2c5b22022-10-12 13:43:18 +0800194typedef base::RepeatingCallback<bool(const base::FilePath&)> DirectoryCreator;
195
196// Unzips the contents of |zip_file|, using the writers provided by
197// |writer_factory|.
198bool Unzip(const base::PlatformFile& zip_file,
199 WriterFactory writer_factory,
200 DirectoryCreator directory_creator,
201 UnzipOptions options = {});
202
203// Unzips the contents of |zip_file| into |dest_dir|.
204// This function does not overwrite any existing file.
205// A filename collision will result in an error.
206// Therefore, |dest_dir| should initially be an empty directory.
207bool Unzip(const base::FilePath& zip_file,
208 const base::FilePath& dest_dir,
209 UnzipOptions options = {});
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000210
211} // namespace zip
212
213#endif // THIRD_PARTY_ZLIB_GOOGLE_ZIP_H_