├── SSZipArchive.podspec ├── SSZipArchive ├── minizip │ ├── mztools.h │ ├── crypt.h │ ├── ioapi.h │ ├── ioapi.c │ ├── mztools.c │ ├── zip.h │ ├── unzip.h │ └── zip.c ├── SSZipArchive.h └── SSZipArchive.m ├── LICENSE └── Readme.markdown /SSZipArchive.podspec: -------------------------------------------------------------------------------- 1 | Pod::Spec.new do |s| 2 | s.name = 'SSZipArchive' 3 | s.version = '0.3.1' 4 | s.summary = 'Utility class for zipping and unzipping files on iOS and Mac.' 5 | s.description = 'SSZipArchive is a simple utility class for zipping and unzipping files on iOS and Mac.' 6 | s.homepage = 'https://github.com/soffes/ssziparchive' 7 | s.license = { :type => 'MIT', :file => 'LICENSE' } 8 | s.author = { 'Sam Soffes' => 'sam@soff.es' } 9 | s.source = { :git => 'https://github.com/AlpinaDigital/SSZipArchive.git', :branch => "master" } 10 | s.ios.deployment_target = '4.0' 11 | s.osx.deployment_target = '10.6' 12 | s.source_files = 'SSZipArchive/**/*', 13 | s.library = 'z' 14 | end 15 | -------------------------------------------------------------------------------- /SSZipArchive/minizip/mztools.h: -------------------------------------------------------------------------------- 1 | /* 2 | Additional tools for Minizip 3 | Code: Xavier Roche '2004 4 | License: Same as ZLIB (www.gzip.org) 5 | */ 6 | 7 | #ifndef _zip_tools_H 8 | #define _zip_tools_H 9 | 10 | #ifdef __cplusplus 11 | extern "C" { 12 | #endif 13 | 14 | #ifndef _ZLIB_H 15 | #include "zlib.h" 16 | #endif 17 | 18 | #include "unzip.h" 19 | 20 | /* Repair a ZIP file (missing central directory) 21 | file: file to recover 22 | fileOut: output file after recovery 23 | fileOutTmp: temporary file name used for recovery 24 | */ 25 | extern int ZEXPORT unzRepair(const char* file, 26 | const char* fileOut, 27 | const char* fileOutTmp, 28 | uLong* nRecovered, 29 | uLong* bytesRecovered); 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2010-2013 Sam Soffes, http://soff.es 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /Readme.markdown: -------------------------------------------------------------------------------- 1 | # SSZipArchive 2 | 3 | SSZipArchive is a simple utility class for zipping and unzipping files. Features: 4 | 5 | * Unzipping zip files 6 | * Unzipping password protected zip files 7 | * Creating zip files 8 | * Appending to zip files 9 | * Zipping files 10 | * Zipping NSData with a filename 11 | 12 | ## Adding to your project 13 | 14 | 1. Add the `SSZipArchive` and `minizip` folders to your project. 15 | 2. Add the `libz` library to your target 16 | 17 | SSZipArchive requires ARC. 18 | 19 | ## Usage 20 | 21 | ``` objective-c 22 | // Unzipping 23 | NSString *zipPath = @"path_to_your_zip_file"; 24 | NSString *destinationPath = @"path_to_the_folder_where_you_want_it_unzipped"; 25 | [SSZipArchive unzipFileAtPath:zipPath toDestination:destinationPath]; 26 | 27 | // Zipping 28 | NSString *zippedPath = @"path_where_you_want_the_file_created"; 29 | NSArray *inputPaths = [NSArray arrayWithObjects: 30 | [[NSBundle mainBundle] pathForResource:@"photo1" ofType:@"jpg"], 31 | [[NSBundle mainBundle] pathForResource:@"photo2" ofType:@"jpg"] 32 | nil]; 33 | [SSZipArchive createZipFileAtPath:zippedPath withFilesAtPaths:inputPaths]; 34 | ``` 35 | 36 | ## Tests 37 | 38 | Simply, open the Xcode 5 or higher project in the Tests directory and press Command-U to run the tests. 39 | 40 | ## License 41 | 42 | SSZipArchive is licensed under the [MIT license](https://github.com/samsoffes/ssziparchive/raw/master/LICENSE). A slightly modified version of [Minizip](http://www.winimage.com/zLibDll/minizip.html) 1.1 is also included and is licensed under the [Zlib license](http://www.zlib.net/zlib_license.html). 43 | 44 | ## Thanks 45 | 46 | Thanks [aish](http://code.google.com/p/ziparchive) for creating [ZipArchive](http://code.google.com/p/ziparchive) which SSZipArchive is based on, Johnnie Walker ([@randomsequence](https://github.com/randomsequence)) for implementing creation support, and John Engelhart ([@johnezang](https://github.com/johnezang)) for all his amazing help along the way. 47 | -------------------------------------------------------------------------------- /SSZipArchive/SSZipArchive.h: -------------------------------------------------------------------------------- 1 | // 2 | // SSZipArchive.h 3 | // SSZipArchive 4 | // 5 | // Created by Sam Soffes on 7/21/10. 6 | // Copyright (c) Sam Soffes 2010-2013. All rights reserved. 7 | // 8 | 9 | #ifndef _SSZIPARCHIVE_H 10 | #define _SSZIPARCHIVE_H 11 | 12 | #import 13 | #include "unzip.h" 14 | 15 | @protocol SSZipArchiveDelegate; 16 | 17 | @interface SSZipArchive : NSObject 18 | 19 | // Unzip 20 | + (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination uniqueId:(NSString *)uniqueId; 21 | + (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination overwrite:(BOOL)overwrite password:(NSString *)password error:(NSError **)error uniqueId:(NSString *)uniqueId; 22 | 23 | + (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination delegate:(id)delegate uniqueId:(NSString *)uniqueId; 24 | + (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination overwrite:(BOOL)overwrite password:(NSString *)password error:(NSError **)error delegate:(id)delegate uniqueId:(NSString *)uniqueId; 25 | 26 | // Zip 27 | + (BOOL)createZipFileAtPath:(NSString *)path withFilesAtPaths:(NSArray *)filenames; 28 | + (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath; 29 | 30 | - (id)initWithPath:(NSString *)path; 31 | - (BOOL)open; 32 | - (BOOL)writeFile:(NSString *)path; 33 | - (BOOL)writeData:(NSData *)data filename:(NSString *)filename; 34 | - (BOOL)close; 35 | 36 | @end 37 | 38 | 39 | @protocol SSZipArchiveDelegate 40 | 41 | @optional 42 | 43 | - (void)zipArchiveWillUnzipArchiveAtPath:(NSString *)path zipInfo:(unz_global_info)zipInfo; 44 | - (void)zipArchiveDidUnzipArchiveAtPath:(NSString *)path zipInfo:(unz_global_info)zipInfo unzippedPath:(NSString *)unzippedPat uniqueId:(NSString *)uniqueId; 45 | 46 | - (void)zipArchiveWillUnzipFileAtIndex:(NSInteger)fileIndex totalFiles:(NSInteger)totalFiles archivePath:(NSString *)archivePath fileInfo:(unz_file_info)fileInfo; 47 | - (void)zipArchiveDidUnzipFileAtIndex:(NSInteger)fileIndex totalFiles:(NSInteger)totalFiles archivePath:(NSString *)archivePath fileInfo:(unz_file_info)fileInfo; 48 | 49 | @end 50 | 51 | #endif /* _SSZIPARCHIVE_H */ 52 | -------------------------------------------------------------------------------- /SSZipArchive/minizip/crypt.h: -------------------------------------------------------------------------------- 1 | /* crypt.h -- base code for crypt/uncrypt ZIPfile 2 | 3 | 4 | Version 1.01e, February 12th, 2005 5 | 6 | Copyright (C) 1998-2005 Gilles Vollant 7 | 8 | This code is a modified version of crypting code in Infozip distribution 9 | 10 | The encryption/decryption parts of this source code (as opposed to the 11 | non-echoing password parts) were originally written in Europe. The 12 | whole source package can be freely distributed, including from the USA. 13 | (Prior to January 2000, re-export from the US was a violation of US law.) 14 | 15 | This encryption code is a direct transcription of the algorithm from 16 | Roger Schlafly, described by Phil Katz in the file appnote.txt. This 17 | file (appnote.txt) is distributed with the PKZIP program (even in the 18 | version without encryption capabilities). 19 | 20 | If you don't need crypting in your application, just define symbols 21 | NOCRYPT and NOUNCRYPT. 22 | 23 | This code support the "Traditional PKWARE Encryption". 24 | 25 | The new AES encryption added on Zip format by Winzip (see the page 26 | http://www.winzip.com/aes_info.htm ) and PKWare PKZip 5.x Strong 27 | Encryption is not supported. 28 | */ 29 | 30 | #define CRC32(c, b) ((*(pcrc_32_tab+(((int)(c) ^ (b)) & 0xff))) ^ ((c) >> 8)) 31 | 32 | /*********************************************************************** 33 | * Return the next byte in the pseudo-random sequence 34 | */ 35 | static int decrypt_byte(unsigned long* pkeys, const unsigned long* pcrc_32_tab) 36 | { 37 | unsigned temp; /* POTENTIAL BUG: temp*(temp^1) may overflow in an 38 | * unpredictable manner on 16-bit systems; not a problem 39 | * with any known compiler so far, though */ 40 | 41 | temp = ((unsigned)(*(pkeys+2)) & 0xffff) | 2; 42 | return (int)(((temp * (temp ^ 1)) >> 8) & 0xff); 43 | } 44 | 45 | /*********************************************************************** 46 | * Update the encryption keys with the next byte of plain text 47 | */ 48 | static int update_keys(unsigned long* pkeys,const unsigned long* pcrc_32_tab,int c) 49 | { 50 | (*(pkeys+0)) = CRC32((*(pkeys+0)), c); 51 | (*(pkeys+1)) += (*(pkeys+0)) & 0xff; 52 | (*(pkeys+1)) = (*(pkeys+1)) * 134775813L + 1; 53 | { 54 | register int keyshift = (int)((*(pkeys+1)) >> 24); 55 | (*(pkeys+2)) = CRC32((*(pkeys+2)), keyshift); 56 | } 57 | return c; 58 | } 59 | 60 | 61 | /*********************************************************************** 62 | * Initialize the encryption keys and the random header according to 63 | * the given password. 64 | */ 65 | static void init_keys(const char* passwd,unsigned long* pkeys,const unsigned long* pcrc_32_tab) 66 | { 67 | *(pkeys+0) = 305419896L; 68 | *(pkeys+1) = 591751049L; 69 | *(pkeys+2) = 878082192L; 70 | while (*passwd != '\0') { 71 | update_keys(pkeys,pcrc_32_tab,(int)*passwd); 72 | passwd++; 73 | } 74 | } 75 | 76 | #define zdecode(pkeys,pcrc_32_tab,c) \ 77 | (update_keys(pkeys,pcrc_32_tab,c ^= decrypt_byte(pkeys,pcrc_32_tab))) 78 | 79 | #define zencode(pkeys,pcrc_32_tab,c,t) \ 80 | (t=decrypt_byte(pkeys,pcrc_32_tab), update_keys(pkeys,pcrc_32_tab,c), t^(c)) 81 | 82 | #ifdef INCLUDECRYPTINGCODE_IFCRYPTALLOWED 83 | 84 | #define RAND_HEAD_LEN 12 85 | /* "last resort" source for second part of crypt seed pattern */ 86 | # ifndef ZCR_SEED2 87 | # define ZCR_SEED2 3141592654UL /* use PI as default pattern */ 88 | # endif 89 | 90 | static int crypthead(const char* passwd, /* password string */ 91 | unsigned char* buf, /* where to write header */ 92 | int bufSize, 93 | unsigned long* pkeys, 94 | const unsigned long* pcrc_32_tab, 95 | unsigned long crcForCrypting) 96 | { 97 | int n; /* index in random header */ 98 | int t; /* temporary */ 99 | int c; /* random byte */ 100 | unsigned char header[RAND_HEAD_LEN-2]; /* random header */ 101 | static unsigned calls = 0; /* ensure different random header each time */ 102 | 103 | if (bufSize> 7) & 0xff; 118 | header[n] = (unsigned char)zencode(pkeys, pcrc_32_tab, c, t); 119 | } 120 | /* Encrypt random header (last two bytes is high word of crc) */ 121 | init_keys(passwd, pkeys, pcrc_32_tab); 122 | for (n = 0; n < RAND_HEAD_LEN-2; n++) 123 | { 124 | buf[n] = (unsigned char)zencode(pkeys, pcrc_32_tab, header[n], t); 125 | } 126 | buf[n++] = (unsigned char)zencode(pkeys, pcrc_32_tab, (int)(crcForCrypting >> 16) & 0xff, t); 127 | buf[n++] = (unsigned char)zencode(pkeys, pcrc_32_tab, (int)(crcForCrypting >> 24) & 0xff, t); 128 | return n; 129 | } 130 | 131 | #endif 132 | -------------------------------------------------------------------------------- /SSZipArchive/minizip/ioapi.h: -------------------------------------------------------------------------------- 1 | /* ioapi.h -- IO base function header for compress/uncompress .zip 2 | part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html ) 3 | 4 | Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html ) 5 | 6 | Modifications for Zip64 support 7 | Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com ) 8 | 9 | For more info read MiniZip_info.txt 10 | 11 | Changes 12 | 13 | Oct-2009 - Defined ZPOS64_T to fpos_t on windows and u_int64_t on linux. (might need to find a better why for this) 14 | Oct-2009 - Change to fseeko64, ftello64 and fopen64 so large files would work on linux. 15 | More if/def section may be needed to support other platforms 16 | Oct-2009 - Defined fxxxx64 calls to normal fopen/ftell/fseek so they would compile on windows. 17 | (but you should use iowin32.c for windows instead) 18 | 19 | */ 20 | 21 | #ifndef _ZLIBIOAPI64_H 22 | #define _ZLIBIOAPI64_H 23 | 24 | #if (!defined(_WIN32)) && (!defined(WIN32)) 25 | 26 | // Linux needs this to support file operation on files larger then 4+GB 27 | // But might need better if/def to select just the platforms that needs them. 28 | 29 | #ifndef __USE_FILE_OFFSET64 30 | #define __USE_FILE_OFFSET64 31 | #endif 32 | #ifndef __USE_LARGEFILE64 33 | #define __USE_LARGEFILE64 34 | #endif 35 | #ifndef _LARGEFILE64_SOURCE 36 | #define _LARGEFILE64_SOURCE 37 | #endif 38 | #ifndef _FILE_OFFSET_BIT 39 | #define _FILE_OFFSET_BIT 64 40 | #endif 41 | #endif 42 | 43 | #include 44 | #include 45 | #include "zlib.h" 46 | 47 | #define USE_FILE32API 48 | #if defined(USE_FILE32API) 49 | #define fopen64 fopen 50 | #define ftello64 ftell 51 | #define fseeko64 fseek 52 | #else 53 | #ifdef _MSC_VER 54 | #define fopen64 fopen 55 | #if (_MSC_VER >= 1400) && (!(defined(NO_MSCVER_FILE64_FUNC))) 56 | #define ftello64 _ftelli64 57 | #define fseeko64 _fseeki64 58 | #else // old MSC 59 | #define ftello64 ftell 60 | #define fseeko64 fseek 61 | #endif 62 | #endif 63 | #endif 64 | 65 | /* 66 | #ifndef ZPOS64_T 67 | #ifdef _WIN32 68 | #define ZPOS64_T fpos_t 69 | #else 70 | #include 71 | #define ZPOS64_T uint64_t 72 | #endif 73 | #endif 74 | */ 75 | 76 | #ifdef HAVE_MINIZIP64_CONF_H 77 | #include "mz64conf.h" 78 | #endif 79 | 80 | /* a type choosen by DEFINE */ 81 | #ifdef HAVE_64BIT_INT_CUSTOM 82 | typedef 64BIT_INT_CUSTOM_TYPE ZPOS64_T; 83 | #else 84 | #ifdef HAS_STDINT_H 85 | #include "stdint.h" 86 | typedef uint64_t ZPOS64_T; 87 | #else 88 | 89 | 90 | #if defined(_MSC_VER) || defined(__BORLANDC__) 91 | typedef unsigned __int64 ZPOS64_T; 92 | #else 93 | typedef unsigned long long int ZPOS64_T; 94 | #endif 95 | #endif 96 | #endif 97 | 98 | 99 | 100 | #ifdef __cplusplus 101 | extern "C" { 102 | #endif 103 | 104 | 105 | #define ZLIB_FILEFUNC_SEEK_CUR (1) 106 | #define ZLIB_FILEFUNC_SEEK_END (2) 107 | #define ZLIB_FILEFUNC_SEEK_SET (0) 108 | 109 | #define ZLIB_FILEFUNC_MODE_READ (1) 110 | #define ZLIB_FILEFUNC_MODE_WRITE (2) 111 | #define ZLIB_FILEFUNC_MODE_READWRITEFILTER (3) 112 | 113 | #define ZLIB_FILEFUNC_MODE_EXISTING (4) 114 | #define ZLIB_FILEFUNC_MODE_CREATE (8) 115 | 116 | 117 | #ifndef ZCALLBACK 118 | #if (defined(WIN32) || defined(_WIN32) || defined (WINDOWS) || defined (_WINDOWS)) && defined(CALLBACK) && defined (USEWINDOWS_CALLBACK) 119 | #define ZCALLBACK CALLBACK 120 | #else 121 | #define ZCALLBACK 122 | #endif 123 | #endif 124 | 125 | 126 | 127 | 128 | typedef voidpf (ZCALLBACK *open_file_func) OF((voidpf opaque, const char* filename, int mode)); 129 | typedef uLong (ZCALLBACK *read_file_func) OF((voidpf opaque, voidpf stream, void* buf, uLong size)); 130 | typedef uLong (ZCALLBACK *write_file_func) OF((voidpf opaque, voidpf stream, const void* buf, uLong size)); 131 | typedef int (ZCALLBACK *close_file_func) OF((voidpf opaque, voidpf stream)); 132 | typedef int (ZCALLBACK *testerror_file_func) OF((voidpf opaque, voidpf stream)); 133 | 134 | typedef long (ZCALLBACK *tell_file_func) OF((voidpf opaque, voidpf stream)); 135 | typedef long (ZCALLBACK *seek_file_func) OF((voidpf opaque, voidpf stream, uLong offset, int origin)); 136 | 137 | 138 | /* here is the "old" 32 bits structure structure */ 139 | typedef struct zlib_filefunc_def_s 140 | { 141 | open_file_func zopen_file; 142 | read_file_func zread_file; 143 | write_file_func zwrite_file; 144 | tell_file_func ztell_file; 145 | seek_file_func zseek_file; 146 | close_file_func zclose_file; 147 | testerror_file_func zerror_file; 148 | voidpf opaque; 149 | } zlib_filefunc_def; 150 | 151 | typedef ZPOS64_T (ZCALLBACK *tell64_file_func) OF((voidpf opaque, voidpf stream)); 152 | typedef long (ZCALLBACK *seek64_file_func) OF((voidpf opaque, voidpf stream, ZPOS64_T offset, int origin)); 153 | typedef voidpf (ZCALLBACK *open64_file_func) OF((voidpf opaque, const void* filename, int mode)); 154 | 155 | typedef struct zlib_filefunc64_def_s 156 | { 157 | open64_file_func zopen64_file; 158 | read_file_func zread_file; 159 | write_file_func zwrite_file; 160 | tell64_file_func ztell64_file; 161 | seek64_file_func zseek64_file; 162 | close_file_func zclose_file; 163 | testerror_file_func zerror_file; 164 | voidpf opaque; 165 | } zlib_filefunc64_def; 166 | 167 | void fill_fopen64_filefunc OF((zlib_filefunc64_def* pzlib_filefunc_def)); 168 | void fill_fopen_filefunc OF((zlib_filefunc_def* pzlib_filefunc_def)); 169 | 170 | /* now internal definition, only for zip.c and unzip.h */ 171 | typedef struct zlib_filefunc64_32_def_s 172 | { 173 | zlib_filefunc64_def zfile_func64; 174 | open_file_func zopen32_file; 175 | tell_file_func ztell32_file; 176 | seek_file_func zseek32_file; 177 | } zlib_filefunc64_32_def; 178 | 179 | 180 | #define ZREAD64(filefunc,filestream,buf,size) ((*((filefunc).zfile_func64.zread_file)) ((filefunc).zfile_func64.opaque,filestream,buf,size)) 181 | #define ZWRITE64(filefunc,filestream,buf,size) ((*((filefunc).zfile_func64.zwrite_file)) ((filefunc).zfile_func64.opaque,filestream,buf,size)) 182 | //#define ZTELL64(filefunc,filestream) ((*((filefunc).ztell64_file)) ((filefunc).opaque,filestream)) 183 | //#define ZSEEK64(filefunc,filestream,pos,mode) ((*((filefunc).zseek64_file)) ((filefunc).opaque,filestream,pos,mode)) 184 | #define ZCLOSE64(filefunc,filestream) ((*((filefunc).zfile_func64.zclose_file)) ((filefunc).zfile_func64.opaque,filestream)) 185 | #define ZERROR64(filefunc,filestream) ((*((filefunc).zfile_func64.zerror_file)) ((filefunc).zfile_func64.opaque,filestream)) 186 | 187 | voidpf call_zopen64 OF((const zlib_filefunc64_32_def* pfilefunc,const void*filename,int mode)); 188 | long call_zseek64 OF((const zlib_filefunc64_32_def* pfilefunc,voidpf filestream, ZPOS64_T offset, int origin)); 189 | ZPOS64_T call_ztell64 OF((const zlib_filefunc64_32_def* pfilefunc,voidpf filestream)); 190 | 191 | void fill_zlib_filefunc64_32_def_from_filefunc32(zlib_filefunc64_32_def* p_filefunc64_32,const zlib_filefunc_def* p_filefunc32); 192 | 193 | #define ZOPEN64(filefunc,filename,mode) (call_zopen64((&(filefunc)),(filename),(mode))) 194 | #define ZTELL64(filefunc,filestream) (call_ztell64((&(filefunc)),(filestream))) 195 | #define ZSEEK64(filefunc,filestream,pos,mode) (call_zseek64((&(filefunc)),(filestream),(pos),(mode))) 196 | 197 | #ifdef __cplusplus 198 | } 199 | #endif 200 | 201 | #endif 202 | -------------------------------------------------------------------------------- /SSZipArchive/minizip/ioapi.c: -------------------------------------------------------------------------------- 1 | /* ioapi.h -- IO base function header for compress/uncompress .zip 2 | part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html ) 3 | 4 | Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html ) 5 | 6 | Modifications for Zip64 support 7 | Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com ) 8 | 9 | For more info read MiniZip_info.txt 10 | 11 | */ 12 | 13 | #if (defined(_WIN32)) 14 | #define _CRT_SECURE_NO_WARNINGS 15 | #endif 16 | 17 | #include "ioapi.h" 18 | 19 | voidpf call_zopen64 (const zlib_filefunc64_32_def* pfilefunc,const void*filename,int mode) 20 | { 21 | if (pfilefunc->zfile_func64.zopen64_file != NULL) 22 | return (*(pfilefunc->zfile_func64.zopen64_file)) (pfilefunc->zfile_func64.opaque,filename,mode); 23 | else 24 | { 25 | return (*(pfilefunc->zopen32_file))(pfilefunc->zfile_func64.opaque,(const char*)filename,mode); 26 | } 27 | } 28 | 29 | long call_zseek64 (const zlib_filefunc64_32_def* pfilefunc,voidpf filestream, ZPOS64_T offset, int origin) 30 | { 31 | if (pfilefunc->zfile_func64.zseek64_file != NULL) 32 | return (*(pfilefunc->zfile_func64.zseek64_file)) (pfilefunc->zfile_func64.opaque,filestream,offset,origin); 33 | else 34 | { 35 | uLong offsetTruncated = (uLong)offset; 36 | if (offsetTruncated != offset) 37 | return -1; 38 | else 39 | return (*(pfilefunc->zseek32_file))(pfilefunc->zfile_func64.opaque,filestream,offsetTruncated,origin); 40 | } 41 | } 42 | 43 | ZPOS64_T call_ztell64 (const zlib_filefunc64_32_def* pfilefunc,voidpf filestream) 44 | { 45 | if (pfilefunc->zfile_func64.zseek64_file != NULL) 46 | return (*(pfilefunc->zfile_func64.ztell64_file)) (pfilefunc->zfile_func64.opaque,filestream); 47 | else 48 | { 49 | uLong tell_uLong = (*(pfilefunc->ztell32_file))(pfilefunc->zfile_func64.opaque,filestream); 50 | if ((tell_uLong) == ((uLong)-1)) 51 | return (ZPOS64_T)-1; 52 | else 53 | return tell_uLong; 54 | } 55 | } 56 | 57 | void fill_zlib_filefunc64_32_def_from_filefunc32(zlib_filefunc64_32_def* p_filefunc64_32,const zlib_filefunc_def* p_filefunc32) 58 | { 59 | p_filefunc64_32->zfile_func64.zopen64_file = NULL; 60 | p_filefunc64_32->zopen32_file = p_filefunc32->zopen_file; 61 | p_filefunc64_32->zfile_func64.zerror_file = p_filefunc32->zerror_file; 62 | p_filefunc64_32->zfile_func64.zread_file = p_filefunc32->zread_file; 63 | p_filefunc64_32->zfile_func64.zwrite_file = p_filefunc32->zwrite_file; 64 | p_filefunc64_32->zfile_func64.ztell64_file = NULL; 65 | p_filefunc64_32->zfile_func64.zseek64_file = NULL; 66 | p_filefunc64_32->zfile_func64.zclose_file = p_filefunc32->zclose_file; 67 | 68 | #ifndef __clang_analyzer__ 69 | p_filefunc64_32->zfile_func64.zerror_file = p_filefunc32->zerror_file; 70 | #endif 71 | 72 | p_filefunc64_32->zfile_func64.opaque = p_filefunc32->opaque; 73 | p_filefunc64_32->zseek32_file = p_filefunc32->zseek_file; 74 | p_filefunc64_32->ztell32_file = p_filefunc32->ztell_file; 75 | } 76 | 77 | 78 | 79 | static voidpf ZCALLBACK fopen_file_func OF((voidpf opaque, const char* filename, int mode)); 80 | static uLong ZCALLBACK fread_file_func OF((voidpf opaque, voidpf stream, void* buf, uLong size)); 81 | static uLong ZCALLBACK fwrite_file_func OF((voidpf opaque, voidpf stream, const void* buf,uLong size)); 82 | static ZPOS64_T ZCALLBACK ftell64_file_func OF((voidpf opaque, voidpf stream)); 83 | static long ZCALLBACK fseek64_file_func OF((voidpf opaque, voidpf stream, ZPOS64_T offset, int origin)); 84 | static int ZCALLBACK fclose_file_func OF((voidpf opaque, voidpf stream)); 85 | static int ZCALLBACK ferror_file_func OF((voidpf opaque, voidpf stream)); 86 | 87 | static voidpf ZCALLBACK fopen_file_func (voidpf opaque, const char* filename, int mode) 88 | { 89 | FILE* file = NULL; 90 | const char* mode_fopen = NULL; 91 | if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ) 92 | mode_fopen = "rb"; 93 | else 94 | if (mode & ZLIB_FILEFUNC_MODE_EXISTING) 95 | mode_fopen = "r+b"; 96 | else 97 | if (mode & ZLIB_FILEFUNC_MODE_CREATE) 98 | mode_fopen = "wb"; 99 | 100 | if ((filename!=NULL) && (mode_fopen != NULL)) 101 | file = fopen(filename, mode_fopen); 102 | return file; 103 | } 104 | 105 | static voidpf ZCALLBACK fopen64_file_func (voidpf opaque, const void* filename, int mode) 106 | { 107 | FILE* file = NULL; 108 | const char* mode_fopen = NULL; 109 | if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ) 110 | mode_fopen = "rb"; 111 | else 112 | if (mode & ZLIB_FILEFUNC_MODE_EXISTING) 113 | mode_fopen = "r+b"; 114 | else 115 | if (mode & ZLIB_FILEFUNC_MODE_CREATE) 116 | mode_fopen = "wb"; 117 | 118 | if ((filename!=NULL) && (mode_fopen != NULL)) 119 | file = fopen64((const char*)filename, mode_fopen); 120 | return file; 121 | } 122 | 123 | 124 | static uLong ZCALLBACK fread_file_func (voidpf opaque, voidpf stream, void* buf, uLong size) 125 | { 126 | uLong ret; 127 | ret = (uLong)fread(buf, 1, (size_t)size, (FILE *)stream); 128 | return ret; 129 | } 130 | 131 | static uLong ZCALLBACK fwrite_file_func (voidpf opaque, voidpf stream, const void* buf, uLong size) 132 | { 133 | uLong ret; 134 | ret = (uLong)fwrite(buf, 1, (size_t)size, (FILE *)stream); 135 | return ret; 136 | } 137 | 138 | static long ZCALLBACK ftell_file_func (voidpf opaque, voidpf stream) 139 | { 140 | long ret; 141 | ret = ftell((FILE *)stream); 142 | return ret; 143 | } 144 | 145 | 146 | static ZPOS64_T ZCALLBACK ftell64_file_func (voidpf opaque, voidpf stream) 147 | { 148 | ZPOS64_T ret; 149 | ret = ftello64((FILE *)stream); 150 | return ret; 151 | } 152 | 153 | static long ZCALLBACK fseek_file_func (voidpf opaque, voidpf stream, uLong offset, int origin) 154 | { 155 | int fseek_origin=0; 156 | long ret; 157 | switch (origin) 158 | { 159 | case ZLIB_FILEFUNC_SEEK_CUR : 160 | fseek_origin = SEEK_CUR; 161 | break; 162 | case ZLIB_FILEFUNC_SEEK_END : 163 | fseek_origin = SEEK_END; 164 | break; 165 | case ZLIB_FILEFUNC_SEEK_SET : 166 | fseek_origin = SEEK_SET; 167 | break; 168 | default: return -1; 169 | } 170 | ret = 0; 171 | if (fseek((FILE *)stream, offset, fseek_origin) != 0) 172 | ret = -1; 173 | return ret; 174 | } 175 | 176 | static long ZCALLBACK fseek64_file_func (voidpf opaque, voidpf stream, ZPOS64_T offset, int origin) 177 | { 178 | int fseek_origin=0; 179 | long ret; 180 | switch (origin) 181 | { 182 | case ZLIB_FILEFUNC_SEEK_CUR : 183 | fseek_origin = SEEK_CUR; 184 | break; 185 | case ZLIB_FILEFUNC_SEEK_END : 186 | fseek_origin = SEEK_END; 187 | break; 188 | case ZLIB_FILEFUNC_SEEK_SET : 189 | fseek_origin = SEEK_SET; 190 | break; 191 | default: return -1; 192 | } 193 | ret = 0; 194 | 195 | if(fseeko64((FILE *)stream, offset, fseek_origin) != 0) 196 | ret = -1; 197 | 198 | return ret; 199 | } 200 | 201 | 202 | static int ZCALLBACK fclose_file_func (voidpf opaque, voidpf stream) 203 | { 204 | int ret; 205 | ret = fclose((FILE *)stream); 206 | return ret; 207 | } 208 | 209 | static int ZCALLBACK ferror_file_func (voidpf opaque, voidpf stream) 210 | { 211 | int ret; 212 | ret = ferror((FILE *)stream); 213 | return ret; 214 | } 215 | 216 | void fill_fopen_filefunc (pzlib_filefunc_def) 217 | zlib_filefunc_def* pzlib_filefunc_def; 218 | { 219 | pzlib_filefunc_def->zopen_file = fopen_file_func; 220 | pzlib_filefunc_def->zread_file = fread_file_func; 221 | pzlib_filefunc_def->zwrite_file = fwrite_file_func; 222 | pzlib_filefunc_def->ztell_file = ftell_file_func; 223 | pzlib_filefunc_def->zseek_file = fseek_file_func; 224 | pzlib_filefunc_def->zclose_file = fclose_file_func; 225 | pzlib_filefunc_def->zerror_file = ferror_file_func; 226 | pzlib_filefunc_def->opaque = NULL; 227 | } 228 | 229 | void fill_fopen64_filefunc (zlib_filefunc64_def* pzlib_filefunc_def) 230 | { 231 | pzlib_filefunc_def->zopen64_file = fopen64_file_func; 232 | pzlib_filefunc_def->zread_file = fread_file_func; 233 | pzlib_filefunc_def->zwrite_file = fwrite_file_func; 234 | pzlib_filefunc_def->ztell64_file = ftell64_file_func; 235 | pzlib_filefunc_def->zseek64_file = fseek64_file_func; 236 | pzlib_filefunc_def->zclose_file = fclose_file_func; 237 | pzlib_filefunc_def->zerror_file = ferror_file_func; 238 | pzlib_filefunc_def->opaque = NULL; 239 | } 240 | -------------------------------------------------------------------------------- /SSZipArchive/minizip/mztools.c: -------------------------------------------------------------------------------- 1 | /* 2 | Additional tools for Minizip 3 | Code: Xavier Roche '2004 4 | License: Same as ZLIB (www.gzip.org) 5 | */ 6 | 7 | /* Code */ 8 | #include 9 | #include 10 | #include 11 | #include "zlib.h" 12 | #include "unzip.h" 13 | #include "mztools.h" 14 | 15 | #define READ_8(adr) ((unsigned char)*(adr)) 16 | #define READ_16(adr) ( READ_8(adr) | (READ_8(adr+1) << 8) ) 17 | #define READ_32(adr) ( READ_16(adr) | (READ_16((adr)+2) << 16) ) 18 | 19 | #define WRITE_8(buff, n) do { \ 20 | *((unsigned char*)(buff)) = (unsigned char) ((n) & 0xff); \ 21 | } while(0) 22 | #define WRITE_16(buff, n) do { \ 23 | WRITE_8((unsigned char*)(buff), n); \ 24 | WRITE_8(((unsigned char*)(buff)) + 1, (n) >> 8); \ 25 | } while(0) 26 | #define WRITE_32(buff, n) do { \ 27 | WRITE_16((unsigned char*)(buff), (n) & 0xffff); \ 28 | WRITE_16((unsigned char*)(buff) + 2, (n) >> 16); \ 29 | } while(0) 30 | 31 | extern int ZEXPORT unzRepair(file, fileOut, fileOutTmp, nRecovered, bytesRecovered) 32 | const char* file; 33 | const char* fileOut; 34 | const char* fileOutTmp; 35 | uLong* nRecovered; 36 | uLong* bytesRecovered; 37 | { 38 | int err = Z_OK; 39 | FILE* fpZip = fopen(file, "rb"); 40 | FILE* fpOut = fopen(fileOut, "wb"); 41 | FILE* fpOutCD = fopen(fileOutTmp, "wb"); 42 | if (fpZip != NULL && fpOut != NULL) { 43 | int entries = 0; 44 | uLong totalBytes = 0; 45 | char header[30]; 46 | char filename[256]; 47 | char extra[1024]; 48 | int offset = 0; 49 | int offsetCD = 0; 50 | while ( fread(header, 1, 30, fpZip) == 30 ) { 51 | int currentOffset = offset; 52 | 53 | /* File entry */ 54 | if (READ_32(header) == 0x04034b50) { 55 | unsigned int version = READ_16(header + 4); 56 | unsigned int gpflag = READ_16(header + 6); 57 | unsigned int method = READ_16(header + 8); 58 | unsigned int filetime = READ_16(header + 10); 59 | unsigned int filedate = READ_16(header + 12); 60 | unsigned int crc = READ_32(header + 14); /* crc */ 61 | unsigned int cpsize = READ_32(header + 18); /* compressed size */ 62 | unsigned int uncpsize = READ_32(header + 22); /* uncompressed sz */ 63 | unsigned int fnsize = READ_16(header + 26); /* file name length */ 64 | unsigned int extsize = READ_16(header + 28); /* extra field length */ 65 | filename[0] = extra[0] = '\0'; 66 | 67 | /* Header */ 68 | if (fwrite(header, 1, 30, fpOut) == 30) { 69 | offset += 30; 70 | } else { 71 | err = Z_ERRNO; 72 | break; 73 | } 74 | 75 | /* Filename */ 76 | if (fnsize > 0) { 77 | if (fread(filename, 1, fnsize, fpZip) == fnsize) { 78 | if (fwrite(filename, 1, fnsize, fpOut) == fnsize) { 79 | offset += fnsize; 80 | } else { 81 | err = Z_ERRNO; 82 | break; 83 | } 84 | } else { 85 | err = Z_ERRNO; 86 | break; 87 | } 88 | } else { 89 | err = Z_STREAM_ERROR; 90 | break; 91 | } 92 | 93 | /* Extra field */ 94 | if (extsize > 0) { 95 | if (fread(extra, 1, extsize, fpZip) == extsize) { 96 | if (fwrite(extra, 1, extsize, fpOut) == extsize) { 97 | offset += extsize; 98 | } else { 99 | err = Z_ERRNO; 100 | break; 101 | } 102 | } else { 103 | err = Z_ERRNO; 104 | break; 105 | } 106 | } 107 | 108 | /* Data */ 109 | { 110 | int dataSize = cpsize; 111 | if (dataSize == 0) { 112 | dataSize = uncpsize; 113 | } 114 | if (dataSize > 0) { 115 | char* data = malloc(dataSize); 116 | if (data != NULL) { 117 | if ((int)fread(data, 1, dataSize, fpZip) == dataSize) { 118 | if ((int)fwrite(data, 1, dataSize, fpOut) == dataSize) { 119 | offset += dataSize; 120 | totalBytes += dataSize; 121 | } else { 122 | err = Z_ERRNO; 123 | } 124 | } else { 125 | err = Z_ERRNO; 126 | } 127 | free(data); 128 | if (err != Z_OK) { 129 | break; 130 | } 131 | } else { 132 | err = Z_MEM_ERROR; 133 | break; 134 | } 135 | } 136 | } 137 | 138 | /* Central directory entry */ 139 | { 140 | char centralDirectoryEntryHeader[46]; 141 | //char* comment = ""; 142 | //int comsize = (int) strlen(comment); 143 | WRITE_32(centralDirectoryEntryHeader, 0x02014b50); 144 | WRITE_16(centralDirectoryEntryHeader + 4, version); 145 | WRITE_16(centralDirectoryEntryHeader + 6, version); 146 | WRITE_16(centralDirectoryEntryHeader + 8, gpflag); 147 | WRITE_16(centralDirectoryEntryHeader + 10, method); 148 | WRITE_16(centralDirectoryEntryHeader + 12, filetime); 149 | WRITE_16(centralDirectoryEntryHeader + 14, filedate); 150 | WRITE_32(centralDirectoryEntryHeader + 16, crc); 151 | WRITE_32(centralDirectoryEntryHeader + 20, cpsize); 152 | WRITE_32(centralDirectoryEntryHeader + 24, uncpsize); 153 | WRITE_16(centralDirectoryEntryHeader + 28, fnsize); 154 | WRITE_16(centralDirectoryEntryHeader + 30, extsize); 155 | WRITE_16(centralDirectoryEntryHeader + 32, 0 /*comsize*/); 156 | WRITE_16(centralDirectoryEntryHeader + 34, 0); /* disk # */ 157 | WRITE_16(centralDirectoryEntryHeader + 36, 0); /* int attrb */ 158 | WRITE_32(centralDirectoryEntryHeader + 38, 0); /* ext attrb */ 159 | WRITE_32(centralDirectoryEntryHeader + 42, currentOffset); 160 | /* Header */ 161 | if (fwrite(centralDirectoryEntryHeader, 1, 46, fpOutCD) == 46) { 162 | offsetCD += 46; 163 | 164 | /* Filename */ 165 | if (fnsize > 0) { 166 | if (fwrite(filename, 1, fnsize, fpOutCD) == fnsize) { 167 | offsetCD += fnsize; 168 | } else { 169 | err = Z_ERRNO; 170 | break; 171 | } 172 | } else { 173 | err = Z_STREAM_ERROR; 174 | break; 175 | } 176 | 177 | /* Extra field */ 178 | if (extsize > 0) { 179 | if (fwrite(extra, 1, extsize, fpOutCD) == extsize) { 180 | offsetCD += extsize; 181 | } else { 182 | err = Z_ERRNO; 183 | break; 184 | } 185 | } 186 | 187 | /* Comment field */ 188 | /* 189 | if (comsize > 0) { 190 | if ((int)fwrite(comment, 1, comsize, fpOutCD) == comsize) { 191 | offsetCD += comsize; 192 | } else { 193 | err = Z_ERRNO; 194 | break; 195 | } 196 | } 197 | */ 198 | 199 | } else { 200 | err = Z_ERRNO; 201 | break; 202 | } 203 | } 204 | 205 | /* Success */ 206 | entries++; 207 | 208 | } else { 209 | break; 210 | } 211 | } 212 | 213 | /* Final central directory */ 214 | { 215 | int entriesZip = entries; 216 | char finalCentralDirectoryHeader[22]; 217 | //char* comment = ""; // "ZIP File recovered by zlib/minizip/mztools"; 218 | //int comsize = (int) strlen(comment); 219 | if (entriesZip > 0xffff) { 220 | entriesZip = 0xffff; 221 | } 222 | WRITE_32(finalCentralDirectoryHeader, 0x06054b50); 223 | WRITE_16(finalCentralDirectoryHeader + 4, 0); /* disk # */ 224 | WRITE_16(finalCentralDirectoryHeader + 6, 0); /* disk # */ 225 | WRITE_16(finalCentralDirectoryHeader + 8, entriesZip); /* hack */ 226 | WRITE_16(finalCentralDirectoryHeader + 10, entriesZip); /* hack */ 227 | WRITE_32(finalCentralDirectoryHeader + 12, offsetCD); /* size of CD */ 228 | WRITE_32(finalCentralDirectoryHeader + 16, offset); /* offset to CD */ 229 | WRITE_16(finalCentralDirectoryHeader + 20, 0 /*comsize*/); /* comment */ 230 | 231 | /* Header */ 232 | if (fwrite(finalCentralDirectoryHeader, 1, 22, fpOutCD) == 22) { 233 | 234 | /* Comment field */ 235 | /* 236 | if (comsize > 0) { 237 | if ((int)fwrite(comment, 1, comsize, fpOutCD) != comsize) { 238 | err = Z_ERRNO; 239 | } 240 | } 241 | */ 242 | } else { 243 | err = Z_ERRNO; 244 | } 245 | } 246 | 247 | /* Final merge (file + central directory) */ 248 | fclose(fpOutCD); 249 | if (err == Z_OK) { 250 | fpOutCD = fopen(fileOutTmp, "rb"); 251 | if (fpOutCD != NULL) { 252 | int nRead; 253 | char buffer[8192]; 254 | while ( (nRead = (int)fread(buffer, 1, sizeof(buffer), fpOutCD)) > 0) { 255 | if ((int)fwrite(buffer, 1, nRead, fpOut) != nRead) { 256 | err = Z_ERRNO; 257 | break; 258 | } 259 | } 260 | fclose(fpOutCD); 261 | } 262 | } 263 | 264 | /* Close */ 265 | fclose(fpZip); 266 | fclose(fpOut); 267 | 268 | /* Wipe temporary file */ 269 | (void)remove(fileOutTmp); 270 | 271 | /* Number of recovered entries */ 272 | if (err == Z_OK) { 273 | if (nRecovered != NULL) { 274 | *nRecovered = entries; 275 | } 276 | if (bytesRecovered != NULL) { 277 | *bytesRecovered = totalBytes; 278 | } 279 | } 280 | } else { 281 | err = Z_STREAM_ERROR; 282 | } 283 | return err; 284 | } 285 | -------------------------------------------------------------------------------- /SSZipArchive/minizip/zip.h: -------------------------------------------------------------------------------- 1 | /* zip.h -- IO on .zip files using zlib 2 | Version 1.1, February 14h, 2010 3 | part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html ) 4 | 5 | Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html ) 6 | 7 | Modifications for Zip64 support 8 | Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com ) 9 | 10 | For more info read MiniZip_info.txt 11 | 12 | --------------------------------------------------------------------------- 13 | 14 | Condition of use and distribution are the same than zlib : 15 | 16 | This software is provided 'as-is', without any express or implied 17 | warranty. In no event will the authors be held liable for any damages 18 | arising from the use of this software. 19 | 20 | Permission is granted to anyone to use this software for any purpose, 21 | including commercial applications, and to alter it and redistribute it 22 | freely, subject to the following restrictions: 23 | 24 | 1. The origin of this software must not be misrepresented; you must not 25 | claim that you wrote the original software. If you use this software 26 | in a product, an acknowledgment in the product documentation would be 27 | appreciated but is not required. 28 | 2. Altered source versions must be plainly marked as such, and must not be 29 | misrepresented as being the original software. 30 | 3. This notice may not be removed or altered from any source distribution. 31 | 32 | --------------------------------------------------------------------------- 33 | 34 | Changes 35 | 36 | See header of zip.h 37 | 38 | */ 39 | 40 | #ifndef _zip12_H 41 | #define _zip12_H 42 | 43 | #ifdef __cplusplus 44 | extern "C" { 45 | #endif 46 | 47 | //#define HAVE_BZIP2 48 | 49 | #ifndef _ZLIB_H 50 | #include "zlib.h" 51 | #endif 52 | 53 | #ifndef _ZLIBIOAPI_H 54 | #include "ioapi.h" 55 | #endif 56 | 57 | #ifdef HAVE_BZIP2 58 | #include "bzlib.h" 59 | #endif 60 | 61 | #define Z_BZIP2ED 12 62 | 63 | #if defined(STRICTZIP) || defined(STRICTZIPUNZIP) 64 | /* like the STRICT of WIN32, we define a pointer that cannot be converted 65 | from (void*) without cast */ 66 | typedef struct TagzipFile__ { int unused; } zipFile__; 67 | typedef zipFile__ *zipFile; 68 | #else 69 | typedef voidp zipFile; 70 | #endif 71 | 72 | #define ZIP_OK (0) 73 | #define ZIP_EOF (0) 74 | #define ZIP_ERRNO (Z_ERRNO) 75 | #define ZIP_PARAMERROR (-102) 76 | #define ZIP_BADZIPFILE (-103) 77 | #define ZIP_INTERNALERROR (-104) 78 | 79 | #ifndef DEF_MEM_LEVEL 80 | # if MAX_MEM_LEVEL >= 8 81 | # define DEF_MEM_LEVEL 8 82 | # else 83 | # define DEF_MEM_LEVEL MAX_MEM_LEVEL 84 | # endif 85 | #endif 86 | /* default memLevel */ 87 | 88 | /* tm_zip contain date/time info */ 89 | typedef struct tm_zip_s 90 | { 91 | uInt tm_sec; /* seconds after the minute - [0,59] */ 92 | uInt tm_min; /* minutes after the hour - [0,59] */ 93 | uInt tm_hour; /* hours since midnight - [0,23] */ 94 | uInt tm_mday; /* day of the month - [1,31] */ 95 | uInt tm_mon; /* months since January - [0,11] */ 96 | uInt tm_year; /* years - [1980..2044] */ 97 | } tm_zip; 98 | 99 | typedef struct 100 | { 101 | tm_zip tmz_date; /* date in understandable format */ 102 | uLong dosDate; /* if dos_date == 0, tmu_date is used */ 103 | /* uLong flag; */ /* general purpose bit flag 2 bytes */ 104 | 105 | uLong internal_fa; /* internal file attributes 2 bytes */ 106 | uLong external_fa; /* external file attributes 4 bytes */ 107 | } zip_fileinfo; 108 | 109 | typedef const char* zipcharpc; 110 | 111 | 112 | #define APPEND_STATUS_CREATE (0) 113 | #define APPEND_STATUS_CREATEAFTER (1) 114 | #define APPEND_STATUS_ADDINZIP (2) 115 | 116 | extern zipFile ZEXPORT zipOpen OF((const char *pathname, int append)); 117 | extern zipFile ZEXPORT zipOpen64 OF((const void *pathname, int append)); 118 | /* 119 | Create a zipfile. 120 | pathname contain on Windows XP a filename like "c:\\zlib\\zlib113.zip" or on 121 | an Unix computer "zlib/zlib113.zip". 122 | if the file pathname exist and append==APPEND_STATUS_CREATEAFTER, the zip 123 | will be created at the end of the file. 124 | (useful if the file contain a self extractor code) 125 | if the file pathname exist and append==APPEND_STATUS_ADDINZIP, we will 126 | add files in existing zip (be sure you don't add file that doesn't exist) 127 | If the zipfile cannot be opened, the return value is NULL. 128 | Else, the return value is a zipFile Handle, usable with other function 129 | of this zip package. 130 | */ 131 | 132 | /* Note : there is no delete function into a zipfile. 133 | If you want delete file into a zipfile, you must open a zipfile, and create another 134 | Of couse, you can use RAW reading and writing to copy the file you did not want delte 135 | */ 136 | 137 | extern zipFile ZEXPORT zipOpen2 OF((const char *pathname, 138 | int append, 139 | zipcharpc* globalcomment, 140 | zlib_filefunc_def* pzlib_filefunc_def)); 141 | 142 | extern zipFile ZEXPORT zipOpen2_64 OF((const void *pathname, 143 | int append, 144 | zipcharpc* globalcomment, 145 | zlib_filefunc64_def* pzlib_filefunc_def)); 146 | 147 | extern int ZEXPORT zipOpenNewFileInZip OF((zipFile file, 148 | const char* filename, 149 | const zip_fileinfo* zipfi, 150 | const void* extrafield_local, 151 | uInt size_extrafield_local, 152 | const void* extrafield_global, 153 | uInt size_extrafield_global, 154 | const char* comment, 155 | int method, 156 | int level)); 157 | 158 | extern int ZEXPORT zipOpenNewFileInZip64 OF((zipFile file, 159 | const char* filename, 160 | const zip_fileinfo* zipfi, 161 | const void* extrafield_local, 162 | uInt size_extrafield_local, 163 | const void* extrafield_global, 164 | uInt size_extrafield_global, 165 | const char* comment, 166 | int method, 167 | int level, 168 | int zip64)); 169 | 170 | /* 171 | Open a file in the ZIP for writing. 172 | filename : the filename in zip (if NULL, '-' without quote will be used 173 | *zipfi contain supplemental information 174 | if extrafield_local!=NULL and size_extrafield_local>0, extrafield_local 175 | contains the extrafield data the the local header 176 | if extrafield_global!=NULL and size_extrafield_global>0, extrafield_global 177 | contains the extrafield data the the local header 178 | if comment != NULL, comment contain the comment string 179 | method contain the compression method (0 for store, Z_DEFLATED for deflate) 180 | level contain the level of compression (can be Z_DEFAULT_COMPRESSION) 181 | zip64 is set to 1 if a zip64 extended information block should be added to the local file header. 182 | this MUST be '1' if the uncompressed size is >= 0xffffffff. 183 | 184 | */ 185 | 186 | 187 | extern int ZEXPORT zipOpenNewFileInZip2 OF((zipFile file, 188 | const char* filename, 189 | const zip_fileinfo* zipfi, 190 | const void* extrafield_local, 191 | uInt size_extrafield_local, 192 | const void* extrafield_global, 193 | uInt size_extrafield_global, 194 | const char* comment, 195 | int method, 196 | int level, 197 | int raw)); 198 | 199 | 200 | extern int ZEXPORT zipOpenNewFileInZip2_64 OF((zipFile file, 201 | const char* filename, 202 | const zip_fileinfo* zipfi, 203 | const void* extrafield_local, 204 | uInt size_extrafield_local, 205 | const void* extrafield_global, 206 | uInt size_extrafield_global, 207 | const char* comment, 208 | int method, 209 | int level, 210 | int raw, 211 | int zip64)); 212 | /* 213 | Same than zipOpenNewFileInZip, except if raw=1, we write raw file 214 | */ 215 | 216 | extern int ZEXPORT zipOpenNewFileInZip3 OF((zipFile file, 217 | const char* filename, 218 | const zip_fileinfo* zipfi, 219 | const void* extrafield_local, 220 | uInt size_extrafield_local, 221 | const void* extrafield_global, 222 | uInt size_extrafield_global, 223 | const char* comment, 224 | int method, 225 | int level, 226 | int raw, 227 | int windowBits, 228 | int memLevel, 229 | int strategy, 230 | const char* password, 231 | uLong crcForCrypting)); 232 | 233 | extern int ZEXPORT zipOpenNewFileInZip3_64 OF((zipFile file, 234 | const char* filename, 235 | const zip_fileinfo* zipfi, 236 | const void* extrafield_local, 237 | uInt size_extrafield_local, 238 | const void* extrafield_global, 239 | uInt size_extrafield_global, 240 | const char* comment, 241 | int method, 242 | int level, 243 | int raw, 244 | int windowBits, 245 | int memLevel, 246 | int strategy, 247 | const char* password, 248 | uLong crcForCrypting, 249 | int zip64 250 | )); 251 | 252 | /* 253 | Same than zipOpenNewFileInZip2, except 254 | windowBits,memLevel,,strategy : see parameter strategy in deflateInit2 255 | password : crypting password (NULL for no crypting) 256 | crcForCrypting : crc of file to compress (needed for crypting) 257 | */ 258 | 259 | extern int ZEXPORT zipOpenNewFileInZip4 OF((zipFile file, 260 | const char* filename, 261 | const zip_fileinfo* zipfi, 262 | const void* extrafield_local, 263 | uInt size_extrafield_local, 264 | const void* extrafield_global, 265 | uInt size_extrafield_global, 266 | const char* comment, 267 | int method, 268 | int level, 269 | int raw, 270 | int windowBits, 271 | int memLevel, 272 | int strategy, 273 | const char* password, 274 | uLong crcForCrypting, 275 | uLong versionMadeBy, 276 | uLong flagBase 277 | )); 278 | 279 | 280 | extern int ZEXPORT zipOpenNewFileInZip4_64 OF((zipFile file, 281 | const char* filename, 282 | const zip_fileinfo* zipfi, 283 | const void* extrafield_local, 284 | uInt size_extrafield_local, 285 | const void* extrafield_global, 286 | uInt size_extrafield_global, 287 | const char* comment, 288 | int method, 289 | int level, 290 | int raw, 291 | int windowBits, 292 | int memLevel, 293 | int strategy, 294 | const char* password, 295 | uLong crcForCrypting, 296 | uLong versionMadeBy, 297 | uLong flagBase, 298 | int zip64 299 | )); 300 | /* 301 | Same than zipOpenNewFileInZip4, except 302 | versionMadeBy : value for Version made by field 303 | flag : value for flag field (compression level info will be added) 304 | */ 305 | 306 | 307 | extern int ZEXPORT zipWriteInFileInZip OF((zipFile file, 308 | const void* buf, 309 | unsigned len)); 310 | /* 311 | Write data in the zipfile 312 | */ 313 | 314 | extern int ZEXPORT zipCloseFileInZip OF((zipFile file)); 315 | /* 316 | Close the current file in the zipfile 317 | */ 318 | 319 | extern int ZEXPORT zipCloseFileInZipRaw OF((zipFile file, 320 | uLong uncompressed_size, 321 | uLong crc32)); 322 | 323 | extern int ZEXPORT zipCloseFileInZipRaw64 OF((zipFile file, 324 | ZPOS64_T uncompressed_size, 325 | uLong crc32)); 326 | 327 | /* 328 | Close the current file in the zipfile, for file opened with 329 | parameter raw=1 in zipOpenNewFileInZip2 330 | uncompressed_size and crc32 are value for the uncompressed size 331 | */ 332 | 333 | extern int ZEXPORT zipClose OF((zipFile file, 334 | const char* global_comment)); 335 | /* 336 | Close the zipfile 337 | */ 338 | 339 | 340 | extern int ZEXPORT zipRemoveExtraInfoBlock OF((char* pData, int* dataLen, short sHeader)); 341 | /* 342 | zipRemoveExtraInfoBlock - Added by Mathias Svensson 343 | 344 | Remove extra information block from a extra information data for the local file header or central directory header 345 | 346 | It is needed to remove ZIP64 extra information blocks when before data is written if using RAW mode. 347 | 348 | 0x0001 is the signature header for the ZIP64 extra information blocks 349 | 350 | usage. 351 | Remove ZIP64 Extra information from a central director extra field data 352 | zipRemoveExtraInfoBlock(pCenDirExtraFieldData, &nCenDirExtraFieldDataLen, 0x0001); 353 | 354 | Remove ZIP64 Extra information from a Local File Header extra field data 355 | zipRemoveExtraInfoBlock(pLocalHeaderExtraFieldData, &nLocalHeaderExtraFieldDataLen, 0x0001); 356 | */ 357 | 358 | #ifdef __cplusplus 359 | } 360 | #endif 361 | 362 | #endif /* _zip64_H */ 363 | -------------------------------------------------------------------------------- /SSZipArchive/minizip/unzip.h: -------------------------------------------------------------------------------- 1 | /* unzip.h -- IO for uncompress .zip files using zlib 2 | Version 1.1, February 14h, 2010 3 | part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html ) 4 | 5 | Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html ) 6 | 7 | Modifications of Unzip for Zip64 8 | Copyright (C) 2007-2008 Even Rouault 9 | 10 | Modifications for Zip64 support on both zip and unzip 11 | Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com ) 12 | 13 | For more info read MiniZip_info.txt 14 | 15 | --------------------------------------------------------------------------------- 16 | 17 | Condition of use and distribution are the same than zlib : 18 | 19 | This software is provided 'as-is', without any express or implied 20 | warranty. In no event will the authors be held liable for any damages 21 | arising from the use of this software. 22 | 23 | Permission is granted to anyone to use this software for any purpose, 24 | including commercial applications, and to alter it and redistribute it 25 | freely, subject to the following restrictions: 26 | 27 | 1. The origin of this software must not be misrepresented; you must not 28 | claim that you wrote the original software. If you use this software 29 | in a product, an acknowledgment in the product documentation would be 30 | appreciated but is not required. 31 | 2. Altered source versions must be plainly marked as such, and must not be 32 | misrepresented as being the original software. 33 | 3. This notice may not be removed or altered from any source distribution. 34 | 35 | --------------------------------------------------------------------------------- 36 | 37 | Changes 38 | 39 | See header of unzip64.c 40 | 41 | */ 42 | 43 | #ifndef _unz64_H 44 | #define _unz64_H 45 | 46 | #ifdef __cplusplus 47 | extern "C" { 48 | #endif 49 | 50 | #ifndef _ZLIB_H 51 | #include "zlib.h" 52 | #endif 53 | 54 | #ifndef _ZLIBIOAPI_H 55 | #include "ioapi.h" 56 | #endif 57 | 58 | #ifdef HAVE_BZIP2 59 | #include "bzlib.h" 60 | #endif 61 | 62 | #define Z_BZIP2ED 12 63 | 64 | #if defined(STRICTUNZIP) || defined(STRICTZIPUNZIP) 65 | /* like the STRICT of WIN32, we define a pointer that cannot be converted 66 | from (void*) without cast */ 67 | typedef struct TagunzFile__ { int unused; } unzFile__; 68 | typedef unzFile__ *unzFile; 69 | #else 70 | typedef voidp unzFile; 71 | #endif 72 | 73 | 74 | #define UNZ_OK (0) 75 | #define UNZ_END_OF_LIST_OF_FILE (-100) 76 | #define UNZ_ERRNO (Z_ERRNO) 77 | #define UNZ_EOF (0) 78 | #define UNZ_PARAMERROR (-102) 79 | #define UNZ_BADZIPFILE (-103) 80 | #define UNZ_INTERNALERROR (-104) 81 | #define UNZ_CRCERROR (-105) 82 | 83 | /* tm_unz contain date/time info */ 84 | typedef struct tm_unz_s 85 | { 86 | uInt tm_sec; /* seconds after the minute - [0,59] */ 87 | uInt tm_min; /* minutes after the hour - [0,59] */ 88 | uInt tm_hour; /* hours since midnight - [0,23] */ 89 | uInt tm_mday; /* day of the month - [1,31] */ 90 | uInt tm_mon; /* months since January - [0,11] */ 91 | uInt tm_year; /* years - [1980..2044] */ 92 | } tm_unz; 93 | 94 | /* unz_global_info structure contain global data about the ZIPfile 95 | These data comes from the end of central dir */ 96 | typedef struct unz_global_info64_s 97 | { 98 | ZPOS64_T number_entry; /* total number of entries in 99 | the central dir on this disk */ 100 | uLong size_comment; /* size of the global comment of the zipfile */ 101 | } unz_global_info64; 102 | 103 | typedef struct unz_global_info_s 104 | { 105 | uLong number_entry; /* total number of entries in 106 | the central dir on this disk */ 107 | uLong size_comment; /* size of the global comment of the zipfile */ 108 | } unz_global_info; 109 | 110 | /* unz_file_info contain information about a file in the zipfile */ 111 | typedef struct unz_file_info64_s 112 | { 113 | uLong version; /* version made by 2 bytes */ 114 | uLong version_needed; /* version needed to extract 2 bytes */ 115 | uLong flag; /* general purpose bit flag 2 bytes */ 116 | uLong compression_method; /* compression method 2 bytes */ 117 | uLong dosDate; /* last mod file date in Dos fmt 4 bytes */ 118 | uLong crc; /* crc-32 4 bytes */ 119 | ZPOS64_T compressed_size; /* compressed size 8 bytes */ 120 | ZPOS64_T uncompressed_size; /* uncompressed size 8 bytes */ 121 | uLong size_filename; /* filename length 2 bytes */ 122 | uLong size_file_extra; /* extra field length 2 bytes */ 123 | uLong size_file_comment; /* file comment length 2 bytes */ 124 | 125 | uLong disk_num_start; /* disk number start 2 bytes */ 126 | uLong internal_fa; /* internal file attributes 2 bytes */ 127 | uLong external_fa; /* external file attributes 4 bytes */ 128 | 129 | tm_unz tmu_date; 130 | } unz_file_info64; 131 | 132 | typedef struct unz_file_info_s 133 | { 134 | uLong version; /* version made by 2 bytes */ 135 | uLong version_needed; /* version needed to extract 2 bytes */ 136 | uLong flag; /* general purpose bit flag 2 bytes */ 137 | uLong compression_method; /* compression method 2 bytes */ 138 | uLong dosDate; /* last mod file date in Dos fmt 4 bytes */ 139 | uLong crc; /* crc-32 4 bytes */ 140 | uLong compressed_size; /* compressed size 4 bytes */ 141 | uLong uncompressed_size; /* uncompressed size 4 bytes */ 142 | uLong size_filename; /* filename length 2 bytes */ 143 | uLong size_file_extra; /* extra field length 2 bytes */ 144 | uLong size_file_comment; /* file comment length 2 bytes */ 145 | 146 | uLong disk_num_start; /* disk number start 2 bytes */ 147 | uLong internal_fa; /* internal file attributes 2 bytes */ 148 | uLong external_fa; /* external file attributes 4 bytes */ 149 | 150 | tm_unz tmu_date; 151 | } unz_file_info; 152 | 153 | extern int ZEXPORT unzStringFileNameCompare OF ((const char* fileName1, 154 | const char* fileName2, 155 | int iCaseSensitivity)); 156 | /* 157 | Compare two filename (fileName1,fileName2). 158 | If iCaseSenisivity = 1, comparision is case sensitivity (like strcmp) 159 | If iCaseSenisivity = 2, comparision is not case sensitivity (like strcmpi 160 | or strcasecmp) 161 | If iCaseSenisivity = 0, case sensitivity is defaut of your operating system 162 | (like 1 on Unix, 2 on Windows) 163 | */ 164 | 165 | 166 | extern unzFile ZEXPORT unzOpen OF((const char *path)); 167 | extern unzFile ZEXPORT unzOpen64 OF((const void *path)); 168 | /* 169 | Open a Zip file. path contain the full pathname (by example, 170 | on a Windows XP computer "c:\\zlib\\zlib113.zip" or on an Unix computer 171 | "zlib/zlib113.zip". 172 | If the zipfile cannot be opened (file don't exist or in not valid), the 173 | return value is NULL. 174 | Else, the return value is a unzFile Handle, usable with other function 175 | of this unzip package. 176 | the "64" function take a const void* pointer, because the path is just the 177 | value passed to the open64_file_func callback. 178 | Under Windows, if UNICODE is defined, using fill_fopen64_filefunc, the path 179 | is a pointer to a wide unicode string (LPCTSTR is LPCWSTR), so const char* 180 | does not describe the reality 181 | */ 182 | 183 | 184 | extern unzFile ZEXPORT unzOpen2 OF((const char *path, 185 | zlib_filefunc_def* pzlib_filefunc_def)); 186 | /* 187 | Open a Zip file, like unzOpen, but provide a set of file low level API 188 | for read/write the zip file (see ioapi.h) 189 | */ 190 | 191 | extern unzFile ZEXPORT unzOpen2_64 OF((const void *path, 192 | zlib_filefunc64_def* pzlib_filefunc_def)); 193 | /* 194 | Open a Zip file, like unz64Open, but provide a set of file low level API 195 | for read/write the zip file (see ioapi.h) 196 | */ 197 | 198 | extern int ZEXPORT unzClose OF((unzFile file)); 199 | /* 200 | Close a ZipFile opened with unzipOpen. 201 | If there is files inside the .Zip opened with unzOpenCurrentFile (see later), 202 | these files MUST be closed with unzipCloseCurrentFile before call unzipClose. 203 | return UNZ_OK if there is no problem. */ 204 | 205 | extern int ZEXPORT unzGetGlobalInfo OF((unzFile file, 206 | unz_global_info *pglobal_info)); 207 | 208 | extern int ZEXPORT unzGetGlobalInfo64 OF((unzFile file, 209 | unz_global_info64 *pglobal_info)); 210 | /* 211 | Write info about the ZipFile in the *pglobal_info structure. 212 | No preparation of the structure is needed 213 | return UNZ_OK if there is no problem. */ 214 | 215 | 216 | extern int ZEXPORT unzGetGlobalComment OF((unzFile file, 217 | char *szComment, 218 | uLong uSizeBuf)); 219 | /* 220 | Get the global comment string of the ZipFile, in the szComment buffer. 221 | uSizeBuf is the size of the szComment buffer. 222 | return the number of byte copied or an error code <0 223 | */ 224 | 225 | 226 | /***************************************************************************/ 227 | /* Unzip package allow you browse the directory of the zipfile */ 228 | 229 | extern int ZEXPORT unzGoToFirstFile OF((unzFile file)); 230 | /* 231 | Set the current file of the zipfile to the first file. 232 | return UNZ_OK if there is no problem 233 | */ 234 | 235 | extern int ZEXPORT unzGoToNextFile OF((unzFile file)); 236 | /* 237 | Set the current file of the zipfile to the next file. 238 | return UNZ_OK if there is no problem 239 | return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest. 240 | */ 241 | 242 | extern int ZEXPORT unzLocateFile OF((unzFile file, 243 | const char *szFileName, 244 | int iCaseSensitivity)); 245 | /* 246 | Try locate the file szFileName in the zipfile. 247 | For the iCaseSensitivity signification, see unzStringFileNameCompare 248 | 249 | return value : 250 | UNZ_OK if the file is found. It becomes the current file. 251 | UNZ_END_OF_LIST_OF_FILE if the file is not found 252 | */ 253 | 254 | 255 | /* ****************************************** */ 256 | /* Ryan supplied functions */ 257 | /* unz_file_info contain information about a file in the zipfile */ 258 | typedef struct unz_file_pos_s 259 | { 260 | uLong pos_in_zip_directory; /* offset in zip file directory */ 261 | uLong num_of_file; /* # of file */ 262 | } unz_file_pos; 263 | 264 | extern int ZEXPORT unzGetFilePos( 265 | unzFile file, 266 | unz_file_pos* file_pos); 267 | 268 | extern int ZEXPORT unzGoToFilePos( 269 | unzFile file, 270 | unz_file_pos* file_pos); 271 | 272 | typedef struct unz64_file_pos_s 273 | { 274 | ZPOS64_T pos_in_zip_directory; /* offset in zip file directory */ 275 | ZPOS64_T num_of_file; /* # of file */ 276 | } unz64_file_pos; 277 | 278 | extern int ZEXPORT unzGetFilePos64( 279 | unzFile file, 280 | unz64_file_pos* file_pos); 281 | 282 | extern int ZEXPORT unzGoToFilePos64( 283 | unzFile file, 284 | const unz64_file_pos* file_pos); 285 | 286 | /* ****************************************** */ 287 | 288 | extern int ZEXPORT unzGetCurrentFileInfo64 OF((unzFile file, 289 | unz_file_info64 *pfile_info, 290 | char *szFileName, 291 | uLong fileNameBufferSize, 292 | void *extraField, 293 | uLong extraFieldBufferSize, 294 | char *szComment, 295 | uLong commentBufferSize)); 296 | 297 | extern int ZEXPORT unzGetCurrentFileInfo OF((unzFile file, 298 | unz_file_info *pfile_info, 299 | char *szFileName, 300 | uLong fileNameBufferSize, 301 | void *extraField, 302 | uLong extraFieldBufferSize, 303 | char *szComment, 304 | uLong commentBufferSize)); 305 | /* 306 | Get Info about the current file 307 | if pfile_info!=NULL, the *pfile_info structure will contain somes info about 308 | the current file 309 | if szFileName!=NULL, the filemane string will be copied in szFileName 310 | (fileNameBufferSize is the size of the buffer) 311 | if extraField!=NULL, the extra field information will be copied in extraField 312 | (extraFieldBufferSize is the size of the buffer). 313 | This is the Central-header version of the extra field 314 | if szComment!=NULL, the comment string of the file will be copied in szComment 315 | (commentBufferSize is the size of the buffer) 316 | */ 317 | 318 | 319 | /** Addition for GDAL : START */ 320 | 321 | extern ZPOS64_T ZEXPORT unzGetCurrentFileZStreamPos64 OF((unzFile file)); 322 | 323 | /** Addition for GDAL : END */ 324 | 325 | 326 | /***************************************************************************/ 327 | /* for reading the content of the current zipfile, you can open it, read data 328 | from it, and close it (you can close it before reading all the file) 329 | */ 330 | 331 | extern int ZEXPORT unzOpenCurrentFile OF((unzFile file)); 332 | /* 333 | Open for reading data the current file in the zipfile. 334 | If there is no error, the return value is UNZ_OK. 335 | */ 336 | 337 | extern int ZEXPORT unzOpenCurrentFilePassword OF((unzFile file, 338 | const char* password)); 339 | /* 340 | Open for reading data the current file in the zipfile. 341 | password is a crypting password 342 | If there is no error, the return value is UNZ_OK. 343 | */ 344 | 345 | extern int ZEXPORT unzOpenCurrentFile2 OF((unzFile file, 346 | int* method, 347 | int* level, 348 | int raw)); 349 | /* 350 | Same than unzOpenCurrentFile, but open for read raw the file (not uncompress) 351 | if raw==1 352 | *method will receive method of compression, *level will receive level of 353 | compression 354 | note : you can set level parameter as NULL (if you did not want known level, 355 | but you CANNOT set method parameter as NULL 356 | */ 357 | 358 | extern int ZEXPORT unzOpenCurrentFile3 OF((unzFile file, 359 | int* method, 360 | int* level, 361 | int raw, 362 | const char* password)); 363 | /* 364 | Same than unzOpenCurrentFile, but open for read raw the file (not uncompress) 365 | if raw==1 366 | *method will receive method of compression, *level will receive level of 367 | compression 368 | note : you can set level parameter as NULL (if you did not want known level, 369 | but you CANNOT set method parameter as NULL 370 | */ 371 | 372 | 373 | extern int ZEXPORT unzCloseCurrentFile OF((unzFile file)); 374 | /* 375 | Close the file in zip opened with unzOpenCurrentFile 376 | Return UNZ_CRCERROR if all the file was read but the CRC is not good 377 | */ 378 | 379 | extern int ZEXPORT unzReadCurrentFile OF((unzFile file, 380 | voidp buf, 381 | unsigned len)); 382 | /* 383 | Read bytes from the current file (opened by unzOpenCurrentFile) 384 | buf contain buffer where data must be copied 385 | len the size of buf. 386 | 387 | return the number of byte copied if somes bytes are copied 388 | return 0 if the end of file was reached 389 | return <0 with error code if there is an error 390 | (UNZ_ERRNO for IO error, or zLib error for uncompress error) 391 | */ 392 | 393 | extern z_off_t ZEXPORT unztell OF((unzFile file)); 394 | 395 | extern ZPOS64_T ZEXPORT unztell64 OF((unzFile file)); 396 | /* 397 | Give the current position in uncompressed data 398 | */ 399 | 400 | extern int ZEXPORT unzeof OF((unzFile file)); 401 | /* 402 | return 1 if the end of file was reached, 0 elsewhere 403 | */ 404 | 405 | extern int ZEXPORT unzGetLocalExtrafield OF((unzFile file, 406 | voidp buf, 407 | unsigned len)); 408 | /* 409 | Read extra field from the current file (opened by unzOpenCurrentFile) 410 | This is the local-header version of the extra field (sometimes, there is 411 | more info in the local-header version than in the central-header) 412 | 413 | if buf==NULL, it return the size of the local extra field 414 | 415 | if buf!=NULL, len is the size of the buffer, the extra header is copied in 416 | buf. 417 | the return value is the number of bytes copied in buf, or (if <0) 418 | the error code 419 | */ 420 | 421 | /***************************************************************************/ 422 | 423 | /* Get the current file offset */ 424 | extern ZPOS64_T ZEXPORT unzGetOffset64 (unzFile file); 425 | extern uLong ZEXPORT unzGetOffset (unzFile file); 426 | 427 | /* Set the current file offset */ 428 | extern int ZEXPORT unzSetOffset64 (unzFile file, ZPOS64_T pos); 429 | extern int ZEXPORT unzSetOffset (unzFile file, uLong pos); 430 | 431 | 432 | 433 | #ifdef __cplusplus 434 | } 435 | #endif 436 | 437 | #endif /* _unz64_H */ 438 | -------------------------------------------------------------------------------- /SSZipArchive/SSZipArchive.m: -------------------------------------------------------------------------------- 1 | // 2 | // SSZipArchive.m 3 | // SSZipArchive 4 | // 5 | // Created by Sam Soffes on 7/21/10. 6 | // Copyright (c) Sam Soffes 2010-2013. All rights reserved. 7 | // 8 | 9 | #import "SSZipArchive.h" 10 | #include "minizip/zip.h" 11 | #import "zlib.h" 12 | #import "zconf.h" 13 | 14 | #include 15 | 16 | #define CHUNK 16384 17 | 18 | @interface SSZipArchive () 19 | + (NSDate *)_dateWithMSDOSFormat:(UInt32)msdosDateTime; 20 | @end 21 | 22 | 23 | @implementation SSZipArchive { 24 | NSString *_path; 25 | NSString *_filename; 26 | zipFile _zip; 27 | } 28 | 29 | 30 | #pragma mark - Unzipping 31 | 32 | + (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination uniqueId:(NSString *)uniqueId{ 33 | return [self unzipFileAtPath:path toDestination:destination delegate:nil uniqueId:uniqueId]; 34 | } 35 | 36 | 37 | + (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination overwrite:(BOOL)overwrite password:(NSString *)password error:(NSError **)error uniqueId:(NSString *)uniqueId { 38 | return [self unzipFileAtPath:path toDestination:destination overwrite:overwrite password:password error:error delegate:nil uniqueId:uniqueId]; 39 | } 40 | 41 | 42 | + (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination delegate:(id)delegate uniqueId:(NSString *)uniqueId{ 43 | return [self unzipFileAtPath:path 44 | toDestination:destination overwrite:YES password:nil error:nil delegate:delegate uniqueId:uniqueId]; 45 | } 46 | 47 | 48 | + (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination overwrite:(BOOL)overwrite password:(NSString *)password error:(NSError **)error delegate:(id)delegate uniqueId:(NSString *)uniqueId { 49 | // Begin opening 50 | zipFile zip = unzOpen((const char*)[path UTF8String]); 51 | if (zip == NULL) { 52 | NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"failed to open zip file" forKey:NSLocalizedDescriptionKey]; 53 | if (error) { 54 | *error = [NSError errorWithDomain:@"SSZipArchiveErrorDomain" code:-1 userInfo:userInfo]; 55 | } 56 | return NO; 57 | } 58 | 59 | unz_global_info globalInfo = {0ul, 0ul}; 60 | unzGetGlobalInfo(zip, &globalInfo); 61 | 62 | // Begin unzipping 63 | if (unzGoToFirstFile(zip) != UNZ_OK) { 64 | NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"failed to open first file in zip file" forKey:NSLocalizedDescriptionKey]; 65 | if (error) { 66 | *error = [NSError errorWithDomain:@"SSZipArchiveErrorDomain" code:-2 userInfo:userInfo]; 67 | } 68 | return NO; 69 | } 70 | 71 | BOOL success = YES; 72 | int ret = 0; 73 | unsigned char buffer[4096] = {0}; 74 | NSFileManager *fileManager = [NSFileManager defaultManager]; 75 | NSMutableSet *directoriesModificationDates = [[NSMutableSet alloc] init]; 76 | 77 | // Message delegate 78 | if ([delegate respondsToSelector:@selector(zipArchiveWillUnzipArchiveAtPath:zipInfo:)]) { 79 | [delegate zipArchiveWillUnzipArchiveAtPath:path zipInfo:globalInfo]; 80 | } 81 | 82 | NSInteger currentFileNumber = 0; 83 | do { 84 | @autoreleasepool { 85 | if ([password length] == 0) { 86 | ret = unzOpenCurrentFile(zip); 87 | } else { 88 | ret = unzOpenCurrentFilePassword(zip, [password cStringUsingEncoding:NSASCIIStringEncoding]); 89 | } 90 | 91 | if (ret != UNZ_OK) { 92 | success = NO; 93 | break; 94 | } 95 | 96 | // Reading data and write to file 97 | unz_file_info fileInfo; 98 | memset(&fileInfo, 0, sizeof(unz_file_info)); 99 | 100 | ret = unzGetCurrentFileInfo(zip, &fileInfo, NULL, 0, NULL, 0, NULL, 0); 101 | if (ret != UNZ_OK) { 102 | success = NO; 103 | unzCloseCurrentFile(zip); 104 | break; 105 | } 106 | 107 | // Message delegate 108 | if ([delegate respondsToSelector:@selector(zipArchiveWillUnzipFileAtIndex:totalFiles:archivePath:fileInfo:)]) { 109 | [delegate zipArchiveWillUnzipFileAtIndex:currentFileNumber totalFiles:(NSInteger)globalInfo.number_entry 110 | archivePath:path fileInfo:fileInfo]; 111 | } 112 | 113 | char *filename = (char *)malloc(fileInfo.size_filename + 1); 114 | unzGetCurrentFileInfo(zip, &fileInfo, filename, fileInfo.size_filename + 1, NULL, 0, NULL, 0); 115 | filename[fileInfo.size_filename] = '\0'; 116 | 117 | // 118 | // NOTE 119 | // I used the ZIP spec from here: 120 | // http://www.pkware.com/documents/casestudies/APPNOTE.TXT 121 | // 122 | // ...to deduce this method of detecting whether the file in the ZIP is a symbolic link. 123 | // If it is, it is listed as a directory but has a data size greater than zero (real 124 | // directories have it equal to 0) and the included, uncompressed data is the symbolic link path. 125 | // 126 | // ZIP files did not originally include support for symbolic links so the specification 127 | // doesn't include anything in them that isn't part of a unix extension that isn't being used 128 | // by the archivers we're testing. Most of this is figured out through trial and error and 129 | // reading ZIP headers in hex editors. This seems to do the trick though. 130 | // 131 | 132 | const uLong ZipCompressionMethodStore = 0; 133 | 134 | BOOL fileIsSymbolicLink = NO; 135 | 136 | if((fileInfo.compression_method == ZipCompressionMethodStore) && // Is it compressed? 137 | (S_ISDIR(fileInfo.external_fa)) && // Is it marked as a directory 138 | (fileInfo.compressed_size > 0)) // Is there any data? 139 | { 140 | fileIsSymbolicLink = YES; 141 | } 142 | 143 | // Check if it contains directory 144 | NSString *strPath = [NSString stringWithCString:filename encoding:NSUTF8StringEncoding]; 145 | BOOL isDirectory = NO; 146 | if (filename[fileInfo.size_filename-1] == '/' || filename[fileInfo.size_filename-1] == '\\') { 147 | isDirectory = YES; 148 | } 149 | free(filename); 150 | 151 | // Contains a path 152 | if ([strPath rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"/\\"]].location != NSNotFound) { 153 | strPath = [strPath stringByReplacingOccurrencesOfString:@"\\" withString:@"/"]; 154 | } 155 | 156 | NSString *fullPath = [destination stringByAppendingPathComponent:strPath]; 157 | NSError *err = nil; 158 | NSDate *modDate = [[self class] _dateWithMSDOSFormat:(UInt32)fileInfo.dosDate]; 159 | NSDictionary *directoryAttr = [NSDictionary dictionaryWithObjectsAndKeys:modDate, NSFileCreationDate, modDate, NSFileModificationDate, nil]; 160 | 161 | if (isDirectory) { 162 | [fileManager createDirectoryAtPath:fullPath withIntermediateDirectories:YES attributes:directoryAttr error:&err]; 163 | } else { 164 | [fileManager createDirectoryAtPath:[fullPath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:directoryAttr error:&err]; 165 | } 166 | if (nil != err) { 167 | NSLog(@"[SSZipArchive] Error: %@", err.localizedDescription); 168 | } 169 | 170 | if(!fileIsSymbolicLink) 171 | [directoriesModificationDates addObject: [NSDictionary dictionaryWithObjectsAndKeys:fullPath, @"path", modDate, @"modDate", nil]]; 172 | 173 | if ([fileManager fileExistsAtPath:fullPath] && !isDirectory && !overwrite) { 174 | unzCloseCurrentFile(zip); 175 | ret = unzGoToNextFile(zip); 176 | continue; 177 | } 178 | 179 | if(!fileIsSymbolicLink) 180 | { 181 | FILE *fp = fopen((const char*)[fullPath UTF8String], "wb"); 182 | while (fp) { 183 | int readBytes = unzReadCurrentFile(zip, buffer, 4096); 184 | 185 | if (readBytes > 0) { 186 | fwrite(buffer, readBytes, 1, fp ); 187 | } else { 188 | break; 189 | } 190 | } 191 | 192 | if (fp) { 193 | fclose(fp); 194 | 195 | // Set the original datetime property 196 | if (fileInfo.dosDate != 0) { 197 | NSDate *orgDate = [[self class] _dateWithMSDOSFormat:(UInt32)fileInfo.dosDate]; 198 | NSDictionary *attr = [NSDictionary dictionaryWithObject:orgDate forKey:NSFileModificationDate]; 199 | 200 | if (attr) { 201 | if ([fileManager setAttributes:attr ofItemAtPath:fullPath error:nil] == NO) { 202 | // Can't set attributes 203 | NSLog(@"[SSZipArchive] Failed to set attributes - whilst setting modification date"); 204 | } 205 | } 206 | } 207 | 208 | // Set the original permissions on the file 209 | uLong permissions = fileInfo.external_fa >> 16; 210 | if (permissions != 0) { 211 | // Store it into a NSNumber 212 | NSNumber *permissionsValue = @(permissions); 213 | 214 | // Retrieve any existing attributes 215 | NSMutableDictionary *attrs = [[NSMutableDictionary alloc] initWithDictionary:[fileManager attributesOfItemAtPath:fullPath error:nil]]; 216 | 217 | // Set the value in the attributes dict 218 | attrs[NSFilePosixPermissions] = permissionsValue; 219 | 220 | // Update attributes 221 | if ([fileManager setAttributes:attrs ofItemAtPath:fullPath error:nil] == NO) { 222 | // Unable to set the permissions attribute 223 | NSLog(@"[SSZipArchive] Failed to set attributes - whilst setting permissions"); 224 | } 225 | } 226 | } 227 | } 228 | else 229 | { 230 | // Get the path for the symbolic link 231 | 232 | NSURL* symlinkURL = [NSURL fileURLWithPath:fullPath]; 233 | NSMutableString* destinationPath = [NSMutableString string]; 234 | 235 | int bytesRead = 0; 236 | while((bytesRead = unzReadCurrentFile(zip, buffer, 4096)) > 0) 237 | { 238 | buffer[bytesRead] = 0; 239 | [destinationPath appendString:[NSString stringWithUTF8String:(const char*)buffer]]; 240 | } 241 | 242 | //NSLog(@"Symlinking to: %@", destinationPath); 243 | 244 | NSURL* destinationURL = [NSURL fileURLWithPath:destinationPath]; 245 | 246 | // Create the symbolic link 247 | NSError* symlinkError = nil; 248 | [fileManager createSymbolicLinkAtURL:symlinkURL withDestinationURL:destinationURL error:&symlinkError]; 249 | 250 | if(symlinkError != nil) 251 | { 252 | NSLog(@"Failed to create symbolic link at \"%@\" to \"%@\". Error: %@", symlinkURL.absoluteString, destinationURL.absoluteString, symlinkError.localizedDescription); 253 | } 254 | } 255 | 256 | unzCloseCurrentFile( zip ); 257 | ret = unzGoToNextFile( zip ); 258 | 259 | // Message delegate 260 | if ([delegate respondsToSelector:@selector(zipArchiveDidUnzipFileAtIndex:totalFiles:archivePath:fileInfo:)]) { 261 | [delegate zipArchiveDidUnzipFileAtIndex:currentFileNumber totalFiles:(NSInteger)globalInfo.number_entry 262 | archivePath:path fileInfo:fileInfo]; 263 | } 264 | 265 | currentFileNumber++; 266 | } 267 | } while(ret == UNZ_OK && ret != UNZ_END_OF_LIST_OF_FILE); 268 | 269 | // Close 270 | unzClose(zip); 271 | 272 | // The process of decompressing the .zip archive causes the modification times on the folders 273 | // to be set to the present time. So, when we are done, they need to be explicitly set. 274 | // set the modification date on all of the directories. 275 | NSError * err = nil; 276 | for (NSDictionary * d in directoriesModificationDates) { 277 | if (![[NSFileManager defaultManager] setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[d objectForKey:@"modDate"], NSFileModificationDate, nil] ofItemAtPath:[d objectForKey:@"path"] error:&err]) { 278 | NSLog(@"[SSZipArchive] Set attributes failed for directory: %@.", [d objectForKey:@"path"]); 279 | } 280 | if (err) { 281 | NSLog(@"[SSZipArchive] Error setting directory file modification date attribute: %@",err.localizedDescription); 282 | } 283 | } 284 | 285 | #if !__has_feature(objc_arc) 286 | [directoriesModificationDates release]; 287 | #endif 288 | 289 | // Message delegate 290 | if (success && [delegate respondsToSelector:@selector(zipArchiveDidUnzipArchiveAtPath:zipInfo:unzippedPath:uniqueId:)]) { 291 | [delegate zipArchiveDidUnzipArchiveAtPath:path zipInfo:globalInfo unzippedPath:destination uniqueId:uniqueId]; 292 | } 293 | 294 | return success; 295 | } 296 | 297 | 298 | #pragma mark - Zipping 299 | 300 | + (BOOL)createZipFileAtPath:(NSString *)path withFilesAtPaths:(NSArray *)paths { 301 | BOOL success = NO; 302 | SSZipArchive *zipArchive = [[SSZipArchive alloc] initWithPath:path]; 303 | if ([zipArchive open]) { 304 | for (NSString *path in paths) { 305 | [zipArchive writeFile:path]; 306 | } 307 | success = [zipArchive close]; 308 | } 309 | 310 | #if !__has_feature(objc_arc) 311 | [zipArchive release]; 312 | #endif 313 | 314 | return success; 315 | } 316 | 317 | 318 | + (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath { 319 | BOOL success = NO; 320 | 321 | NSFileManager *fileManager = nil; 322 | SSZipArchive *zipArchive = [[SSZipArchive alloc] initWithPath:path]; 323 | 324 | if ([zipArchive open]) { 325 | // use a local filemanager (queue/thread compatibility) 326 | fileManager = [[NSFileManager alloc] init]; 327 | NSDirectoryEnumerator *dirEnumerator = [fileManager enumeratorAtPath:directoryPath]; 328 | 329 | NSString *fileName; 330 | while ((fileName = [dirEnumerator nextObject])) { 331 | BOOL isDir; 332 | NSString *fullFilePath = [directoryPath stringByAppendingPathComponent:fileName]; 333 | [fileManager fileExistsAtPath:fullFilePath isDirectory:&isDir]; 334 | if (!isDir) { 335 | [zipArchive writeFileAtPath:fullFilePath withFileName:fileName]; 336 | } 337 | } 338 | success = [zipArchive close]; 339 | } 340 | 341 | #if !__has_feature(objc_arc) 342 | [fileManager release]; 343 | [zipArchive release]; 344 | #endif 345 | 346 | return success; 347 | } 348 | 349 | 350 | - (id)initWithPath:(NSString *)path { 351 | if ((self = [super init])) { 352 | _path = [path copy]; 353 | } 354 | return self; 355 | } 356 | 357 | 358 | #if !__has_feature(objc_arc) 359 | - (void)dealloc { 360 | [_path release]; 361 | [super dealloc]; 362 | } 363 | #endif 364 | 365 | 366 | - (BOOL)open { 367 | NSAssert((_zip == NULL), @"Attempting open an archive which is already open"); 368 | _zip = zipOpen([_path UTF8String], APPEND_STATUS_CREATE); 369 | return (NULL != _zip); 370 | } 371 | 372 | 373 | - (void)zipInfo:(zip_fileinfo*)zipInfo setDate:(NSDate*)date { 374 | NSCalendar *currentCalendar = [NSCalendar currentCalendar]; 375 | uint flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; 376 | NSDateComponents *components = [currentCalendar components:flags fromDate:date]; 377 | zipInfo->tmz_date.tm_sec = (unsigned int)components.second; 378 | zipInfo->tmz_date.tm_min = (unsigned int)components.minute; 379 | zipInfo->tmz_date.tm_hour = (unsigned int)components.hour; 380 | zipInfo->tmz_date.tm_mday = (unsigned int)components.day; 381 | zipInfo->tmz_date.tm_mon = (unsigned int)components.month - 1; 382 | zipInfo->tmz_date.tm_year = (unsigned int)components.year; 383 | } 384 | 385 | 386 | - (BOOL)writeFile:(NSString *)path 387 | { 388 | return [self writeFileAtPath:path withFileName:nil]; 389 | } 390 | 391 | // supports writing files with logical folder/directory structure 392 | // *path* is the absolute path of the file that will be compressed 393 | // *fileName* is the relative name of the file how it is stored within the zip e.g. /folder/subfolder/text1.txt 394 | - (BOOL)writeFileAtPath:(NSString *)path withFileName:(NSString *)fileName { 395 | NSAssert((_zip != NULL), @"Attempting to write to an archive which was never opened"); 396 | 397 | FILE *input = fopen([path UTF8String], "r"); 398 | if (NULL == input) { 399 | return NO; 400 | } 401 | 402 | const char *afileName; 403 | if (!fileName) { 404 | afileName = [path.lastPathComponent UTF8String]; 405 | } 406 | else { 407 | afileName = [fileName UTF8String]; 408 | } 409 | 410 | zip_fileinfo zipInfo = {{0}}; 411 | 412 | NSDictionary *attr = [[NSFileManager defaultManager] attributesOfItemAtPath:path error: nil]; 413 | if( attr ) 414 | { 415 | NSDate *fileDate = (NSDate *)[attr objectForKey:NSFileModificationDate]; 416 | if( fileDate ) 417 | { 418 | [self zipInfo:&zipInfo setDate: fileDate ]; 419 | } 420 | 421 | // Write permissions into the external attributes, for details on this see here: http://unix.stackexchange.com/a/14727 422 | // Get the permissions value from the files attributes 423 | NSNumber *permissionsValue = (NSNumber *)[attr objectForKey:NSFilePosixPermissions]; 424 | if (permissionsValue) { 425 | // Get the short value for the permissions 426 | short permissionsShort = permissionsValue.shortValue; 427 | 428 | // Convert this into an octal by adding 010000, 010000 being the flag for a regular file 429 | NSInteger permissionsOctal = 0100000 + permissionsShort; 430 | 431 | // Convert this into a long value 432 | uLong permissionsLong = @(permissionsOctal).unsignedLongValue; 433 | 434 | // Store this into the external file attributes once it has been shifted 16 places left to form part of the second from last byte 435 | zipInfo.external_fa = permissionsLong << 16L; 436 | } 437 | } 438 | 439 | zipOpenNewFileInZip(_zip, afileName, &zipInfo, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION); 440 | 441 | void *buffer = malloc(CHUNK); 442 | unsigned int len = 0; 443 | 444 | while (!feof(input)) 445 | { 446 | len = (unsigned int) fread(buffer, 1, CHUNK, input); 447 | zipWriteInFileInZip(_zip, buffer, len); 448 | } 449 | 450 | zipCloseFileInZip(_zip); 451 | free(buffer); 452 | fclose(input); 453 | return YES; 454 | } 455 | 456 | 457 | - (BOOL)writeData:(NSData *)data filename:(NSString *)filename { 458 | if (!_zip) { 459 | return NO; 460 | } 461 | if (!data) { 462 | return NO; 463 | } 464 | zip_fileinfo zipInfo = {{0,0,0,0,0,0},0,0,0}; 465 | [self zipInfo:&zipInfo setDate:[NSDate date]]; 466 | 467 | zipOpenNewFileInZip(_zip, [filename UTF8String], &zipInfo, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION); 468 | 469 | zipWriteInFileInZip(_zip, data.bytes, (unsigned int)data.length); 470 | 471 | zipCloseFileInZip(_zip); 472 | return YES; 473 | } 474 | 475 | 476 | - (BOOL)close { 477 | NSAssert((_zip != NULL), @"[SSZipArchive] Attempting to close an archive which was never opened"); 478 | zipClose(_zip, NULL); 479 | return YES; 480 | } 481 | 482 | 483 | #pragma mark - Private 484 | 485 | // Format from http://newsgroups.derkeiler.com/Archive/Comp/comp.os.msdos.programmer/2009-04/msg00060.html 486 | // Two consecutive words, or a longword, YYYYYYYMMMMDDDDD hhhhhmmmmmmsssss 487 | // YYYYYYY is years from 1980 = 0 488 | // sssss is (seconds/2). 489 | // 490 | // 3658 = 0011 0110 0101 1000 = 0011011 0010 11000 = 27 2 24 = 2007-02-24 491 | // 7423 = 0111 0100 0010 0011 - 01110 100001 00011 = 14 33 2 = 14:33:06 492 | + (NSDate *)_dateWithMSDOSFormat:(UInt32)msdosDateTime { 493 | static const UInt32 kYearMask = 0xFE000000; 494 | static const UInt32 kMonthMask = 0x1E00000; 495 | static const UInt32 kDayMask = 0x1F0000; 496 | static const UInt32 kHourMask = 0xF800; 497 | static const UInt32 kMinuteMask = 0x7E0; 498 | static const UInt32 kSecondMask = 0x1F; 499 | 500 | static NSCalendar *gregorian; 501 | static dispatch_once_t onceToken; 502 | dispatch_once(&onceToken, ^{ 503 | gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 504 | }); 505 | 506 | NSDateComponents *components = [[NSDateComponents alloc] init]; 507 | 508 | NSAssert(0xFFFFFFFF == (kYearMask | kMonthMask | kDayMask | kHourMask | kMinuteMask | kSecondMask), @"[SSZipArchive] MSDOS date masks don't add up"); 509 | 510 | [components setYear:1980 + ((msdosDateTime & kYearMask) >> 25)]; 511 | [components setMonth:(msdosDateTime & kMonthMask) >> 21]; 512 | [components setDay:(msdosDateTime & kDayMask) >> 16]; 513 | [components setHour:(msdosDateTime & kHourMask) >> 11]; 514 | [components setMinute:(msdosDateTime & kMinuteMask) >> 5]; 515 | [components setSecond:(msdosDateTime & kSecondMask) * 2]; 516 | 517 | NSDate *date = [NSDate dateWithTimeInterval:0 sinceDate:[gregorian dateFromComponents:components]]; 518 | 519 | #if !__has_feature(objc_arc) 520 | [components release]; 521 | #endif 522 | 523 | return date; 524 | } 525 | 526 | @end 527 | -------------------------------------------------------------------------------- /SSZipArchive/minizip/zip.c: -------------------------------------------------------------------------------- 1 | /* zip.c -- IO on .zip files using zlib 2 | Version 1.1, February 14h, 2010 3 | part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html ) 4 | 5 | Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html ) 6 | 7 | Modifications for Zip64 support 8 | Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com ) 9 | 10 | For more info read MiniZip_info.txt 11 | 12 | Changes 13 | Oct-2009 - Mathias Svensson - Remove old C style function prototypes 14 | Oct-2009 - Mathias Svensson - Added Zip64 Support when creating new file archives 15 | Oct-2009 - Mathias Svensson - Did some code cleanup and refactoring to get better overview of some functions. 16 | Oct-2009 - Mathias Svensson - Added zipRemoveExtraInfoBlock to strip extra field data from its ZIP64 data 17 | It is used when recreting zip archive with RAW when deleting items from a zip. 18 | ZIP64 data is automaticly added to items that needs it, and existing ZIP64 data need to be removed. 19 | Oct-2009 - Mathias Svensson - Added support for BZIP2 as compression mode (bzip2 lib is required) 20 | Jan-2010 - back to unzip and minizip 1.0 name scheme, with compatibility layer 21 | 22 | */ 23 | 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include "zlib.h" 30 | #include "zip.h" 31 | 32 | #ifdef STDC 33 | # include 34 | # include 35 | # include 36 | #endif 37 | #ifdef NO_ERRNO_H 38 | extern int errno; 39 | #else 40 | # include 41 | #endif 42 | 43 | 44 | #ifndef local 45 | # define local static 46 | #endif 47 | /* compile with -Dlocal if your debugger can't find static symbols */ 48 | 49 | #ifndef VERSIONMADEBY 50 | # define VERSIONMADEBY (0x0) /* platform depedent */ 51 | #endif 52 | 53 | #ifndef Z_BUFSIZE 54 | #define Z_BUFSIZE (64*1024) //(16384) 55 | #endif 56 | 57 | #ifndef Z_MAXFILENAMEINZIP 58 | #define Z_MAXFILENAMEINZIP (256) 59 | #endif 60 | 61 | #ifndef ALLOC 62 | # define ALLOC(size) (malloc(size)) 63 | #endif 64 | #ifndef TRYFREE 65 | # define TRYFREE(p) {if (p) free(p);} 66 | #endif 67 | 68 | /* 69 | #define SIZECENTRALDIRITEM (0x2e) 70 | #define SIZEZIPLOCALHEADER (0x1e) 71 | */ 72 | 73 | /* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */ 74 | 75 | 76 | // NOT sure that this work on ALL platform 77 | #define MAKEULONG64(a, b) ((ZPOS64_T)(((unsigned long)(a)) | ((ZPOS64_T)((unsigned long)(b))) << 32)) 78 | 79 | #ifndef SEEK_CUR 80 | #define SEEK_CUR 1 81 | #endif 82 | 83 | #ifndef SEEK_END 84 | #define SEEK_END 2 85 | #endif 86 | 87 | #ifndef SEEK_SET 88 | #define SEEK_SET 0 89 | #endif 90 | 91 | #ifndef DEF_MEM_LEVEL 92 | #if MAX_MEM_LEVEL >= 8 93 | # define DEF_MEM_LEVEL 8 94 | #else 95 | # define DEF_MEM_LEVEL MAX_MEM_LEVEL 96 | #endif 97 | #endif 98 | const char zip_copyright[] =" zip 1.01 Copyright 1998-2004 Gilles Vollant - http://www.winimage.com/zLibDll"; 99 | 100 | 101 | #define SIZEDATA_INDATABLOCK (4096-(4*4)) 102 | 103 | #define LOCALHEADERMAGIC (0x04034b50) 104 | #define CENTRALHEADERMAGIC (0x02014b50) 105 | #define ENDHEADERMAGIC (0x06054b50) 106 | #define ZIP64ENDHEADERMAGIC (0x6064b50) 107 | #define ZIP64ENDLOCHEADERMAGIC (0x7064b50) 108 | 109 | #define FLAG_LOCALHEADER_OFFSET (0x06) 110 | #define CRC_LOCALHEADER_OFFSET (0x0e) 111 | 112 | #define SIZECENTRALHEADER (0x2e) /* 46 */ 113 | 114 | typedef struct linkedlist_datablock_internal_s 115 | { 116 | struct linkedlist_datablock_internal_s* next_datablock; 117 | uLong avail_in_this_block; 118 | uLong filled_in_this_block; 119 | uLong unused; /* for future use and alignement */ 120 | unsigned char data[SIZEDATA_INDATABLOCK]; 121 | } linkedlist_datablock_internal; 122 | 123 | typedef struct linkedlist_data_s 124 | { 125 | linkedlist_datablock_internal* first_block; 126 | linkedlist_datablock_internal* last_block; 127 | } linkedlist_data; 128 | 129 | 130 | typedef struct 131 | { 132 | z_stream stream; /* zLib stream structure for inflate */ 133 | #ifdef HAVE_BZIP2 134 | bz_stream bstream; /* bzLib stream structure for bziped */ 135 | #endif 136 | 137 | int stream_initialised; /* 1 is stream is initialised */ 138 | uInt pos_in_buffered_data; /* last written byte in buffered_data */ 139 | 140 | ZPOS64_T pos_local_header; /* offset of the local header of the file 141 | currenty writing */ 142 | char* central_header; /* central header data for the current file */ 143 | uLong size_centralExtra; 144 | uLong size_centralheader; /* size of the central header for cur file */ 145 | uLong size_centralExtraFree; /* Extra bytes allocated to the centralheader but that are not used */ 146 | uLong flag; /* flag of the file currently writing */ 147 | 148 | int method; /* compression method of file currenty wr.*/ 149 | int raw; /* 1 for directly writing raw data */ 150 | Byte buffered_data[Z_BUFSIZE];/* buffer contain compressed data to be writ*/ 151 | uLong dosDate; 152 | uLong crc32; 153 | int encrypt; 154 | int zip64; /* Add ZIP64 extened information in the extra field */ 155 | ZPOS64_T pos_zip64extrainfo; 156 | ZPOS64_T totalCompressedData; 157 | ZPOS64_T totalUncompressedData; 158 | #ifndef NOCRYPT 159 | unsigned long keys[3]; /* keys defining the pseudo-random sequence */ 160 | const unsigned long* pcrc_32_tab; 161 | int crypt_header_size; 162 | #endif 163 | } curfile64_info; 164 | 165 | typedef struct 166 | { 167 | zlib_filefunc64_32_def z_filefunc; 168 | voidpf filestream; /* io structore of the zipfile */ 169 | linkedlist_data central_dir;/* datablock with central dir in construction*/ 170 | int in_opened_file_inzip; /* 1 if a file in the zip is currently writ.*/ 171 | curfile64_info ci; /* info on the file curretly writing */ 172 | 173 | ZPOS64_T begin_pos; /* position of the beginning of the zipfile */ 174 | ZPOS64_T add_position_when_writting_offset; 175 | ZPOS64_T number_entry; 176 | 177 | #ifndef NO_ADDFILEINEXISTINGZIP 178 | char *globalcomment; 179 | #endif 180 | 181 | } zip64_internal; 182 | 183 | 184 | #ifndef NOCRYPT 185 | #define INCLUDECRYPTINGCODE_IFCRYPTALLOWED 186 | #include "crypt.h" 187 | #endif 188 | 189 | local linkedlist_datablock_internal* allocate_new_datablock() 190 | { 191 | linkedlist_datablock_internal* ldi; 192 | ldi = (linkedlist_datablock_internal*) 193 | ALLOC(sizeof(linkedlist_datablock_internal)); 194 | if (ldi!=NULL) 195 | { 196 | ldi->next_datablock = NULL ; 197 | ldi->filled_in_this_block = 0 ; 198 | ldi->avail_in_this_block = SIZEDATA_INDATABLOCK ; 199 | } 200 | return ldi; 201 | } 202 | 203 | local void free_datablock(linkedlist_datablock_internal* ldi) 204 | { 205 | while (ldi!=NULL) 206 | { 207 | linkedlist_datablock_internal* ldinext = ldi->next_datablock; 208 | TRYFREE(ldi); 209 | ldi = ldinext; 210 | } 211 | } 212 | 213 | local void init_linkedlist(linkedlist_data* ll) 214 | { 215 | ll->first_block = ll->last_block = NULL; 216 | } 217 | 218 | local void free_linkedlist(linkedlist_data* ll) 219 | { 220 | free_datablock(ll->first_block); 221 | ll->first_block = ll->last_block = NULL; 222 | } 223 | 224 | 225 | local int add_data_in_datablock(linkedlist_data* ll, const void* buf, uLong len) 226 | { 227 | linkedlist_datablock_internal* ldi; 228 | const unsigned char* from_copy; 229 | 230 | if (ll==NULL) 231 | return ZIP_INTERNALERROR; 232 | 233 | if (ll->last_block == NULL) 234 | { 235 | ll->first_block = ll->last_block = allocate_new_datablock(); 236 | if (ll->first_block == NULL) 237 | return ZIP_INTERNALERROR; 238 | } 239 | 240 | ldi = ll->last_block; 241 | from_copy = (unsigned char*)buf; 242 | 243 | while (len>0) 244 | { 245 | uInt copy_this; 246 | uInt i; 247 | unsigned char* to_copy; 248 | 249 | if (ldi->avail_in_this_block==0) 250 | { 251 | ldi->next_datablock = allocate_new_datablock(); 252 | if (ldi->next_datablock == NULL) 253 | return ZIP_INTERNALERROR; 254 | ldi = ldi->next_datablock ; 255 | ll->last_block = ldi; 256 | } 257 | 258 | if (ldi->avail_in_this_block < len) 259 | copy_this = (uInt)ldi->avail_in_this_block; 260 | else 261 | copy_this = (uInt)len; 262 | 263 | to_copy = &(ldi->data[ldi->filled_in_this_block]); 264 | 265 | for (i=0;ifilled_in_this_block += copy_this; 269 | ldi->avail_in_this_block -= copy_this; 270 | from_copy += copy_this ; 271 | len -= copy_this; 272 | } 273 | return ZIP_OK; 274 | } 275 | 276 | 277 | 278 | /****************************************************************************/ 279 | 280 | #ifndef NO_ADDFILEINEXISTINGZIP 281 | /* =========================================================================== 282 | Inputs a long in LSB order to the given file 283 | nbByte == 1, 2 ,4 or 8 (byte, short or long, ZPOS64_T) 284 | */ 285 | 286 | local int zip64local_putValue OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T x, int nbByte)); 287 | local int zip64local_putValue (const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T x, int nbByte) 288 | { 289 | unsigned char buf[8]; 290 | int n; 291 | for (n = 0; n < nbByte; n++) 292 | { 293 | buf[n] = (unsigned char)(x & 0xff); 294 | x >>= 8; 295 | } 296 | if (x != 0) 297 | { /* data overflow - hack for ZIP64 (X Roche) */ 298 | for (n = 0; n < nbByte; n++) 299 | { 300 | buf[n] = 0xff; 301 | } 302 | } 303 | 304 | if (ZWRITE64(*pzlib_filefunc_def,filestream,buf,nbByte)!=(uLong)nbByte) 305 | return ZIP_ERRNO; 306 | else 307 | return ZIP_OK; 308 | } 309 | 310 | local void zip64local_putValue_inmemory OF((void* dest, ZPOS64_T x, int nbByte)); 311 | local void zip64local_putValue_inmemory (void* dest, ZPOS64_T x, int nbByte) 312 | { 313 | unsigned char* buf=(unsigned char*)dest; 314 | int n; 315 | for (n = 0; n < nbByte; n++) { 316 | buf[n] = (unsigned char)(x & 0xff); 317 | x >>= 8; 318 | } 319 | 320 | if (x != 0) 321 | { /* data overflow - hack for ZIP64 */ 322 | for (n = 0; n < nbByte; n++) 323 | { 324 | buf[n] = 0xff; 325 | } 326 | } 327 | } 328 | 329 | /****************************************************************************/ 330 | 331 | 332 | local uLong zip64local_TmzDateToDosDate(const tm_zip* ptm) 333 | { 334 | uLong year = (uLong)ptm->tm_year; 335 | if (year>=1980) 336 | year-=1980; 337 | else if (year>=80) 338 | year-=80; 339 | return 340 | (uLong) (((ptm->tm_mday) + (32 * (ptm->tm_mon+1)) + (512 * year)) << 16) | 341 | ((ptm->tm_sec/2) + (32* ptm->tm_min) + (2048 * (uLong)ptm->tm_hour)); 342 | } 343 | 344 | 345 | /****************************************************************************/ 346 | 347 | local int zip64local_getByte OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, int *pi)); 348 | 349 | local int zip64local_getByte(const zlib_filefunc64_32_def* pzlib_filefunc_def,voidpf filestream,int* pi) 350 | { 351 | unsigned char c; 352 | int err = (int)ZREAD64(*pzlib_filefunc_def,filestream,&c,1); 353 | if (err==1) 354 | { 355 | *pi = (int)c; 356 | return ZIP_OK; 357 | } 358 | else 359 | { 360 | if (ZERROR64(*pzlib_filefunc_def,filestream)) 361 | return ZIP_ERRNO; 362 | else 363 | return ZIP_EOF; 364 | } 365 | } 366 | 367 | 368 | /* =========================================================================== 369 | Reads a long in LSB order from the given gz_stream. Sets 370 | */ 371 | local int zip64local_getShort OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong *pX)); 372 | 373 | local int zip64local_getShort (const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong* pX) 374 | { 375 | uLong x ; 376 | int i = 0; 377 | int err; 378 | 379 | err = zip64local_getByte(pzlib_filefunc_def,filestream,&i); 380 | x = (uLong)i; 381 | 382 | if (err==ZIP_OK) 383 | err = zip64local_getByte(pzlib_filefunc_def,filestream,&i); 384 | x += ((uLong)i)<<8; 385 | 386 | if (err==ZIP_OK) 387 | *pX = x; 388 | else 389 | *pX = 0; 390 | return err; 391 | } 392 | 393 | local int zip64local_getLong OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong *pX)); 394 | 395 | local int zip64local_getLong (const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong* pX) 396 | { 397 | uLong x ; 398 | int i = 0; 399 | int err; 400 | 401 | err = zip64local_getByte(pzlib_filefunc_def,filestream,&i); 402 | x = (uLong)i; 403 | 404 | if (err==ZIP_OK) 405 | err = zip64local_getByte(pzlib_filefunc_def,filestream,&i); 406 | x += ((uLong)i)<<8; 407 | 408 | if (err==ZIP_OK) 409 | err = zip64local_getByte(pzlib_filefunc_def,filestream,&i); 410 | x += ((uLong)i)<<16; 411 | 412 | if (err==ZIP_OK) 413 | err = zip64local_getByte(pzlib_filefunc_def,filestream,&i); 414 | x += ((uLong)i)<<24; 415 | 416 | if (err==ZIP_OK) 417 | *pX = x; 418 | else 419 | *pX = 0; 420 | return err; 421 | } 422 | 423 | local int zip64local_getLong64 OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T *pX)); 424 | 425 | 426 | local int zip64local_getLong64 (const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T *pX) 427 | { 428 | ZPOS64_T x; 429 | int i = 0; 430 | int err; 431 | 432 | err = zip64local_getByte(pzlib_filefunc_def,filestream,&i); 433 | x = (ZPOS64_T)i; 434 | 435 | if (err==ZIP_OK) 436 | err = zip64local_getByte(pzlib_filefunc_def,filestream,&i); 437 | x += ((ZPOS64_T)i)<<8; 438 | 439 | if (err==ZIP_OK) 440 | err = zip64local_getByte(pzlib_filefunc_def,filestream,&i); 441 | x += ((ZPOS64_T)i)<<16; 442 | 443 | if (err==ZIP_OK) 444 | err = zip64local_getByte(pzlib_filefunc_def,filestream,&i); 445 | x += ((ZPOS64_T)i)<<24; 446 | 447 | if (err==ZIP_OK) 448 | err = zip64local_getByte(pzlib_filefunc_def,filestream,&i); 449 | x += ((ZPOS64_T)i)<<32; 450 | 451 | if (err==ZIP_OK) 452 | err = zip64local_getByte(pzlib_filefunc_def,filestream,&i); 453 | x += ((ZPOS64_T)i)<<40; 454 | 455 | if (err==ZIP_OK) 456 | err = zip64local_getByte(pzlib_filefunc_def,filestream,&i); 457 | x += ((ZPOS64_T)i)<<48; 458 | 459 | if (err==ZIP_OK) 460 | err = zip64local_getByte(pzlib_filefunc_def,filestream,&i); 461 | x += ((ZPOS64_T)i)<<56; 462 | 463 | if (err==ZIP_OK) 464 | *pX = x; 465 | else 466 | *pX = 0; 467 | 468 | return err; 469 | } 470 | 471 | #ifndef BUFREADCOMMENT 472 | #define BUFREADCOMMENT (0x400) 473 | #endif 474 | /* 475 | Locate the Central directory of a zipfile (at the end, just before 476 | the global comment) 477 | */ 478 | local ZPOS64_T zip64local_SearchCentralDir OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream)); 479 | 480 | local ZPOS64_T zip64local_SearchCentralDir(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream) 481 | { 482 | unsigned char* buf; 483 | ZPOS64_T uSizeFile; 484 | ZPOS64_T uBackRead; 485 | ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */ 486 | ZPOS64_T uPosFound=0; 487 | 488 | if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0) 489 | return 0; 490 | 491 | 492 | uSizeFile = ZTELL64(*pzlib_filefunc_def,filestream); 493 | 494 | if (uMaxBack>uSizeFile) 495 | uMaxBack = uSizeFile; 496 | 497 | buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4); 498 | if (buf==NULL) 499 | return 0; 500 | 501 | uBackRead = 4; 502 | while (uBackReaduMaxBack) 508 | uBackRead = uMaxBack; 509 | else 510 | uBackRead+=BUFREADCOMMENT; 511 | uReadPos = uSizeFile-uBackRead ; 512 | 513 | uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ? 514 | (BUFREADCOMMENT+4) : (uLong)(uSizeFile-uReadPos); 515 | if (ZSEEK64(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0) 516 | break; 517 | 518 | if (ZREAD64(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize) 519 | break; 520 | 521 | for (i=(int)uReadSize-3; (i--)>0;) 522 | if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) && 523 | ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06)) 524 | { 525 | uPosFound = uReadPos+i; 526 | break; 527 | } 528 | 529 | if (uPosFound!=0) 530 | break; 531 | } 532 | TRYFREE(buf); 533 | return uPosFound; 534 | } 535 | 536 | /* 537 | Locate the End of Zip64 Central directory locator and from there find the CD of a zipfile (at the end, just before 538 | the global comment) 539 | */ 540 | local ZPOS64_T zip64local_SearchCentralDir64 OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream)); 541 | 542 | local ZPOS64_T zip64local_SearchCentralDir64(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream) 543 | { 544 | unsigned char* buf; 545 | ZPOS64_T uSizeFile; 546 | ZPOS64_T uBackRead; 547 | ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */ 548 | ZPOS64_T uPosFound=0; 549 | uLong uL; 550 | ZPOS64_T relativeOffset; 551 | 552 | if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0) 553 | return 0; 554 | 555 | uSizeFile = ZTELL64(*pzlib_filefunc_def,filestream); 556 | 557 | if (uMaxBack>uSizeFile) 558 | uMaxBack = uSizeFile; 559 | 560 | buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4); 561 | if (buf==NULL) 562 | return 0; 563 | 564 | uBackRead = 4; 565 | while (uBackReaduMaxBack) 571 | uBackRead = uMaxBack; 572 | else 573 | uBackRead+=BUFREADCOMMENT; 574 | uReadPos = uSizeFile-uBackRead ; 575 | 576 | uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ? 577 | (BUFREADCOMMENT+4) : (uLong)(uSizeFile-uReadPos); 578 | if (ZSEEK64(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0) 579 | break; 580 | 581 | if (ZREAD64(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize) 582 | break; 583 | 584 | for (i=(int)uReadSize-3; (i--)>0;) 585 | { 586 | // Signature "0x07064b50" Zip64 end of central directory locater 587 | if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) && ((*(buf+i+2))==0x06) && ((*(buf+i+3))==0x07)) 588 | { 589 | uPosFound = uReadPos+i; 590 | break; 591 | } 592 | } 593 | 594 | if (uPosFound!=0) 595 | break; 596 | } 597 | 598 | TRYFREE(buf); 599 | if (uPosFound == 0) 600 | return 0; 601 | 602 | /* Zip64 end of central directory locator */ 603 | if (ZSEEK64(*pzlib_filefunc_def,filestream, uPosFound,ZLIB_FILEFUNC_SEEK_SET)!=0) 604 | return 0; 605 | 606 | /* the signature, already checked */ 607 | if (zip64local_getLong(pzlib_filefunc_def,filestream,&uL)!=ZIP_OK) 608 | return 0; 609 | 610 | /* number of the disk with the start of the zip64 end of central directory */ 611 | if (zip64local_getLong(pzlib_filefunc_def,filestream,&uL)!=ZIP_OK) 612 | return 0; 613 | if (uL != 0) 614 | return 0; 615 | 616 | /* relative offset of the zip64 end of central directory record */ 617 | if (zip64local_getLong64(pzlib_filefunc_def,filestream,&relativeOffset)!=ZIP_OK) 618 | return 0; 619 | 620 | /* total number of disks */ 621 | if (zip64local_getLong(pzlib_filefunc_def,filestream,&uL)!=ZIP_OK) 622 | return 0; 623 | if (uL != 1) 624 | return 0; 625 | 626 | /* Goto Zip64 end of central directory record */ 627 | if (ZSEEK64(*pzlib_filefunc_def,filestream, relativeOffset,ZLIB_FILEFUNC_SEEK_SET)!=0) 628 | return 0; 629 | 630 | /* the signature */ 631 | if (zip64local_getLong(pzlib_filefunc_def,filestream,&uL)!=ZIP_OK) 632 | return 0; 633 | 634 | if (uL != 0x06064b50) // signature of 'Zip64 end of central directory' 635 | return 0; 636 | 637 | return relativeOffset; 638 | } 639 | 640 | int LoadCentralDirectoryRecord(zip64_internal* pziinit); 641 | int LoadCentralDirectoryRecord(zip64_internal* pziinit) 642 | { 643 | int err=ZIP_OK; 644 | ZPOS64_T byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/ 645 | 646 | ZPOS64_T size_central_dir; /* size of the central directory */ 647 | ZPOS64_T offset_central_dir; /* offset of start of central directory */ 648 | ZPOS64_T central_pos; 649 | uLong uL; 650 | 651 | uLong number_disk; /* number of the current dist, used for 652 | spaning ZIP, unsupported, always 0*/ 653 | uLong number_disk_with_CD; /* number the the disk with central dir, used 654 | for spaning ZIP, unsupported, always 0*/ 655 | ZPOS64_T number_entry; 656 | ZPOS64_T number_entry_CD; /* total number of entries in 657 | the central dir 658 | (same than number_entry on nospan) */ 659 | uLong VersionMadeBy; 660 | uLong VersionNeeded; 661 | uLong size_comment; 662 | 663 | int hasZIP64Record = 0; 664 | 665 | // check first if we find a ZIP64 record 666 | central_pos = zip64local_SearchCentralDir64(&pziinit->z_filefunc,pziinit->filestream); 667 | if(central_pos > 0) 668 | { 669 | hasZIP64Record = 1; 670 | } 671 | else if(central_pos == 0) 672 | { 673 | central_pos = zip64local_SearchCentralDir(&pziinit->z_filefunc,pziinit->filestream); 674 | } 675 | 676 | /* disable to allow appending to empty ZIP archive 677 | if (central_pos==0) 678 | err=ZIP_ERRNO; 679 | */ 680 | 681 | if(hasZIP64Record) 682 | { 683 | ZPOS64_T sizeEndOfCentralDirectory; 684 | if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, central_pos, ZLIB_FILEFUNC_SEEK_SET) != 0) 685 | err=ZIP_ERRNO; 686 | 687 | /* the signature, already checked */ 688 | if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream,&uL)!=ZIP_OK) 689 | err=ZIP_ERRNO; 690 | 691 | /* size of zip64 end of central directory record */ 692 | if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream, &sizeEndOfCentralDirectory)!=ZIP_OK) 693 | err=ZIP_ERRNO; 694 | 695 | /* version made by */ 696 | if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &VersionMadeBy)!=ZIP_OK) 697 | err=ZIP_ERRNO; 698 | 699 | /* version needed to extract */ 700 | if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &VersionNeeded)!=ZIP_OK) 701 | err=ZIP_ERRNO; 702 | 703 | /* number of this disk */ 704 | if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream,&number_disk)!=ZIP_OK) 705 | err=ZIP_ERRNO; 706 | 707 | /* number of the disk with the start of the central directory */ 708 | if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream,&number_disk_with_CD)!=ZIP_OK) 709 | err=ZIP_ERRNO; 710 | 711 | /* total number of entries in the central directory on this disk */ 712 | if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream, &number_entry)!=ZIP_OK) 713 | err=ZIP_ERRNO; 714 | 715 | /* total number of entries in the central directory */ 716 | if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream,&number_entry_CD)!=ZIP_OK) 717 | err=ZIP_ERRNO; 718 | 719 | if ((number_entry_CD!=number_entry) || (number_disk_with_CD!=0) || (number_disk!=0)) 720 | err=ZIP_BADZIPFILE; 721 | 722 | /* size of the central directory */ 723 | if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream,&size_central_dir)!=ZIP_OK) 724 | err=ZIP_ERRNO; 725 | 726 | /* offset of start of central directory with respect to the 727 | starting disk number */ 728 | if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream,&offset_central_dir)!=ZIP_OK) 729 | err=ZIP_ERRNO; 730 | 731 | // TODO.. 732 | // read the comment from the standard central header. 733 | size_comment = 0; 734 | } 735 | else 736 | { 737 | // Read End of central Directory info 738 | if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, central_pos,ZLIB_FILEFUNC_SEEK_SET)!=0) 739 | err=ZIP_ERRNO; 740 | 741 | /* the signature, already checked */ 742 | if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream,&uL)!=ZIP_OK) 743 | err=ZIP_ERRNO; 744 | 745 | /* number of this disk */ 746 | if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream,&number_disk)!=ZIP_OK) 747 | err=ZIP_ERRNO; 748 | 749 | /* number of the disk with the start of the central directory */ 750 | if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream,&number_disk_with_CD)!=ZIP_OK) 751 | err=ZIP_ERRNO; 752 | 753 | /* total number of entries in the central dir on this disk */ 754 | number_entry = 0; 755 | if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &uL)!=ZIP_OK) 756 | err=ZIP_ERRNO; 757 | else 758 | number_entry = uL; 759 | 760 | /* total number of entries in the central dir */ 761 | number_entry_CD = 0; 762 | if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &uL)!=ZIP_OK) 763 | err=ZIP_ERRNO; 764 | else 765 | number_entry_CD = uL; 766 | 767 | if ((number_entry_CD!=number_entry) || (number_disk_with_CD!=0) || (number_disk!=0)) 768 | err=ZIP_BADZIPFILE; 769 | 770 | /* size of the central directory */ 771 | size_central_dir = 0; 772 | if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream, &uL)!=ZIP_OK) 773 | err=ZIP_ERRNO; 774 | else 775 | size_central_dir = uL; 776 | 777 | /* offset of start of central directory with respect to the starting disk number */ 778 | offset_central_dir = 0; 779 | if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream, &uL)!=ZIP_OK) 780 | err=ZIP_ERRNO; 781 | else 782 | offset_central_dir = uL; 783 | 784 | 785 | /* zipfile global comment length */ 786 | if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &size_comment)!=ZIP_OK) 787 | err=ZIP_ERRNO; 788 | } 789 | 790 | if ((central_posz_filefunc, pziinit->filestream); 797 | return ZIP_ERRNO; 798 | } 799 | 800 | if (size_comment>0) 801 | { 802 | pziinit->globalcomment = (char*)ALLOC(size_comment+1); 803 | if (pziinit->globalcomment) 804 | { 805 | size_comment = ZREAD64(pziinit->z_filefunc, pziinit->filestream, pziinit->globalcomment,size_comment); 806 | pziinit->globalcomment[size_comment]=0; 807 | } 808 | } 809 | 810 | byte_before_the_zipfile = central_pos - (offset_central_dir+size_central_dir); 811 | pziinit->add_position_when_writting_offset = byte_before_the_zipfile; 812 | 813 | { 814 | ZPOS64_T size_central_dir_to_read = size_central_dir; 815 | size_t buf_size = SIZEDATA_INDATABLOCK; 816 | void* buf_read = (void*)ALLOC(buf_size); 817 | if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, offset_central_dir + byte_before_the_zipfile, ZLIB_FILEFUNC_SEEK_SET) != 0) 818 | err=ZIP_ERRNO; 819 | 820 | while ((size_central_dir_to_read>0) && (err==ZIP_OK)) 821 | { 822 | ZPOS64_T read_this = SIZEDATA_INDATABLOCK; 823 | if (read_this > size_central_dir_to_read) 824 | read_this = size_central_dir_to_read; 825 | 826 | if (ZREAD64(pziinit->z_filefunc, pziinit->filestream,buf_read,(uLong)read_this) != read_this) 827 | err=ZIP_ERRNO; 828 | 829 | if (err==ZIP_OK) 830 | err = add_data_in_datablock(&pziinit->central_dir,buf_read, (uLong)read_this); 831 | 832 | size_central_dir_to_read-=read_this; 833 | } 834 | TRYFREE(buf_read); 835 | } 836 | pziinit->begin_pos = byte_before_the_zipfile; 837 | pziinit->number_entry = number_entry_CD; 838 | 839 | if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, offset_central_dir+byte_before_the_zipfile,ZLIB_FILEFUNC_SEEK_SET) != 0) 840 | err=ZIP_ERRNO; 841 | 842 | return err; 843 | } 844 | 845 | 846 | #endif /* !NO_ADDFILEINEXISTINGZIP*/ 847 | 848 | 849 | /************************************************************/ 850 | extern zipFile ZEXPORT zipOpen3 (const void *pathname, int append, zipcharpc* globalcomment, zlib_filefunc64_32_def* pzlib_filefunc64_32_def); 851 | extern zipFile ZEXPORT zipOpen3 (const void *pathname, int append, zipcharpc* globalcomment, zlib_filefunc64_32_def* pzlib_filefunc64_32_def) 852 | { 853 | zip64_internal ziinit; 854 | zip64_internal* zi; 855 | int err=ZIP_OK; 856 | 857 | ziinit.z_filefunc.zseek32_file = NULL; 858 | ziinit.z_filefunc.ztell32_file = NULL; 859 | if (pzlib_filefunc64_32_def==NULL) 860 | fill_fopen64_filefunc(&ziinit.z_filefunc.zfile_func64); 861 | else 862 | ziinit.z_filefunc = *pzlib_filefunc64_32_def; 863 | 864 | ziinit.filestream = ZOPEN64(ziinit.z_filefunc, 865 | pathname, 866 | (append == APPEND_STATUS_CREATE) ? 867 | (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_WRITE | ZLIB_FILEFUNC_MODE_CREATE) : 868 | (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_WRITE | ZLIB_FILEFUNC_MODE_EXISTING)); 869 | 870 | if (ziinit.filestream == NULL) 871 | return NULL; 872 | 873 | if (append == APPEND_STATUS_CREATEAFTER) 874 | ZSEEK64(ziinit.z_filefunc,ziinit.filestream,0,SEEK_END); 875 | 876 | ziinit.begin_pos = ZTELL64(ziinit.z_filefunc,ziinit.filestream); 877 | ziinit.in_opened_file_inzip = 0; 878 | ziinit.ci.stream_initialised = 0; 879 | ziinit.number_entry = 0; 880 | ziinit.add_position_when_writting_offset = 0; 881 | init_linkedlist(&(ziinit.central_dir)); 882 | 883 | 884 | 885 | zi = (zip64_internal*)ALLOC(sizeof(zip64_internal)); 886 | if (zi==NULL) 887 | { 888 | ZCLOSE64(ziinit.z_filefunc,ziinit.filestream); 889 | return NULL; 890 | } 891 | 892 | /* now we add file in a zipfile */ 893 | # ifndef NO_ADDFILEINEXISTINGZIP 894 | ziinit.globalcomment = NULL; 895 | if (append == APPEND_STATUS_ADDINZIP) 896 | { 897 | // Read and Cache Central Directory Records 898 | err = LoadCentralDirectoryRecord(&ziinit); 899 | } 900 | 901 | if (globalcomment) 902 | { 903 | *globalcomment = ziinit.globalcomment; 904 | } 905 | # endif /* !NO_ADDFILEINEXISTINGZIP*/ 906 | 907 | if (err != ZIP_OK) 908 | { 909 | # ifndef NO_ADDFILEINEXISTINGZIP 910 | TRYFREE(ziinit.globalcomment); 911 | # endif /* !NO_ADDFILEINEXISTINGZIP*/ 912 | TRYFREE(zi); 913 | return NULL; 914 | } 915 | else 916 | { 917 | *zi = ziinit; 918 | return (zipFile)zi; 919 | } 920 | } 921 | 922 | extern zipFile ZEXPORT zipOpen2 (const char *pathname, int append, zipcharpc* globalcomment, zlib_filefunc_def* pzlib_filefunc32_def) 923 | { 924 | if (pzlib_filefunc32_def != NULL) 925 | { 926 | zlib_filefunc64_32_def zlib_filefunc64_32_def_fill; 927 | fill_zlib_filefunc64_32_def_from_filefunc32(&zlib_filefunc64_32_def_fill,pzlib_filefunc32_def); 928 | return zipOpen3(pathname, append, globalcomment, &zlib_filefunc64_32_def_fill); 929 | } 930 | else 931 | return zipOpen3(pathname, append, globalcomment, NULL); 932 | } 933 | 934 | extern zipFile ZEXPORT zipOpen2_64 (const void *pathname, int append, zipcharpc* globalcomment, zlib_filefunc64_def* pzlib_filefunc_def) 935 | { 936 | if (pzlib_filefunc_def != NULL) 937 | { 938 | zlib_filefunc64_32_def zlib_filefunc64_32_def_fill; 939 | zlib_filefunc64_32_def_fill.zfile_func64 = *pzlib_filefunc_def; 940 | zlib_filefunc64_32_def_fill.ztell32_file = NULL; 941 | zlib_filefunc64_32_def_fill.zseek32_file = NULL; 942 | return zipOpen3(pathname, append, globalcomment, &zlib_filefunc64_32_def_fill); 943 | } 944 | else 945 | return zipOpen3(pathname, append, globalcomment, NULL); 946 | } 947 | 948 | 949 | 950 | extern zipFile ZEXPORT zipOpen (const char* pathname, int append) 951 | { 952 | return zipOpen3((const void*)pathname,append,NULL,NULL); 953 | } 954 | 955 | extern zipFile ZEXPORT zipOpen64 (const void* pathname, int append) 956 | { 957 | return zipOpen3(pathname,append,NULL,NULL); 958 | } 959 | 960 | int Write_LocalFileHeader(zip64_internal* zi, const char* filename, uInt size_extrafield_local, const void* extrafield_local); 961 | int Write_LocalFileHeader(zip64_internal* zi, const char* filename, uInt size_extrafield_local, const void* extrafield_local) 962 | { 963 | /* write the local header */ 964 | int err; 965 | uInt size_filename = (uInt)strlen(filename); 966 | uInt size_extrafield = size_extrafield_local; 967 | 968 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)LOCALHEADERMAGIC, 4); 969 | 970 | if (err==ZIP_OK) 971 | { 972 | if(zi->ci.zip64) 973 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)45,2);/* version needed to extract */ 974 | else 975 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)20,2);/* version needed to extract */ 976 | } 977 | 978 | if (err==ZIP_OK) 979 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.flag,2); 980 | 981 | if (err==ZIP_OK) 982 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.method,2); 983 | 984 | if (err==ZIP_OK) 985 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.dosDate,4); 986 | 987 | // CRC / Compressed size / Uncompressed size will be filled in later and rewritten later 988 | if (err==ZIP_OK) 989 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* crc 32, unknown */ 990 | if (err==ZIP_OK) 991 | { 992 | if(zi->ci.zip64) 993 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0xFFFFFFFF,4); /* compressed size, unknown */ 994 | else 995 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* compressed size, unknown */ 996 | } 997 | if (err==ZIP_OK) 998 | { 999 | if(zi->ci.zip64) 1000 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0xFFFFFFFF,4); /* uncompressed size, unknown */ 1001 | else 1002 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* uncompressed size, unknown */ 1003 | } 1004 | 1005 | if (err==ZIP_OK) 1006 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_filename,2); 1007 | 1008 | if(zi->ci.zip64) 1009 | { 1010 | size_extrafield += 20; 1011 | } 1012 | 1013 | if (err==ZIP_OK) 1014 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_extrafield,2); 1015 | 1016 | if ((err==ZIP_OK) && (size_filename > 0)) 1017 | { 1018 | if (ZWRITE64(zi->z_filefunc,zi->filestream,filename,size_filename)!=size_filename) 1019 | err = ZIP_ERRNO; 1020 | } 1021 | 1022 | if ((err==ZIP_OK) && (size_extrafield_local > 0)) 1023 | { 1024 | if (ZWRITE64(zi->z_filefunc, zi->filestream, extrafield_local, size_extrafield_local) != size_extrafield_local) 1025 | err = ZIP_ERRNO; 1026 | } 1027 | 1028 | 1029 | if ((err==ZIP_OK) && (zi->ci.zip64)) 1030 | { 1031 | // write the Zip64 extended info 1032 | short HeaderID = 1; 1033 | short DataSize = 16; 1034 | ZPOS64_T CompressedSize = 0; 1035 | ZPOS64_T UncompressedSize = 0; 1036 | 1037 | // Remember position of Zip64 extended info for the local file header. (needed when we update size after done with file) 1038 | zi->ci.pos_zip64extrainfo = ZTELL64(zi->z_filefunc,zi->filestream); 1039 | 1040 | #ifndef __clang_analyzer__ 1041 | err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (short)HeaderID,2); 1042 | err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (short)DataSize,2); 1043 | 1044 | err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (ZPOS64_T)UncompressedSize,8); 1045 | err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (ZPOS64_T)CompressedSize,8); 1046 | #endif 1047 | } 1048 | 1049 | return err; 1050 | } 1051 | 1052 | /* 1053 | NOTE. 1054 | When writing RAW the ZIP64 extended information in extrafield_local and extrafield_global needs to be stripped 1055 | before calling this function it can be done with zipRemoveExtraInfoBlock 1056 | 1057 | It is not done here because then we need to realloc a new buffer since parameters are 'const' and I want to minimize 1058 | unnecessary allocations. 1059 | */ 1060 | extern int ZEXPORT zipOpenNewFileInZip4_64 (zipFile file, const char* filename, const zip_fileinfo* zipfi, 1061 | const void* extrafield_local, uInt size_extrafield_local, 1062 | const void* extrafield_global, uInt size_extrafield_global, 1063 | const char* comment, int method, int level, int raw, 1064 | int windowBits,int memLevel, int strategy, 1065 | const char* password, uLong crcForCrypting, 1066 | uLong versionMadeBy, uLong flagBase, int zip64) 1067 | { 1068 | zip64_internal* zi; 1069 | uInt size_filename; 1070 | uInt size_comment; 1071 | uInt i; 1072 | int err = ZIP_OK; 1073 | 1074 | # ifdef NOCRYPT 1075 | if (password != NULL) 1076 | return ZIP_PARAMERROR; 1077 | # endif 1078 | 1079 | if (file == NULL) 1080 | return ZIP_PARAMERROR; 1081 | 1082 | #ifdef HAVE_BZIP2 1083 | if ((method!=0) && (method!=Z_DEFLATED) && (method!=Z_BZIP2ED)) 1084 | return ZIP_PARAMERROR; 1085 | #else 1086 | if ((method!=0) && (method!=Z_DEFLATED)) 1087 | return ZIP_PARAMERROR; 1088 | #endif 1089 | 1090 | zi = (zip64_internal*)file; 1091 | 1092 | if (zi->in_opened_file_inzip == 1) 1093 | { 1094 | err = zipCloseFileInZip (file); 1095 | if (err != ZIP_OK) 1096 | return err; 1097 | } 1098 | 1099 | if (filename==NULL) 1100 | filename="-"; 1101 | 1102 | if (comment==NULL) 1103 | size_comment = 0; 1104 | else 1105 | size_comment = (uInt)strlen(comment); 1106 | 1107 | size_filename = (uInt)strlen(filename); 1108 | 1109 | if (zipfi == NULL) 1110 | zi->ci.dosDate = 0; 1111 | else 1112 | { 1113 | if (zipfi->dosDate != 0) 1114 | zi->ci.dosDate = zipfi->dosDate; 1115 | else 1116 | zi->ci.dosDate = zip64local_TmzDateToDosDate(&zipfi->tmz_date); 1117 | } 1118 | 1119 | zi->ci.flag = flagBase; 1120 | if (level==8 || level==9) 1121 | zi->ci.flag |= 2; 1122 | if (level==2) 1123 | zi->ci.flag |= 4; 1124 | if (level==1) 1125 | zi->ci.flag |= 6; 1126 | if (password != NULL) 1127 | zi->ci.flag |= 1; 1128 | 1129 | zi->ci.crc32 = 0; 1130 | zi->ci.method = method; 1131 | zi->ci.encrypt = 0; 1132 | zi->ci.stream_initialised = 0; 1133 | zi->ci.pos_in_buffered_data = 0; 1134 | zi->ci.raw = raw; 1135 | zi->ci.pos_local_header = ZTELL64(zi->z_filefunc,zi->filestream); 1136 | 1137 | zi->ci.size_centralheader = SIZECENTRALHEADER + size_filename + size_extrafield_global + size_comment; 1138 | zi->ci.size_centralExtraFree = 32; // Extra space we have reserved in case we need to add ZIP64 extra info data 1139 | 1140 | zi->ci.central_header = (char*)ALLOC((uInt)zi->ci.size_centralheader + zi->ci.size_centralExtraFree); 1141 | 1142 | zi->ci.size_centralExtra = size_extrafield_global; 1143 | zip64local_putValue_inmemory(zi->ci.central_header,(uLong)CENTRALHEADERMAGIC,4); 1144 | /* version info */ 1145 | zip64local_putValue_inmemory(zi->ci.central_header+4,(uLong)versionMadeBy,2); 1146 | zip64local_putValue_inmemory(zi->ci.central_header+6,(uLong)20,2); 1147 | zip64local_putValue_inmemory(zi->ci.central_header+8,(uLong)zi->ci.flag,2); 1148 | zip64local_putValue_inmemory(zi->ci.central_header+10,(uLong)zi->ci.method,2); 1149 | zip64local_putValue_inmemory(zi->ci.central_header+12,(uLong)zi->ci.dosDate,4); 1150 | zip64local_putValue_inmemory(zi->ci.central_header+16,(uLong)0,4); /*crc*/ 1151 | zip64local_putValue_inmemory(zi->ci.central_header+20,(uLong)0,4); /*compr size*/ 1152 | zip64local_putValue_inmemory(zi->ci.central_header+24,(uLong)0,4); /*uncompr size*/ 1153 | zip64local_putValue_inmemory(zi->ci.central_header+28,(uLong)size_filename,2); 1154 | zip64local_putValue_inmemory(zi->ci.central_header+30,(uLong)size_extrafield_global,2); 1155 | zip64local_putValue_inmemory(zi->ci.central_header+32,(uLong)size_comment,2); 1156 | zip64local_putValue_inmemory(zi->ci.central_header+34,(uLong)0,2); /*disk nm start*/ 1157 | 1158 | if (zipfi==NULL) 1159 | zip64local_putValue_inmemory(zi->ci.central_header+36,(uLong)0,2); 1160 | else 1161 | zip64local_putValue_inmemory(zi->ci.central_header+36,(uLong)zipfi->internal_fa,2); 1162 | 1163 | if (zipfi==NULL) 1164 | zip64local_putValue_inmemory(zi->ci.central_header+38,(uLong)0,4); 1165 | else 1166 | zip64local_putValue_inmemory(zi->ci.central_header+38,(uLong)zipfi->external_fa,4); 1167 | 1168 | if(zi->ci.pos_local_header >= 0xffffffff) 1169 | zip64local_putValue_inmemory(zi->ci.central_header+42,(uLong)0xffffffff,4); 1170 | else 1171 | zip64local_putValue_inmemory(zi->ci.central_header+42,(uLong)zi->ci.pos_local_header - zi->add_position_when_writting_offset,4); 1172 | 1173 | for (i=0;ici.central_header+SIZECENTRALHEADER+i) = *(filename+i); 1175 | 1176 | for (i=0;ici.central_header+SIZECENTRALHEADER+size_filename+i) = 1178 | *(((const char*)extrafield_global)+i); 1179 | 1180 | for (i=0;ici.central_header+SIZECENTRALHEADER+size_filename+ 1182 | size_extrafield_global+i) = *(comment+i); 1183 | if (zi->ci.central_header == NULL) 1184 | return ZIP_INTERNALERROR; 1185 | 1186 | zi->ci.zip64 = zip64; 1187 | zi->ci.totalCompressedData = 0; 1188 | zi->ci.totalUncompressedData = 0; 1189 | zi->ci.pos_zip64extrainfo = 0; 1190 | 1191 | err = Write_LocalFileHeader(zi, filename, size_extrafield_local, extrafield_local); 1192 | 1193 | #ifdef HAVE_BZIP2 1194 | zi->ci.bstream.avail_in = (uInt)0; 1195 | zi->ci.bstream.avail_out = (uInt)Z_BUFSIZE; 1196 | zi->ci.bstream.next_out = (char*)zi->ci.buffered_data; 1197 | zi->ci.bstream.total_in_hi32 = 0; 1198 | zi->ci.bstream.total_in_lo32 = 0; 1199 | zi->ci.bstream.total_out_hi32 = 0; 1200 | zi->ci.bstream.total_out_lo32 = 0; 1201 | #endif 1202 | 1203 | zi->ci.stream.avail_in = (uInt)0; 1204 | zi->ci.stream.avail_out = (uInt)Z_BUFSIZE; 1205 | zi->ci.stream.next_out = zi->ci.buffered_data; 1206 | zi->ci.stream.total_in = 0; 1207 | zi->ci.stream.total_out = 0; 1208 | zi->ci.stream.data_type = Z_BINARY; 1209 | 1210 | #ifdef HAVE_BZIP2 1211 | if ((err==ZIP_OK) && (zi->ci.method == Z_DEFLATED || zi->ci.method == Z_BZIP2ED) && (!zi->ci.raw)) 1212 | #else 1213 | if ((err==ZIP_OK) && (zi->ci.method == Z_DEFLATED) && (!zi->ci.raw)) 1214 | #endif 1215 | { 1216 | if(zi->ci.method == Z_DEFLATED) 1217 | { 1218 | zi->ci.stream.zalloc = (alloc_func)0; 1219 | zi->ci.stream.zfree = (free_func)0; 1220 | zi->ci.stream.opaque = (voidpf)0; 1221 | 1222 | if (windowBits>0) 1223 | windowBits = -windowBits; 1224 | 1225 | err = deflateInit2(&zi->ci.stream, level, Z_DEFLATED, windowBits, memLevel, strategy); 1226 | 1227 | if (err==Z_OK) 1228 | zi->ci.stream_initialised = Z_DEFLATED; 1229 | } 1230 | else if(zi->ci.method == Z_BZIP2ED) 1231 | { 1232 | #ifdef HAVE_BZIP2 1233 | // Init BZip stuff here 1234 | zi->ci.bstream.bzalloc = 0; 1235 | zi->ci.bstream.bzfree = 0; 1236 | zi->ci.bstream.opaque = (voidpf)0; 1237 | 1238 | err = BZ2_bzCompressInit(&zi->ci.bstream, level, 0,35); 1239 | if(err == BZ_OK) 1240 | zi->ci.stream_initialised = Z_BZIP2ED; 1241 | #endif 1242 | } 1243 | 1244 | } 1245 | 1246 | # ifndef NOCRYPT 1247 | zi->ci.crypt_header_size = 0; 1248 | if ((err==Z_OK) && (password != NULL)) 1249 | { 1250 | unsigned char bufHead[RAND_HEAD_LEN]; 1251 | unsigned int sizeHead; 1252 | zi->ci.encrypt = 1; 1253 | zi->ci.pcrc_32_tab = (const unsigned long*)get_crc_table(); 1254 | /*init_keys(password,zi->ci.keys,zi->ci.pcrc_32_tab);*/ 1255 | 1256 | sizeHead=crypthead(password,bufHead,RAND_HEAD_LEN,zi->ci.keys,zi->ci.pcrc_32_tab,crcForCrypting); 1257 | zi->ci.crypt_header_size = sizeHead; 1258 | 1259 | if (ZWRITE64(zi->z_filefunc,zi->filestream,bufHead,sizeHead) != sizeHead) 1260 | err = ZIP_ERRNO; 1261 | } 1262 | # endif 1263 | 1264 | if (err==Z_OK) 1265 | zi->in_opened_file_inzip = 1; 1266 | return err; 1267 | } 1268 | 1269 | extern int ZEXPORT zipOpenNewFileInZip4 (zipFile file, const char* filename, const zip_fileinfo* zipfi, 1270 | const void* extrafield_local, uInt size_extrafield_local, 1271 | const void* extrafield_global, uInt size_extrafield_global, 1272 | const char* comment, int method, int level, int raw, 1273 | int windowBits,int memLevel, int strategy, 1274 | const char* password, uLong crcForCrypting, 1275 | uLong versionMadeBy, uLong flagBase) 1276 | { 1277 | return zipOpenNewFileInZip4_64 (file, filename, zipfi, 1278 | extrafield_local, size_extrafield_local, 1279 | extrafield_global, size_extrafield_global, 1280 | comment, method, level, raw, 1281 | windowBits, memLevel, strategy, 1282 | password, crcForCrypting, versionMadeBy, flagBase, 0); 1283 | } 1284 | 1285 | extern int ZEXPORT zipOpenNewFileInZip3 (zipFile file, const char* filename, const zip_fileinfo* zipfi, 1286 | const void* extrafield_local, uInt size_extrafield_local, 1287 | const void* extrafield_global, uInt size_extrafield_global, 1288 | const char* comment, int method, int level, int raw, 1289 | int windowBits,int memLevel, int strategy, 1290 | const char* password, uLong crcForCrypting) 1291 | { 1292 | return zipOpenNewFileInZip4_64 (file, filename, zipfi, 1293 | extrafield_local, size_extrafield_local, 1294 | extrafield_global, size_extrafield_global, 1295 | comment, method, level, raw, 1296 | windowBits, memLevel, strategy, 1297 | password, crcForCrypting, VERSIONMADEBY, 0, 0); 1298 | } 1299 | 1300 | extern int ZEXPORT zipOpenNewFileInZip3_64(zipFile file, const char* filename, const zip_fileinfo* zipfi, 1301 | const void* extrafield_local, uInt size_extrafield_local, 1302 | const void* extrafield_global, uInt size_extrafield_global, 1303 | const char* comment, int method, int level, int raw, 1304 | int windowBits,int memLevel, int strategy, 1305 | const char* password, uLong crcForCrypting, int zip64) 1306 | { 1307 | return zipOpenNewFileInZip4_64 (file, filename, zipfi, 1308 | extrafield_local, size_extrafield_local, 1309 | extrafield_global, size_extrafield_global, 1310 | comment, method, level, raw, 1311 | windowBits, memLevel, strategy, 1312 | password, crcForCrypting, VERSIONMADEBY, 0, zip64); 1313 | } 1314 | 1315 | extern int ZEXPORT zipOpenNewFileInZip2(zipFile file, const char* filename, const zip_fileinfo* zipfi, 1316 | const void* extrafield_local, uInt size_extrafield_local, 1317 | const void* extrafield_global, uInt size_extrafield_global, 1318 | const char* comment, int method, int level, int raw) 1319 | { 1320 | return zipOpenNewFileInZip4_64 (file, filename, zipfi, 1321 | extrafield_local, size_extrafield_local, 1322 | extrafield_global, size_extrafield_global, 1323 | comment, method, level, raw, 1324 | -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, 1325 | NULL, 0, VERSIONMADEBY, 0, 0); 1326 | } 1327 | 1328 | extern int ZEXPORT zipOpenNewFileInZip2_64(zipFile file, const char* filename, const zip_fileinfo* zipfi, 1329 | const void* extrafield_local, uInt size_extrafield_local, 1330 | const void* extrafield_global, uInt size_extrafield_global, 1331 | const char* comment, int method, int level, int raw, int zip64) 1332 | { 1333 | return zipOpenNewFileInZip4_64 (file, filename, zipfi, 1334 | extrafield_local, size_extrafield_local, 1335 | extrafield_global, size_extrafield_global, 1336 | comment, method, level, raw, 1337 | -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, 1338 | NULL, 0, VERSIONMADEBY, 0, zip64); 1339 | } 1340 | 1341 | extern int ZEXPORT zipOpenNewFileInZip64 (zipFile file, const char* filename, const zip_fileinfo* zipfi, 1342 | const void* extrafield_local, uInt size_extrafield_local, 1343 | const void*extrafield_global, uInt size_extrafield_global, 1344 | const char* comment, int method, int level, int zip64) 1345 | { 1346 | return zipOpenNewFileInZip4_64 (file, filename, zipfi, 1347 | extrafield_local, size_extrafield_local, 1348 | extrafield_global, size_extrafield_global, 1349 | comment, method, level, 0, 1350 | -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, 1351 | NULL, 0, VERSIONMADEBY, 0, zip64); 1352 | } 1353 | 1354 | extern int ZEXPORT zipOpenNewFileInZip (zipFile file, const char* filename, const zip_fileinfo* zipfi, 1355 | const void* extrafield_local, uInt size_extrafield_local, 1356 | const void*extrafield_global, uInt size_extrafield_global, 1357 | const char* comment, int method, int level) 1358 | { 1359 | return zipOpenNewFileInZip4_64 (file, filename, zipfi, 1360 | extrafield_local, size_extrafield_local, 1361 | extrafield_global, size_extrafield_global, 1362 | comment, method, level, 0, 1363 | -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, 1364 | NULL, 0, VERSIONMADEBY, 0, 0); 1365 | } 1366 | 1367 | local int zip64FlushWriteBuffer(zip64_internal* zi) 1368 | { 1369 | int err=ZIP_OK; 1370 | 1371 | if (zi->ci.encrypt != 0) 1372 | { 1373 | #ifndef NOCRYPT 1374 | uInt i; 1375 | int t; 1376 | for (i=0;ici.pos_in_buffered_data;i++) 1377 | zi->ci.buffered_data[i] = zencode(zi->ci.keys, zi->ci.pcrc_32_tab, zi->ci.buffered_data[i],t); 1378 | #endif 1379 | } 1380 | 1381 | if (ZWRITE64(zi->z_filefunc,zi->filestream,zi->ci.buffered_data,zi->ci.pos_in_buffered_data) != zi->ci.pos_in_buffered_data) 1382 | err = ZIP_ERRNO; 1383 | 1384 | zi->ci.totalCompressedData += zi->ci.pos_in_buffered_data; 1385 | 1386 | #ifdef HAVE_BZIP2 1387 | if(zi->ci.method == Z_BZIP2ED) 1388 | { 1389 | zi->ci.totalUncompressedData += zi->ci.bstream.total_in_lo32; 1390 | zi->ci.bstream.total_in_lo32 = 0; 1391 | zi->ci.bstream.total_in_hi32 = 0; 1392 | } 1393 | else 1394 | #endif 1395 | { 1396 | zi->ci.totalUncompressedData += zi->ci.stream.total_in; 1397 | zi->ci.stream.total_in = 0; 1398 | } 1399 | 1400 | 1401 | zi->ci.pos_in_buffered_data = 0; 1402 | 1403 | return err; 1404 | } 1405 | 1406 | extern int ZEXPORT zipWriteInFileInZip (zipFile file,const void* buf,unsigned int len) 1407 | { 1408 | zip64_internal* zi; 1409 | int err=ZIP_OK; 1410 | 1411 | if (file == NULL) 1412 | return ZIP_PARAMERROR; 1413 | zi = (zip64_internal*)file; 1414 | 1415 | if (zi->in_opened_file_inzip == 0) 1416 | return ZIP_PARAMERROR; 1417 | 1418 | zi->ci.crc32 = crc32(zi->ci.crc32,buf,(uInt)len); 1419 | 1420 | #ifdef HAVE_BZIP2 1421 | if(zi->ci.method == Z_BZIP2ED && (!zi->ci.raw)) 1422 | { 1423 | zi->ci.bstream.next_in = (void*)buf; 1424 | zi->ci.bstream.avail_in = len; 1425 | err = BZ_RUN_OK; 1426 | 1427 | while ((err==BZ_RUN_OK) && (zi->ci.bstream.avail_in>0)) 1428 | { 1429 | if (zi->ci.bstream.avail_out == 0) 1430 | { 1431 | if (zip64FlushWriteBuffer(zi) == ZIP_ERRNO) 1432 | err = ZIP_ERRNO; 1433 | zi->ci.bstream.avail_out = (uInt)Z_BUFSIZE; 1434 | zi->ci.bstream.next_out = (char*)zi->ci.buffered_data; 1435 | } 1436 | 1437 | 1438 | if(err != BZ_RUN_OK) 1439 | break; 1440 | 1441 | if ((zi->ci.method == Z_BZIP2ED) && (!zi->ci.raw)) 1442 | { 1443 | uLong uTotalOutBefore_lo = zi->ci.bstream.total_out_lo32; 1444 | // uLong uTotalOutBefore_hi = zi->ci.bstream.total_out_hi32; 1445 | err=BZ2_bzCompress(&zi->ci.bstream, BZ_RUN); 1446 | 1447 | zi->ci.pos_in_buffered_data += (uInt)(zi->ci.bstream.total_out_lo32 - uTotalOutBefore_lo) ; 1448 | } 1449 | } 1450 | 1451 | if(err == BZ_RUN_OK) 1452 | err = ZIP_OK; 1453 | } 1454 | else 1455 | #endif 1456 | { 1457 | zi->ci.stream.next_in = (Bytef*)buf; 1458 | zi->ci.stream.avail_in = len; 1459 | 1460 | while ((err==ZIP_OK) && (zi->ci.stream.avail_in>0)) 1461 | { 1462 | if (zi->ci.stream.avail_out == 0) 1463 | { 1464 | if (zip64FlushWriteBuffer(zi) == ZIP_ERRNO) 1465 | err = ZIP_ERRNO; 1466 | zi->ci.stream.avail_out = (uInt)Z_BUFSIZE; 1467 | zi->ci.stream.next_out = zi->ci.buffered_data; 1468 | } 1469 | 1470 | 1471 | if(err != ZIP_OK) 1472 | break; 1473 | 1474 | if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw)) 1475 | { 1476 | uLong uTotalOutBefore = zi->ci.stream.total_out; 1477 | err=deflate(&zi->ci.stream, Z_NO_FLUSH); 1478 | if(uTotalOutBefore > zi->ci.stream.total_out) 1479 | { 1480 | int bBreak = 0; 1481 | bBreak++; 1482 | } 1483 | 1484 | zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - uTotalOutBefore) ; 1485 | } 1486 | else 1487 | { 1488 | uInt copy_this,i; 1489 | if (zi->ci.stream.avail_in < zi->ci.stream.avail_out) 1490 | copy_this = zi->ci.stream.avail_in; 1491 | else 1492 | copy_this = zi->ci.stream.avail_out; 1493 | 1494 | for (i = 0; i < copy_this; i++) 1495 | *(((char*)zi->ci.stream.next_out)+i) = 1496 | *(((const char*)zi->ci.stream.next_in)+i); 1497 | { 1498 | zi->ci.stream.avail_in -= copy_this; 1499 | zi->ci.stream.avail_out-= copy_this; 1500 | zi->ci.stream.next_in+= copy_this; 1501 | zi->ci.stream.next_out+= copy_this; 1502 | zi->ci.stream.total_in+= copy_this; 1503 | zi->ci.stream.total_out+= copy_this; 1504 | zi->ci.pos_in_buffered_data += copy_this; 1505 | } 1506 | } 1507 | }// while(...) 1508 | } 1509 | 1510 | return err; 1511 | } 1512 | 1513 | extern int ZEXPORT zipCloseFileInZipRaw (zipFile file, uLong uncompressed_size, uLong crc32) 1514 | { 1515 | return zipCloseFileInZipRaw64 (file, uncompressed_size, crc32); 1516 | } 1517 | 1518 | extern int ZEXPORT zipCloseFileInZipRaw64 (zipFile file, ZPOS64_T uncompressed_size, uLong crc32) 1519 | { 1520 | zip64_internal* zi; 1521 | ZPOS64_T compressed_size; 1522 | uLong invalidValue = 0xffffffff; 1523 | short datasize = 0; 1524 | int err=ZIP_OK; 1525 | 1526 | if (file == NULL) 1527 | return ZIP_PARAMERROR; 1528 | zi = (zip64_internal*)file; 1529 | 1530 | if (zi->in_opened_file_inzip == 0) 1531 | return ZIP_PARAMERROR; 1532 | zi->ci.stream.avail_in = 0; 1533 | 1534 | if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw)) 1535 | { 1536 | while (err==ZIP_OK) 1537 | { 1538 | uLong uTotalOutBefore; 1539 | if (zi->ci.stream.avail_out == 0) 1540 | { 1541 | if (zip64FlushWriteBuffer(zi) == ZIP_ERRNO) 1542 | { 1543 | #ifndef __clang_analyzer__ 1544 | err = ZIP_ERRNO; 1545 | #endif 1546 | } 1547 | zi->ci.stream.avail_out = (uInt)Z_BUFSIZE; 1548 | #ifndef __clang_analyzer__ 1549 | zi->ci.stream.next_out = zi->ci.buffered_data; 1550 | #endif 1551 | } 1552 | uTotalOutBefore = zi->ci.stream.total_out; 1553 | err=deflate(&zi->ci.stream, Z_FINISH); 1554 | zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - uTotalOutBefore) ; 1555 | } 1556 | } 1557 | else if ((zi->ci.method == Z_BZIP2ED) && (!zi->ci.raw)) 1558 | { 1559 | #ifdef HAVE_BZIP2 1560 | err = BZ_FINISH_OK; 1561 | while (err==BZ_FINISH_OK) 1562 | { 1563 | uLong uTotalOutBefore; 1564 | if (zi->ci.bstream.avail_out == 0) 1565 | { 1566 | if (zip64FlushWriteBuffer(zi) == ZIP_ERRNO) 1567 | err = ZIP_ERRNO; 1568 | zi->ci.bstream.avail_out = (uInt)Z_BUFSIZE; 1569 | zi->ci.bstream.next_out = (char*)zi->ci.buffered_data; 1570 | } 1571 | uTotalOutBefore = zi->ci.bstream.total_out_lo32; 1572 | err=BZ2_bzCompress(&zi->ci.bstream, BZ_FINISH); 1573 | if(err == BZ_STREAM_END) 1574 | err = Z_STREAM_END; 1575 | 1576 | zi->ci.pos_in_buffered_data += (uInt)(zi->ci.bstream.total_out_lo32 - uTotalOutBefore); 1577 | } 1578 | 1579 | if(err == BZ_FINISH_OK) 1580 | err = ZIP_OK; 1581 | #endif 1582 | } 1583 | 1584 | if (err==Z_STREAM_END) 1585 | err=ZIP_OK; /* this is normal */ 1586 | 1587 | if ((zi->ci.pos_in_buffered_data>0) && (err==ZIP_OK)) 1588 | { 1589 | if (zip64FlushWriteBuffer(zi)==ZIP_ERRNO) 1590 | err = ZIP_ERRNO; 1591 | } 1592 | 1593 | if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw)) 1594 | { 1595 | int tmp_err = deflateEnd(&zi->ci.stream); 1596 | if (err == ZIP_OK) 1597 | err = tmp_err; 1598 | zi->ci.stream_initialised = 0; 1599 | } 1600 | #ifdef HAVE_BZIP2 1601 | else if((zi->ci.method == Z_BZIP2ED) && (!zi->ci.raw)) 1602 | { 1603 | int tmperr = BZ2_bzCompressEnd(&zi->ci.bstream); 1604 | if (err==ZIP_OK) 1605 | err = tmperr; 1606 | zi->ci.stream_initialised = 0; 1607 | } 1608 | #endif 1609 | 1610 | if (!zi->ci.raw) 1611 | { 1612 | crc32 = (uLong)zi->ci.crc32; 1613 | uncompressed_size = zi->ci.totalUncompressedData; 1614 | } 1615 | compressed_size = zi->ci.totalCompressedData; 1616 | 1617 | # ifndef NOCRYPT 1618 | compressed_size += zi->ci.crypt_header_size; 1619 | # endif 1620 | 1621 | // update Current Item crc and sizes, 1622 | if(compressed_size >= 0xffffffff || uncompressed_size >= 0xffffffff || zi->ci.pos_local_header >= 0xffffffff) 1623 | { 1624 | /*version Made by*/ 1625 | zip64local_putValue_inmemory(zi->ci.central_header+4,(uLong)45,2); 1626 | /*version needed*/ 1627 | zip64local_putValue_inmemory(zi->ci.central_header+6,(uLong)45,2); 1628 | 1629 | } 1630 | 1631 | zip64local_putValue_inmemory(zi->ci.central_header+16,crc32,4); /*crc*/ 1632 | 1633 | 1634 | if(compressed_size >= 0xffffffff) 1635 | zip64local_putValue_inmemory(zi->ci.central_header+20, invalidValue,4); /*compr size*/ 1636 | else 1637 | zip64local_putValue_inmemory(zi->ci.central_header+20, compressed_size,4); /*compr size*/ 1638 | 1639 | /// set internal file attributes field 1640 | if (zi->ci.stream.data_type == Z_ASCII) 1641 | zip64local_putValue_inmemory(zi->ci.central_header+36,(uLong)Z_ASCII,2); 1642 | 1643 | if(uncompressed_size >= 0xffffffff) 1644 | zip64local_putValue_inmemory(zi->ci.central_header+24, invalidValue,4); /*uncompr size*/ 1645 | else 1646 | zip64local_putValue_inmemory(zi->ci.central_header+24, uncompressed_size,4); /*uncompr size*/ 1647 | 1648 | // Add ZIP64 extra info field for uncompressed size 1649 | if(uncompressed_size >= 0xffffffff) 1650 | datasize += 8; 1651 | 1652 | // Add ZIP64 extra info field for compressed size 1653 | if(compressed_size >= 0xffffffff) 1654 | datasize += 8; 1655 | 1656 | // Add ZIP64 extra info field for relative offset to local file header of current file 1657 | if(zi->ci.pos_local_header >= 0xffffffff) 1658 | datasize += 8; 1659 | 1660 | if(datasize > 0) 1661 | { 1662 | char* p = NULL; 1663 | 1664 | if((uLong)(datasize + 4) > zi->ci.size_centralExtraFree) 1665 | { 1666 | // we can not write more data to the buffer that we have room for. 1667 | return ZIP_BADZIPFILE; 1668 | } 1669 | 1670 | p = zi->ci.central_header + zi->ci.size_centralheader; 1671 | 1672 | // Add Extra Information Header for 'ZIP64 information' 1673 | zip64local_putValue_inmemory(p, 0x0001, 2); // HeaderID 1674 | p += 2; 1675 | zip64local_putValue_inmemory(p, datasize, 2); // DataSize 1676 | p += 2; 1677 | 1678 | if(uncompressed_size >= 0xffffffff) 1679 | { 1680 | zip64local_putValue_inmemory(p, uncompressed_size, 8); 1681 | p += 8; 1682 | } 1683 | 1684 | if(compressed_size >= 0xffffffff) 1685 | { 1686 | zip64local_putValue_inmemory(p, compressed_size, 8); 1687 | p += 8; 1688 | } 1689 | 1690 | if(zi->ci.pos_local_header >= 0xffffffff) 1691 | { 1692 | zip64local_putValue_inmemory(p, zi->ci.pos_local_header, 8); 1693 | #ifndef __clang_analyzer__ 1694 | p += 8; 1695 | #endif 1696 | } 1697 | 1698 | // Update how much extra free space we got in the memory buffer 1699 | // and increase the centralheader size so the new ZIP64 fields are included 1700 | // ( 4 below is the size of HeaderID and DataSize field ) 1701 | zi->ci.size_centralExtraFree -= datasize + 4; 1702 | zi->ci.size_centralheader += datasize + 4; 1703 | 1704 | // Update the extra info size field 1705 | zi->ci.size_centralExtra += datasize + 4; 1706 | zip64local_putValue_inmemory(zi->ci.central_header+30,(uLong)zi->ci.size_centralExtra,2); 1707 | } 1708 | 1709 | if (err==ZIP_OK) 1710 | err = add_data_in_datablock(&zi->central_dir, zi->ci.central_header, (uLong)zi->ci.size_centralheader); 1711 | 1712 | free(zi->ci.central_header); 1713 | 1714 | if (err==ZIP_OK) 1715 | { 1716 | // Update the LocalFileHeader with the new values. 1717 | 1718 | ZPOS64_T cur_pos_inzip = ZTELL64(zi->z_filefunc,zi->filestream); 1719 | 1720 | if (ZSEEK64(zi->z_filefunc,zi->filestream, zi->ci.pos_local_header + 14,ZLIB_FILEFUNC_SEEK_SET)!=0) 1721 | err = ZIP_ERRNO; 1722 | 1723 | if (err==ZIP_OK) 1724 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,crc32,4); /* crc 32, unknown */ 1725 | 1726 | if(uncompressed_size >= 0xffffffff) 1727 | { 1728 | if(zi->ci.pos_zip64extrainfo > 0) 1729 | { 1730 | // Update the size in the ZIP64 extended field. 1731 | if (ZSEEK64(zi->z_filefunc,zi->filestream, zi->ci.pos_zip64extrainfo + 4,ZLIB_FILEFUNC_SEEK_SET)!=0) 1732 | err = ZIP_ERRNO; 1733 | 1734 | if (err==ZIP_OK) /* compressed size, unknown */ 1735 | err = zip64local_putValue(&zi->z_filefunc, zi->filestream, uncompressed_size, 8); 1736 | 1737 | if (err==ZIP_OK) /* uncompressed size, unknown */ 1738 | err = zip64local_putValue(&zi->z_filefunc, zi->filestream, compressed_size, 8); 1739 | } 1740 | } 1741 | else 1742 | { 1743 | if (err==ZIP_OK) /* compressed size, unknown */ 1744 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,compressed_size,4); 1745 | 1746 | if (err==ZIP_OK) /* uncompressed size, unknown */ 1747 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,uncompressed_size,4); 1748 | } 1749 | 1750 | if (ZSEEK64(zi->z_filefunc,zi->filestream, cur_pos_inzip,ZLIB_FILEFUNC_SEEK_SET)!=0) 1751 | err = ZIP_ERRNO; 1752 | } 1753 | 1754 | zi->number_entry ++; 1755 | zi->in_opened_file_inzip = 0; 1756 | 1757 | return err; 1758 | } 1759 | 1760 | extern int ZEXPORT zipCloseFileInZip (zipFile file) 1761 | { 1762 | return zipCloseFileInZipRaw (file,0,0); 1763 | } 1764 | 1765 | int Write_Zip64EndOfCentralDirectoryLocator(zip64_internal* zi, ZPOS64_T zip64eocd_pos_inzip); 1766 | int Write_Zip64EndOfCentralDirectoryLocator(zip64_internal* zi, ZPOS64_T zip64eocd_pos_inzip) 1767 | { 1768 | int err = ZIP_OK; 1769 | ZPOS64_T pos = zip64eocd_pos_inzip - zi->add_position_when_writting_offset; 1770 | 1771 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)ZIP64ENDLOCHEADERMAGIC,4); 1772 | 1773 | /*num disks*/ 1774 | if (err==ZIP_OK) /* number of the disk with the start of the central directory */ 1775 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); 1776 | 1777 | /*relative offset*/ 1778 | if (err==ZIP_OK) /* Relative offset to the Zip64EndOfCentralDirectory */ 1779 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream, pos,8); 1780 | 1781 | /*total disks*/ /* Do not support spawning of disk so always say 1 here*/ 1782 | if (err==ZIP_OK) /* number of the disk with the start of the central directory */ 1783 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)1,4); 1784 | 1785 | return err; 1786 | } 1787 | 1788 | int Write_Zip64EndOfCentralDirectoryRecord(zip64_internal* zi, uLong size_centraldir, ZPOS64_T centraldir_pos_inzip); 1789 | int Write_Zip64EndOfCentralDirectoryRecord(zip64_internal* zi, uLong size_centraldir, ZPOS64_T centraldir_pos_inzip) 1790 | { 1791 | int err = ZIP_OK; 1792 | 1793 | uLong Zip64DataSize = 44; 1794 | 1795 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)ZIP64ENDHEADERMAGIC,4); 1796 | 1797 | if (err==ZIP_OK) /* size of this 'zip64 end of central directory' */ 1798 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(ZPOS64_T)Zip64DataSize,8); // why ZPOS64_T of this ? 1799 | 1800 | if (err==ZIP_OK) /* version made by */ 1801 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)45,2); 1802 | 1803 | if (err==ZIP_OK) /* version needed */ 1804 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)45,2); 1805 | 1806 | if (err==ZIP_OK) /* number of this disk */ 1807 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); 1808 | 1809 | if (err==ZIP_OK) /* number of the disk with the start of the central directory */ 1810 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); 1811 | 1812 | if (err==ZIP_OK) /* total number of entries in the central dir on this disk */ 1813 | err = zip64local_putValue(&zi->z_filefunc, zi->filestream, zi->number_entry, 8); 1814 | 1815 | if (err==ZIP_OK) /* total number of entries in the central dir */ 1816 | err = zip64local_putValue(&zi->z_filefunc, zi->filestream, zi->number_entry, 8); 1817 | 1818 | if (err==ZIP_OK) /* size of the central directory */ 1819 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(ZPOS64_T)size_centraldir,8); 1820 | 1821 | if (err==ZIP_OK) /* offset of start of central directory with respect to the starting disk number */ 1822 | { 1823 | ZPOS64_T pos = centraldir_pos_inzip - zi->add_position_when_writting_offset; 1824 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream, (ZPOS64_T)pos,8); 1825 | } 1826 | return err; 1827 | } 1828 | 1829 | int Write_EndOfCentralDirectoryRecord(zip64_internal* zi, uLong size_centraldir, ZPOS64_T centraldir_pos_inzip); 1830 | int Write_EndOfCentralDirectoryRecord(zip64_internal* zi, uLong size_centraldir, ZPOS64_T centraldir_pos_inzip) 1831 | { 1832 | int err = ZIP_OK; 1833 | 1834 | /*signature*/ 1835 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)ENDHEADERMAGIC,4); 1836 | 1837 | if (err==ZIP_OK) /* number of this disk */ 1838 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,2); 1839 | 1840 | if (err==ZIP_OK) /* number of the disk with the start of the central directory */ 1841 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,2); 1842 | 1843 | if (err==ZIP_OK) /* total number of entries in the central dir on this disk */ 1844 | { 1845 | { 1846 | if(zi->number_entry >= 0xFFFF) 1847 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0xffff,2); // use value in ZIP64 record 1848 | else 1849 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->number_entry,2); 1850 | } 1851 | } 1852 | 1853 | if (err==ZIP_OK) /* total number of entries in the central dir */ 1854 | { 1855 | if(zi->number_entry >= 0xFFFF) 1856 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0xffff,2); // use value in ZIP64 record 1857 | else 1858 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->number_entry,2); 1859 | } 1860 | 1861 | if (err==ZIP_OK) /* size of the central directory */ 1862 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_centraldir,4); 1863 | 1864 | if (err==ZIP_OK) /* offset of start of central directory with respect to the starting disk number */ 1865 | { 1866 | ZPOS64_T pos = centraldir_pos_inzip - zi->add_position_when_writting_offset; 1867 | if(pos >= 0xffffffff) 1868 | { 1869 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream, (uLong)0xffffffff,4); 1870 | } 1871 | else 1872 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream, (uLong)(centraldir_pos_inzip - zi->add_position_when_writting_offset),4); 1873 | } 1874 | 1875 | return err; 1876 | } 1877 | 1878 | int Write_GlobalComment(zip64_internal* zi, const char* global_comment); 1879 | int Write_GlobalComment(zip64_internal* zi, const char* global_comment) 1880 | { 1881 | int err = ZIP_OK; 1882 | uInt size_global_comment = 0; 1883 | 1884 | if(global_comment != NULL) 1885 | size_global_comment = (uInt)strlen(global_comment); 1886 | 1887 | err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_global_comment,2); 1888 | 1889 | if (err == ZIP_OK && size_global_comment > 0) 1890 | { 1891 | if (ZWRITE64(zi->z_filefunc,zi->filestream, global_comment, size_global_comment) != size_global_comment) 1892 | err = ZIP_ERRNO; 1893 | } 1894 | return err; 1895 | } 1896 | 1897 | extern int ZEXPORT zipClose (zipFile file, const char* global_comment) 1898 | { 1899 | zip64_internal* zi; 1900 | int err = 0; 1901 | uLong size_centraldir = 0; 1902 | ZPOS64_T centraldir_pos_inzip; 1903 | ZPOS64_T pos; 1904 | 1905 | if (file == NULL) 1906 | return ZIP_PARAMERROR; 1907 | 1908 | zi = (zip64_internal*)file; 1909 | 1910 | if (zi->in_opened_file_inzip == 1) 1911 | { 1912 | err = zipCloseFileInZip (file); 1913 | } 1914 | 1915 | #ifndef NO_ADDFILEINEXISTINGZIP 1916 | if (global_comment==NULL) 1917 | global_comment = zi->globalcomment; 1918 | #endif 1919 | 1920 | centraldir_pos_inzip = ZTELL64(zi->z_filefunc,zi->filestream); 1921 | 1922 | if (err==ZIP_OK) 1923 | { 1924 | linkedlist_datablock_internal* ldi = zi->central_dir.first_block; 1925 | while (ldi!=NULL) 1926 | { 1927 | if ((err==ZIP_OK) && (ldi->filled_in_this_block>0)) 1928 | { 1929 | if (ZWRITE64(zi->z_filefunc,zi->filestream, ldi->data, ldi->filled_in_this_block) != ldi->filled_in_this_block) 1930 | err = ZIP_ERRNO; 1931 | } 1932 | 1933 | size_centraldir += ldi->filled_in_this_block; 1934 | ldi = ldi->next_datablock; 1935 | } 1936 | } 1937 | free_linkedlist(&(zi->central_dir)); 1938 | 1939 | pos = centraldir_pos_inzip - zi->add_position_when_writting_offset; 1940 | if(pos >= 0xffffffff) 1941 | { 1942 | ZPOS64_T Zip64EOCDpos = ZTELL64(zi->z_filefunc,zi->filestream); 1943 | Write_Zip64EndOfCentralDirectoryRecord(zi, size_centraldir, centraldir_pos_inzip); 1944 | 1945 | Write_Zip64EndOfCentralDirectoryLocator(zi, Zip64EOCDpos); 1946 | } 1947 | 1948 | if (err==ZIP_OK) 1949 | err = Write_EndOfCentralDirectoryRecord(zi, size_centraldir, centraldir_pos_inzip); 1950 | 1951 | if(err == ZIP_OK) 1952 | err = Write_GlobalComment(zi, global_comment); 1953 | 1954 | if (ZCLOSE64(zi->z_filefunc,zi->filestream) != 0) 1955 | if (err == ZIP_OK) 1956 | err = ZIP_ERRNO; 1957 | 1958 | #ifndef NO_ADDFILEINEXISTINGZIP 1959 | TRYFREE(zi->globalcomment); 1960 | #endif 1961 | TRYFREE(zi); 1962 | 1963 | return err; 1964 | } 1965 | 1966 | extern int ZEXPORT zipRemoveExtraInfoBlock (char* pData, int* dataLen, short sHeader) 1967 | { 1968 | char* p = pData; 1969 | int size = 0; 1970 | char* pNewHeader; 1971 | char* pTmp; 1972 | short header; 1973 | short dataSize; 1974 | 1975 | int retVal = ZIP_OK; 1976 | 1977 | if(pData == NULL || *dataLen < 4) 1978 | return ZIP_PARAMERROR; 1979 | 1980 | pNewHeader = (char*)ALLOC(*dataLen); 1981 | pTmp = pNewHeader; 1982 | 1983 | while(p < (pData + *dataLen)) 1984 | { 1985 | header = *(short*)p; 1986 | dataSize = *(((short*)p)+1); 1987 | 1988 | if( header == sHeader ) // Header found. 1989 | { 1990 | p += dataSize + 4; // skip it. do not copy to temp buffer 1991 | } 1992 | else 1993 | { 1994 | // Extra Info block should not be removed, So copy it to the temp buffer. 1995 | memcpy(pTmp, p, dataSize + 4); 1996 | p += dataSize + 4; 1997 | size += dataSize + 4; 1998 | } 1999 | 2000 | } 2001 | 2002 | if(size < *dataLen) 2003 | { 2004 | // clean old extra info block. 2005 | memset(pData,0, *dataLen); 2006 | 2007 | // copy the new extra info block over the old 2008 | if(size > 0) 2009 | memcpy(pData, pNewHeader, size); 2010 | 2011 | // set the new extra info size 2012 | *dataLen = size; 2013 | 2014 | retVal = ZIP_OK; 2015 | } 2016 | else 2017 | retVal = ZIP_ERRNO; 2018 | 2019 | TRYFREE(pNewHeader); 2020 | 2021 | return retVal; 2022 | } 2023 | --------------------------------------------------------------------------------