DecompressLibrary - 一个通用的库,用于将 zip、gz 或 tar.gz 文件解压缩到内存缓冲区






4.93/5 (39投票s)
该库提供了一个易于使用的类 (CDecompressLibrary),它可以检测并解压缩 zip、gz 或 tar.gz 归档文件到内存缓冲区。
引言
我开发这个类是因为我需要一个通用的类,它可以内部检测文件是否为归档文件,然后读取归档文件中包含的压缩文件。对于 Zip 和 gz 归档文件,我在 CodeProject 的文章中找到了源代码,分别是 Hans Dietrich 和 Jonathan de Halleux。
我还实现了类中检测和读取 tar 文件的功能。
Using the Code
该类提供以下方法:检测和打开归档文件、返回归档文件中的文件数量以及根据文件位置从归档文件中读取文件。在头文件中,我们可以找到这些方法及其描述。
/***********************************************************************
Open a compressed file.
Parameters
pszCompressedFile - name of the file
Return values:
TRUE - file opened
FALSE - file could not be opened
(use GetErrorText() to find the error)\
Note:
The archive file must have one of these extensions:
- .zip
- .gz
- .tar.gz
- .tgz
***********************************************************************/
BOOL OpenArchive(IN const char* pszArchiveFile);
/***********************************************************************
Closes the archive, destroys all the internal objects
***********************************************************************/
void CloseArchive();
/***********************************************************************
Get number of files in the archive
Return values:
Number of files in the archive.
-1 if there are errors.
***********************************************************************/
int GetCompressedFilesCount();
/***********************************************************************
Get a buffer that contain the file in archive at a specific position
Params:
nFileIndex - file position in archive
pszFileBuffer - buffer containing the file data
nFileSize - file size
fIsFile - TRUE if this is a regular file,
FALSE otherwise (directory, link, etc)
szFileName - name of the file
Return values:
TRUE - file found and buffer filled correctly
FALSE - some error (use GetErrorText() to find the error)
Note:
All data buffers will be allocated/deleted internally in this class,
they should not be changed from outside !
***********************************************************************/
BOOL GetArchiveFile( IN int nFileIndex,
OUT char** pszFileBuffer,
OUT int& nFileSize,
OUT BOOL& fIsFile,
OUT CString& szFileName);
/***********************************************************************
Get last error if there were any problems
***********************************************************************/
CString GetErrorText();
BOOL OpenArchive(IN const char* pszArchiveFile);
如何使用
将 DecompressLibrary
项目包含在您的工作区中。接下来,包含 DecompressClass.h 头文件。示例用法
CDecompressClass testObj;
// open archive
testObj.OpenArchive("archivefile.zip");
// if we opened ok, read each file from the archive
for (int i=0; i < testObj.GetCompressedFilesCount(); i++)
{
// will contain the file buffer
char* pszFileBuffer;
// will contain the file length
int nFileLength;
// will contain the file name
CString szFileName;
// flag if is a file or not (directory, link, etc)
BOOL fIsFile;
testObj.GetArchiveFile(i, /*position in archive*/
&pszFileBuffer, /*will point to the memory buffer
containing the file content*/
nFileLength, /*will contain the file length*/
fIsFile, /*will show if it's a file or not*/
szFileName /*name of the file*/
);
}
历史
- 版本 1.0 - 2005 年 11 月 14 日,类的首次实现
演示应用
您会找到一个用于测试该类的测试对话框
用法
此软件由 Marius Daniel Ciorecan 在 https://elapseit.com 创建,并发布到公共领域。您可以以任何您喜欢的方式自由使用它。如果您修改或扩展它,请考虑在此处发布新代码供大家共享。此软件按“原样”提供,不提供任何明示或暗示的保证。对于因本软件造成的任何损害或业务损失,我概不负责。