forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZipArchive.cpp
More file actions
144 lines (124 loc) · 3.16 KB
/
ZipArchive.cpp
File metadata and controls
144 lines (124 loc) · 3.16 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//
// ZipArchive.cpp
//
// Library: Zip
// Package: Zip
// Module: ZipArchive
//
// Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/Zip/ZipArchive.h"
#include "Poco/Zip/SkipCallback.h"
#include "Poco/Exception.h"
#include <cstring>
namespace Poco {
namespace Zip {
const std::string ZipArchive::EMPTY_COMMENT;
ZipArchive::ZipArchive(std::istream& in):
_entries(),
_infos(),
_disks(),
_disks64()
{
poco_assert_dbg (in);
SkipCallback skip;
parse(in, skip);
}
ZipArchive::ZipArchive(const FileHeaders& entries, const FileInfos& infos, const DirectoryInfos& dirs, const DirectoryInfos64& dirs64):
_entries(entries),
_infos(infos),
_disks(dirs),
_disks64(dirs64)
{
}
ZipArchive::ZipArchive(std::istream& in, ParseCallback& pc):
_entries(),
_infos(),
_disks(),
_disks64()
{
poco_assert_dbg (in);
parse(in, pc);
}
ZipArchive::~ZipArchive()
{
}
void ZipArchive::parse(std::istream& in, ParseCallback& pc)
{
// read 4 bytes
bool haveSynced = false;
while (in.good() && !in.eof())
{
char header[ZipCommon::HEADER_SIZE]={'\x00', '\x00', '\x00', '\x00'};
in.read(header, ZipCommon::HEADER_SIZE);
if (in.eof())
return;
if (std::memcmp(header, ZipLocalFileHeader::HEADER, ZipCommon::HEADER_SIZE) == 0)
{
ZipLocalFileHeader entry(in, true, pc);
poco_assert (_entries.insert(std::make_pair(entry.getFileName(), entry)).second);
haveSynced = false;
}
else if (std::memcmp(header, ZipFileInfo::HEADER, ZipCommon::HEADER_SIZE) == 0)
{
ZipFileInfo info(in, true);
FileHeaders::iterator it = _entries.find(info.getFileName());
if (it != _entries.end())
{
it->second.setStartPos(info.getOffset());
}
poco_assert (_infos.insert(std::make_pair(info.getFileName(), info)).second);
haveSynced = false;
}
else if (std::memcmp(header, ZipArchiveInfo::HEADER, ZipCommon::HEADER_SIZE) == 0)
{
ZipArchiveInfo nfo(in, true);
poco_assert (_disks.insert(std::make_pair(nfo.getDiskNumber(), nfo)).second);
haveSynced = false;
}
else if (std::memcmp(header, ZipArchiveInfo64::HEADER, ZipCommon::HEADER_SIZE) == 0)
{
ZipArchiveInfo64 nfo(in, true);
poco_assert (_disks64.insert(std::make_pair(nfo.getDiskNumber(), nfo)).second);
haveSynced = false;
}
else
{
if (!haveSynced)
{
// Some Zip files have extra data behind the ZipLocalFileHeader.
// Try to re-sync.
ZipUtil::sync(in);
haveSynced = true;
}
else
{
if (_disks.empty() && _disks64.empty())
throw Poco::IllegalStateException("Illegal header in zip file");
else
throw Poco::IllegalStateException("Garbage after directory header");
}
}
}
}
const std::string& ZipArchive::getZipComment() const
{
// It seems that only the "first" disk is populated (look at Compress::close()), so getting the first ZipArchiveInfo
DirectoryInfos::const_iterator it = _disks.begin();
if (it != _disks.end())
{
return it->second.getZipComment();
}
else
{
DirectoryInfos64::const_iterator it64 = _disks64.begin();
if (it64 != _disks64.end())
return it->second.getZipComment();
else
return EMPTY_COMMENT;
}
}
} } // namespace Poco::Zip