summaryrefslogtreecommitdiff
path: root/Help/unzip.h
blob: aba0d1f840c51c5f902b968d695b97a06e3f8b9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122

// UNZIPPING functions -- for unzipping.

// This file is a repackaged form of extracts from the zlib code available
// at http://www.gzip.org/zlib, by Jean-Loup Gailly and Mark Adler. The original
// copyright notice may be found in unzip.c. The repackaging was done
// by Lucian Wischik to simplify and extend its use in Windows/C++. Also
// encryption and Unicode filenames have been added.

// An HZIP identifies a zip file that has been opened
DECLARE_HANDLE(HZIP);

// return codes from any of the zip functions
typedef DWORD ZRESULT;
// These are the result codes:
#define ZR_OK         0x00000000
// The following come from general system stuff (e.g. files not openable)
#define ZR_GENMASK    0x0000FF00
#define ZR_NODUPH     0x00000100     // couldn't duplicate the handle
#define ZR_NOFILE     0x00000200     // couldn't create/open the file
#define ZR_NOALLOC    0x00000300     // failed to allocate some resource
#define ZR_WRITE      0x00000400     // a general error writing to the file
#define ZR_NOTFOUND   0x00000500     // couldn't find that file in the zip
#define ZR_MORE       0x00000600     // there's still more data to be unzipped
#define ZR_CORRUPT    0x00000700     // the zipfile is corrupt or not a zipfile
#define ZR_READ       0x00000800     // a general error reading the file
#define ZR_PASSWORD   0x00001000     // we didn't get the right password to unzip the file
// The following come from mistakes on the part of the caller
#define ZR_CALLERMASK 0x00FF0000
#define ZR_ARGS       0x00010000     // general mistake with the arguments
#define ZR_MEMSIZE    0x00030000     // the memory size is too small
#define ZR_FAILED     0x00040000     // the thing was already failed when you called this function
#define ZR_ENDED      0x00050000     // the zip creation has already been closed
#define ZR_MISSIZE    0x00060000     // the indicated input file size turned out mistaken
#define ZR_ZMODE      0x00080000     // tried to mix creating/opening a zip 
// The following come from bugs within the zip library itself
#define ZR_BUGMASK    0xFF000000
#define ZR_NOTINITED  0x01000000     // initialisation didn't work
#define ZR_SEEK       0x02000000     // trying to seek in an unseekable file
#define ZR_NOCHANGE   0x04000000     // changed its mind on storage, but not allowed
#define ZR_FLATE      0x05000000     // an internal error in the de/inflation code

// parameter types
#define ZIP_HANDLE    1 // expects HANDLE hFile
#define ZIP_FILENAME  2 // expects TCHAR* filename
#define ZIP_MEMORY    3 // expects memory buffer

// OpenZip - opens a zip file and returns a handle with which you can
// subsequently examine its contents. You can open a zip file from:
// from a file (by handle): OpenZipHandle(hfile,0);
// from a file (by name):   OpenZipFile("c:\\test.zip","password");
// from a memory block:     OpenZipMem(bufstart, buflen,0);
// If the file is opened through a pipe, then items may only be
// accessed in increasing order, and an item may only be unzipped once,
// although GetZipItem can be called immediately before and after unzipping
// it. If it's opened in any other way, then full random access is possible.
// Note: pipe input is not yet implemented.
// Note: zip passwords are ascii, not unicode.
// Note: for windows-ce, you cannot close the handle until after CloseZip.
// but for real windows, the zip makes its own copy of your handle, so you
// can close yours anytime.
HZIP OpenZip(void *z,unsigned int len,DWORD flags,const char *password);
#define OpenZipHandle(h,password)  OpenZip((void*)h,0,ZIP_HANDLE,password)
#define OpenZipFile(fn,password)   OpenZip((void*)fn,0,ZIP_FILENAME,password)
#define OpenZipMem(z,len,password) OpenZip(z,len,ZIP_MEMORY,password)

// GetZipItem - call this to get information about an item in the zip.
// If index is -1 and the file wasn't opened through a pipe,
// then it returns information about the whole zipfile
// (and in particular ze.index returns the number of index items).
// Note: the item might be a directory (ze.attr & FILE_ATTRIBUTE_DIRECTORY)
// See below for notes on what happens when you unzip such an item.
// Note: if you are opening the zip through a pipe, then random access
// is not possible and GetZipItem(-1) fails and you can't discover the number
// of items except by calling GetZipItem on each one of them in turn,
// starting at 0, until eventually the call fails. Also, in the event that
// you are opening through a pipe and the zip was itself created into a pipe,
// then then comp_size and sometimes unc_size as well may not be known until
// after the item has been unzipped.
typedef struct {
	int index;                  // index of this file within the zip
	TCHAR name[MAX_PATH];       // filename within the zip
	DWORD attr;                 // attributes, as in GetFileAttributes.
	FILETIME atime,ctime,mtime; // access, create, modify filetimes
	long comp_size;             // sizes of item, compressed and uncompressed. These
	long unc_size;              // may be -1 if not yet known (e.g. being streamed in)
} ZIPENTRY;
ZRESULT GetZipItem(HZIP hz,int index,ZIPENTRY *ze);

// FindZipItem - finds an item by name. ic means 'insensitive to case'.
// If index is not NULL it contains the index where the search begin on input.
// It returns the index of the item, and returns information about it.
// If nothing was found, then index is set to -1 and the function returns
// an error code.
// name is allowed to contain wildcards.
ZRESULT FindZipItem(HZIP hz, const TCHAR *name, BOOL ic, int *index, ZIPENTRY *ze);

// UnzipItem - given an index to an item, unzips it. You can unzip to:
// to a file (by handle): UnzipItemHandle(hz,i, hfile);
// to a file (by name):   UnzipItemFile(hz,i, ze.name);
// to a memory block:     UnzipItemMem(hz,i, buf,buflen);
// In the final case, if the buffer isn't large enough to hold it all,
// then the return code indicates that more is yet to come. If it was
// large enough, and you want to know precisely how big, GetZipItem.
// Note: zip files are normally stored with relative pathnames. If you
// unzip with ZIP_FILENAME a relative pathname then the item gets created
// relative to the current directory - it first ensures that all necessary
// subdirectories have been created. Also, the item may itself be a directory.
// If you unzip a directory with ZIP_FILENAME, then the directory gets created.
// If you unzip it to a handle or a memory block, then nothing gets created
// and it emits 0 bytes.
ZRESULT UnzipItem(HZIP hz,int index,void *dst,unsigned int len,DWORD type);
#define UnzipItemHandle(hz,index,h)  UnzipItem(hz,index,(void*)h,0,ZIP_HANDLE)
#define UnzipItemFile(hz,index,fn)   UnzipItem(hz,index,(void*)fn,0,ZIP_FILENAME)
#define UnzipItemMem(hz,index,z,len) UnzipItem(hz,index,z,len,ZIP_MEMORY)

// if unzipping to a filename, and it's a relative filename, then it will be relative to here.
// (defaults to current-directory).
ZRESULT SetUnzipBaseDir(HZIP hz,const TCHAR *dir);

// CloseZip - the zip handle must be closed with this function.
ZRESULT CloseZip(HZIP hz);