forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZipFileInfo.cpp
More file actions
139 lines (118 loc) · 3.18 KB
/
ZipFileInfo.cpp
File metadata and controls
139 lines (118 loc) · 3.18 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
//
// ZipFileInfo.cpp
//
// $Id: //poco/1.4/Zip/src/ZipFileInfo.cpp#1 $
//
// Library: Zip
// Package: Zip
// Module: ZipFileInfo
//
// Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/Zip/ZipFileInfo.h"
#include "Poco/Zip/ZipLocalFileHeader.h"
#include "Poco/Buffer.h"
#include <istream>
#include <cstring>
namespace Poco {
namespace Zip {
const char ZipFileInfo::HEADER[ZipCommon::HEADER_SIZE] = {'\x50', '\x4b', '\x01', '\x02'};
ZipFileInfo::ZipFileInfo(const ZipLocalFileHeader& header):
_rawInfo(),
_crc32(0),
_compressedSize(0),
_uncompressedSize(0),
_fileName(),
_lastModifiedAt(),
_extraField()
{
std::memset(_rawInfo, 0, FULLHEADER_SIZE);
std::memcpy(_rawInfo, HEADER, ZipCommon::HEADER_SIZE);
setCompressedSize(header.getCompressedSize());
setUncompressedSize(header.getUncompressedSize());
setCRC(header.getCRC());
setCompressionMethod(header.getCompressionMethod());
setCompressionLevel(header.getCompressionLevel());
setRequiredVersion(header.getMajorVersionNumber(), header.getMinorVersionNumber());
setHostSystem(header.getHostSystem());
setLastModifiedAt(header.lastModifiedAt());
setEncryption(false);
setFileName(header.getFileName());
if (getHostSystem() == ZipCommon::HS_UNIX)
setUnixAttributes();
}
ZipFileInfo::ZipFileInfo(std::istream& in, bool assumeHeaderRead):
_rawInfo(),
_crc32(0),
_compressedSize(0),
_uncompressedSize(0),
_fileName(),
_lastModifiedAt(),
_extraField()
{
// sanity check
poco_assert_dbg (RELATIVEOFFSETLOCALHEADER_POS + RELATIVEOFFSETLOCALHEADER_SIZE == FULLHEADER_SIZE);
parse(in, assumeHeaderRead);
}
ZipFileInfo::~ZipFileInfo()
{
}
void ZipFileInfo::parse(std::istream& inp, bool assumeHeaderRead)
{
if (!assumeHeaderRead)
{
inp.read(_rawInfo, ZipCommon::HEADER_SIZE);
}
else
{
std::memcpy(_rawInfo, HEADER, ZipCommon::HEADER_SIZE);
}
poco_assert (std::memcmp(_rawInfo, HEADER, ZipCommon::HEADER_SIZE) == 0);
// read the rest of the header
inp.read(_rawInfo + ZipCommon::HEADER_SIZE, FULLHEADER_SIZE - ZipCommon::HEADER_SIZE);
_crc32 = getCRCFromHeader();
_compressedSize = getCompressedSizeFromHeader();
_uncompressedSize = getUncompressedSizeFromHeader();
parseDateTime();
Poco::UInt16 len = getFileNameLength();
Poco::Buffer<char> buf(len);
inp.read(buf.begin(), len);
_fileName = std::string(buf.begin(), len);
if (hasExtraField())
{
len = getExtraFieldLength();
Poco::Buffer<char> xtra(len);
inp.read(xtra.begin(), len);
_extraField = std::string(xtra.begin(), len);
}
len = getFileCommentLength();
if (len > 0)
{
Poco::Buffer<char> buf2(len);
inp.read(buf2.begin(), len);
_fileComment = std::string(buf2.begin(), len);
}
}
std::string ZipFileInfo::createHeader() const
{
std::string result(_rawInfo, FULLHEADER_SIZE);
result.append(_fileName);
result.append(_extraField);
result.append(_fileComment);
return result;
}
void ZipFileInfo::setUnixAttributes()
{
bool isDir = isDirectory();
int mode;
if (isDir)
mode = DEFAULT_UNIX_DIR_MODE;
else
mode = DEFAULT_UNIX_FILE_MODE;
Poco::UInt32 attrs = (mode << 16) | (isDir ? 0x10 : 0);
setExternalFileAttributes(attrs);
}
} } // namespace Poco::Zip