⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zipstorage.h

📁 ZIP压缩代码:基于开源ZipArchive和zlib
💻 H
📖 第 1 页 / 共 2 页
字号:
////////////////////////////////////////////////////////////////////////////////
// $Workfile: ZipStorage.h $
// $Archive: /ZipArchive/ZipStorage.h $
// $Date: 03-01-09 10:30 $ $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 ZipStorage.h
* Interface for the CZipStorage class.	
*
*/

#if !defined(AFX_ZIPSTORAGE_H__941824FE_3320_4794_BDE3_BE334ED8984B__INCLUDED_)
#define AFX_ZIPSTORAGE_H__941824FE_3320_4794_BDE3_BE334ED8984B__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "ZipFile.h"	
#include "ZipAutoBuffer.h"
#include "ZipString.h"
#include "ZipMemFile.h"
#include "ZipExport.h"



/**
	A base class for functional objects (functors) that are used as a callbacks during various actions.
	You need to derive your own class and overload \c Callback method to use it.
	Do not derive from CZipCallback directly but from CZipSpanCallback (as a callback when there is a need 
	for a disk change in a disk-spanning archive) or from CZipActionCallback for other actions.
*/
struct ZIP_API CZipCallback
{
	/**
		Method called as a callback. 
		Return \c false from inside the method to abort the current operation. If it is a span callback functor,
		a CZipException with CZipException::aborted code will be thrown, otherwise the code will be CZipException::abortedAction or CZipException::abortedSafely.
		The following actions can be safely aborted (without having the archive corrupted):
		- counting bytes before deleting files
		- testing
		- saving central directory on non-disk-spanning archive 
		(saved data is removed in case of break	and you can save it again);
		it the archive is disk-spanning and if saving is aborted, the archive
		will not be damaged, but saved part of the central directory will be not removed
		and the new central directory will have to be saved after it

		\note Overrride this method in the derived class. If you define this method inside the class declaration, it should be inlined
			by the compiler making the action progress faster.
	*/
	virtual bool Callback(int iProgress) = 0;


	CZipString m_szExternalFile;	///< if the action (adding, extracting or disk-spanning) uses an external file, its filename is stored here	
};

/**
	Derive from this a class to be used as a callback functor for the disk change callback.
	You need to override member function CZipCallback::Callback. The meaning of \e iProgress parameter is the reason for calling:
		- -1 : a disk needed for reading		<BR>
	other codes occurs during writing:
		- >=0 : min. number of bytes needed
		- -2 : the file with the archive name already exists on the disk
		- -3 : the disk is probably write - protected
		- -4 : couldn't create a file
	

	Return \c false from the callback function to abort operation: the proper exception will be thrown.

	\see CZipCallback::Callback
	\see CZipArchive::SetSpanCallback
*/
struct ZIP_API CZipSpanCallback : public CZipCallback
{
	DWORD m_uDiskNeeded;		///< the number of a disk needed (counting from 1)
};



/**
	Derive from this a class to be used as a callback functors when adding, extracting, deleting, testing a file
	or saving central directory.
	You need to override member function CZipCallback::Callback. The meaning of \e iProgress parameter is the count
	of data just processed. It is a smallest number of bytes after which the callback method is called and it depends
	on the value of \e nBufSize in the CZipArchive methods that uses the callback feature. In case of saving the central 
	directory action it is the count of file headers just written (see CZipArchive::cbSave)
	\see CZipCallback::Callback
	\see CZipArchive::SetCallback
*/
struct ZIP_API CZipActionCallback : public CZipCallback
{

	CZipActionCallback()
	{		
		m_uTotalToDo = 0;
		m_uTotalSoFar = 0;
	}
	
	/**
		The type of the callback. It is set to one of CZipArchive::CallbackType values when the action begins.
		It's useful if you have more than one callback assigned to the same functor.
	*/
	int m_iType;

	/**
		Used by the ZipArchive library to init the callback function with the filenames. Resets #m_uTotalToDo and #m_uTotalSoFar variables to 0.
		#m_iType variable is already set to the proper value. Called at the beginning of the action.
	*/
	virtual void Init(LPCTSTR lpszFileInZip = NULL, LPCTSTR lpszExternalFile = NULL) 
	{
		m_szFileInZip = lpszFileInZip;
		m_szExternalFile = lpszExternalFile;
		m_uTotalToDo = 0;  // not yet known
		m_uTotalSoFar = 0; // nothing yet done
	}

	/**
		Called by the ZipArchive functions that use the callback feature after calculating total data to process.
		\param uTotalToDo
			total data to process; set #m_uTotalToDo to this value
	*/
	virtual void SetTotal(DWORD uTotalToDo)
	{
		m_uTotalToDo = uTotalToDo;
// 		m_uTotalSoFar = 0; // already done in CZipArchive::CZipClbckStrg::Get
	}



