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

Introduction

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_INDEX_TYPE index = zip.FindFile(_T("file1.dat"));
#ifdef _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 _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 Fast feature.

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

A set is composed of characters or ranges. A range looks like this: character hyphen character, e.g.: 0 - 9 or A - Z. So a whole set can look 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.

To suppress the special syntactic significance of any of []*?!^-\, and match the character exactly, precede it with a \.

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);    
    _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 _ZIP64    
        _tprintf(_T("%I64u: %s\r\n"), index, name);
#else
        _tprintf(_T("%u: %s\r\n"), index, name);
#endif
    }
    zip.Close();

See Also API Calls

Article ID: 0610242025
Back To Top Up