📄 zipcentraldir.h
字号:
////////////////////////////////////////////////////////////////////////////////
// $Workfile: ZipCentralDir.h $
// $Archive: /ZipArchive/ZipCentralDir.h $
// $Date: 02-03-31 16:59 $ $Author: Tadeusz Dracz $
////////////////////////////////////////////////////////////////////////////////
// This source file is part of the ZipArchive library source distribution and
// is Copyright 2000-2003 by Tadeusz Dracz (http://www.artpol-software.com/)
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// For the licensing details see the file License.txt
////////////////////////////////////////////////////////////////////////////////
/**
* \file ZipCentralDir.h
* Interface for the CZipCentralDir class.
*
*/
#if !defined(AFX_CENTRALDIR_H__859029E8_8927_4717_9D4B_E26E5DA12BAE__INCLUDED_)
#define AFX_CENTRALDIR_H__859029E8_8927_4717_9D4B_E26E5DA12BAE__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#if (_MSC_VER > 1000) && (defined ZIP_HAS_DLL)
#pragma warning( disable : 4251 ) // needs to have dll-interface to be used by clients of class
#endif
#include "ZipException.h"
#include "ZipFileHeader.h"
#include "ZipAutoBuffer.h"
#include "ZipCollections.h"
#include "ZipCompatibility.h"
#include "ZipExport.h"
#define ZIPARCHIVE_DATADESCRIPTOR_LEN 12
/**
A class representing the central directory record in the archive.
*/
class ZIP_API CZipCentralDir
{
/**
Used in fast finding files by the filename.
A structure for the internal use only.
\see CZipCentralDir::m_findarray
\see CZipArchive::GetFindFastElement
\see CZipArchive::FindFile
\see CZipArchive::EnableFindFast
*/
struct ZIP_API CZipFindFast
{
CZipFindFast()
{
m_uIndex = 0;
m_pHeader= NULL;
}
CZipFindFast(CZipFileHeader* pHeader, WORD uIndex):m_pHeader(pHeader), m_uIndex(uIndex){}
/**
A pointer to the structure in CZipCentralDir. We extract a name from it.
*/
CZipFileHeader* m_pHeader;
/**
The index in the central directory of the \e m_pHeader.
*/
WORD m_uIndex;
};
public:
/**
Stores general information about the central directory record.
*/
struct ZIP_API Info
{
DWORD m_uCentrDirPos; ///< the position of the beginning of the central directory (as located by #Locate)
DWORD m_uBytesBeforeZip;///< The number of bytes before the actual zip archive in a file. It is non-zero for self-extracting archives.
WORD m_uThisDisk; ///< number of the disk with the central directory end record (the number of disks in the disk-spanning archive)
WORD m_uDiskWithCD; ///< number of the disk with the start of the central directory
WORD m_uDiskEntriesNo; ///< total number of entries in the central dir on this disk
WORD m_uEntriesNumber; ///< total number of entries in the central dir
DWORD m_uSize; ///< size of the central directory (valid only if #m_bOnDisk is \c true; use #GetSize instead)
DWORD m_uOffset; ///< offset of start of central directory with respect to the starting disk number
///< (as written in the central directory record);
///< valid only if #m_bOnDisk is \c true
bool m_bOnDisk; ///< \c true if the central directory is physically present in the archive
protected:
friend CZipCentralDir;
bool CheckIfOK_1()
{
return ((DWORD)m_uCentrDirPos >= m_uOffset + m_uSize);
}
void SetBytesBeforeZip(bool bIsSpan)
{
m_uBytesBeforeZip = bIsSpan ? 0 : m_uCentrDirPos - m_uSize - m_uOffset;
}
bool CheckIfOK_2()
{
return (m_uSize || !m_uEntriesNumber) && (m_uEntriesNumber || !m_uSize);
}
void DiskChange(int iCurrentDisk)
{
m_uThisDisk = (WORD)iCurrentDisk;
if (m_uEntriesNumber)
{
m_uDiskEntriesNo = 0;
}
else
{
m_uDiskWithCD = m_uThisDisk;
m_uOffset = 0;
}
}
};
CZipCentralDir();
virtual ~CZipCentralDir();
static char m_gszSignature[]; ///< central dir signature
char m_szSignature[4]; ///< end of central dir signature (must be 0x06054b50)
CZipAutoBuffer m_pszComment; ///< the archive comment
CZipAutoBuffer m_pLocalExtraField; ///< a local extra field
CZipFileHeader* m_pOpenedFile; ///< points to a currently opened file or NULL if no file is opened
/**
Called by CZipArchive::OpenInternal.
*/
void Init();
/**
Read the central directory from the archive.
\note Throws exceptions.
*/
void Read();
/**
Open the file.
\param uIndex
zero-based index of the file to open
\note Throws exceptions.
*/
void OpenFile(WORD uIndex);
/**
Test if the given file header index is valid.
\param uIndex
a zero-based index
\return \c true if the file with the given index exists inside the archive; otherwise \c false;
*/
bool IsValidIndex(int uIndex)const;
/**
Remove the file header from the central directory.
\param pHeader
the header to remove
\param iIndex if index is not known set it to -1
\param bShift
\note Throws exceptions.
*/
void RemoveFile(CZipFileHeader* pHeader, int iIndex = -1, bool bShift = true);
/**
Remove last file from the central directory.
*/
void RemoveLastFile(CZipFileHeader* pHeader = NULL, int iIndex = -1)
{
if (iIndex == -1)
{
iIndex = m_headers.GetSize() - 1;
if (iIndex == -1)
return;
}
if (!pHeader)
pHeader = m_headers[iIndex];
DWORD uNewSize = pHeader->m_uOffset + GetBytesBefore();
// then remove
RemoveFile(pHeader, iIndex);
m_pStorage->Flush();
m_pStorage->m_pFile->SetLength(uNewSize);
m_info.m_bOnDisk = false; // it is true when AutoFlush is set to true
}
/**
Remove all files
\note Throws exceptions.
*/
void RemoveAll();
/**
Cleanup the structure.
\param bEverything
- \c true - clear some attributes and remove all the file headers from memory
- \c false - do not remove the file headers. It is called in that manner
from CZipArchive::CloseFileAfterTestFailed so that the
next file can be tested for the integrity
\see CZipArchive::CloseFileAfterTestFailed
*/
void Clear(bool bEverything = true);
/**
Add a new file to the central directory.
\param header
copy data from it to the new file header
\param iReplaceIndex if different from -1, the index of the file to be replaced
\return the pointer to the new header
\note Throws exceptions.
*/
CZipFileHeader* AddNewFile(const CZipFileHeader & header, int iReplaceIndex = -1);
/**
return the header filename, converted if needed
*/
CZipString GetProperHeaderFileName(const CZipFileHeader* pHeader) const
{
if (!m_bConvertAfterOpen)
{
CZipFileHeader fh = *pHeader;
ConvertFileName(true, false, &fh);
return fh.GetFileName();
}
else
return pHeader->GetFileName();
}
/**
Remove physically the central directory from the archive.
Called during adding or deleting files.
\note Throws exceptions.
*/
void RemoveFromDisk();
/**
Get the central directory size.
\param bWhole
if \c true, include the size of the file headers
\return the size of the central directory
\see CZipArchive::GetCentralDirSize
*/
DWORD GetSize(bool bWhole = false) const;
/**
Close a file inside archive opened for reading.
\param bAfterException \c true if closing after exception
\note Throws exceptions.
*/
void CloseFile(bool bAfterException = false);
/**
Close a file inside archive opened for reading.
\note Throws exceptions.
*/
void CloseNewFile();
/**
Write the central directory to the archive.
\note Throws exceptions.
*/
void Write(CZipActionCallback* pCallback);
/**
\see CZipArchive::EnableFindFast
*/
void EnableFindFast(bool bEnable, bool bCaseSensitive);
/**
\see CZipArchive::FindFile
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -