-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZipUtils.h
90 lines (74 loc) · 2.72 KB
/
ZipUtils.h
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
#pragma once
#include <memory>
#include <vector>
#include "Logging.h"
#include <minizip/unzip.h>
#include <minizip/zip.h>
namespace ZipSync {
typedef std::unique_ptr<std::remove_pointer<unzFile>::type, int (*)(unzFile)> UnzFileUniquePtr;
/**
* RAII wrapper around unzFile from minizip.h.
* Automatically closes file on destruction.
*/
class UnzFileHolder : public UnzFileUniquePtr {
public:
UnzFileHolder(unzFile zf);
UnzFileHolder(const char *path);
~UnzFileHolder();
operator unzFile() const { return get(); }
};
typedef std::unique_ptr<std::remove_pointer<zipFile>::type, int (*)(zipFile)> ZipFileUniquePtr;
/**
* RAII wrapper around zipFile from minizip.h.
* Automatically closes file on destruction.
*/
class ZipFileHolder : public ZipFileUniquePtr {
public:
ZipFileHolder(zipFile zf);
ZipFileHolder(const char *path);
~ZipFileHolder();
operator zipFile() const { return get(); }
};
/**
* Performs whatever call you wrap into it and checks its return code.
* If the return code is nonzero, then exception is thrown.
*/
#define SAFE_CALL(...) \
do { \
int mz_errcode = __VA_ARGS__; \
if (mz_errcode != 0) g_logger->errorf(lcMinizipError, "Minizip error %d", mz_errcode); \
} while (0)
//note: file must be NOT opened
void unzGetCurrentFilePosition(unzFile zf, uint32_t *localHeaderStart, uint32_t *fileDataStart, uint32_t *fileDataEnd);
class UnzFileIndexed {
struct Entry {
uint32_t byterangeStart;
unz_file_pos unzPos;
bool operator< (const Entry &b) const;
};
UnzFileUniquePtr _zfHandle;
std::vector<Entry> _sortedEntries;
public:
~UnzFileIndexed();
UnzFileIndexed();
UnzFileIndexed(UnzFileIndexed &&) = default;
operator unzFile() const { return _zfHandle.get(); }
void Clear();
void Open(const char *path);
void LocateByByterange(uint32_t start, uint32_t end);
};
/*
//like unzLocateFile, but also checks exact match by byterange (which includes local file header)
bool unzLocateFileAtBytes(unzFile zf, const char *filename, uint32_t from, uint32_t to);
*/
void minizipCopyFile(unzFile zf, zipFile zfOut, const char *filename, int method, int flags, uint16_t internalAttribs, uint32_t externalAttribs, uint32_t dosDate, bool copyRaw, uint32_t crc, uint32_t contentsSize);
struct FileAttribInfo {
uint32_t offset;
uint32_t externalAttribs;
uint16_t internalAttribs;
};
//given a tightly packed zip file without central directory, rebuilds it and appends it to the end of file
void minizipAddCentralDirectory(const char *filename, std::vector<FileAttribInfo> attribs = {});
//repack given zip file so that it gets accepted by ZipSync
void minizipNormalize(const char *srcFilename, const char *dstFilename = NULL);
}