📄 ziparchive.h
字号:
///////////////////////////////////////////////////////////////////////////////
// $RCSfile: ZipArchive.h,v $
// $Revision: 1.3 $
// $Date: 2005/03/07 20:40:37 $ $Author: Tadeusz Dracz $
////////////////////////////////////////////////////////////////////////////////
// This source file is part of the ZipArchive library source distribution and
// is Copyrighted 2000-2005 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
//
// Check the site http://www.artpol-software.com for the updated version of the library.
////////////////////////////////////////////////////////////////////////////////
//
// The following information files are distributed along with this library:
// License.txt - licensing information
// gpl.txt - General Public License text
// Readme.txt - general information
// ChangeLog.txt - revision history
// faq.txt - frequently asked questions
// Appnote.txt - details on the zip format
// ( also available at ftp://ftp.pkware.com/appnote.zip)
//
//
/**
* \file ZipArchive.h
* Interface for the CZipArchive class.
*
*/
#if !defined(AFX_ZIPARCHIVE_H__A7F528A6_1872_4071_BE66_D56CC2DDE0E6__INCLUDED_)
#define AFX_ZIPARCHIVE_H__A7F528A6_1872_4071_BE66_D56CC2DDE0E6__INCLUDED_
/**
\namespace ziparchv
A helper namespace.
\par Members
- CZipFileMapping
*/
/**
\struct CZipFileMapping ZipFileMapping.h
Maps a file to the memory. A system-specific implementation.
Stored in ziparchv namespace.
\c #include "ZipFileMapping.h"
*/
// to ensure that the correct files are copied
// (see "Compatibility" chapter in the documentation).
// Rebuild the project after copying the files.
#ifdef ZIP_ARCHIVE_MFC_PROJ
#ifndef ZIP_ARCHIVE_MFC
#error You need to copy files from the MFC subdirectory\
to the ZipArchive root directory and rebuild the project
#endif
#elif defined ZIP_ARCHIVE_STL_PROJ
#ifndef ZIP_ARCHIVE_STL
#error You need to copy files from the STL subdirectory\
to the ZipArchive root directory and rebuild the project
#endif
#endif //
#if (_MSC_VER > 1000) && (defined ZIP_HAS_DLL)
#pragma warning( disable : 4251 ) // needs to have dll-interface to be used by clients of class
#pragma warning( disable : 4275 ) // non dll-interface class used as base for dll-interface
#endif
#ifdef __GNUC__
#include "zlib.h"
#else
#include "zlib/zlib.h"
#endif
#include "ZipException.h"
#include "ZipAutoBuffer.h"
#include "ZipCentralDir.h"
#include "ZipStorage.h"
#include "ZipPathComponent.h"
#include "ZipString.h"
#include "ZipExport.h"
/**
Structure used as a parameter in CZipArchive::AddNewFile(CZipAddNewFileInfo& );
Use one of constructors provided and then adjust the member variables that are
set to default values by #Defaults method.
*/
struct ZIP_API CZipAddNewFileInfo
{
CZipAddNewFileInfo(LPCTSTR lpszFilePath, bool bFullPath = true)
: m_szFilePath(lpszFilePath),m_bFullPath(bFullPath)
{
m_pFile = NULL;
Defaults();
}
CZipAddNewFileInfo(LPCTSTR lpszFilePath, LPCTSTR lpszFileNameInZip)
: m_szFilePath(lpszFilePath), m_szFileNameInZip(lpszFileNameInZip)
{
m_pFile = NULL;
Defaults();
}
CZipAddNewFileInfo(CZipAbstractFile* pFile, LPCTSTR lpszFileNameInZip)
: m_pFile(pFile), m_szFileNameInZip(lpszFileNameInZip)
{
Defaults();
}
/**
the full path to the file to be added; if it is empty you need to set #m_pFile
*/
CZipString m_szFilePath;
/**
the file name that will be stored in the archive (if the file is a directory,
there is a path separator automatically added at the end); #SetRootPath
function has no effect on this parameter
*/
CZipString m_szFileNameInZip;
/**
It has only the meaning when #m_szFileNameInZip is not specified and #m_szFilePath is not empty.
- if \c true, include full path of the file inside the archive (even if #m_szRootPath is set)<BR>
- if \c false only the filename without a path is stored in the archive <BR>
in this case if #m_szRootPath is set previously with #SetRootPath
and if the beginning of #m_szFilePath equals #m_szRootPath
then the filename is set to the remaining part of m_szFilePath
(you can say to m_lpszFilePath minus #m_szRootPath)
*/
bool m_bFullPath;
/**
the compression level, see #OpenNewFile
*/
int m_iComprLevel;
/**
the smartness level of of the library; can be one or more #Smartness
values (you can OR them);<BR>
if zipsmCheckForEff is specified and due to it the file needs to be
reinserted into the archive without a compression and the callback
functor is specified, the callback functor's method Callback is called with the first argument set to
DWORD (-1) (you need to remember the last argument if you need the size of the file)
*/
int m_iSmartLevel;
/**
the index of the existing file in the archive to be replaced by the file being added (the new file
goes into the same physical place as the old file - the archive may of course grow or shrink as a result)
- >= 0 the index of the file to be replaced
- -1 do not replace any file and add the new file at the end of the archive (default)
- -2 if the new file has the same name as a file already in the archive then replace it or add at the end of the archive if it's filename is unique; it uses CZipArchive::FindFile with
the arguments \e iCaseSensitive and \e bFileNameOnly set to default values
\note
- you replace files in disk-spanning archives (i.e. use a value different from -1)
- if the space size taken by the old file is different from the space size needed by the new file, the callback is called while moving data (see CZipArchive::cbReplace)
- this replaces the file physically, so no information from the file being replaced is retained (such as attributes, modification time, etc.)
- if you use an invalid index, the function will fail; if you specify the last file in the archive to be replaced, it'll be removed and the usual action taken
- the new file encryption does not depend on the old file encryption but only on the current password settings (see CZipArchive::SetPassword)
- if #m_iComprLevel is not 0 then a temporary archive is created in the temporary directory
(see CZipArchive::SetTempPath) or in the memory (if you use CZipArchive::zipsmMemoryFlag in #m_iSmartLevel)
- <B>the most complicated scenario </B>is when you try to replace the file and use CZipArchive::zipsmCheckForEff flag in #m_iSmartLevel and the file compression proves to be
inefficient (you can use the sample application \e ZipArc to observe the following process):
- first you get CZipArchive::cbAdd callback - the file is being compressed to a temporary archive,
- then the file compression proves to be inefficient and the file needs to be stored instead of compressed,
but first the space inside archive must be adjusted to fit the file being added in the place of file being replaced - you get CZipArchive::cbReplace,
- then, at the end, the file is being stored - you get CZipArchive::cbAddStore callback
\see SetAdvanced
\see CZipArchive::WillBeDuplicated
*/
int m_iReplaceIndex;
/**
the size of the buffer used while file operations
*/
unsigned long m_nBufSize;
/**
instead of from a physical file, the data for compression is taken from the CZipAbstractFile object (e.g. from CZipMemFile)
\note
- you have to leave #m_szFilePath empty if you set #m_pFile to not NULL
- the time of the file in zip is set to the current time, and the attributes to the default
file attributes (depending on the system)
- you cannot add directories this way
*/
CZipAbstractFile* m_pFile;
/**
Set default values for #m_iSmartLevel, #m_iReplaceIndex, #m_nBufSize and #m_iComprLevel
*/
void Defaults();
};
/**
The representation of the zip archive file.
This class provides all the operations on the zip archive.
*/
class ZIP_API CZipArchive
{
public:
/**
The purpose of this structure is to hold the data that allow communication
with the zlib library
*/
struct ZIP_API CZipInternalInfo
{
CZipInternalInfo()
{
m_iBufferSize = 65536;
}
virtual ~CZipInternalInfo(){}
/**
Allocate internal buffer of m_iBufferSize size
*/
void Init()
{
m_pBuffer.Allocate(m_iBufferSize);
}
void ReleaseBuf()
{
m_pBuffer.Release();
}
z_stream m_stream; ///< zlib library data stream
DWORD m_uUncomprLeft; ///< bytes left to uncompress
DWORD m_uComprLeft; ///< bytes left to decompress
DWORD m_uCrc32; ///< crc32 file control value
/**
The size of the buffer used in decompressing data.
Set before opening the archive.
It is usually set with CZipArchive::SetAdvanced
(specify this value as the second argument).
\see CZipArchive::SetAdvanced
*/
DWORD m_iBufferSize;
/**
This buffer caches data during compression and decompression.
*/
CZipAutoBuffer m_pBuffer;
};
CZipArchive();
virtual ~CZipArchive();
/**
In non-UNICODE version just copy \e szSingle to \e szWide.
In UNICODE version works the same way as ZipPlatform::SingleToWide
\param szSingle
\param szWide
\return (in non-UNICODE version the number of characters copied)
\see ZipPlatform::SingleToWide
*/
static int SingleToWide(const CZipAutoBuffer &szSingle, CZipString& szWide);
/**
In non-UNICODE version just copy \e lpWide to \e szSingle.
In UNICODE version works the same way as ZipPlatform::WideToSingle
\param lpWide
\param szSingle
\return (in non-UNICODE version the number of characters copied)
\see ZipPlatform::WideToSingle
*/
static int WideToSingle(LPCTSTR lpWide, CZipAutoBuffer &szSingle);
#ifdef _UNICODE
/**
Call it to change how the Unicode string conversion is handled.
If \e bUseAnsi is set to \c true, ANSI (CP_ACP) code page is used,
otherwise translation is performed using UTF-8. Set it to \c false
e.g. for Japanese or Korean languages. Default is \c true.
\param bUseAnsi
if \c true, CP_ACP code page is used for the conversion, otherwise CP_UTF8 is used
\see EnableOemConversion
*/
static void WideConversionUseAnsi(bool bUseAnsi)
{
g_bWideConversionUseAnsi = bUseAnsi;
}
#endif
/**
Set the password for the file to be opened or created.
Use this method BEFORE opening or adding a file, but AFTER opening an archive
\param lpszPassword
set it to NULL to clear password
\return \c false if the password contains ASCII characters
with values 128 or higher or the file inside archive is opened
*/
bool SetPassword(LPCTSTR lpszPassword = NULL);
/**
\return the current archive password or an empty string if there is no password
*/
CZipString GetPassword()const ;
/**
Set the buffer sizes. No buffer can be set smaller than 1024.
Use this method before opening the archive. The optimal size for
the write buffer in the disk spanning archive is the size of the volume.
\param iWriteBuffer
the write cache size used
\see CZipStorage::m_iWriteBufferSize
\param iGeneralBuffer
buffer used in extracting, compressing, deleting, getting (#GetFromArchive) files, renaming and replacing
\see CZipInternalInfo::m_iBufferSize
\param iSearchBuffer
the buffer used in searching for the central dir
\see CZipCentralDir::m_iBufferSize
\see GetAdvanced
*/
void SetAdvanced(int iWriteBuffer = 65536, int iGeneralBuffer = 65536, int iSearchBuffer = 32768);
/**
retreive buffer size as set by #SetAdvanced
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -