首页 > 代码库 > unzip.cpp.300

unzip.cpp.300

#include "stdafx.h" #ifdef ZIP_STD #include <stdio.h> #include <string.h> #include <malloc.h> #include <time.h> #ifdef _MSC_VER #include <sys/utime.h> // microsoft puts it here #else #include <utime.h> #endif #if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__MINGW32__) #include <direct.h> #define lumkdir(t) (mkdir(t)) #else #include <unistd.h> #define lumkdir(t) (mkdir(t,0755)) #endif #include <sys/types.h> #include <sys/stat.h> #include "unzip.h" // typedef unsigned short WORD; #define _tcslen strlen #define _tcsicmp stricmp #define _tcsncpy strncpy #define _tcsstr strstr #define INVALID_HANDLE_VALUE 0 #ifndef _T #define _T(s) s #endif #ifndef S_IWUSR #define S_IWUSR 0000200 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR) #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG) #endif // #else

#include <windows.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <tchar.h> #include "unzip.h" #endif

// #ifdef UNICODE #define _tsprintf swprintf #else #define _tsprintf sprintf #endif

// THIS FILE is almost entirely based upon code by Jean-loup Gailly // and Mark Adler. It has been modified by Lucian Wischik. // The modifications were: incorporate the bugfixes of 1.1.4, allow // unzipping to/from handles/pipes/files/memory, encryption, unicode, // a windowsish api, and putting everything into a single .cpp file. // The original code may be found at http://www.gzip.org/zlib/ // The original copyright text follows. // // // // zlib.h -- interface of the ‘zlib‘ general purpose compression library //  version 1.1.3, July 9th, 1998 // //  Copyright (C) 1995-1998 Jean-loup Gailly and Mark Adler // //  This software is provided ‘as-is‘, without any express or implied //  warranty.  In no event will the authors be held liable for any damages //  arising from the use of this software. // //  Permission is granted to anyone to use this software for any purpose, //  including commercial applications, and to alter it and redistribute it //  freely, subject to the following restrictions: // //  1. The origin of this software must not be misrepresented; you must not //     claim that you wrote the original software. If you use this software //     in a product, an acknowledgment in the product documentation would be //     appreciated but is not required. //  2. Altered source versions must be plainly marked as such, and must not be //     misrepresented as being the original software. //  3. This notice may not be removed or altered from any source distribution. // //  Jean-loup Gailly        Mark Adler //  jloup@gzip.org          madler@alumni.caltech.edu // // //  The data format used by the zlib library is described by RFCs (Request for //  Comments) 1950 to 1952 in the files ftp://ds.internic.net/rfc/rfc1950.txt //  (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format). // // //     The ‘zlib‘ compression library provides in-memory compression and //  decompression functions, including integrity checks of the uncompressed //  data.  This version of the library supports only one compression method //  (deflation) but other algorithms will be added later and will have the same //  stream interface. // //     Compression can be done in a single step if the buffers are large //  enough (for example if an input file is mmap‘ed), or can be done by //  repeated calls of the compression function.  In the latter case, the //  application must provide more input and/or consume the output //  (providing more output space) before each call. // //     The library also supports reading and writing files in gzip (.gz) format //  with an interface similar to that of stdio. // //     The library does not install any signal handler. The decoder checks //  the consistency of the compressed data, so the library should never //  crash even in case of corrupted input. // // for more info about .ZIP format, see ftp://ftp.cdrom.com/pub/infozip/doc/appnote-970311-iz.zip //   PkWare has also a specification at ftp://ftp.pkware.com/probdesc.zip

#define ZIP_HANDLE   1 #define ZIP_FILENAME 2 #define ZIP_MEMORY   3

#define zmalloc(len) malloc(len)

#define zfree(p) free(p)

typedef unsigned long lutime_t;       // define it ourselves since we don‘t include time.h

/* void *zmalloc(unsigned int len) { char *buf = new char[len+32];   for (int i=0; i<16; i++)   { buf[i]=i;     buf[len+31-i]=i;   }   *((unsigned int*)buf) = len;   char c[1000]; wsprintf(c,"malloc 0x%lx  - %lu",buf+16,len);   OutputDebugString(c);   return buf+16; }

void zfree(void *buf) { char c[1000]; wsprintf(c,"free   0x%lx",buf);   OutputDebugString(c);   char *p = ((char*)buf)-16;   unsigned int len = *((unsigned int*)p);   bool blown=false;   for (int i=0; i<16; i++)   { char lo = p[i];     char hi = p[len+31-i];     if (hi!=i || (lo!=i && i>4)) blown=true;   }   if (blown)   { OutputDebugString("BLOWN!!!");   }   delete[] p; } */

