zlib库剖析:实现概览 Zipper.cpp
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了zlib库剖析:实现概览 Zipper.cpp相关的知识,希望对你有一定的参考价值。
// Zipper.cpp: implementation of the CZipper class. // //////////////////////////////////////////////////////////////////////
#include "Zipper.h" #include "zip.h" #include "iowin32.h" #include "common.h" #include "directory.h" #include "file_manager.h" #include <time.h> #include <sys/types.h> #include <sys/stat.h> #include <string.h> ////////////////////////////////////////////////////////////////////// // Construction/Destruction //////////////////////////////////////////////////////////////////////
const int BUFFERSIZE = 2048;
CZipper::CZipper(const char * szFilePath) : m_uzFile(0) { CloseZip();
if (szFilePath) OpenZip(szFilePath); }
CZipper::~CZipper() { CloseZip(); }
bool CZipper::CloseZip() { int nRet = m_uzFile ? zipClose(m_uzFile, NULL) : ZIP_OK;
m_uzFile = NULL; memset(&m_info,0, sizeof(m_info));
return (nRet == ZIP_OK); }
void CZipper::GetFileInfo(Z_FileInfo& info) { info = m_info; }
// works with prior opened zip bool CZipper::AddFileToZip(const char * szFilePath) { if (!m_uzFile) return false;
string strFileName = szFilePath; size_t last_slash_pos= strFileName.rfind(DIR_DELIMITER); strFileName = strFileName.substr(last_slash_pos+1, string::npos); if(strFileName =="") return false; // save file attributes zip_fileinfo zfi;
zfi.internal_fa = 0; zfi.external_fa = 0; // save file time struct stat fileStatusInfo;
if (stat(szFilePath, &fileStatusInfo) == 0) { //stat_info.st_mtime struct tm * newtime = localtime(&fileStatusInfo.st_mtime); zfi.dosDate = 0; zfi.tmz_date.tm_year = newtime->tm_year+1900; zfi.tmz_date.tm_mon = newtime->tm_mon; zfi.tmz_date.tm_mday = newtime->tm_mday; zfi.tmz_date.tm_hour = newtime->tm_hour; zfi.tmz_date.tm_min = newtime->tm_min; zfi.tmz_date.tm_sec = newtime->tm_sec; }
FILE *fp = fopen(szFilePath,"rb"); if(!fp) return false;
int nRet = zipOpenNewFileInZip(m_uzFile, strFileName.c_str(), &zfi, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION);
if (nRet == ZIP_OK) { m_info.nFileCount++;
// read the file and output to zip char pBuffer[BUFFERSIZE]; unsigned long dwBytesRead = 0, dwFileSize = 0;
while (nRet == ZIP_OK && !feof( fp )) { dwBytesRead =fread( pBuffer,1, BUFFERSIZE, fp); dwFileSize += dwBytesRead;
if (dwBytesRead) nRet = zipWriteInFileInZip(m_uzFile, pBuffer, dwBytesRead); else break; }
m_info.dwUncompressedSize += dwFileSize; }
zipCloseFileInZip(m_uzFile);
fclose(fp);
return (nRet == ZIP_OK); }
// extended interface bool CZipper::OpenZip(const char * szFilePath) { CloseZip();
if (!szFilePath || !strlen(szFilePath)) return false;
bool bAppend = false;
m_uzFile = zipOpen(szFilePath, bAppend ? 1 : 0);
return (m_uzFile != NULL); }
以上是关于zlib库剖析:实现概览 Zipper.cpp的主要内容,如果未能解决你的问题,请参考以下文章
NHibernate3剖析:Mapping篇之ConfORM实战:概览