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

bool CZipArchive::OpenFile ( WORD  uIndex  ) 

Open the file with the given index in the archive for extracting. Not successful opening the file doesn't lock the whole archive, so you can try to open another one (after catching an exception if it was thrown). Throw exception CZipException::badPassword if the password was not set for the encrypted file.

Parameters:
uIndex the index of the file
Returns:
true if successful
Note:
Throws exceptions.

Definition at line 184 of file ziparchive.cpp.

References CZipException::badPassword, CZipException::badZipFile, CheckForError(), CryptCheck(), CryptInitKeys(), CurrentFile(), extract, CZipAutoBuffer::GetSize(), CZipArchive::CZipInternalInfo::Init(), CZipStorage::IsSpanMode(), CZipCentralDir::IsValidIndex(), m_centralDir, m_iFileOpened, m_info, m_pszPassword, m_storage, CZipArchive::CZipInternalInfo::m_stream, CZipArchive::CZipInternalInfo::m_uComprLeft, CZipFileHeader::m_uComprSize, CZipArchive::CZipInternalInfo::m_uCrc32, CZipFileHeader::m_uMethod, CZipArchive::CZipInternalInfo::m_uUncomprLeft, CZipFileHeader::m_uUncomprSize, CZipCentralDir::OpenFile(), and ThrowError().

Referenced by ExtractFile(), and TestFile().

{
      if (!m_centralDir.IsValidIndex(uIndex))
      {
            ASSERT(FALSE);
            return false;
      }
      if (m_storage.IsSpanMode() == 1)
      {
            TRACE(_T("%s(%i) : You cannot extract from the span in creation.\n"),__FILE__,__LINE__);
            return false;
      }


      if (m_iFileOpened)
      {
            TRACE(_T("%s(%i) : A file already opened.\n"),__FILE__,__LINE__);
            return false;
      }

      m_info.Init();
      m_centralDir.OpenFile(uIndex);
      if (CurrentFile()->IsEncrypted())
      {

            if (m_pszPassword.GetSize() == 0)
            {
                  TRACE(_T("%s(%i) : Password not set for the encrypted file.\n"),__FILE__,__LINE__);
                        ThrowError(CZipException::badPassword);
            }
            CryptInitKeys();
            if (!CryptCheck())
                  ThrowError(CZipException::badPassword);

      }
      else if (m_pszPassword.GetSize() != 0)
      {
            TRACE(_T("%s(%i) : Password set for a not encrypted file. Ignoring password.\n"),__FILE__,__LINE__);
      }

      WORD uMethod = CurrentFile()->m_uMethod;

      if ((uMethod != 0) &&(uMethod != Z_DEFLATED))
            ThrowError(CZipException::badZipFile);

      if (uMethod == Z_DEFLATED)
      {
//          m_info.m_stream.opaque =  m_bDetectZlibMemoryLeaks ? &m_list : 0;
            int err = inflateInit2(&m_info.m_stream, -MAX_WBITS);
            //                * windowBits is passed < 0 to tell that there is no zlib header.
            //          * Note that in this case inflate *requires* an extra "dummy" byte
            //          * after the compressed stream in order to complete decompression and
            //          * return Z_STREAM_END.
            CheckForError(err);
      }
      m_info.m_uComprLeft = CurrentFile()->m_uComprSize;
      if (CurrentFile()->IsEncrypted())
            m_info.m_uComprLeft -= ZIPARCHIVE_ENCR_HEADER_LEN;
      m_info.m_uUncomprLeft = CurrentFile()->m_uUncomprSize;
      m_info.m_uCrc32 = 0;
      m_info.m_stream.total_out = 0;
      m_info.m_stream.avail_in = 0;

      m_iFileOpened = extract;
      return true;
}


Generated by  Doxygen 1.6.0   Back to index