Zip.h (12392B)
1 /* 2 =========================================================================== 3 4 Doom 3 BFG Edition GPL Source Code 5 Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. 6 7 This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code"). 8 9 Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify 10 it under the terms of the GNU General Public License as published by 11 the Free Software Foundation, either version 3 of the License, or 12 (at your option) any later version. 13 14 Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>. 21 22 In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below. 23 24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. 25 26 =========================================================================== 27 */ 28 #ifndef __ZIP_H__ 29 #define __ZIP_H__ 30 31 #include "zlib/zlib.h" 32 33 /* 34 ================================================================================================ 35 36 Contains external code for building ZipFiles. 37 38 The Unzip Package allows extraction of a file from .ZIP file, compatible with 39 PKZip 2.04g, !WinZip, !InfoZip tools and compatibles. Encryption and multi-volume ZipFiles 40 (span) are not supported. Old compressions used by old PKZip 1.x are not supported. 41 42 ================================================================================================ 43 */ 44 #if defined(STRICTZIP) || defined(STRICTZIPUNZIP) 45 /* like the STRICT of WIN32, we define a pointer that cannot be converted 46 from (void*) without cast */ 47 typedef struct TagzipFile__ { int unused; } zipFile__; 48 typedef zipFile__ *zipFile; 49 #else 50 typedef void* zipFile; 51 #endif 52 53 #define ZIP_OK (0) 54 #define ZIP_EOF (0) 55 #define ZIP_ERRNO (Z_ERRNO) 56 #define ZIP_PARAMERROR (-102) 57 #define ZIP_BADZIPFILE (-103) 58 #define ZIP_INTERNALERROR (-104) 59 60 #ifndef Z_BUFSIZE 61 #define Z_BUFSIZE (16384) 62 #endif 63 64 /* 65 ======================== 66 tm_zip 67 contains date/time info 68 ======================== 69 */ 70 typedef struct tm_zip_s { 71 unsigned int tm_sec; /* seconds after the minute - [0,59] */ 72 unsigned int tm_min; /* minutes after the hour - [0,59] */ 73 unsigned int tm_hour; /* hours since midnight - [0,23] */ 74 unsigned int tm_mday; /* day of the month - [1,31] */ 75 unsigned int tm_mon; /* months since January - [0,11] */ 76 unsigned int tm_year; /* years - [1980..2044] */ 77 } tm_zip; 78 79 /* 80 ======================== 81 zip_fileinfo 82 ======================== 83 */ 84 typedef struct { 85 tm_zip tmz_date; /* date in understandable format */ 86 unsigned long dosDate; /* if dos_date == 0, tmu_date is used */ 87 // unsigned long flag; /* general purpose bit flag 2 bytes */ 88 89 unsigned long internal_fa; /* internal file attributes 2 bytes */ 90 unsigned long external_fa; /* external file attributes 4 bytes */ 91 } zip_fileinfo; 92 93 #define NOCRYPT // ignore passwords 94 #define SIZEDATA_INDATABLOCK (4096-(4*4)) 95 96 /* 97 ======================== 98 linkedlist_datablock_internal 99 ======================== 100 */ 101 typedef struct linkedlist_datablock_internal_s { 102 struct linkedlist_datablock_internal_s* next_datablock; 103 unsigned long avail_in_this_block; 104 unsigned long filled_in_this_block; 105 unsigned long unused; /* for future use and alignement */ 106 unsigned char data[SIZEDATA_INDATABLOCK]; 107 } linkedlist_datablock_internal; 108 109 /* 110 ======================== 111 linkedlist_data 112 ======================== 113 */ 114 typedef struct linkedlist_data_s { 115 linkedlist_datablock_internal* first_block; 116 linkedlist_datablock_internal* last_block; 117 } linkedlist_data; 118 119 /* 120 ======================== 121 curfile_info 122 ======================== 123 */ 124 typedef struct { 125 z_stream stream; /* zLib stream structure for inflate */ 126 int stream_initialised; /* 1 is stream is initialised */ 127 unsigned int pos_in_buffered_data; /* last written byte in buffered_data */ 128 129 unsigned long pos_local_header; /* offset of the local header of the file currenty writing */ 130 char* central_header; /* central header data for the current file */ 131 unsigned long size_centralheader; /* size of the central header for cur file */ 132 unsigned long flag; /* flag of the file currently writing */ 133 134 int method; /* compression method of file currenty wr.*/ 135 int raw; /* 1 for directly writing raw data */ 136 byte buffered_data[Z_BUFSIZE];/* buffer contain compressed data to be writ*/ 137 unsigned long dosDate; 138 unsigned long crc32; 139 int encrypt; 140 #ifndef NOCRYPT 141 unsigned long keys[3]; /* keys defining the pseudo-random sequence */ 142 const unsigned long* pcrc_32_tab; 143 int crypt_header_size; 144 #endif 145 } curfile_info; 146 147 //#define NO_ADDFILEINEXISTINGZIP 148 /* 149 ======================== 150 zip_internal 151 ======================== 152 */ 153 typedef struct { 154 idFile* filestream; /* io structore of the zipfile */ 155 linkedlist_data central_dir; /* datablock with central dir in construction*/ 156 int in_opened_file_inzip; /* 1 if a file in the zip is currently writ.*/ 157 curfile_info ci; /* info on the file curretly writing */ 158 159 unsigned long begin_pos; /* position of the beginning of the zipfile */ 160 unsigned long add_position_when_writting_offset; 161 unsigned long number_entry; 162 #ifndef NO_ADDFILEINEXISTINGZIP 163 char* globalcomment; 164 #endif 165 } zip_internal; 166 167 #define APPEND_STATUS_CREATE (0) 168 #define APPEND_STATUS_CREATEAFTER (1) 169 #define APPEND_STATUS_ADDINZIP (2) 170 171 /* 172 Create a zipfile. 173 pathname contain on Windows XP a filename like "c:\\zlib\\zlib113.zip" or on 174 an Unix computer "zlib/zlib113.zip". 175 if the file pathname exist and append==APPEND_STATUS_CREATEAFTER, the zip 176 will be created at the end of the file. 177 (useful if the file contain a self extractor code) 178 if the file pathname exist and append==APPEND_STATUS_ADDINZIP, we will 179 add files in existing zip (be sure you don't add file that doesn't exist) 180 If the zipfile cannot be opened, the return value is NULL. 181 Else, the return value is a zipFile Handle, usable with other function 182 of this zip package. 183 */ 184 185 /* Note : there is no delete function into a zipfile. 186 If you want delete file into a zipfile, you must open a zipfile, and create another 187 Of couse, you can use RAW reading and writing to copy the file you did not want delte 188 */ 189 extern zipFile zipOpen( const char *pathname, int append ); 190 191 /* 192 Open a file in the ZIP for writing. 193 filename : the filename in zip (if NULL, '-' without quote will be used 194 *zipfi contain supplemental information 195 if extrafield_local!=NULL and size_extrafield_local>0, extrafield_local 196 contains the extrafield data the the local header 197 if extrafield_global!=NULL and size_extrafield_global>0, extrafield_global 198 contains the extrafield data the the local header 199 if comment != NULL, comment contain the comment string 200 method contain the compression method (0 for store, Z_DEFLATED for deflate) 201 level contain the level of compression (can be Z_DEFAULT_COMPRESSION) 202 */ 203 extern zipFile zipOpen2( const char* pathname, int append, char* globalcomment ); 204 205 extern int zipOpenNewFileInZip( zipFile file, const char* filename, const zip_fileinfo* zipfi, const void* extrafield_local, 206 uInt size_extrafield_local, const void* extrafield_global, uInt size_extrafield_global, const char* comment, 207 int method, int level ); 208 209 /* 210 Same than zipOpenNewFileInZip, except if raw=1, we write raw file 211 */ 212 extern int zipOpenNewFileInZip2( zipFile file, const char* filename, const zip_fileinfo* zipfi, const void* extrafield_local, uInt size_extrafield_local, 213 const void* extrafield_global, uInt size_extrafield_global, const char* comment, int method, int level, int raw ); 214 215 /* 216 Same than zipOpenNewFileInZip2, except 217 windowBits,memLevel,,strategy : see parameter strategy in deflateInit2 218 password : crypting password (NULL for no crypting) 219 crcForCtypting : crc of file to compress (needed for crypting) 220 */ 221 extern int zipOpenNewFileInZip3( zipFile file, const char* filename, const zip_fileinfo* zipfi, const void* extrafield_local, uInt size_extrafield_local, 222 const void* extrafield_global, uInt size_extrafield_global, const char* comment, int method, int level, int raw, int windowBits, 223 int memLevel, int strategy, const char* password, uLong crcForCtypting ); 224 225 /* 226 Write data in the zipfile 227 */ 228 extern int zipWriteInFileInZip( zipFile file, const void* buf, unsigned int len ); 229 230 231 /* 232 Close the current file in the zipfile 233 */ 234 extern int zipCloseFileInZip( zipFile file ); 235 236 237 /* 238 Close the current file in the zipfile, for fiel opened with 239 parameter raw=1 in zipOpenNewFileInZip2 240 uncompressed_size and crc32 are value for the uncompressed size 241 */ 242 extern int zipCloseFileInZipRaw( zipFile file, uLong uncompressed_size, uLong crc32 ); 243 244 245 /* 246 Close the zipfile 247 */ 248 extern int zipClose( zipFile file, const char* global_comment ); 249 250 251 /* 252 ================================================ 253 idZipBuilder 254 255 simple interface for zipping up a folder of files 256 by default, the source folder files are added recursively 257 ================================================ 258 */ 259 class idZipBuilder { 260 public: 261 idZipBuilder() {} 262 ~idZipBuilder() {} 263 264 public: 265 // adds a list of file extensions ( e.g. "bcm|bmodel|" ) to be compressed in the zip file 266 void AddFileFilters( const char *filters ); 267 // adds a list of file extensions ( e.g. "genmodel|" ) to be added to the zip file, in an uncompressed form 268 void AddUncompressedFileFilters( const char *filters ); 269 // builds a zip file of all the files in the specified folder, overwriting if necessary 270 bool Build( const char* zipPath, const char *folder, bool cleanFolder ); 271 // updates a zip file with the files in the specified folder 272 bool Update( const char* zipPath, const char *folder, bool cleanFolder ); 273 274 // helper function to zip up all the files and put in a new zip file 275 static bool BuildMapFolderZip( const char *mapFileName ); 276 // helper function to update a map folder zip for newer files 277 static bool UpdateMapFolderZip( const char *mapFileName ); 278 279 // combines multiple in-memory files into a single memory file 280 static idFile_Memory * CombineFiles( const idList< idFile_Memory * > & srcFiles ); 281 // extracts multiple in-memory files from a single memory file 282 static bool ExtractFiles( idFile_Memory * & srcFile, idList< idFile_Memory * > & destFiles ); 283 284 void CleanSourceFolder(); 285 286 bool CreateZipFileFromFileList( const char *name, const idList< idFile_Memory * > & srcFiles ); 287 288 zipFile CreateZipFile( const char *name ); 289 bool AddFile( zipFile zf, idFile_Memory *fm, bool deleteFile ); 290 void CloseZipFile( zipFile zf ); 291 private: 292 bool CreateZipFile( bool appendFiles ); 293 bool CreateZipFileFromFiles( const idList< idFile_Memory * > & srcFiles ); 294 bool GetFileTime( const idStr &filename, unsigned long *dostime ) const; 295 bool IsFiltered( const idStr &filename ) const; 296 bool IsUncompressed( const idStr &filename ) const; 297 298 private: 299 idStr zipFileName; // os path to the zip file 300 idStr sourceFolderName; // source folder of files to zip or add 301 idStrList filterExts; // file extensions we want to compressed 302 idStrList uncompressedFilterExts; // file extensions we don't want to compress 303 }; 304 305 #endif /* __ZIP_H__ */