	/**
		Total number of data to process. The value of this variable is set after calling by the library #SetTotal method (it is 0 before).
		Depending on the action it is set then to:
		- adding file: the size the external file being added (or if callback is CZipArchive::cbAddTmp, the size of compressed data: CZipFileHeader::m_uComprSize)
		- extracting file: the size of uncompressed data (CZipFileHeader::m_uUncomprSize)
		- testing file: the same as above
		- deleting file: the count of bytes to move - the size of all files to remain above the first file to delete (calculated from offsets CZipFileHeader::m_uOffset
		- saving central directory: the number of files in the archive

	*/
	DWORD m_uTotalToDo;				
	DWORD m_uTotalSoFar;			///< total number of bytes processed so far 
	CZipString m_szFileInZip;		///< file in zip archive being currently processed


	/**
		\return the number of bytes left to process 
	*/
	DWORD LeftToDo() {return m_uTotalToDo - m_uTotalSoFar;}


	/**
		Called after the action finishes (it is not called in case of an exception, but
		it is called before throwing CZipException::abortedAction or CZipException::abortedSafely)
	*/
	virtual void CallbackEnd()
	{
		ASSERT(m_uTotalSoFar == m_uTotalToDo);
	};

	/**
		Used internally to call Callback and increase #m_uTotalSoFar by \e iProgress
	*/
	virtual bool operator()(int iProgress)
	{
		m_uTotalSoFar += iProgress;
		return Callback(iProgress);
	}

	/**
		Used only when creating map before deletion (see CZipArchive::cbDeleteCnt) or 
		saving the central directory record. You'll be notified every nth step (n is \e m_iStep value) with \e iProgress set to 
		\e m_iStep . Do not set it to low values or you can have a long waiting on archives
		with huge number of files. 

		\b Default: 256.
	*/
	static int m_iStep;


	/**
		Used internally to return #m_iStep value but not 0 (return 1 in this case).
	*/
	int GetStep(){return m_iStep ? m_iStep : 1;} // do not allow 0 (crash)
		
};


/**
	A low-level class - operates physically on archive (CZipArchive operates logically)
*/
class ZIP_API CZipStorage  
{
	friend class CZipCentralDir;
public:
	
	/**
		The type of the disk spanning archive.
		\see CZipArchive::GetSpanMode
	*/
	enum ZipSpanMode {
		noSpan,			///< no disk spanning
		pkzipSpan,		///< \ref PKSpan "disk spanning compatible with PKZIP"
		tdSpan,			///< \ref TDSpan "TD mode disk spanning archive"
		/**
			Detect the type automatically.
			If the archive is on the removable device assume PKZIP compatible,
			otherwise TD mode compatible.
		*/
		suggestedAuto,	
		/**
			If the disk spanning archive is on the removable device 
			assume it is TD mode compatible
		*/
		suggestedTd
	};

	CZipStorage();
	virtual ~CZipStorage();

/**
	Open the archive in memory (new or existing).
	The parameters are the same as CZipArchive::OpenMode.
	\param	mf
	\param	iMode
	\note Throws exceptions.

	\see CZipArchive::Open(LPCTSTR, int, int)
*/
	void Open(CZipMemFile& mf, int iMode);

/**
	Open or create an archive.
	The parameters are the same as CZipArchive::Open.
	\param	szPathName
	\param	iMode
	\param	iVolumeSize
	\note Throws exceptions.
	
	\see CZipArchive::Open(CZipMemFile& , int);
*/
	void Open(LPCTSTR szPathName, int iMode, int iVolumeSize);


	/**
		Close the disk-spanning archive and reopens as an existing disk-spanning archive or set mode to #noSpan 
	*/
	void FinalizeSpan();

	
/**
	Called only by CZipCentralDir::Read() when opening an existing archive.
	\param	uLastDisk
		the disk number the central directory is on
	\note Throws exceptions.

*/
	void UpdateSpanMode(WORD uLastDisk);


/**
	Write chunk of data to the archive.
	\param	pBuf
		buffer with data
	\param	iSize
		bytes to write
	\param	bAtOnce
		if \c true, the whole data must fit in the current volume, 
		otherwise the disk change is performed
	\note Throws exceptions.
*/
	void Write(const void *pBuf, DWORD iSize, bool bAtOnce);

/**
	Read chunk of data from the archive.
	\param	pBuf
		buffer to receive data
	\param	iSize
		bytes to read
	\param	bAtOnce
		if \c true, the specified number of bytes must be read from the same 
		volume (no disk change allowed)
	\note Throws exceptions.
*/
	DWORD Read(void* pBuf, DWORD iSize, bool bAtOnce);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -