home *** CD-ROM | disk | FTP | other *** search
- // ZIPFindDlg.cpp : implementation file
- //
-
- #include "stdafx.h"
- #include <io.h>
- #include "ZIPFind.h"
- #include "ZIPFindDlg.h"
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
- #define CENTRALDIRSIG 0x02014B50
- #define DIRTAILSIG 0x06054B50
-
- #pragma pack(1) // Give me it like it is !!
-
- typedef struct DIRTAIL
- {
- long Signature; // should be DIRTAILSIG
- unsigned short ThisDisk; // number of this disk
- unsigned short DirDisk; // disk # holding dir
- unsigned short NumEntries; // number of entries (this disk)
- unsigned short TotEntries; // total # files in ZIP
- long DirSize; // size of central dir
- long DirOffset; // offset of central dir
- unsigned short BannerLength; // length of ZIP file banner
- } DIRTAIL;
-
- typedef struct DIRENTRY
- {
- long Signature; // should be $02014B50
- unsigned short CreatorVersion; // version of PKZIP used for create
- unsigned short ExtractorVersion; // version needed for extract
- unsigned short GenBits; // general purpose bit flags
- unsigned short CompressMethod; // compression method used
- unsigned short Modtime; // last modification time
- unsigned short ModDate; // last modification date
- long Crc32; // 32-bit CRC
- long CompressedSize; // compressed size of file
- long OriginalSize; // original size of file
- unsigned short FileNameLen; // length of file name
- unsigned short ExtraLen; // extra info length
- unsigned short CommentLen; // length of file comment
- unsigned short DiskNumStart; // starting disk number
- unsigned short FileAttribs; // file attributes
- long FileFlags; // file flag bits
- long HeaderPos; // pointer to local header
- } DIRENTRY;
-
- /////////////////////////////////////////////////////////////////////////////
- // CAboutDlg dialog used for App About
-
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
-
- // Dialog Data
- //{{AFX_DATA(CAboutDlg)
- enum { IDD = IDD_ABOUTBOX };
- //}}AFX_DATA
-
- // ClassWizard generated virtual function overrides
- //{{AFX_VIRTUAL(CAboutDlg)
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- //}}AFX_VIRTUAL
-
- // Implementation
- protected:
- //{{AFX_MSG(CAboutDlg)
- //}}AFX_MSG
- DECLARE_MESSAGE_MAP()
- };
-
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
- //{{AFX_DATA_INIT(CAboutDlg)
- //}}AFX_DATA_INIT
- }
-
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CAboutDlg)
- //}}AFX_DATA_MAP
- }
-
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- //{{AFX_MSG_MAP(CAboutDlg)
- // No message handlers
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CZIPFindDlg dialog
-
- CZIPFindDlg::CZIPFindDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CZIPFindDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CZIPFindDlg)
- // NOTE: the ClassWizard will add member initialization here
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
-
- void CZIPFindDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CZIPFindDlg)
- // NOTE: the ClassWizard will add DDX and DDV calls here
- //}}AFX_DATA_MAP
- }
-
- BEGIN_MESSAGE_MAP(CZIPFindDlg, CDialog)
- //{{AFX_MSG_MAP(CZIPFindDlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_SEARCH, OnSearch)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CZIPFindDlg message handlers
-
- BOOL CZIPFindDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
-
- // Add "About..." menu item to system menu.
-
- // IDM_ABOUTBOX must be in the system command range.
- ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
- ASSERT(IDM_ABOUTBOX < 0xF000);
-
- CMenu* pSysMenu = GetSystemMenu(FALSE);
- CString strAboutMenu;
- strAboutMenu.LoadString(IDS_ABOUTBOX);
- if (!strAboutMenu.IsEmpty())
- {
- pSysMenu->AppendMenu(MF_SEPARATOR);
- pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
- }
-
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
-
- // Set up drive list combo box
-
- char drvName [5];
- CComboBox * p = (CComboBox *)GetDlgItem(IDC_DRIVELIST);
-
- for (int drv = 2; drv < 26; drv++)
- {
- lstrcpy (drvName, "X:\\");
- drvName [0] = drv + 'A';
- if (GetDriveType (drvName) == DRIVE_FIXED)
- {
- char volName [128], sz [128];
- GetVolumeInformation (drvName, volName, sizeof (volName), 0, 0, 0, 0, 0);
- sprintf (sz, "%c: [%s]", drv + 'A', volName);
- p->AddString (sz);
- }
- }
-
- p->SetCurSel (p->AddString("[All Drives]"));
-
- GetDlgItem(IDC_FILENAME)->SetFocus(); // Filename edit box has focus
-
- return TRUE; // return TRUE unless you set the focus to a control
- }
-
- void CZIPFindDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
-
- // If you add a minimize button to your dialog, you will need the code below
- // to draw the icon. For MFC applications using the document/view model,
- // this is automatically done for you by the framework.
-
- void CZIPFindDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
-
- SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
-
- // Center icon in client rectangle
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
-
- // Draw the icon
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
-
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
-
- HCURSOR CZIPFindDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
-
- // Fast scanner for Signature
-
- int FindSig (char * buff, int len, long Signature)
- {
- for (char * p = buff; p <= &buff [len - 4]; p++)
- {
- p = (char *) memchr (p, Signature & 255, &buff [len] - p);
- if (p == NULL || buff + len - p < 4) break;
- if (*(long *)p == Signature) return p - buff;
- }
-
- return (-1);
- }
-
- // Scan the ZIP file for the wanted signature
-
- long GetSigOffset (HFILE fd, long Signature)
- {
- int bp; // found position of sig
- int bytesRead; // bytes read from the file
- long fs; // size of the ZIP file
- long pos; // current seek position
- static char sigbuff [4096]; // buffer for sig searches
-
- fs = _llseek (fd, 0, FILE_END);
- pos = (fs <= sizeof (sigbuff)) ? 0 : fs - sizeof (sigbuff);
- _llseek (fd, pos, 0);
-
- // Get initial buffer content
-
- _lread (fd, sigbuff, sizeof (sigbuff));
- bp = FindSig (sigbuff, sizeof (sigbuff), Signature);
-
- // This is the main search loop...
-
- while (bp < 0 && pos > 0)
- {
- memcpy (&sigbuff [sizeof (sigbuff) - 4], sigbuff, 4);
- pos -= sizeof (sigbuff) - 4;
- if (pos < 0) pos = 0;
- _llseek (fd, pos, 0);
- bytesRead = _lread (fd, sigbuff, sizeof (sigbuff) - 4);
-
- if (bytesRead < sizeof (sigbuff) - 4)
- memcpy (&sigbuff [bytesRead], &sigbuff [sizeof (sigbuff) - 4], 4);
-
- if (bytesRead > 0)
- {
- bytesRead += 4;
- bp = FindSig (sigbuff, bytesRead, Signature);
- }
- }
-
- return (bp < 0) ? -1 : (pos + bp);
- }
-
-
- // Read a ZIP file directory into memory
-
- char * ReadDirectory (HFILE fd, int * numFiles)
- {
- long tailpos;
- DIRTAIL tail;
- char * dir = NULL;
-
- if ((tailpos = GetSigOffset (fd, DIRTAILSIG)) >= 0)
- {
- _llseek (fd, tailpos, 0);
- _lread (fd, &tail, sizeof (tail));
- *numFiles = tail.TotEntries;
- _llseek (fd, tail.DirOffset, 0);
- _lread (fd, dir = (char *) malloc (tail.DirSize), tail.DirSize);
-
- }
-
- return (dir);
- }
-
- // Try to find a match on ZIP file contents
-
- int MatchFile (DIRENTRY * p, int numFiles, char * FileSpec)
- {
- int next;
- char * p1, * p2;
- char buff [128];
- char spec [128];
-
- while (numFiles--)
- {
- // We need this sanity check to guard against nested ZIP's
-
- if (p->Signature != CENTRALDIRSIG) break;
-
- memcpy (buff, p + 1, p->FileNameLen);
- buff [p->FileNameLen] = '\0';
-
- // Now do a rather simplistic pattern match
-
- _strupr (buff);
- strcpy (spec, FileSpec);
- if (strcmp (buff, spec) == 0) return (1);
-
- if ((p1 = strchr (buff, '.')) != NULL && (p2 = strchr (spec, '.')) != NULL)
- {
- *p1++ = '\0';
- *p2++ = '\0';
-
- if (strcmp (buff, spec) == 0 || *buff == '*' || *spec == '*')
- if (strcmp (p1, p2) == 0 || *p1 == '*' || *p2 == '*')
- return (1);
- }
-
- next = p->FileNameLen + p->ExtraLen + p->CommentLen + sizeof (DIRENTRY);
- p = (DIRENTRY *)(next + (char *) p);
- }
-
- return (0);
- }
-
- // See if the ZIP file contains the wanted file spec
-
- void ValidateZIP (char * ZipFileName, CListBox * FileList, char * FileSpec)
- {
- HFILE fd; // file descriptor for this ZIP file
- char * p; // pointer to ZIP file directory
- int numFiles; // number of files in this ZIP file
-
- if ((fd = _lopen (ZipFileName, OF_READ)) != -1)
- {
- if ((p = ReadDirectory (fd, &numFiles)) != NULL)
- {
- if (MatchFile ((DIRENTRY *) p, numFiles, FileSpec)) FileList->AddString (ZipFileName);
- free (p);
- }
-
- _lclose (fd);
- }
- }
-
- void ScanZipFiles (char * PathSpec, CListBox * FileList, char * FileSpec, CStatic * progress)
- {
- long findHandle;
- char Path [256], temp [256]; // Recursive ? Don't do this at home...
- _finddata_t findBuffer;
-
- strcat (strcpy (Path, PathSpec), "*.*");
- findHandle = _findfirst (Path, &findBuffer);
- while (findHandle != -1)
- {
- // Is it a ZIP file ?
-
- if ((findBuffer.attrib & _A_SUBDIR) == 0)
- {
- if (strcmpi (&findBuffer.name [strlen (findBuffer.name) - 4], ".ZIP") == 0)
- {
- strcat (strcpy (temp, PathSpec), findBuffer.name);
- progress->SetWindowText (temp);
- ValidateZIP (temp, FileList, FileSpec);
- }
- }
- else if (findBuffer.name [0] != '.')
- {
- // It's recursion time !
- strcat (strcat (strcpy (temp, PathSpec), findBuffer.name), "\\");
- ScanZipFiles (temp, FileList, FileSpec, progress);
- }
-
- // Set up for the next directory entry...*/
-
- if (_findnext (findHandle, &findBuffer) != 0) break;
- }
-
- if (findHandle != -1) _findclose (findHandle);
- }
-
- void CZIPFindDlg::OnSearch()
- {
- CEdit * fName = (CEdit *)GetDlgItem(IDC_FILENAME);
- CListBox * fList = (CListBox *)GetDlgItem(IDC_FOUNDLIST);
- CComboBox * dList = (CComboBox *)GetDlgItem(IDC_DRIVELIST);
- CStatic * progress = (CStatic *)GetDlgItem(IDC_PROGRESS);
-
- char szBuff [128];
- fName->GetWindowText (szBuff, sizeof (szBuff));
- if (szBuff [0] == '\0') MessageBox ("No file name was specified.");
- else
- {
- // automatic wait cursor handling
- CWaitCursor cWait;
-
- // disable the OnSearch button
- (CButton *)GetDlgItem(IDC_SEARCH)->EnableWindow (FALSE);
-
- // clear the list box
- fList->ShowWindow (SW_HIDE);
- fList->ResetContent();
-
- // Show progress item
- progress->ShowWindow (SW_SHOW);
-
- // get current drive selection
- char driveSel [20];
- dList->GetLBText (dList->GetCurSel(), driveSel);
-
- for (int drv = 2; drv < 26; drv++)
- if (driveSel[0] == '[' || driveSel[0] == drv + 'A')
- {
- char searchPath [4];
- strcpy (searchPath, "X:\\");
- searchPath [0] = drv + 'A';
- ScanZipFiles (searchPath, fList, _strupr (szBuff), progress);
- }
-
- // enable the OnSearch button
- (CButton *)GetDlgItem(IDC_SEARCH)->EnableWindow (TRUE);
- progress->ShowWindow (SW_HIDE);
- fList->ShowWindow (SW_SHOW);
- }
- }
-
-
- BOOL CZIPFindDlg::OnCommand(WPARAM wParam, LPARAM lParam)
- {
- if (wParam == IDOK) // Interpret ENTER as a click on "Search" button
- {
- OnSearch ();
- return FALSE;
- }
-
- return CDialog::OnCommand(wParam, lParam);
- }
-
-