Welcome to mirror list, hosted at ThFree Co, Russian Federation.

zip_creator.cpp « coding - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: be37a15b559f542e85ab7c6bbdd5df71208a15d6 (plain)
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
#include "zip_creator.hpp"

#include "../../coding/file_name_utils.hpp"
#include "../../coding/internal/file_data.hpp"
#include "../../coding/constants.hpp"

#include "../../std/vector.hpp"
#include "../../std/ctime.hpp"
#include "../../std/algorithm.hpp"

#include "../../3party/zlib/contrib/minizip/zip.h"


namespace
{

class ZipHandle
{
  zipFile m_zipFileHandle;

public:
  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)
{
  ZipHandle zip(zipFilePath);
  if (!zip.Handle())
    return false;

  // Special syntax to initialize struct with zeroes
  zip_fileinfo zipInfo = zip_fileinfo();
  CreateTMZip(zipInfo.tmz_date);
  string fileName = filePath;
  my::GetNameFromFullPath(fileName);
  if (::zipOpenNewFileInZip(zip.Handle(), 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 = READ_FILE_BUFFER_SIZE;
  vector<char> buffer(bufSize);
  size_t const fileSize = f.Size();
  size_t currSize = 0;

  while (currSize < fileSize)
  {
    size_t const toRead = min(bufSize, fileSize - currSize);
    f.Read(currSize, &buffer[0], toRead);

    if (ZIP_OK != ::zipWriteInFileInZip(zip.Handle(), &buffer[0], toRead))
      return false;

    currSize += toRead;
  }

  return true;
}