Skip Navigation Links
Skip Navigation LinksHome > ZipArchive > How to Use > Article
Requesting Information, Predicting Names and Sizes
Applies To: All

Requesting Information

File Information

You can request different types of information about files inside an archive. To find out about types of information you may retrieve, examine members of the CZipFileHeader class. To request the CZipFileHeader object for a particular file, call one of the following methods:

Filename Conversion

When calling the CZipFileHeader::GetFileName() method for the first time, the conversion takes place which converts the filename from an internal representation to the CZipString object. This conversion takes into account filename code pages (see Unicode Support: Using Non-English Characters in Filenames, Comments and Passwords) and directory separators (to match the current platform's default directory separator).

Local Header Information

If you wish to obtain the data that is stored in a local header, you need to make sure that the local information has already been read before. The local information is read when you start extracting a file. If you don't want to extract a file, but you still want to read the local information, call the CZipArchive::ReadLocalHeader() method. The following fields hold the local information:
Sample Code
CZipArchive zip;
// open an existing archive
zip.Open(_T("C:\\Temp\\test.zip"));
for (ZIP_INDEX_TYPE i = 0; i < zip.GetCount(); i++)
{
CZipFileHeader* info = zip[i];
LPCTSTR name = info->GetFileName();
ZIP_SIZE_TYPE uncomprSize = info->m_uUncomprSize;
ZIP_SIZE_TYPE comprSize = info->m_uComprSize;
// read some sample local information
zip.ReadLocalHeader(i);
int extraSize = info->m_aLocalExtraData.GetTotalSize();
#ifdef _ZIP_ZIP64
_tprintf(_T("%I64u: Name: %s Size: %I64u Compressed Size: %I64u\r\n\
Size of Local Extra Data: %d\r\n"),
i, name, uncomprSize, comprSize, extraSize);
#else
_tprintf(_T("%u: Name: %s Size: %u Compressed Size: %u\r\n\
Size of Local Extra Data: %d\r\n"),
i, name, uncomprSize, comprSize, extraSize);
#endif
}

Iterating Over Items

To find out about the number of files in an archive, use the CZipArchive::GetCount() method. This method and the GetSize() and GetCount() methods (collections' members) in some implementations, return an unsigned type. To check the data type used by collections, examine the ZIP_ARRAY_SIZE_TYPE definition. You should pay special attention when reverse-iterating over such collections. If ZIP_ARRAY_SIZE_TYPE is represented by an unsigned type, the iteration variable will never be negative and the archive will report an invalid index when an overflow happens. If the number of items is the maximum value for the unsigned type, the loop may run forever. The code below represents an invalid code.
Sample Code
/* THIS IS A BAD CODE. */
/* IT WILL THROW AN EXCEPTION OR RUN FOREVER */
CZipArchive zip;
zip.Open(_T("C:\\Temp\\test.zip"));
ZIP_INDEX_TYPE uMax = zip.GetCount();
// incorrect reverse iteration
for (ZIP_INDEX_TYPE i = uMax - 1; i >= 0; i--)
{
CZipFileHeader info;
zip.GetFileInfo(info, i);
//... do something
}
zip.Close();

The correct way for reverse iteration illustrates the code below.

Sample Code
CZipArchive zip;
zip.Open(_T("C:\\Temp\\test.zip"));
ZIP_INDEX_TYPE i = zip.GetCount();
// correct reverse iteration
while (i > 0)
{
i--;
CZipFileHeader info;
zip.GetFileInfo(info, i);
// ... do something
}
zip.Close();

Please pay attention when you use your code in both MFC and STL versions, because you will use both signed and unsigned types as ZIP_ARRAY_SIZE_TYPE. This is caused by types used by MFC and STL libraries. Operations that work on these types and produce negative results will be interpreted differently in both versions.

When you don't use Zip64 extensions (see Zip64 Format: Crossing the Limits of File Sizes and Number of Files and Segments), you can use a signed type (int) for the file index and volume number types. Just make sure that _ZIP_STRICT_U16 is not defined in _features.h file.

Archive Information

The ZipArchive Library provides among others the following methods for requesting archive-related information:

Predicting Names and Sizes

The ZipArchive Library provides the following methods for predicting names and sizes:
Article ID: 0610242128
Back To Top Up