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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
#include "zip_creator.hpp"
#include "../../coding/file_name_utils.hpp"
#include "../../coding/internal/file_data.hpp"
#include "../../std/vector.hpp"
#include "../../std/iostream.hpp"
#include "../../std/ctime.hpp"
#include "../../3party/zlib/contrib/minizip/zip.h"
class ZipHandle
{
public:
zipFile m_zipFile;
ZipHandle(string const & filePath)
{
m_zipFile = zipOpen(filePath.c_str(), 0);
}
~ZipHandle()
{
if (m_zipFile)
zipClose(m_zipFile, NULL);
}
};
namespace
{
void CreateTMZip(tm_zip & res)
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
res.tm_sec = timeinfo->tm_sec;
res.tm_min = timeinfo->tm_min;
res.tm_hour = timeinfo->tm_hour;
res.tm_mday = timeinfo->tm_mday;
res.tm_mon = timeinfo->tm_mon;
res.tm_year = timeinfo->tm_year;
}
}
bool createZipFromPathDeflatedAndDefaultCompression(string const & filePath, string const & zipFilePath)
{
ZipHandle zip(zipFilePath);
if (!zip.m_zipFile)
return false;
zip_fileinfo zipInfo = { 0 };
CreateTMZip(zipInfo.tmz_date);
string fileName = filePath;
my::GetNameFromFullPath(fileName);
if (zipOpenNewFileInZip (zip.m_zipFile, fileName.c_str(), &zipInfo, NULL, 0, NULL, 0, "ZIP from MapsWithMe", Z_DEFLATED, Z_DEFAULT_COMPRESSION) < 0)
return false;
my::FileData f(filePath, my::FileData::OP_READ);
size_t const bufSize = 512 * 1024;
vector<char> buffer(bufSize);
size_t const fileSize = f.Size();
size_t currSize = 0;
while (currSize < fileSize)
{
size_t toRead = fileSize - currSize;
if (toRead > bufSize)
toRead = bufSize;
f.Read(currSize, &buffer[0], toRead);
if (ZIP_OK != zipWriteInFileInZip (zip.m_zipFile, &buffer[0], toRead))
return false;
currSize += toRead;
}
return true;
}
|