// ПРИМЕР >>
#include "stdafx.h"
#include "SimpleZipArchive.h"
int main(int argc, char* argv[])
{
CSimpleZipArchive zip("d:\\_myzip.zip");
zip.AddFile("d:\\file1.txt");
zip.AddFile("d:\\file2.doc");
zip.AddFile("d:\\file3.xls");
zip.RemoveFile("d:\\file2.doc");
zip.ZipIt();
return 0;
}
// КЛАСС >>
#if !defined(_SIMPLEZIPARCHIVE_H_INCLUDED_)
#define _SIMPLEZIPARCHIVE_H_INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#pragma comment(lib, ".\\zlib\\zlib.lib")
#include ".\zlib\zip.h"
#include <sys/stat.h>
#include <time.h>
#include <map>
#include <string>
using namespace std;
#define READ_BUFFER_SIZE 65535
//Спасибо тов. Bell-у за наше счастливое детство :)
struct CaseInsensitiveLess : public binary_function<string, string, bool>
{
bool operator()(const string &s1, const string &s2) const
{
return stricmp(s1.c_str(), s2.c_str()) < 0;
}
};
typedef map<string, bool, CaseInsensitiveLess> STRCI_MAP;
class CSimpleZipArchive
{
public:
CSimpleZipArchive(const char* _zipName = NULL) { SetZipFileName(_zipName); }
virtual ~CSimpleZipArchive() {}
void SetZipFileName(const char* _zipName)
{
if (_zipName && 0 != *_zipName)
m_ZipFileName = _zipName;
}
const char* GetZipFileName() const { return m_ZipFileName.c_str(); }
bool RemoveFile(const char* _fileName)
{
if (!_fileName || 0 == *_fileName) return false;
STRCI_MAP::iterator findIter = m_Files.find(string(_fileName));
if (findIter != m_Files.end())
{
m_Files.erase(findIter);
return true;
}
else
return false;
}
bool AddFile(const char* _fileName)
{
if (!_fileName || 0 == *_fileName) return false;
pair<STRCI_MAP::iterator, bool> result =
m_Files.insert(STRCI_MAP::value_type(string(_fileName), true));
return result.second;
}
bool ZipIt() const
{
bool result = false;
if ( 0 == m_Files.size()) return result;
STRCI_MAP::const_iterator Iter = m_Files.begin();
zipFile zipfile = zipOpen(m_ZipFileName.c_str(), 0);
if (zipfile != NULL)
{
while (Iter != m_Files.end())
{
result = false;
const char* srcFullFileName = Iter->first.c_str();
const char* p = strrchr(srcFullFileName, '\\');
const char* srcFileName = (NULL == p) ? srcFullFileName : p + 1;
char read_buf[READ_BUFFER_SIZE];
FILE* file = fopen(srcFullFileName, "rb");
if (file != NULL)
{
zip_fileinfo zfileinfo = {0};
struct _stat file_stat = {0};
_fstat(_fileno(file), &file_stat);
struct tm* file_time = localtime(&file_stat.st_mtime);
tm_zip* zip_time = &zfileinfo.tmz_date;
memcpy(zip_time, file_time, sizeof(tm_zip));
int compression = Z_BEST_COMPRESSION;
if ( ZIP_OK == zipOpenNewFileInZip(zipfile, srcFileName,
&zfileinfo, NULL, 0, NULL, 0, NULL, Z_DEFLATED, compression))
{
while(!feof(file))
{
result = false;
size_t count = fread( read_buf, sizeof(char), READ_BUFFER_SIZE, file );
if(!ferror(file))
{
if ( ZIP_OK == zipWriteInFileInZip(zipfile, read_buf, count))
{
result = true;
continue;
}
else break;
}else break;
}
result = result && (Z_OK == zipCloseFileInZip(zipfile));
}
result = result && (0 == fclose(file));
}
(result) ? Iter++ : Iter = m_Files.end();
}
result = result && (Z_OK == zipClose(zipfile, NULL));
}
return result;
}
private:
STRCI_MAP m_Files;
string m_ZipFileName;
};
#endif // !defined(_SIMPLEZIPARCHIVE_H_INCLUDED_)