forked from kriskbx/gitlab-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathzip.js
More file actions
179 lines (149 loc) · 6.03 KB
/
Copy pathzip.js
File metadata and controls
179 lines (149 loc) · 6.03 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
171
172
173
174
175
176
177
178
179
import fs from 'fs';
import zlib from 'zlib';
const METHOD_STORE = 0;
const METHOD_DEFLATE = 8;
/**
* convert a JS Date to DOS date/time fields used by the zip format
*/
function dosDateTime(date) {
const time = ((date.getHours() & 0x1f) << 11) | ((date.getMinutes() & 0x3f) << 5) | ((date.getSeconds() >> 1) & 0x1f);
const day = (((Math.max(date.getFullYear(), 1980) - 1980) & 0x7f) << 9) | (((date.getMonth() + 1) & 0xf) << 5) | (date.getDate() & 0x1f);
return {time, day};
}
/**
* reverse of dosDateTime
*/
function dosToDate(day, time) {
const year = ((day >> 9) & 0x7f) + 1980;
const month = ((day >> 5) & 0xf) - 1;
const date = day & 0x1f;
const hours = (time >> 11) & 0x1f;
const minutes = (time >> 5) & 0x3f;
const seconds = (time & 0x1f) * 2;
return new Date(year, month, date, hours, minutes, seconds);
}
/**
* minimal zip (deflate) writer, so archiving does not require
* an external dependency.
*/
class Zip {
constructor() {
this.entries = [];
}
/**
* add a file to the archive
* @param name path of the file inside the archive, e.g. "01/abc.json"
* @param data Buffer with the file's contents
* @param date last modified date of the entry
*/
addFile(name, data, date = new Date()) {
const compressed = zlib.deflateRawSync(data);
const useCompressed = compressed.length < data.length;
this.entries.push({
name,
data,
payload: useCompressed ? compressed : data,
method: useCompressed ? METHOD_DEFLATE : METHOD_STORE,
crc: zlib.crc32(data),
date
});
return this;
}
/**
* build the zip file as a Buffer
* @returns {Buffer}
*/
toBuffer() {
const localParts = [];
const centralParts = [];
let offset = 0;
for (const entry of this.entries) {
const nameBuf = Buffer.from(entry.name, 'utf8');
const {time, day} = dosDateTime(entry.date);
const localHeader = Buffer.alloc(30);
localHeader.writeUInt32LE(0x04034b50, 0);
localHeader.writeUInt16LE(20, 4);
localHeader.writeUInt16LE(0x0800, 6);
localHeader.writeUInt16LE(entry.method, 8);
localHeader.writeUInt16LE(time, 10);
localHeader.writeUInt16LE(day, 12);
localHeader.writeUInt32LE(entry.crc, 14);
localHeader.writeUInt32LE(entry.payload.length, 18);
localHeader.writeUInt32LE(entry.data.length, 22);
localHeader.writeUInt16LE(nameBuf.length, 26);
localHeader.writeUInt16LE(0, 28);
localParts.push(localHeader, nameBuf, entry.payload);
const centralHeader = Buffer.alloc(46);
centralHeader.writeUInt32LE(0x02014b50, 0);
centralHeader.writeUInt16LE(20, 4);
centralHeader.writeUInt16LE(20, 6);
centralHeader.writeUInt16LE(0x0800, 8);
centralHeader.writeUInt16LE(entry.method, 10);
centralHeader.writeUInt16LE(time, 12);
centralHeader.writeUInt16LE(day, 14);
centralHeader.writeUInt32LE(entry.crc, 16);
centralHeader.writeUInt32LE(entry.payload.length, 20);
centralHeader.writeUInt32LE(entry.data.length, 24);
centralHeader.writeUInt16LE(nameBuf.length, 28);
centralHeader.writeUInt16LE(0, 30);
centralHeader.writeUInt16LE(0, 32);
centralHeader.writeUInt16LE(0, 34);
centralHeader.writeUInt32LE(0, 38);
centralHeader.writeUInt32LE(offset, 42);
centralParts.push(centralHeader, nameBuf);
offset += localHeader.length + nameBuf.length + entry.payload.length;
}
const centralSize = centralParts.reduce((n, b) => n + b.length, 0);
const centralOffset = offset;
const end = Buffer.alloc(22);
end.writeUInt32LE(0x06054b50, 0);
end.writeUInt16LE(0, 4);
end.writeUInt16LE(0, 6);
end.writeUInt16LE(this.entries.length, 8);
end.writeUInt16LE(this.entries.length, 10);
end.writeUInt32LE(centralSize, 12);
end.writeUInt32LE(centralOffset, 16);
end.writeUInt16LE(0, 20);
return Buffer.concat([...localParts, ...centralParts, end]);
}
/**
* write the zip archive to disk atomically
* @param file
*/
write(file) {
const tmpFile = `${file}.tmp`;
fs.writeFileSync(tmpFile, this.toBuffer());
fs.renameSync(tmpFile, file);
}
/**
* read an existing zip archive (as produced by this class) back into
* a Zip instance, so new files can be appended to it. Returns an
* empty Zip if the file does not exist.
* @param file
* @returns {Zip}
*/
static fromFile(file) {
const zip = new Zip();
if (!fs.existsSync(file)) return zip;
const buffer = fs.readFileSync(file);
let offset = 0;
while (offset < buffer.length && buffer.readUInt32LE(offset) === 0x04034b50) {
const method = buffer.readUInt16LE(offset + 8);
const time = buffer.readUInt16LE(offset + 10);
const day = buffer.readUInt16LE(offset + 12);
const crc = buffer.readUInt32LE(offset + 14);
const compressedSize = buffer.readUInt32LE(offset + 18);
const nameLength = buffer.readUInt16LE(offset + 26);
const extraLength = buffer.readUInt16LE(offset + 28);
const nameStart = offset + 30;
const name = buffer.toString('utf8', nameStart, nameStart + nameLength);
const dataStart = nameStart + nameLength + extraLength;
const payload = buffer.subarray(dataStart, dataStart + compressedSize);
const data = method === METHOD_DEFLATE ? zlib.inflateRawSync(payload) : Buffer.from(payload);
zip.entries.push({name, data, payload: Buffer.from(payload), method, crc, date: dosToDate(day, time)});
offset = dataStart + compressedSize;
}
return zip;
}
}
export default Zip;