1 /* unzip.h -- IO for uncompress .zip files using zlib
2 Version 1.2.0, September 16th, 2017
3 part of the MiniZip project
5 Copyright (C) 2012-2017 Nathan Moinvaziri
6 https://github.com/nmoinvaz/minizip
7 Copyright (C) 2009-2010 Mathias Svensson
8 Modifications for Zip64 support on both zip and unzip
10 Copyright (C) 2007-2008 Even Rouault
11 Modifications of Unzip for Zip64
12 Copyright (C) 1998-2010 Gilles Vollant
13 http://www.winimage.com/zLibDll/minizip.html
15 This program is distributed under the terms of the same license as zlib.
16 See the accompanying LICENSE file for the full text of the license.
40 #if defined(STRICTUNZIP) || defined(STRICTZIPUNZIP)
41 /* like the STRICT of WIN32, we define a pointer that cannot be converted
42 from (void*) without cast */
43 typedef struct TagunzFile__ { int unused; } unz_file__;
44 typedef unz_file__ *unzFile;
46 typedef voidp unzFile;
50 #define UNZ_END_OF_LIST_OF_FILE (-100)
51 #define UNZ_ERRNO (Z_ERRNO)
53 #define UNZ_PARAMERROR (-102)
54 #define UNZ_BADZIPFILE (-103)
55 #define UNZ_INTERNALERROR (-104)
56 #define UNZ_CRCERROR (-105)
57 #define UNZ_BADPASSWORD (-106)
59 /* unz_global_info structure contain global data about the ZIPfile
60 These data comes from the end of central dir */
61 typedef struct unz_global_info64_s
63 uint64_t number_entry; /* total number of entries in the central dir on this disk */
64 uint32_t number_disk_with_CD; /* number the the disk with central dir, used for spanning ZIP*/
65 uint16_t size_comment; /* size of the global comment of the zipfile */
68 typedef struct unz_global_info_s
70 uint32_t number_entry; /* total number of entries in the central dir on this disk */
71 uint32_t number_disk_with_CD; /* number the the disk with central dir, used for spanning ZIP*/
72 uint16_t size_comment; /* size of the global comment of the zipfile */
75 /* unz_file_info contain information about a file in the zipfile */
76 /* https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT */
77 typedef struct unz_file_info64_s
79 uint16_t version; /* version made by 2 bytes */
80 uint16_t version_needed; /* version needed to extract 2 bytes */
81 uint16_t flag; /* general purpose bit flag 2 bytes */
82 uint16_t compression_method; /* compression method 2 bytes */
83 uint32_t dos_date; /* last mod file date in Dos fmt 4 bytes */
84 uint32_t crc; /* crc-32 4 bytes */
85 uint64_t compressed_size; /* compressed size 8 bytes */
86 uint64_t uncompressed_size; /* uncompressed size 8 bytes */
87 uint16_t size_filename; /* filename length 2 bytes */
88 uint16_t size_file_extra; /* extra field length 2 bytes */
89 uint16_t size_file_comment; /* file comment length 2 bytes */
91 uint32_t disk_num_start; /* disk number start 4 bytes */
92 uint16_t internal_fa; /* internal file attributes 2 bytes */
93 uint32_t external_fa; /* external file attributes 4 bytes */
97 uint16_t size_file_extra_internal;
100 typedef struct unz_file_info_s
102 uint16_t version; /* version made by 2 bytes */
103 uint16_t version_needed; /* version needed to extract 2 bytes */
104 uint16_t flag; /* general purpose bit flag 2 bytes */
105 uint16_t compression_method; /* compression method 2 bytes */
106 uint32_t dos_date; /* last mod file date in Dos fmt 4 bytes */
107 uint32_t crc; /* crc-32 4 bytes */
108 uint32_t compressed_size; /* compressed size 4 bytes */
109 uint32_t uncompressed_size; /* uncompressed size 4 bytes */
110 uint16_t size_filename; /* filename length 2 bytes */
111 uint16_t size_file_extra; /* extra field length 2 bytes */
112 uint16_t size_file_comment; /* file comment length 2 bytes */
114 uint16_t disk_num_start; /* disk number start 2 bytes */
115 uint16_t internal_fa; /* internal file attributes 2 bytes */
116 uint32_t external_fa; /* external file attributes 4 bytes */
118 uint64_t disk_offset;
121 /***************************************************************************/
122 /* Opening and close a zip file */
124 extern unzFile ZEXPORT unzOpen(const char *path);
125 extern unzFile ZEXPORT unzOpen64(const void *path);
128 path should contain the full path (by example, on a Windows XP computer
129 "c:\\zlib\\zlib113.zip" or on an Unix computer "zlib/zlib113.zip".
130 return NULL if zipfile cannot be opened or doesn't exist
131 return unzFile handle if no error
133 NOTE: The "64" function take a const void *pointer, because the path is just the value passed to the
134 open64_file_func callback. Under Windows, if UNICODE is defined, using fill_fopen64_filefunc, the path
135 is a pointer to a wide unicode string (LPCTSTR is LPCWSTR), so const char *does not describe the reality */
137 extern unzFile ZEXPORT unzOpen2(const char *path, zlib_filefunc_def *pzlib_filefunc_def);
138 /* Open a Zip file, like unzOpen, but provide a set of file low level API for read/write operations */
139 extern unzFile ZEXPORT unzOpen2_64(const void *path, zlib_filefunc64_def *pzlib_filefunc_def);
140 /* Open a Zip file, like unz64Open, but provide a set of file low level API for read/write 64-bit operations */
142 extern int ZEXPORT unzClose(unzFile file);
143 /* Close a ZipFile opened with unzOpen. If there is files inside the .Zip opened with unzOpenCurrentFile,
144 these files MUST be closed with unzipCloseCurrentFile before call unzipClose.
146 return UNZ_OK if there is no error */
148 extern int ZEXPORT unzGetGlobalInfo(unzFile file, unz_global_info *pglobal_info);
149 extern int ZEXPORT unzGetGlobalInfo64(unzFile file, unz_global_info64 *pglobal_info);
150 /* Write info about the ZipFile in the *pglobal_info structure.
152 return UNZ_OK if no error */
154 extern int ZEXPORT unzGetGlobalComment(unzFile file, char *comment, uint16_t comment_size);
155 /* Get the global comment string of the ZipFile, in the comment buffer.
157 uSizeBuf is the size of the szComment buffer.
158 return the number of byte copied or an error code <0 */
160 extern uint64_t ZEXPORT unzCountEntries(const unzFile file);
162 /***************************************************************************/
163 /* Reading the content of the current zipfile, you can open it, read data from it, and close it
164 (you can close it before reading all the file) */
166 extern int ZEXPORT unzOpenCurrentFile(unzFile file);
167 /* Open for reading data the current file in the zipfile.
169 return UNZ_OK if no error */
171 extern int ZEXPORT unzOpenCurrentFilePassword(unzFile file, const char *password);
172 /* Open for reading data the current file in the zipfile.
173 password is a crypting password
175 return UNZ_OK if no error */
177 extern int ZEXPORT unzOpenCurrentFile2(unzFile file, int *method, int *level, int raw);
178 /* Same as unzOpenCurrentFile, but open for read raw the file (not uncompress)
179 if raw==1 *method will receive method of compression, *level will receive level of compression
181 NOTE: you can set level parameter as NULL (if you did not want known level,
182 but you CANNOT set method parameter as NULL */
184 extern int ZEXPORT unzOpenCurrentFile3(unzFile file, int *method, int *level, int raw, const char *password);
185 /* Same as unzOpenCurrentFile, but takes extra parameter password for encrypted files */
187 extern int ZEXPORT unzReadCurrentFile(unzFile file, voidp buf, uint32_t len);
188 /* Read bytes from the current file (opened by unzOpenCurrentFile)
189 buf contain buffer where data must be copied
192 return the number of byte copied if somes bytes are copied
193 return 0 if the end of file was reached
194 return <0 with error code if there is an error (UNZ_ERRNO for IO error, or zLib error for uncompress error) */
196 extern int ZEXPORT unzGetCurrentFileInfo(unzFile file, unz_file_info *pfile_info, char *filename,
197 uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size);
198 extern int ZEXPORT unzGetCurrentFileInfo64(unzFile file, unz_file_info64 *pfile_info, char *filename,
199 uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size);
200 /* Get Info about the current file
202 pfile_info if != NULL, the *pfile_info structure will contain somes info about the current file
203 filename if != NULL, the file name string will be copied in filename
204 filename_size is the size of the filename buffer
205 extrafield if != NULL, the extra field information from the central header will be copied in to
206 extrafield_size is the size of the extraField buffer
207 comment if != NULL, the comment string of the file will be copied in to
208 comment_size is the size of the comment buffer */
210 extern int ZEXPORT unzGetLocalExtrafield(unzFile file, voidp buf, uint32_t len);
211 /* Read extra field from the current file (opened by unzOpenCurrentFile)
212 This is the local-header version of the extra field (sometimes, there is
213 more info in the local-header version than in the central-header)
215 if buf == NULL, it return the size of the local extra field
216 if buf != NULL, len is the size of the buffer, the extra header is copied in buf.
218 return number of bytes copied in buf, or (if <0) the error code */
220 extern int ZEXPORT unzCloseCurrentFile(unzFile file);
221 /* Close the file in zip opened with unzOpenCurrentFile
223 return UNZ_CRCERROR if all the file was read but the CRC is not good */
225 /***************************************************************************/
226 /* Browse the directory of the zipfile */
228 typedef int (*unzFileNameComparer)(unzFile file, const char *filename1, const char *filename2);
229 typedef int (*unzIteratorFunction)(unzFile file);
230 typedef int (*unzIteratorFunction2)(unzFile file, unz_file_info64 *pfile_info, char *filename,
231 uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size);
233 extern int ZEXPORT unzGoToFirstFile(unzFile file);
234 /* Set the current file of the zipfile to the first file.
236 return UNZ_OK if no error */
238 extern int ZEXPORT unzGoToFirstFile2(unzFile file, unz_file_info64 *pfile_info, char *filename,
239 uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size);
240 /* Set the current file of the zipfile to the first file and retrieves the current info on success.
241 Not as seek intensive as unzGoToFirstFile + unzGetCurrentFileInfo.
243 return UNZ_OK if no error */
245 extern int ZEXPORT unzGoToNextFile(unzFile file);
246 /* Set the current file of the zipfile to the next file.
248 return UNZ_OK if no error
249 return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest */
251 extern int ZEXPORT unzGoToNextFile2(unzFile file, unz_file_info64 *pfile_info, char *filename,
252 uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size);
253 /* Set the current file of the zipfile to the next file and retrieves the current
254 info on success. Does less seeking around than unzGotoNextFile + unzGetCurrentFileInfo.
256 return UNZ_OK if no error
257 return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest */
259 extern int ZEXPORT unzLocateFile(unzFile file, const char *filename, unzFileNameComparer filename_compare_func);
260 /* Try locate the file szFileName in the zipfile. For custom filename comparison pass in comparison function.
262 return UNZ_OK if the file is found (it becomes the current file)
263 return UNZ_END_OF_LIST_OF_FILE if the file is not found */
265 /***************************************************************************/
266 /* Raw access to zip file */
268 typedef struct unz_file_pos_s
270 uint32_t pos_in_zip_directory; /* offset in zip file directory */
271 uint32_t num_of_file; /* # of file */
274 extern int ZEXPORT unzGetFilePos(unzFile file, unz_file_pos *file_pos);
275 extern int ZEXPORT unzGoToFilePos(unzFile file, unz_file_pos *file_pos);
277 typedef struct unz64_file_pos_s
279 uint64_t pos_in_zip_directory; /* offset in zip file directory */
280 uint64_t num_of_file; /* # of file */
283 extern int ZEXPORT unzGetFilePos64(unzFile file, unz64_file_pos *file_pos);
284 extern int ZEXPORT unzGoToFilePos64(unzFile file, const unz64_file_pos *file_pos);
286 extern int32_t ZEXPORT unzGetOffset(unzFile file);
287 extern int64_t ZEXPORT unzGetOffset64(unzFile file);
288 /* Get the current file offset */
290 extern int ZEXPORT unzSetOffset(unzFile file, uint32_t pos);
291 extern int ZEXPORT unzSetOffset64(unzFile file, uint64_t pos);
292 /* Set the current file offset */
294 extern int32_t ZEXPORT unzTell(unzFile file);
295 extern int64_t ZEXPORT unzTell64(unzFile file);
296 /* return current position in uncompressed data */
298 extern int ZEXPORT unzSeek(unzFile file, uint32_t offset, int origin);
299 extern int ZEXPORT unzSeek64(unzFile file, uint64_t offset, int origin);
300 /* Seek within the uncompressed data if compression method is storage */
302 extern int ZEXPORT unzEndOfFile(unzFile file);
303 /* return 1 if the end of file was reached, 0 elsewhere */
305 /***************************************************************************/