• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef FOUNDATION_APPEXECFWK_STANDARD_TOOLS_ZIP_H
16 #define FOUNDATION_APPEXECFWK_STANDARD_TOOLS_ZIP_H
17 
18 #include <fcntl.h>
19 #include <functional>
20 #include <iostream>
21 #include <memory>
22 #include <sys/stat.h>
23 #include <time.h>
24 #include <vector>
25 #include "file_path.h"
26 #include "zip_utils.h"
27 #include "zlib_callback_info_base.h"
28 namespace OHOS {
29 namespace AppExecFwk {
30 namespace LIBZIP {
31 
32 class WriterDelegate;
33 // Abstraction for file access operation required by Zip().
34 // Can be passed to the ZipParams for providing custom access to the files,
35 // for example over IPC.
36 // If none is provided, the files are accessed directly.
37 // All parameters paths are expected to be absolute.
38 class FileAccessor {
39 public:
40     virtual ~FileAccessor() = default;
41 
42     struct DirectoryContentEntry {
DirectoryContentEntryDirectoryContentEntry43         DirectoryContentEntry(const FilePath &path, bool directory) : path(path), isDirectory(directory)
44         {}
45         FilePath path;
46         bool isDirectory = false;
47     };
48 };
49 
50 class ZipParams {
51 public:
52     ZipParams(const std::vector<FilePath> &srcDir, const FilePath &destFile);
53 
54     // Does not take ownership of |destFd|.
55     ZipParams(const std::vector<FilePath> &srcDir, int destFd);
~ZipParams()56     virtual ~ZipParams()
57     {}
DestFd()58     int DestFd() const
59     {
60         return destFd_;
61     }
62 
SrcDir()63     const std::vector<FilePath> &SrcDir() const
64     {
65         return srcDir_;
66     }
67 
DestFile()68     const FilePath &DestFile() const
69     {
70         return destFile_;
71     }
72 
73     // Restricts the files actually zipped to the paths listed in
74     // |srcRelativePaths|. They must be relative to the |srcDir| passed in the
75     // constructor and will be used as the file names in the created zip file. All
76     // source paths must be under |srcDir| in the file system hierarchy.
SetFilesTozip(const std::vector<std::pair<FilePath,FilePath>> & srcRelativePaths)77     void SetFilesTozip(const std::vector<std::pair<FilePath, FilePath>> &srcRelativePaths)
78     {
79         srcFiles_ = srcRelativePaths;
80     }
GetFilesTozip()81     const std::vector<std::pair<FilePath, FilePath>> &GetFilesTozip() const
82     {
83         return srcFiles_;
84     }
85 
86     using FilterCallback = std::function<bool(const FilePath &)>;
SetFilterCallback(FilterCallback filterCallback)87     void SetFilterCallback(FilterCallback filterCallback)
88     {
89         filterCallback_ = filterCallback;
90     }
GetFilterCallback()91     const FilterCallback &GetFilterCallback() const
92     {
93         return filterCallback_;
94     }
95 
SetIncludeHiddenFiles(bool includeHiddenFiles)96     void SetIncludeHiddenFiles(bool includeHiddenFiles)
97     {
98         includeHiddenFiles_ = includeHiddenFiles;
99     }
GetIncludeHiddenFiles()100     bool GetIncludeHiddenFiles() const
101     {
102         return includeHiddenFiles_;
103     }
104 
105     // Sets a custom file accessor for file operations. Default is to directly
106     // access the files (with fopen and the rest).
107     // Useful in cases where running in a sandbox process and file access has to
108     // go through IPC, for example.
SetFileAccessor(std::unique_ptr<FileAccessor> file_accessor)109     void SetFileAccessor(std::unique_ptr<FileAccessor> file_accessor)
110     {
111         fileAccessor_ = std::move(file_accessor);
112     }
GetFileAccessor()113     FileAccessor *GetFileAccessor() const
114     {
115         return fileAccessor_.get();
116     }
117 
118 private:
119     std::vector<FilePath> srcDir_;
120 
121     FilePath destFile_;
122 
123     int destFd_ = kInvalidPlatformFile;
124 
125     // The relative paths to the files that should be included in the zip file. If
126     // this is empty, all files in |srcDir_| are included.
127     std::vector<std::pair<FilePath, FilePath>> srcFiles_;
128 
129     // Filter used to exclude files from the ZIP file. Only effective when
130     // |srcFiles_| is empty.
131     FilterCallback filterCallback_;
132 
133     // Whether hidden files should be included in the ZIP file. Only effective
134     // when |srcFiles_| is empty.
135     bool includeHiddenFiles_ = true;
136 
137     // Abstraction around file system access used to read files. An implementation
138     // that accesses files directly is provided by default.
139     std::unique_ptr<FileAccessor> fileAccessor_;
140 };
141 
142 // Convenience method for callers who don't need to set up the filter callback.
143 // If |includeHiddenFiles| is true, files starting with "." are included.
144 // Otherwise they are omitted.
145 // example No1
146 // srcDir = /ziptest/zipdata/
147 // destFile = /ziptest/hapresult/hapfourfile.zip
148 // example No2
149 // srcDir = /ziptest/zipdata/zip1/zip1-1.cpp
150 // destFile = /ziptest/hapresult/singlefile.zip
151 // options is default value.
152 bool Zip(const std::string &srcPath, const std::string &destPath, const OPTIONS &options,
153     bool includeHiddenFiles, std::shared_ptr<ZlibCallbackInfoBase> zlibCallbackInfo);
154 
155 // Convenience method for callers who don't need to set up the filter callback.
156 // If |includeHiddenFiles| is true, files starting with "." are included.
157 // Otherwise they are omitted.
158 // example
159 // srcFiles = [/ziptest/zipdata/zip1-1.txt, /ziptest/zipdata/zip1-2.txt]
160 // destFile = /ziptest/hapresult/hapfourfile.zip
161 // options is default value.
162 bool Zips(const std::vector<std::string> &srcFiles, const std::string &destPath, const OPTIONS &options,
163     bool includeHiddenFiles, std::shared_ptr<ZlibCallbackInfoBase> zlibCallbackInfo);
164 
165 // Unzip the contents of zipFile into destDir.
166 // example No1
167 // srcDir = /ziptest/hapresult/hapfourfile.zip
168 // destFile = /ziptest/hapunzipdir/01
169 // example No2
170 // srcDir = /ziptest/hapresult/singlefile.zip
171 // destFile = /ziptest/hapunzipdir/single
172 // options is default value.
173 bool Unzip(const std::string &srcFile, const std::string &destFile, const OPTIONS options,
174     std::shared_ptr<ZlibCallbackInfoBase> zlibCallbackInfo);
175 
176 ErrCode GetOriginalSize(const std::string &srcFile, int64_t &originalSize);
177 }  // namespace LIBZIP
178 }  // namespace AppExecFwk
179 }  // namespace OHOS
180 #endif  // FOUNDATION_APPEXECFWK_STANDARD_TOOLS_ZIP_H
181