This repository was archived by the owner on Dec 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 328
Expand file tree
/
Copy pathZipWriter.cpp
More file actions
54 lines (45 loc) · 1.35 KB
/
ZipWriter.cpp
File metadata and controls
54 lines (45 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "stdafx.h"
#include <string>
#include <cstring>
#include <sstream>
#include "ZipWriter.h"
#include "FolderUtilities.h"
ZipWriter::ZipWriter()
{
}
ZipWriter::~ZipWriter()
{
}
bool ZipWriter::Initialize(string filename)
{
_zipFilename = filename;
memset(&_zipArchive, 0, sizeof(mz_zip_archive));
return mz_zip_writer_init_file(&_zipArchive, _zipFilename.c_str(), 0) != 0;
}
bool ZipWriter::Save()
{
bool result = mz_zip_writer_finalize_archive(&_zipArchive) != 0;
result &= mz_zip_writer_end(&_zipArchive) != 0;
return result;
}
void ZipWriter::AddFile(string filepath, string zipFilename)
{
if(!mz_zip_writer_add_file(&_zipArchive, zipFilename.c_str(), filepath.c_str(), "", 0, MZ_BEST_COMPRESSION)) {
std::cout << "mz_zip_writer_add_file() failed!" << std::endl;
}
}
void ZipWriter::AddFile(vector<uint8_t> &fileData, string zipFilename)
{
if(!mz_zip_writer_add_mem(&_zipArchive, zipFilename.c_str(), fileData.data(), fileData.size(), MZ_BEST_COMPRESSION)) {
std::cout << "mz_zip_writer_add_file() failed!" << std::endl;
}
}
void ZipWriter::AddFile(std::stringstream &filestream, string zipFilename)
{
filestream.seekg(0, std::ios::end);
size_t bufferSize = (size_t)filestream.tellg();
filestream.seekg(0, std::ios::beg);
vector<uint8_t> buffer(bufferSize);
filestream.read((char*)buffer.data(), bufferSize);
AddFile(buffer, zipFilename);
}