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 
16 #ifndef FOUNDATION_APPEXECFWK_STANDARD_TOOLS_ZIP_INTERNAL_H
17 #define FOUNDATION_APPEXECFWK_STANDARD_TOOLS_ZIP_INTERNAL_H
18 
19 #include <string>
20 #include <time.h>
21 #include "contrib/minizip/unzip.h"
22 #include "contrib/minizip/zip.h"
23 #include "zip_utils.h"
24 
25 namespace OHOS {
26 namespace AppExecFwk {
27 namespace LIBZIP {
28 namespace {
29 const int kZipMaxPath = 256;
30 const int kZipBufSize = 8192;
31 }  // namespace
32 
33 // Callback function for zlib that opens a file stream from a file descriptor.
34 // Since we do not own the file descriptor, dup it so that we can fdopen/fclose
35 // a file stream.
36 void *FdOpenFileFunc(void *opaque, const char *filename, int mode);
37 
38 int FdCloseFileFunc(void *opaque, void *stream);
39 
40 // Fills |pzlib_filecunc_def| appropriately to handle the zip file
41 // referred to by |fd|.
42 void FillFdOpenFileFunc(zlib_filefunc_def *pzlibFilefuncDef, PlatformFile fd);
43 
44 unzFile OpenFdForUnzipping(PlatformFile zipFD);
45 
46 unzFile OpenForUnzipping(std::string &fileNameUtf8);
47 
48 // Opens the file referred to by |zipFD| for unzipping.
49 unzFile OpenFdForUnzipping(int zipFD);
50 
51 // Creates a custom unzFile object which reads data from the specified string.
52 // This custom unzFile object overrides the I/O API functions of zlib so it can
53 // read data from the specified string.
54 unzFile PrepareMemoryForUnzipping(const std::string &data);
55 
56 // Opens the given file name in UTF-8 for zipping, with some setup for
57 // Windows. |append_flag| will be passed to zipOpen2().
58 zipFile OpenForZipping(const std::string &fileNameUtf8, int appendFlag);
59 
60 // Opens the file referred to by |zipFD| for zipping. |append_flag| will be
61 // passed to zipOpen2().
62 zipFile OpenFdForZipping(PlatformFile zipFd, int appendFlag);
63 
64 bool ZipOpenNewFileInZip(
65     zipFile zipFile, const std::string &strPath, const OPTIONS &options, const struct tm *lastModifiedTime);
66 
67 }  // namespace LIBZIP
68 }  // namespace AppExecFwk
69 }  // namespace OHOS
70 
71 #endif  // FOUNDATION_APPEXECFWK_STANDARD_TOOLS_ZIP_INTERNAL_H
72