Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Multithreaded Zip Search

0.00/5 (No votes)
27 Sep 2003 1  
This is an small app which searches inside ZIP archives

Sample Image - ZipSearch.jpg

Introduction

This is a small dialog based multithreaded app which helps us to search inside ZIP archives. It uses the CZipArchive class to open and process ZIP archives.

Background

This app uses CZipArchive class, for more about this CZipArchive information follow the link

void CZipSearchDlg::EnumerateDirectory(CString Directory)
{
  int nCount,i;
  CString strFilename;
  BOOL bResult;
  CFileFind ZIPFiles;
  CZipArchive ZIPFile;
  CZipFileHeader ZIPFileHeader;
  CLike CompareLike;
  CString strStatus;
  BOOL bDisplayStatus=TRUE;
  SHFILEINFO    sfi;

  LV_ITEM lvItem;struct ItemFile *ifFile;

  Directory.TrimLeft();Directory.TrimRight();
  if(Directory.Right(1)=="\\")
    bResult=ZIPFiles.FindFile(Directory+"*.*");
  else
    bResult=ZIPFiles.FindFile(Directory+"\\"+"*.*");
  //Does the recursive search
  while(bResult && !m_bStopEnumeration && !m_bSkipDirectory)
  {
    if(bDisplayStatus)
    {
      strStatus.Format("Processing %s",Directory);
      m_wndStatusBar.SetText(strStatus,0,0);
      bDisplayStatus=TRUE;
    }
    bResult=ZIPFiles.FindNextFile();
    if(!ZIPFiles.IsDots())
      if(ZIPFiles.IsDirectory())
      {
        //Calls itself again
        EnumerateDirectory(ZIPFiles.GetFilePath());
        bDisplayStatus=TRUE;
      }
      else
      {
        strFilename=ZIPFiles.GetFileName();
        strFilename.MakeUpper();
        //Checks whether it is a ZIP file or not.
        if(strFilename.Right(3)=="ZIP")
        {
          try
          {
            ZIPFile.Open(ZIPFiles.GetFilePath());
            nCount=ZIPFile.GetNoEntries();
            if(nCount>50)
            {
              //Updates the statusbar
              strStatus.Format("Processing %s", ZIPFiles.GetFilePath());
              m_wndStatusBar.SetText(strStatus,0,0);
              m_wndProgressSearch.SetRange(0,nCount-1);
              m_wndProgressSearch.ShowWindow(SW_SHOW);
            }
            //Searches inside ZIP file
            for(i=0; i<nCount && !m_bStopEnumeration &&
                !m_bSkipDirectory && !m_bSkipFile;i++)
            {
              if(nCount>10)
                m_wndProgressSearch.SetPos(i);
              ZIPFile.GetFileInfo(ZIPFileHeader,(WORD)i);
              if(CompareLike.IsLike(ZIPFileHeader.GetFileName(), 
                m_strFilename))
              {
                SHGetFileInfo(ZIPFileHeader.GetFileName(),
                  0,&sfi,sizeof(SHFILEINFO),
                  SHGFI_USEFILEATTRIBUTES|SHGFI_SYSICONINDEX|
                  SHGFI_TYPENAME|
                  (m_lvResult.GetStyle()&
                     LVS_ICON?SHGFI_LARGEICON:SHGFI_SMALLICON));

                ifFile=new ItemFile;
                ifFile->strFilename=ZIPFileHeader.GetFileName();
                ifFile->strType=sfi.szTypeName;
                if(ifFile->strType.IsEmpty())
                if(ifFile->strFilename.ReverseFind('.')>0)
                {
                  ifFile->strType=ifFile->strFilename.Right((
                    ifFile->strFilename.GetLength() -
                    ifFile->strFilename.ReverseFind('.'))-1);
                  ifFile->strType.MakeUpper();
                  ifFile->strType+=" File";
                }
                else
                  ifFile->strType="Unknown";
                // If it satisfies the search criteria then obtain the file 
                // information and it's associated icon
                ifFile->strSize.Format("%u KB",
                  (ZIPFileHeader.m_uUncomprSize/1024)?
                  (ZIPFileHeader.m_uUncomprSize/1024):1);
                ifFile->strZIPFilename=ZIPFiles.GetFileName();
                ifFile->strPath=ZIPFiles.GetRoot();
                                
                lvItem.mask=LVIF_TEXT|LVIF_IMAGE|LVIF_PARAM;
                lvItem.iItem=m_lvResult.GetItemCount();
                lvItem.iSubItem=0;
                lvItem.iImage=sfi.iIcon;
                lvItem.pszText=(LPSTR)ifFile->strFilename.operator LPCTSTR();
                lvItem.lParam=(LPARAM)ifFile;
                m_lvResult.InsertItem(&lvItem);
                                
                }
              }
              if(nCount>10)
              {
                m_wndProgressSearch.ShowWindow(SW_HIDE);
                bDisplayStatus=TRUE;
              }
              m_bSkipFile=FALSE;
              ZIPFile.Close();                    
            }
            catch(...)
            {
              if(!ZIPFile.IsClosed())
                ZIPFile.Close();
            }
          }    
       }
    }
  m_bSkipDirectory=FALSE;
  ZIPFiles.Close();
}

Points of Interest

Yes, I learned how to write a multithreaded app and how to process ZIP archives.

History

  • ZipSearch Version 1.0 � First Release

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here