📄 pkzipcoder.cpp
字号:
// PKZipCoder.cpp: implementation of the CPKZipCoder class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "PKZipCoder.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
// Create a zip archive with name lpszFileName
BOOL CPKZipCoder::FileCreate(const char *lpszFileName)
{
// Delete the file if it exists
unlink(lpszFileName);
try
{
// Create the archive file
m_zipArchive.Open(lpszFileName, CZipArchive::zipCreate);
return TRUE;
}
catch(...)
{
return FALSE;
}
}
// Open an existing zip archive named lpszFileName
BOOL CPKZipCoder::FileOpen(const char *lpszFileName)
{
try
{
// Open the archive file
m_zipArchive.Open(lpszFileName, CZipArchive::zipOpen);
return TRUE;
}
catch(...)
{
return FALSE;
}
}
// Add a file to current zip archive
BOOL CPKZipCoder::FileAdd(const char *lpszFileName)
{
// Check file
if(FileExists(lpszFileName))
{
// add the new file to archive
return (BOOL)m_zipArchive.AddNewFile(lpszFileName, Z_BEST_COMPRESSION, FALSE);
}
else
{
return FALSE;
}
}
// Extract a file to disk from the current zip archive
BOOL CPKZipCoder::FileExtract(int nFileIndex,
const char *lpszPath,
char *lpszOriginalFileName, // [out] the original zipped file name
long nNameBuffSize) // [in] the buffer size of lpszOriginalFileName
{
CZipFileHeader fileinfo;
// Get file info
if(m_zipArchive.GetFileInfo(fileinfo, nFileIndex))
{
try
{
CZipString strFileName = fileinfo.GetFileName();
// copy the file name
if(strFileName.GetLength() < nNameBuffSize && lpszOriginalFileName)
{
strcpy(lpszOriginalFileName, strFileName);
}
// Extract file
return m_zipArchive.ExtractFile(nFileIndex, lpszPath, FALSE);
}
catch(...)
{
}
}
return FALSE;
}
// Create a memory based zip archive
BOOL CPKZipCoder::MemCreate()
{
try
{
// create an archive based on memory file
m_zipArchive.Open(m_zipMemFile, CZipArchive::zipCreate);
return TRUE;
}
catch(...)
{
return FALSE;
}
}
// Open a zip archive in memory
BOOL CPKZipCoder::MemOpen(const void *pMemFileData, long nDataSize)
{
// load data to memory file
if(GetDataFromMem(pMemFileData, nDataSize, m_zipMemFile))
{
try
{
// open an archive based on the memory file
m_zipArchive.Open(m_zipMemFile);
return TRUE;
}
catch(...)
{
}
}
return FALSE;
}
// Add a memory file to current zip archive
BOOL CPKZipCoder::MemAdd(const char *lpszDumbFileName, // [in] the stored filename in zip
const void *pFileData,
long nDataSize)
{
CZipMemFile memfile;
char lpszBuff[16];
char *lpszFile = lpszBuff;
// generate the dumb file name
if(lpszDumbFileName && strlen(lpszDumbFileName))
{
lpszFile = (char *)lpszDumbFileName;
}
else
{
int nCount = m_zipArchive.GetCount(TRUE);
sprintf(lpszBuff, "%08X", nCount);
}
// load data to memory file
if(GetDataFromMem(pFileData, nDataSize, memfile))
{
// add the memory file to the archive
return m_zipArchive.AddNewFile(memfile, lpszFile, Z_BEST_COMPRESSION);
}
return FALSE;
}
// Extract a file from current archive to memory
BOOL CPKZipCoder::MemExtract(int nFileIndex,
void *pFileData,// [out] buffer to retrieve the memory file
long nBuffSize, // [in] size of pFileData buffer in bytes
char *lpszOriginalFileName, // [out] the original filename
long nNameBuffSize) // [in] size of lpszOriginalFileName buffer
{
CZipMemFile memfile;
CZipFileHeader fileinfo;
// get the file info
if(m_zipArchive.GetFileInfo(fileinfo, nFileIndex))
{
// copy the extracted file name
if(lpszOriginalFileName)
{
CZipString strFileName = fileinfo.GetFileName();
if(strFileName.GetLength() < nNameBuffSize)
{
strcpy(lpszOriginalFileName, strFileName);
}
}
// Extract to memory file
if(m_zipArchive.ExtractFile(nFileIndex, memfile))
{
// dump memory file to buffer
return SetDataToMem(pFileData, nBuffSize, memfile);
}
}
return FALSE;
}
// Get the file count in the current zip archive
int CPKZipCoder::GetFileCount()
{
try
{
return m_zipArchive.GetCount(TRUE);
}
catch(...)
{
return 0;
}
}
// Get the file size of the file nFileIndex in zip archive
long CPKZipCoder::GetFileSize(int nFileIndex)
{
CZipFileHeader fileinfo;
// get file info
if(m_zipArchive.GetFileInfo(fileinfo, nFileIndex))
{
return fileinfo.m_uUncomprSize;
}
return 0;
}
// Close the zip archive. If it is on disk, save it.
BOOL CPKZipCoder::Close()
{
try
{
m_zipArchive.Close();
return TRUE;
}
catch(...)
{
return FALSE;
}
}
// Get the whole archive size in bytes
long CPKZipCoder::GetArchiveSize()
{
return m_zipMemFile.GetLength();
}
// Get the memory data of the whole archive
BOOL CPKZipCoder::MemGetArchive(void *pArchiveData, // [out] data buffer
long nBuffSize) // [in] buffer size of pArchiveData
{
Close();
// dump the archive memory file to buffer
return SetDataToMem(pArchiveData, nBuffSize, m_zipMemFile);
}
// Is the file exists?
BOOL CPKZipCoder::FileExists(const char *lpszFileName)
{
FILE *fp = NULL;
fp = fopen(lpszFileName, "rb");
if(fp == NULL)
{
return FALSE;
}
else
{
fclose(fp);
return TRUE;
}
}
// Load memory data to CZipMemFile
BOOL CPKZipCoder::GetDataFromMem(const void *pData, long nDataSize, CZipMemFile &memfile)
{
try
{
memfile.SetLength(nDataSize);
memfile.Write(pData, nDataSize);
memfile.SeekToBegin();
return TRUE;
}
catch(...)
{
return FALSE;
}
}
// Save CZipMemFile to memory buffer
BOOL CPKZipCoder::SetDataToMem(void *pData, long nDataSize, CZipMemFile &memfile)
{
try
{
UINT nSize;
ZIP_ULONGLONG nPos;
nSize = memfile.GetLength();
if(nSize <= (UINT)nDataSize)
{
nPos = memfile.SeekToBegin();
memfile.Read(pData, nSize);
memfile.Seek(nPos, CZipAbstractFile::begin);
return TRUE;
}
}
catch(...)
{
}
return FALSE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -