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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
#include "coding/zip_creator.hpp"
#include "base/string_utils.hpp"
#include "coding/internal/file_data.hpp"
#include "coding/reader.hpp"
#include "coding/constants.hpp"
#include "base/file_name_utils.hpp"
#include "base/logging.hpp"
#include "base/scope_guard.hpp"
#include <algorithm>
#include <ctime>
#include <vector>
#include "3party/minizip/zip.h"
using namespace std;
namespace
{
class ZipHandle
{
zipFile m_zipFileHandle;
public:
explicit ZipHandle(string const & filePath)
{
m_zipFileHandle = zipOpen(filePath.c_str(), 0);
}
~ZipHandle()
{
if (m_zipFileHandle)
zipClose(m_zipFileHandle, NULL);
}
zipFile Handle() const { return m_zipFileHandle; }
};
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)
{
// 2. Open zip file for writing.
SCOPE_GUARD(outFileGuard, bind(&base::DeleteFileX, cref(zipFilePath)));
ZipHandle zip(zipFilePath);
if (!zip.Handle())
return false;
zip_fileinfo zipInfo = {};
CreateTMZip(zipInfo.tmz_date);
string fileName = filePath;
base::GetNameFromFullPath(fileName);
if (!strings::IsASCIIString(fileName))
fileName = "MapsMe.kml";
if (zipOpenNewFileInZip(zip.Handle(), fileName.c_str(), &zipInfo,
NULL, 0, NULL, 0, "ZIP from MapsWithMe", Z_DEFLATED, Z_DEFAULT_COMPRESSION) < 0)
{
return false;
}
// Write source file into zip file.
try
{
base::FileData file(filePath, base::FileData::OP_READ);
uint64_t const fileSize = file.Size();
uint64_t currSize = 0;
char buffer[ZIP_FILE_BUFFER_SIZE];
while (currSize < fileSize)
{
unsigned int const toRead = min(ZIP_FILE_BUFFER_SIZE, static_cast<unsigned int>(fileSize - currSize));
file.Read(currSize, buffer, toRead);
if (ZIP_OK != zipWriteInFileInZip(zip.Handle(), buffer, toRead))
return false;
currSize += toRead;
}
}
catch (Reader::Exception const & ex)
{
LOG(LERROR, ("Error reading file:", filePath, ex.Msg()));
return false;
}
// Success.
outFileGuard.release();
return true;
}
|