The classes can enumerate the compressed files stored inside a ZIP file and extract the files one by one. There is nothing really magic about this, because it uses the ZLIB library maintained by Jean-loup Gailly and Mark Adler.
The CZipArchive class gives access to the compressed
entries through the GetFile()
methods. The uncompressed
data is returned in a CZipFile class that can be used as a
memory stream.
To enumerate the entries in the ZIP use the FindFirstFile()
,
FindNextFile()
and FindClose()
methods. They
operate exactly like the Win32 API FindFile counterparts.
The classes cannot compress data, delete, overwrite or add new entries to the
ZIP file. It does however support compressed and encrypted ZIP 2.0 files.
So, to open and use a ZIP file, use the following syntax:
CZipArchive zip;
zip.Open(_T("test.zip"));
...
zip.Close();
To extract a file from the ZIP archive:
CZipFile file;
if( zip.GetFile(_T("readme.txt"), &file) == TRUE ) {
DWORD dwSize = file.GetSize();
LPSTR pStr = (LPSTR) _alloca(dwSize);
file.Read(pStr, dwSize, NULL);
file.Close();
}
Enumerate the entries in the ZIP like this:
ZIP_FIND_DATA fd;
HANDLE hFD = zip.FindFirstFile(_T("*.*"), &fd);
if( hFD != INVALID_HANDLE_VALUE ) {
while( true ) {
printf(_T("%s"), fd.cFileName);
if( zip.FindNextFile(hFD, &fd) == FALSE ) break;
}
zip.FindClose(hFD);
}
If you're dealing with an archive containing encrypted files,
call the SetPassword
method on CZipArchive
before
you start retrieving files.
Interface
Name | Description | |
---|---|---|
![]() | Open | Opens a ZIP archive. |
![]() | Close | Closes the archive. |
![]() | GetFile | Extracts a file entry. |
![]() | SetPassword | Set password to use on encrypted files. |
![]() | FindFirstFile | Enumerates files in ZIP. |
![]() | FinNextFile | Gets the next file in FindFirstFile enumeration. |
![]() | FindClose | Closes the find enumeration. |
Notes
Source Code Dependencies
Microsoft Visual C++ 6.0Microsoft ATL Library
ZLIB
Useful Links
The ZLIB homeDownload Files
![]() | Source Code (6 Kb) ZLIB files (100 Kb) |