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
|
#include "zip_reader.hpp"
#include "../base/scope_guard.hpp"
#include "../base/logging.hpp"
#include "../std/bind.hpp"
#include "../3party/zlib/contrib/minizip/unzip.h"
ZipFileReader::ZipFileReader(string const & container, string const & file)
: base_type(container)
{
unzFile zip = unzOpen64(container.c_str());
if (!zip)
MYTHROW(OpenZipException, ("Can't get zip file handle", container));
MY_SCOPE_GUARD(zipGuard, bind(&unzClose, zip));
if (UNZ_OK != unzLocateFile(zip, file.c_str(), 1))
MYTHROW(LocateZipException, ("Can't locate file inside zip", file));
if (UNZ_OK != unzOpenCurrentFile(zip))
MYTHROW(LocateZipException, ("Can't open file inside zip", file));
uint64_t offset = unzGetCurrentFileZStreamPos64(zip);
unzCloseCurrentFile(zip);
if (offset > Size())
MYTHROW(LocateZipException, ("Invalid offset inside zip", file));
unz_file_info64 fileInfo;
if (UNZ_OK != unzGetCurrentFileInfo64(zip, &fileInfo, NULL, 0, NULL, 0, NULL, 0))
MYTHROW(LocateZipException, ("Can't get uncompressed file size inside zip", file));
if (fileInfo.compressed_size != fileInfo.uncompressed_size)
MYTHROW(InvalidZipException, ("File should be uncompressed inside zip", file));
LOG(LDEBUG, (file, "offset:", offset, "size:", fileInfo.uncompressed_size));
SetOffsetAndSize(offset, fileInfo.uncompressed_size);
}
vector<string> ZipFileReader::FilesList(string const & zipContainer)
{
unzFile zip = unzOpen64(zipContainer.c_str());
if (!zip)
MYTHROW(OpenZipException, ("Can't get zip file handle", zipContainer));
MY_SCOPE_GUARD(zipGuard, bind(&unzClose, zip));
if (UNZ_OK != unzGoToFirstFile(zip))
MYTHROW(LocateZipException, ("Can't find first file inside zip", zipContainer));
vector<string> filesList;
do
{
char fileName[256];
if (UNZ_OK != unzGetCurrentFileInfo64(zip, NULL, fileName, ARRAY_SIZE(fileName), NULL, 0, NULL, 0))
MYTHROW(LocateZipException, ("Can't get file name inside zip", zipContainer));
filesList.push_back(fileName);
} while (UNZ_OK == unzGoToNextFile(zip));
return filesList;
}
|