Logo Search packages:      
Sourcecode: harbour version File versions  Download package

bool CZipArchive::TestFile ( WORD  uIndex,
DWORD  uBufSize = 65536 
)

Test the file with the given index for the integrity. You can set the callback functor with SetCallback. The method throws exceptions but performs all the necessary cleanup before, so that the next file can be tested after catching the exception.

Parameters:
uIndex index of the file to test
uBufSize the size of the buffer used during extraction
Returns:
false if the incorrect action has been taken by the user or the programmer (it is when OpenFile or GetFileInfo returned false or uBufSize is 0). If the file didn't passed the test or there was a disk I/O error or the password supplied was incorrect, an exception is thrown.
Note:
Throws exceptions.
See also:
SetCallback

Definition at line 2018 of file ziparchive.cpp.

References CZipException::abortedAction, CZipException::abortedSafely, CZipException::badZipFile, CZipActionCallback::CallbackEnd(), cbTest, CloseFile(), CloseFileAfterTestFailed(), CZipException::dirWithSize, GetCallback(), CZipCentralDir::GetProperHeaderFileName(), CZipAutoBuffer::GetSize(), CZipActionCallback::Init(), CZipFileHeader::IsDirectory(), CZipFileHeader::IsEncrypted(), CZipStorage::IsSpanMode(), m_centralDir, m_storage, CZipFileHeader::m_uComprSize, CZipFileHeader::m_uUncomprSize, OpenFile(), ReadFile(), CZipActionCallback::SetTotal(), and CZipException::Throw().

{
      if (m_storage.IsSpanMode() == 1)
      {
            TRACE(_T("%s(%i) : You cannot test the spanning archive in creation.\n"),__FILE__,__LINE__);
            return false;
      }
      if (!uBufSize)
            return false;

      CZipFileHeader* pHeader = m_centralDir[uIndex];
      CZipActionCallback* pCallback = GetCallback(cbTest);
      if (pCallback)
      {
            pCallback->Init(m_centralDir.GetProperHeaderFileName(pHeader));
      }

      if (pHeader->IsDirectory())
      {
            if (pCallback)
                  pCallback->SetTotal(0);

            // we do not test whether the password for the encrypted directory
            // is correct, since it seems to be senseless (anyway password
            // encrypted directories should be avoided - it adds 12 bytes)
            DWORD iSize = pHeader->m_uComprSize;
            if ((iSize != 0 || iSize != pHeader->m_uUncomprSize)
                  // different treating compressed directories
                  && !(pHeader->IsEncrypted() && iSize == 12 && !pHeader->m_uUncomprSize))
                  CZipException::Throw(CZipException::dirWithSize);

            if (pCallback)
                  pCallback->CallbackEnd();

            return true;
      }
      else
      {
            try
            {
                  if (pCallback)
                        pCallback->SetTotal(pHeader->m_uUncomprSize);

                  if (!OpenFile(uIndex))
                        return false;
                  CZipAutoBuffer buf(uBufSize);
                  DWORD iRead;
                  int iAborted = 0;
                  do
                  {
                        iRead = ReadFile(buf, buf.GetSize());
                        if (pCallback && iRead)
                              if (!(*pCallback)(iRead))
                              {
                                    if (iRead == buf.GetSize() && ReadFile(buf, 1) != 0) // test one byte if there is something left
                                          iAborted = CZipException::abortedAction;
                                    else
                                          iAborted = CZipException::abortedSafely; // we did it!
                                    break;
                              }
                  }
                  while (iRead == buf.GetSize());
                  bool bRet = CloseFile() != -1;
                  if (!bRet && iAborted == CZipException::abortedSafely)
                        iAborted = CZipException::abortedAction; // sorry, finished, but not successfull

                  if (pCallback)
                        pCallback->CallbackEnd();

                  if (iAborted)
                        CZipException::Throw(iAborted); // throw to distuingiush from other return codes
                  if (bRet)
                        return true;
                  else
                        CZipException::Throw(CZipException::badZipFile);
                  return false; // to satisfy the compiler and eliminate warning
            }
            catch(...)
            {
                  CloseFileAfterTestFailed();
                  throw;
            }
      }
}


Generated by  Doxygen 1.6.0   Back to index