typedef struct tm_unz_s { unsigned int tm_sec;            // seconds after the minute - [0,59]   unsigned int tm_min;            // minutes after the hour - [0,59]   unsigned int tm_hour;           // hours since midnight - [0,23]   unsigned int tm_mday;           // day of the month - [1,31]   unsigned int tm_mon;            // months since January - [0,11]   unsigned int tm_year;           // years - [1980..2044] } tm_unz;

 

// ---------------------------------------------------------------------- // some windows<->linux portability things #ifdef ZIP_STD DWORD GetFilePosU(HANDLE hfout) { struct stat st; fstat(fileno(hfout),&st);   if ((st.st_mode&S_IFREG)==0) return 0xFFFFFFFF;   return ftell(hfout); }

bool FileExists(const TCHAR *fn) { struct stat st;   int res=stat(fn,&st);   return (res==0); }

FILETIME dosdatetime2filetime(WORD dosdate,WORD dostime) { struct tm t;   t.tm_year = (WORD)(((dosdate>>9)&0x7f) + 1980 - 1900);   t.tm_isdst = -1;   t.tm_mon = (WORD)((dosdate>>5)&0xf - 1);   t.tm_mday = (WORD)(dosdate&0x1f);   t.tm_hour = (WORD)((dostime>>11)&0x1f);   t.tm_min = (WORD)((dostime>>5)&0x3f);   t.tm_sec = (WORD)((dostime&0x1f)*2);   time_t t2 = mktime(&t);   return t2; }

void LocalFileTimeToFileTime(FILETIME *lft, FILETIME *ft) { *ft = *lft; }

FILETIME timet2filetime(const lutime_t t) { return t; }

#else // ---------------------------------------------------------------------- DWORD GetFilePosU(HANDLE hfout) { return SetFilePointer(hfout,0,0,FILE_CURRENT); }

FILETIME timet2filetime(const lutime_t t) { LONGLONG i = Int32x32To64(t,10000000) + 116444736000000000LL;   FILETIME ft;   ft.dwLowDateTime = (DWORD) i;   ft.dwHighDateTime = (DWORD)(i >>32);   return ft; }

FILETIME dosdatetime2filetime(WORD dosdate,WORD dostime) { // date: bits 0-4 are day of month 1-31. Bits 5-8 are month 1..12. Bits 9-15 are year-1980   // time: bits 0-4 are seconds/2, bits 5-10 are minute 0..59. Bits 11-15 are hour 0..23   SYSTEMTIME st;   st.wYear = (WORD)(((dosdate>>9)&0x7f) + 1980);   st.wMonth = (WORD)((dosdate>>5)&0xf);   st.wDay = (WORD)(dosdate&0x1f);   st.wHour = (WORD)((dostime>>11)&0x1f);   st.wMinute = (WORD)((dostime>>5)&0x3f);   st.wSecond = (WORD)((dostime&0x1f)*2);   st.wMilliseconds = 0;   FILETIME ft; SystemTimeToFileTime(&st,&ft);   return ft; }

bool FileExists(const TCHAR *fn) { return (GetFileAttributes(fn)!=0xFFFFFFFF); } #endif // ----------------------------------------------------------------------

 

// unz_global_info structure contain global data about the ZIPfile typedef struct unz_global_info_s { unsigned long number_entry;         // total number of entries in the central dir on this disk   unsigned long size_comment;         // size of the global comment of the zipfile } unz_global_info;

// unz_file_info contain information about a file in the zipfile typedef struct unz_file_info_s { unsigned long version;              // version made by                 2 bytes   unsigned long version_needed;       // version needed to extract       2 bytes   unsigned long flag;                 // general purpose bit flag        2 bytes   unsigned long compression_method;   // compression method              2 bytes   unsigned long dosDate;              // last mod file date in Dos fmt   4 bytes   unsigned long crc;                  // crc-32                          4 bytes   unsigned long compressed_size;      // compressed size                 4 bytes   unsigned long uncompressed_size;    // uncompressed size               4 bytes   unsigned long size_filename;        // filename length                 2 bytes   unsigned long size_file_extra;      // extra field length              2 bytes   unsigned long size_file_comment;    // file comment length             2 bytes   unsigned long disk_num_start;       // disk number start               2 bytes   unsigned long internal_fa;          // internal file attributes        2 bytes   unsigned long external_fa;          // external file attributes        4 bytes   tm_unz tmu_date; } unz_file_info;

#define UNZ_OK                  (0) #define UNZ_END_OF_LIST_OF_FILE (-100) #define UNZ_ERRNO               (Z_ERRNO) #define UNZ_EOF                 (0) #define UNZ_PARAMERROR          (-102) #define UNZ_BADZIPFILE          (-103) #define UNZ_INTERNALERROR       (-104) #define UNZ_CRCERROR            (-105) #define UNZ_PASSWORD            (-106)

 

 

 

#define ZLIB_VERSION "1.1.3"

// Allowed flush values; see deflate() for details #define Z_NO_FLUSH      0 #define Z_SYNC_FLUSH    2 #define Z_FULL_FLUSH    3 #define Z_FINISH        4

// compression levels #define Z_NO_COMPRESSION         0 #define Z_BEST_SPEED             1

unzip.cpp.300