Unicode C
Unicode C
Iterate Through ZIP Entries Using EntryAt and ZipEntry.GetNext
See more Zip Examples
This example demonstrates how to iterate through all entries in a ZIP archive using:
-
Zip.EntryAtto obtain the first entry -
ZipEntry.GetNextto advance through the remaining entries
The example prints each ZIP entry's stored filename and whether the entry is a file or directory.
This approach is useful when sequentially processing ZIP entries without repeatedly calling EntryAt by index.
Suppose the ZIP archive contains:
docs/
docs/readme.txt
images/logo.png
hello.txt The example iterates through each entry in the order stored within the ZIP archive.
Chilkat Unicode C Downloads
#include <C_CkZipW.h>
#include <C_CkZipEntryW.h>
void ChilkatSample(void)
{
BOOL success;
HCkZipW zip;
HCkZipEntryW entry;
success = FALSE;
zip = CkZipW_Create();
// Open an existing ZIP archive.
success = CkZipW_OpenZip(zip,L"c:/temp/example.zip");
if (success == FALSE) {
wprintf(L"%s\n",CkZipW_lastErrorText(zip));
CkZipW_Dispose(zip);
return;
}
// A ZIP archive may contain zero entries.
if (CkZipW_getNumEntries(zip) == 0) {
wprintf(L"The ZIP archive is empty.\n");
CkZipW_CloseZip(zip);
CkZipW_Dispose(zip);
return;
}
// ------------------------------------------------------------
// Get the first ZIP entry.
//
// EntryAt(0,entry) initializes the ZipEntry object so that
// it represents the first entry in the ZIP archive.
//
entry = CkZipEntryW_Create();
success = CkZipW_EntryAt(zip,0,entry);
if (success == FALSE) {
wprintf(L"%s\n",CkZipW_lastErrorText(zip));
CkZipW_Dispose(zip);
CkZipEntryW_Dispose(entry);
return;
}
// ------------------------------------------------------------
// Iterate through all ZIP entries.
//
// GetNext updates the same ZipEntry object so that it
// represents the next entry in the ZIP archive.
//
while ((success == TRUE)) {
if (CkZipEntryW_getIsDirectory(entry) == TRUE) {
wprintf(L"[Directory] %s\n",CkZipEntryW_fileName(entry));
}
else {
wprintf(L"[File] %s\n",CkZipEntryW_fileName(entry));
}
// Advance to the next entry.
success = CkZipEntryW_GetNext(entry);
}
CkZipW_CloseZip(zip);
wprintf(L"Done.\n");
CkZipW_Dispose(zip);
CkZipEntryW_Dispose(entry);
}