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