| 1 | 
           cycrow | 
           1 | 
           #ifndef _zip_H
  | 
        
        
            | 
            | 
           2 | 
           #define _zip_H
  | 
        
        
            | 
            | 
           3 | 
              | 
        
        
            | 
            | 
           4 | 
              | 
        
        
            | 
            | 
           5 | 
           // ZIP functions -- for creating zip files
  | 
        
        
            | 
            | 
           6 | 
           // This file is a repackaged form of the Info-Zip source code available
  | 
        
        
            | 
            | 
           7 | 
           // at www.info-zip.org. The original copyright notice may be found in
  | 
        
        
            | 
            | 
           8 | 
           // zip.cpp. The repackaging was done by Lucian Wischik to simplify and
  | 
        
        
            | 
            | 
           9 | 
           // extend its use in Windows/C++. Also to add encryption and unicode.
  | 
        
        
            | 
            | 
           10 | 
              | 
        
        
            | 
            | 
           11 | 
              | 
        
        
            | 
            | 
           12 | 
           #ifndef _unzip_H
  | 
        
        
            | 
            | 
           13 | 
           DECLARE_HANDLE(HZIP);
  | 
        
        
            | 
            | 
           14 | 
           #endif
  | 
        
        
            | 
            | 
           15 | 
           // An HZIP identifies a zip file that is being created
  | 
        
        
            | 
            | 
           16 | 
              | 
        
        
            | 
            | 
           17 | 
           typedef DWORD ZRESULT;
  | 
        
        
            | 
            | 
           18 | 
           // return codes from any of the zip functions. Listed later.
  | 
        
        
            | 
            | 
           19 | 
              | 
        
        
            | 
            | 
           20 | 
              | 
        
        
            | 
            | 
           21 | 
              | 
        
        
            | 
            | 
           22 | 
           HZIP CreateZip(const TCHAR *fn, const char *password);
  | 
        
        
            | 
            | 
           23 | 
           HZIP CreateZip(void *buf,unsigned int len, const char *password);
  | 
        
        
            | 
            | 
           24 | 
           HZIP CreateZipHandle(HANDLE h, const char *password);
  | 
        
        
            | 
            | 
           25 | 
           // CreateZip - call this to start the creation of a zip file.
  | 
        
        
            | 
            | 
           26 | 
           // As the zip is being created, it will be stored somewhere:
  | 
        
        
            | 
            | 
           27 | 
           // to a pipe:              CreateZipHandle(hpipe_write);
  | 
        
        
            | 
            | 
           28 | 
           // in a file (by handle):  CreateZipHandle(hfile);
  | 
        
        
            | 
            | 
           29 | 
           // in a file (by name):    CreateZip("c:\\test.zip");
  | 
        
        
            | 
            | 
           30 | 
           // in memory:              CreateZip(buf, len);
  | 
        
        
            | 
            | 
           31 | 
           // or in pagefile memory:  CreateZip(0, len);
  | 
        
        
            | 
            | 
           32 | 
           // The final case stores it in memory backed by the system paging file,
  | 
        
        
            | 
            | 
           33 | 
           // where the zip may not exceed len bytes. This is a bit friendlier than
  | 
        
        
            | 
            | 
           34 | 
           // allocating memory with new[]: it won't lead to fragmentation, and the
  | 
        
        
            | 
            | 
           35 | 
           // memory won't be touched unless needed. That means you can give very
  | 
        
        
            | 
            | 
           36 | 
           // large estimates of the maximum-size without too much worry.
  | 
        
        
            | 
            | 
           37 | 
           // As for the password, it lets you encrypt every file in the archive.
  | 
        
        
            | 
            | 
           38 | 
           // (This api doesn't support per-file encryption.)
  | 
        
        
            | 
            | 
           39 | 
           // Note: because pipes don't allow random access, the structure of a zipfile
  | 
        
        
            | 
            | 
           40 | 
           // created into a pipe is slightly different from that created into a file
  | 
        
        
            | 
            | 
           41 | 
           // or memory. In particular, the compressed-size of the item cannot be
  | 
        
        
            | 
            | 
           42 | 
           // stored in the zipfile until after the item itself. (Also, for an item added
  | 
        
        
            | 
            | 
           43 | 
           // itself via a pipe, the uncompressed-size might not either be known until
  | 
        
        
            | 
            | 
           44 | 
           // after.) This is not normally a problem. But if you try to unzip via a pipe
  | 
        
        
            | 
            | 
           45 | 
           // as well, then the unzipper will not know these things about the item until
  | 
        
        
            | 
            | 
           46 | 
           // after it has been unzipped. Therefore: for unzippers which don't just write
  | 
        
        
            | 
            | 
           47 | 
           // each item to disk or to a pipe, but instead pre-allocate memory space into
  | 
        
        
            | 
            | 
           48 | 
           // which to unzip them, then either you have to create the zip not to a pipe,
  | 
        
        
            | 
            | 
           49 | 
           // or you have to add items not from a pipe, or at least when adding items
  | 
        
        
            | 
            | 
           50 | 
           // from a pipe you have to specify the length.
  | 
        
        
            | 
            | 
           51 | 
           // Note: for windows-ce, you cannot close the handle until after CloseZip.
  | 
        
        
            | 
            | 
           52 | 
           // but for real windows, the zip makes its own copy of your handle, so you
  | 
        
        
            | 
            | 
           53 | 
           // can close yours anytime.
  | 
        
        
            | 
            | 
           54 | 
              | 
        
        
            | 
            | 
           55 | 
              | 
        
        
            | 
            | 
           56 | 
           ZRESULT ZipAdd(HZIP hz,const TCHAR *dstzn, const TCHAR *fn);
  | 
        
        
            | 
            | 
           57 | 
           ZRESULT ZipAdd(HZIP hz,const TCHAR *dstzn, void *src,unsigned int len);
  | 
        
        
            | 
            | 
           58 | 
           ZRESULT ZipAddHandle(HZIP hz,const TCHAR *dstzn, HANDLE h);
  | 
        
        
            | 
            | 
           59 | 
           ZRESULT ZipAddHandle(HZIP hz,const TCHAR *dstzn, HANDLE h, unsigned int len);
  | 
        
        
            | 
            | 
           60 | 
           ZRESULT ZipAddFolder(HZIP hz,const TCHAR *dstzn);
  | 
        
        
            | 
            | 
           61 | 
           // ZipAdd - call this for each file to be added to the zip.
  | 
        
        
            | 
            | 
           62 | 
           // dstzn is the name that the file will be stored as in the zip file.
  | 
        
        
            | 
            | 
           63 | 
           // The file to be added to the zip can come
  | 
        
        
            | 
            | 
           64 | 
           // from a pipe:  ZipAddHandle(hz,"file.dat", hpipe_read);
  | 
        
        
            | 
            | 
           65 | 
           // from a file:  ZipAddHandle(hz,"file.dat", hfile);
  | 
        
        
            | 
            | 
           66 | 
           // from a filen: ZipAdd(hz,"file.dat", "c:\\docs\\origfile.dat");
  | 
        
        
            | 
            | 
           67 | 
           // from memory:  ZipAdd(hz,"subdir\\file.dat", buf,len);
  | 
        
        
            | 
            | 
           68 | 
           // (folder):     ZipAddFolder(hz,"subdir");
  | 
        
        
            | 
            | 
           69 | 
           // Note: if adding an item from a pipe, and if also creating the zip file itself
  | 
        
        
            | 
            | 
           70 | 
           // to a pipe, then you might wish to pass a non-zero length to the ZipAddHandle
  | 
        
        
            | 
            | 
           71 | 
           // function. This will let the zipfile store the item's size ahead of the
  | 
        
        
            | 
            | 
           72 | 
           // compressed item itself, which in turn makes it easier when unzipping the
  | 
        
        
            | 
            | 
           73 | 
           // zipfile from a pipe.
  | 
        
        
            | 
            | 
           74 | 
              | 
        
        
            | 
            | 
           75 | 
           ZRESULT ZipGetMemory(HZIP hz, void **buf, unsigned long *len);
  | 
        
        
            | 
            | 
           76 | 
           // ZipGetMemory - If the zip was created in memory, via ZipCreate(0,len),
  | 
        
        
            | 
            | 
           77 | 
           // then this function will return information about that memory block.
  | 
        
        
            | 
            | 
           78 | 
           // buf will receive a pointer to its start, and len its length.
  | 
        
        
            | 
            | 
           79 | 
           // Note: you can't add any more after calling this.
  | 
        
        
            | 
            | 
           80 | 
              | 
        
        
            | 
            | 
           81 | 
           ZRESULT CloseZip(HZIP hz);
  | 
        
        
            | 
            | 
           82 | 
           // CloseZip - the zip handle must be closed with this function.
  | 
        
        
            | 
            | 
           83 | 
              | 
        
        
            | 
            | 
           84 | 
           unsigned int FormatZipMessage(ZRESULT code, TCHAR *buf,unsigned int len);
  | 
        
        
            | 
            | 
           85 | 
           // FormatZipMessage - given an error code, formats it as a string.
  | 
        
        
            | 
            | 
           86 | 
           // It returns the length of the error message. If buf/len points
  | 
        
        
            | 
            | 
           87 | 
           // to a real buffer, then it also writes as much as possible into there.
  | 
        
        
            | 
            | 
           88 | 
              | 
        
        
            | 
            | 
           89 | 
              | 
        
        
            | 
            | 
           90 | 
              | 
        
        
            | 
            | 
           91 | 
           // These are the result codes:
  | 
        
        
            | 
            | 
           92 | 
           #define ZR_OK         0x00000000     // nb. the pseudo-code zr-recent is never returned,
  | 
        
        
            | 
            | 
           93 | 
           #define ZR_RECENT     0x00000001     // but can be passed to FormatZipMessage.
  | 
        
        
            | 
            | 
           94 | 
           // The following come from general system stuff (e.g. files not openable)
  | 
        
        
            | 
            | 
           95 | 
           #define ZR_GENMASK    0x0000FF00
  | 
        
        
            | 
            | 
           96 | 
           #define ZR_NODUPH     0x00000100     // couldn't duplicate the handle
  | 
        
        
            | 
            | 
           97 | 
           #define ZR_NOFILE     0x00000200     // couldn't create/open the file
  | 
        
        
            | 
            | 
           98 | 
           #define ZR_NOALLOC    0x00000300     // failed to allocate some resource
  | 
        
        
            | 
            | 
           99 | 
           #define ZR_WRITE      0x00000400     // a general error writing to the file
  | 
        
        
            | 
            | 
           100 | 
           #define ZR_NOTFOUND   0x00000500     // couldn't find that file in the zip
  | 
        
        
            | 
            | 
           101 | 
           #define ZR_MORE       0x00000600     // there's still more data to be unzipped
  | 
        
        
            | 
            | 
           102 | 
           #define ZR_CORRUPT    0x00000700     // the zipfile is corrupt or not a zipfile
  | 
        
        
            | 
            | 
           103 | 
           #define ZR_READ       0x00000800     // a general error reading the file
  | 
        
        
            | 
            | 
           104 | 
           // The following come from mistakes on the part of the caller
  | 
        
        
            | 
            | 
           105 | 
           #define ZR_CALLERMASK 0x00FF0000
  | 
        
        
            | 
            | 
           106 | 
           #define ZR_ARGS       0x00010000     // general mistake with the arguments
  | 
        
        
            | 
            | 
           107 | 
           #define ZR_NOTMMAP    0x00020000     // tried to ZipGetMemory, but that only works on mmap zipfiles, which yours wasn't
  | 
        
        
            | 
            | 
           108 | 
           #define ZR_MEMSIZE    0x00030000     // the memory size is too small
  | 
        
        
            | 
            | 
           109 | 
           #define ZR_FAILED     0x00040000     // the thing was already failed when you called this function
  | 
        
        
            | 
            | 
           110 | 
           #define ZR_ENDED      0x00050000     // the zip creation has already been closed
  | 
        
        
            | 
            | 
           111 | 
           #define ZR_MISSIZE    0x00060000     // the indicated input file size turned out mistaken
  | 
        
        
            | 
            | 
           112 | 
           #define ZR_PARTIALUNZ 0x00070000     // the file had already been partially unzipped
  | 
        
        
            | 
            | 
           113 | 
           #define ZR_ZMODE      0x00080000     // tried to mix creating/opening a zip 
  | 
        
        
            | 
            | 
           114 | 
           // The following come from bugs within the zip library itself
  | 
        
        
            | 
            | 
           115 | 
           #define ZR_BUGMASK    0xFF000000
  | 
        
        
            | 
            | 
           116 | 
           #define ZR_NOTINITED  0x01000000     // initialisation didn't work
  | 
        
        
            | 
            | 
           117 | 
           #define ZR_SEEK       0x02000000     // trying to seek in an unseekable file
  | 
        
        
            | 
            | 
           118 | 
           #define ZR_NOCHANGE   0x04000000     // changed its mind on storage, but not allowed
  | 
        
        
            | 
            | 
           119 | 
           #define ZR_FLATE      0x05000000     // an internal error in the de/inflation code
  | 
        
        
            | 
            | 
           120 | 
              | 
        
        
            | 
            | 
           121 | 
              | 
        
        
            | 
            | 
           122 | 
              | 
        
        
            | 
            | 
           123 | 
              | 
        
        
            | 
            | 
           124 | 
              | 
        
        
            | 
            | 
           125 | 
              | 
        
        
            | 
            | 
           126 | 
           // e.g.
  | 
        
        
            | 
            | 
           127 | 
           //
  | 
        
        
            | 
            | 
           128 | 
           // (1) Traditional use, creating a zipfile from existing files
  | 
        
        
            | 
            | 
           129 | 
           //     HZIP hz = CreateZip("c:\\simple1.zip",0);
  | 
        
        
            | 
            | 
           130 | 
           //     ZipAdd(hz,"znsimple.bmp", "c:\\simple.bmp");
  | 
        
        
            | 
            | 
           131 | 
           //     ZipAdd(hz,"znsimple.txt", "c:\\simple.txt");
  | 
        
        
            | 
            | 
           132 | 
           //     CloseZip(hz);
  | 
        
        
            | 
            | 
           133 | 
           //
  | 
        
        
            | 
            | 
           134 | 
           // (2) Memory use, creating an auto-allocated mem-based zip file from various sources
  | 
        
        
            | 
            | 
           135 | 
           //     HZIP hz = CreateZip(0,100000, 0);
  | 
        
        
            | 
            | 
           136 | 
           //     // adding a conventional file...
  | 
        
        
            | 
            | 
           137 | 
           //     ZipAdd(hz,"src1.txt",  "c:\\src1.txt");
  | 
        
        
            | 
            | 
           138 | 
           //     // adding something from memory...
  | 
        
        
            | 
            | 
           139 | 
           //     char buf[1000]; for (int i=0; i<1000; i++) buf[i]=(char)(i&0x7F);
  | 
        
        
            | 
            | 
           140 | 
           //     ZipAdd(hz,"file.dat",  buf,1000);
  | 
        
        
            | 
            | 
           141 | 
           //     // adding something from a pipe...
  | 
        
        
            | 
            | 
           142 | 
           //     HANDLE hread,hwrite; CreatePipe(&hread,&hwrite,NULL,0);
  | 
        
        
            | 
            | 
           143 | 
           //     HANDLE hthread = CreateThread(0,0,ThreadFunc,(void*)hwrite,0,0);
  | 
        
        
            | 
            | 
           144 | 
           //     ZipAdd(hz,"unz3.dat",  hread,1000);  // the '1000' is optional.
  | 
        
        
            | 
            | 
           145 | 
           //     WaitForSingleObject(hthread,INFINITE);
  | 
        
        
            | 
            | 
           146 | 
           //     CloseHandle(hthread); CloseHandle(hread);
  | 
        
        
            | 
            | 
           147 | 
           //     ... meanwhile DWORD WINAPI ThreadFunc(void *dat)
  | 
        
        
            | 
            | 
           148 | 
           //                   { HANDLE hwrite = (HANDLE)dat;
  | 
        
        
            | 
            | 
           149 | 
           //                     char buf[1000]={17};
  | 
        
        
            | 
            | 
           150 | 
           //                     DWORD writ; WriteFile(hwrite,buf,1000,&writ,NULL);
  | 
        
        
            | 
            | 
           151 | 
           //                     CloseHandle(hwrite);
  | 
        
        
            | 
            | 
           152 | 
           //                     return 0;
  | 
        
        
            | 
            | 
           153 | 
           //                   }
  | 
        
        
            | 
            | 
           154 | 
           //     // and now that the zip is created, let's do something with it:
  | 
        
        
            | 
            | 
           155 | 
           //     void *zbuf; unsigned long zlen; ZipGetMemory(hz,&zbuf,&zlen);
  | 
        
        
            | 
            | 
           156 | 
           //     HANDLE hfz = CreateFile("test2.zip",GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
  | 
        
        
            | 
            | 
           157 | 
           //     DWORD writ; WriteFile(hfz,zbuf,zlen,&writ,NULL);
  | 
        
        
            | 
            | 
           158 | 
           //     CloseHandle(hfz);
  | 
        
        
            | 
            | 
           159 | 
           //     CloseZip(hz);
  | 
        
        
            | 
            | 
           160 | 
           //
  | 
        
        
            | 
            | 
           161 | 
           // (3) Handle use, for file handles and pipes
  | 
        
        
            | 
            | 
           162 | 
           //     HANDLE hzread,hzwrite; CreatePipe(&hzread,&hzwrite,0,0);
  | 
        
        
            | 
            | 
           163 | 
           //     HANDLE hthread = CreateThread(0,0,ZipReceiverThread,(void*)hzread,0,0);
  | 
        
        
            | 
            | 
           164 | 
           //     HZIP hz = CreateZipHandle(hzwrite,0);
  | 
        
        
            | 
            | 
           165 | 
           //     // ... add to it
  | 
        
        
            | 
            | 
           166 | 
           //     CloseZip(hz);
  | 
        
        
            | 
            | 
           167 | 
           //     CloseHandle(hzwrite);
  | 
        
        
            | 
            | 
           168 | 
           //     WaitForSingleObject(hthread,INFINITE);
  | 
        
        
            | 
            | 
           169 | 
           //     CloseHandle(hthread);
  | 
        
        
            | 
            | 
           170 | 
           //     ... meanwhile DWORD WINAPI ZipReceiverThread(void *dat)
  | 
        
        
            | 
            | 
           171 | 
           //                   { HANDLE hread = (HANDLE)dat;
  | 
        
        
            | 
            | 
           172 | 
           //                     char buf[1000];
  | 
        
        
            | 
            | 
           173 | 
           //                     while (true)
  | 
        
        
            | 
            | 
           174 | 
           //                     { DWORD red; ReadFile(hread,buf,1000,&red,NULL);
  | 
        
        
            | 
            | 
           175 | 
           //                       // ... and do something with this zip data we're receiving
  | 
        
        
            | 
            | 
           176 | 
           //                       if (red==0) break;
  | 
        
        
            | 
            | 
           177 | 
           //                     }
  | 
        
        
            | 
            | 
           178 | 
           //                     CloseHandle(hread);
  | 
        
        
            | 
            | 
           179 | 
           //                     return 0;
  | 
        
        
            | 
            | 
           180 | 
           //                   }
  | 
        
        
            | 
            | 
           181 | 
              | 
        
        
            | 
            | 
           182 | 
              | 
        
        
            | 
            | 
           183 | 
              | 
        
        
            | 
            | 
           184 | 
           // Now we indulge in a little skullduggery so that the code works whether
  | 
        
        
            | 
            | 
           185 | 
           // the user has included just zip or both zip and unzip.
  | 
        
        
            | 
            | 
           186 | 
           // Idea: if header files for both zip and unzip are present, then presumably
  | 
        
        
            | 
            | 
           187 | 
           // the cpp files for zip and unzip are both present, so we will call
  | 
        
        
            | 
            | 
           188 | 
           // one or the other of them based on a dynamic choice. If the header file
  | 
        
        
            | 
            | 
           189 | 
           // for only one is present, then we will bind to that particular one.
  | 
        
        
            | 
            | 
           190 | 
           ZRESULT CloseZipZ(HZIP hz);
  | 
        
        
            | 
            | 
           191 | 
           unsigned int FormatZipMessageZ(ZRESULT code, char *buf,unsigned int len);
  | 
        
        
            | 
            | 
           192 | 
           bool IsZipHandleZ(HZIP hz);
  | 
        
        
            | 
            | 
           193 | 
           #ifdef _unzip_H
  | 
        
        
            | 
            | 
           194 | 
           #undef CloseZip
  | 
        
        
            | 
            | 
           195 | 
           #define CloseZip(hz) (IsZipHandleZ(hz)?CloseZipZ(hz):CloseZipU(hz))
  | 
        
        
            | 
            | 
           196 | 
           #else
  | 
        
        
            | 
            | 
           197 | 
           #define CloseZip CloseZipZ
  | 
        
        
            | 
            | 
           198 | 
           #define FormatZipMessage FormatZipMessageZ
  | 
        
        
            | 
            | 
           199 | 
           #endif
  | 
        
        
            | 
            | 
           200 | 
              | 
        
        
            | 
            | 
           201 | 
              | 
        
        
            | 
            | 
           202 | 
              | 
        
        
            | 
            | 
           203 | 
           #endif
  |