Skip Navigation Links
Skip Navigation LinksHome > ZipArchive > How to Use > Article
Searching in Archive
Applies To: All

Introduction

The ZipArchive Library provides several methods for searching for files in an archive.

Searching by Names: the Find Fast Feature

The Find Fast feature, if enabled, builds an additional array that holds sorted names of files in an archive. The search methods perform a binary-search on this array for the fastest results. This array is built automatically when you use one of the below methods for the first time. If you want to build the array in a more convenient time for you, use the CZipArchive::EnableFindFast() method. These methods require using names the way they appear in archive (considering the case-sensitivity settings) - no wildcards are allowed. You can find real indexes (such than can be used as arguments of other archive processing methods) of the sorted files using the CZipArchive::GetFindFastIndex() method.
Sample Code
CZipArchive zip;
// open an existing archive
zip.Open(_T("C:\\Temp\\test.zip"));
zip.EnableFindFast();
ZIP_INDEX_TYPE index = zip.FindFile(_T("file1.dat"));
#ifdef _ZIP_ZIP64
_tprintf(_T("Index found: %I64u\r\n"), index);
#else
_tprintf(_T("Index found: %u\r\n"), index);
#endif
// prepare an array of names to find
CZipStringArray names;
names.Add(_T("file2.dat"));
names.Add(_T("file3.dat"));
names.Add(_T("not existing.dat"));
CZipIndexesArray indexes;
zip.GetIndexes(names, indexes);
zip.Close();
// the size of both arrays should be always equal
ASSERT(names.GetCount() == indexes.GetCount());
for (ZIP_ARRAY_SIZE_TYPE i = 0; i < indexes.GetCount(); i++)
{
index = indexes[i];
LPCTSTR name = names[i];
if (index == ZIP_FILE_INDEX_NOT_FOUND)
_tprintf(_T("The file %s was not found\r\n"), name);
else
{
#ifdef _ZIP_ZIP64
_tprintf(_T("The file %s has index %I64u\r\n"), name, index);
#else
_tprintf(_T("The file %s has index %u\r\n"), name, index);
#endif
}
}

Searching by Wildcards

To search for files in an archive using wildcards, use the CZipArchive::FindMatches() method. This method does not use the Find Fastfeature.

Pattern Matching

The pattern matching functionality is used in several places in the ZipArchive Library. The construction of the pattern string is the same in all cases.

In the pattern string:

  • * matches any sequence of characters (zero or more).
  • ? matches any character.
  • [SET] matches any character in the specified set.
  • [!SET] or [^SET] matches any character not in the specified set.
  • \ suppress the special syntactic significance of any of []*?!^-\ characters. Note, than in C++ this character is also a special character and also needs to be escaped in a string. This means that to use backslash in the pattern as a normal character, you need to actually write e.g. "folder\\\\file.*".

A set is composed of characters or ranges. A range has the following syntax: character hyphen character, e.g.: 0 - 9 or A - Z. A whole set can look for example like this: [0 - 9a - zA - Z_]. This set is the minimal set of characters allowed in the set pattern construct. Other characters are allowed (only 8-bit characters), if your system supports them.

Sample Code
CZipArchive zip;
// open an existing archive
zip.Open(_T("C:\\Temp\\test.zip"));
CZipIndexesArray indexes;
// find files using wildcards
//zip.FindMatches(_T("file?.*"), indexes);
zip.FindMatches(_T("*"), indexes);
_tprintf(_T("Matches found at positions:\r\n"));
for (ZIP_ARRAY_SIZE_TYPE i = 0; i < indexes.GetCount(); i++)
{
ZIP_INDEX_TYPE index = indexes[i];
LPCTSTR name = zip[index]->GetFileName();
#ifdef _ZIP_ZIP64
_tprintf(_T("%I64u: %s\r\n"), index, name);
#else
_tprintf(_T("%u: %s\r\n"), index, name);
#endif
}
zip.Close();

Searching for Files in Specified Directory

To find all files that are located in a particular directory inside of an archive, use a mask similar to this one:"directory\\\\*"

See Also API Links

Article ID: 0610242025
Back To Top Up