Repository navigation
Expand file tree
/
Copy pathArchiveEntryEntrySource.js
More file actions
171 lines (149 loc) · 6.15 KB
/
Copy pathArchiveEntryEntrySource.js
File metadata and controls
171 lines (149 loc) · 6.15 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import EntrySource from './EntrySource.js';
import Constants from '../../Constants.js';
import GenericExtraField from '../Structure/ExtraField/GenericExtraField.js';
import {symbols} from 'armarius-io';
export default class ArchiveEntryEntrySource extends EntrySource {
/** @type {ArchiveEntry} */ sourceEntry;
/** @type {import("armarius-io").IO} */ entryDataReader;
/** @type {boolean} */ zip64;
/**
* @param {ArchiveEntry} sourceEntry
* @param {EntrySourceOptions|EntrySourceOptionsObject} options
*/
constructor(sourceEntry, options = {}) {
super(options);
this.sourceEntry = sourceEntry;
if (this.fileNameString === null) {
this.setFileNameString(sourceEntry.getFileNameString());
}
if (this.fileCommentString === null) {
this.setFileCommentString(sourceEntry.getFileCommentString());
}
this.madeByVersion = Math.max(this.madeByVersion, sourceEntry.centralDirectoryFileHeader.madeByVersion);
this.extractionVersion = Math.max(this.extractionVersion, sourceEntry.centralDirectoryFileHeader.extractionVersion);
this.zip64 = !!sourceEntry.zip64ExtendedInformation;
this.options.unicodeFileNameField = this.options.unicodeFileNameField || !!this.sourceEntry.unicodeFileName;
this.options.unicodeCommentField = this.options.unicodeCommentField || !!this.sourceEntry.unicodeFileComment;
this.modTime = sourceEntry.getLastModDate();
if (sourceEntry.extendedTimestamp) {
this.acTime = this.getDateOrDefault(sourceEntry.extendedTimestamp.getAcTime(), this.acTime);
this.crTime = this.getDateOrDefault(sourceEntry.extendedTimestamp.getCrTime(), this.crTime);
} else {
this.crTime = this.modTime;
}
}
/**
* @param {?number} date
* @param {Date} defaultDate
* @protected
* @returns {Date}
*/
getDateOrDefault(date, defaultDate) {
if (date !== null) {
return new Date(date);
}
return defaultDate;
}
/**
* @inheritDoc
*/
async generateCentralDirectoryFileHeader() {
let header = this.getBaseCentralDirectoryFileHeader();
header.bitFlag = this.getBitFlag(this.sourceEntry.centralDirectoryFileHeader.bitFlag);
header.compressionMethod = this.sourceEntry.centralDirectoryFileHeader.compressionMethod;
header.crc32 = this.sourceEntry.getCrc();
header.compressedSize = this.isZip64() ? Constants.MAX_UINT32 : Number(this.sourceEntry.getCompressedSize());
header.uncompressedSize = this.isZip64() ? Constants.MAX_UINT32 : Number(this.sourceEntry.getUncompressedSize());
header.internalFileAttributes = this.sourceEntry.centralDirectoryFileHeader.internalFileAttributes;
header.externalFileAttributes = this.sourceEntry.centralDirectoryFileHeader.externalFileAttributes;
this.setExtraFields(header, true);
return header;
}
/**
* @param {FileHeader} header
* @param {boolean} centralDir
* @protected
*/
setExtraFields(header, centralDir = false) {
let extraFields = centralDir ?
this.sourceEntry.centralDirectoryFileHeader.getExtraFields() :
this.sourceEntry.localFileHeader.getExtraFields();
for (let [type, field] of extraFields) {
// The Zip64 extended information field is specific to the header it
// lives in (it holds this entry's offset and the sizes as stored in
// that particular header). A copied one would be wrong for the new
// header, and a source local header may even contain one while its
// central directory does not. setCommonExtraFields() below recreates
// a complete field whenever the new entry actually needs Zip64, so we
// never carry the source field over.
if (type === Constants.EXTRAFIELD_TYPE_ZIP64_EXTENDED_INFO) {
continue;
}
if (!(field instanceof GenericExtraField)) {
header.setExtraField(type, field);
}
}
this.setCommonExtraFields(header, this.sourceEntry.getUncompressedSize(),
this.sourceEntry.getCompressedSize(), centralDir);
}
/**
* @inheritDoc
*/
async generateDataChunk(length) {
if (this.sourceEntry.isDirectory()) {
return new Uint8Array(0);
}
if (!this.entryDataReader) {
this.entryDataReader = await this.sourceEntry.getRawDataReader();
}
return await this.entryDataReader.read(Math.min(length, this.entryDataReader.byteLength - this.entryDataReader.offset));
}
/**
* @inheritDoc
*/
async generateDataDescriptor() {
return null;
}
/**
* @inheritDoc
*/
hasDataDescriptor() {
return false;
}
/**
* @inheritDoc
*/
async generateLocalFileHeader() {
await this.sourceEntry.readLocalFileHeader();
this.zip64 = this.sourceEntry.zip64ExtendedInformation ||
this.getLocalHeaderOffset() > Constants.MAX_UINT32;
let header = this.getBaseLocalFileHeader();
header.bitFlag = this.getBitFlag(this.sourceEntry.localFileHeader.bitFlag);
header.compressionMethod = this.sourceEntry.centralDirectoryFileHeader.compressionMethod;
header.crc32 = this.sourceEntry.getCrc();
header.compressedSize = this.isZip64() ?
Constants.MAX_UINT32 : Number(this.sourceEntry.getCompressedSize());
header.uncompressedSize = this.isZip64() ?
Constants.MAX_UINT32 : Number(this.sourceEntry.getUncompressedSize());
this.setExtraFields(header, false);
return header;
}
/**
* @inheritDoc
*/
getDataEOF() {
return this.sourceEntry.isDirectory() ||
(this.entryDataReader && this.entryDataReader.offset >= this.entryDataReader.byteLength);
}
/**
* @inheritDoc
*/
isZip64() {
return super.isZip64() || this.zip64;
}
async [symbols.asyncDispose] () {
if (this.entryDataReader) {
await this.entryDataReader[symbols.asyncDispose]();
}
}
}