@loaders.gl/zip 4.0.3 → 4.1.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/dist/dist.dev.js +151 -6
  2. package/dist/filesystems/zip-filesystem.d.ts.map +1 -1
  3. package/dist/filesystems/zip-filesystem.js.map +1 -1
  4. package/dist/hash-file-utility.d.ts.map +1 -1
  5. package/dist/hash-file-utility.js.map +1 -1
  6. package/dist/index.cjs +208 -12
  7. package/dist/index.d.ts +1 -1
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +1 -1
  10. package/dist/index.js.map +1 -1
  11. package/dist/lib/tar/header.d.ts.map +1 -1
  12. package/dist/lib/tar/header.js.map +1 -1
  13. package/dist/lib/tar/tar.d.ts.map +1 -1
  14. package/dist/lib/tar/tar.js.map +1 -1
  15. package/dist/lib/tar/types.d.ts.map +1 -1
  16. package/dist/lib/tar/types.js.map +1 -1
  17. package/dist/lib/tar/utils.d.ts.map +1 -1
  18. package/dist/lib/tar/utils.js.map +1 -1
  19. package/dist/parse-zip/cd-file-header.d.ts +18 -0
  20. package/dist/parse-zip/cd-file-header.d.ts.map +1 -1
  21. package/dist/parse-zip/cd-file-header.js +101 -1
  22. package/dist/parse-zip/cd-file-header.js.map +1 -1
  23. package/dist/parse-zip/end-of-central-directory.d.ts.map +1 -1
  24. package/dist/parse-zip/end-of-central-directory.js.map +1 -1
  25. package/dist/parse-zip/local-file-header.d.ts.map +1 -1
  26. package/dist/parse-zip/local-file-header.js.map +1 -1
  27. package/dist/parse-zip/search-from-the-end.d.ts.map +1 -1
  28. package/dist/parse-zip/search-from-the-end.js.map +1 -1
  29. package/dist/parse-zip/zip64-info-generation.d.ts +27 -0
  30. package/dist/parse-zip/zip64-info-generation.d.ts.map +1 -0
  31. package/dist/parse-zip/zip64-info-generation.js +47 -0
  32. package/dist/parse-zip/zip64-info-generation.js.map +1 -0
  33. package/dist/tar-builder.d.ts.map +1 -1
  34. package/dist/tar-builder.js.map +1 -1
  35. package/package.json +5 -5
  36. package/src/filesystems/zip-filesystem.ts +3 -0
  37. package/src/hash-file-utility.ts +3 -0
  38. package/src/index.ts +2 -1
  39. package/src/lib/tar/header.ts +3 -0
  40. package/src/lib/tar/tar.ts +3 -0
  41. package/src/lib/tar/types.ts +3 -0
  42. package/src/lib/tar/utils.ts +3 -0
  43. package/src/parse-zip/cd-file-header.ts +185 -1
  44. package/src/parse-zip/end-of-central-directory.ts +3 -0
  45. package/src/parse-zip/local-file-header.ts +3 -0
  46. package/src/parse-zip/search-from-the-end.ts +3 -0
  47. package/src/parse-zip/zip64-info-generation.ts +90 -0
  48. package/src/tar-builder.ts +3 -0
@@ -1,6 +1,10 @@
1
- import {FileProvider, compareArrayBuffers} from '@loaders.gl/loader-utils';
1
+ // loaders.gl, MIT license
2
+ // Copyright (c) vis.gl contributors
3
+
4
+ import {FileProvider, compareArrayBuffers, concatenateArrayBuffers} from '@loaders.gl/loader-utils';
2
5
  import {parseEoCDRecord} from './end-of-central-directory';
3
6
  import {ZipSignature} from './search-from-the-end';
7
+ import {createZip64Info, NUMBER_SETTERS} from './zip64-info-generation';
4
8
 
5
9
  /**
6
10
  * zip central directory file header info
@@ -185,3 +189,183 @@ const findExpectedData = (zip64data: Zip64Data): {length: number; name: string}[
185
189
 
186
190
  return zip64dataList;
187
191
  };
192
+
193
+ /** info that can be placed into cd header */
194
+ type GenerateCDOptions = {
195
+ /** CRC-32 of uncompressed data */
196
+ crc32: number;
197
+ /** File name */
198
+ fileName: string;
199
+ /** File size */
200
+ length: number;
201
+ /** Relative offset of local file header */
202
+ offset: number;
203
+ };
204
+
205
+ /**
206
+ * generates cd header for the file
207
+ * @param options info that can be placed into cd header
208
+ * @returns buffer with header
209
+ */
210
+ export function generateCDHeader(options: GenerateCDOptions): ArrayBuffer {
211
+ const optionsToUse = {
212
+ ...options,
213
+ fnlength: options.fileName.length,
214
+ extraLength: 0
215
+ };
216
+
217
+ let zip64header: ArrayBuffer = new ArrayBuffer(0);
218
+
219
+ const optionsToZip64: any = {};
220
+ if (optionsToUse.offset >= 0xffffffff) {
221
+ optionsToZip64.offset = optionsToUse.offset;
222
+ optionsToUse.offset = 0xffffffff;
223
+ }
224
+ if (optionsToUse.length >= 0xffffffff) {
225
+ optionsToZip64.size = optionsToUse.length;
226
+ optionsToUse.length = 0xffffffff;
227
+ }
228
+
229
+ if (Object.keys(optionsToZip64).length) {
230
+ zip64header = createZip64Info(optionsToZip64);
231
+ optionsToUse.extraLength = zip64header.byteLength;
232
+ }
233
+ const header = new DataView(new ArrayBuffer(46));
234
+
235
+ for (const field of ZIP_HEADER_FIELDS) {
236
+ NUMBER_SETTERS[field.size](
237
+ header,
238
+ field.offset,
239
+ optionsToUse[field.name ?? ''] ?? field.default ?? 0
240
+ );
241
+ }
242
+
243
+ const encodedName = new TextEncoder().encode(optionsToUse.fileName);
244
+
245
+ const resHeader = concatenateArrayBuffers(header.buffer, encodedName, zip64header);
246
+
247
+ return resHeader;
248
+ }
249
+
250
+ /** Fields map */
251
+ const ZIP_HEADER_FIELDS = [
252
+ // Central directory file header signature = 0x02014b50
253
+ {
254
+ offset: 0,
255
+ size: 4,
256
+ default: new DataView(signature.buffer).getUint32(0, true)
257
+ },
258
+
259
+ // Version made by
260
+ {
261
+ offset: 4,
262
+ size: 2,
263
+ default: 45
264
+ },
265
+
266
+ // Version needed to extract (minimum)
267
+ {
268
+ offset: 6,
269
+ size: 2,
270
+ default: 45
271
+ },
272
+
273
+ // General purpose bit flag
274
+ {
275
+ offset: 8,
276
+ size: 2,
277
+ default: 0
278
+ },
279
+
280
+ // Compression method
281
+ {
282
+ offset: 10,
283
+ size: 2,
284
+ default: 0
285
+ },
286
+
287
+ // File last modification time
288
+ {
289
+ offset: 12,
290
+ size: 2,
291
+ default: 0
292
+ },
293
+
294
+ // File last modification date
295
+ {
296
+ offset: 14,
297
+ size: 2,
298
+ default: 0
299
+ },
300
+
301
+ // CRC-32 of uncompressed data
302
+ {
303
+ offset: 16,
304
+ size: 4,
305
+ name: 'crc32'
306
+ },
307
+
308
+ // Compressed size (or 0xffffffff for ZIP64)
309
+ {
310
+ offset: 20,
311
+ size: 4,
312
+ name: 'length'
313
+ },
314
+
315
+ // Uncompressed size (or 0xffffffff for ZIP64)
316
+ {
317
+ offset: 24,
318
+ size: 4,
319
+ name: 'length'
320
+ },
321
+
322
+ // File name length (n)
323
+ {
324
+ offset: 28,
325
+ size: 2,
326
+ name: 'fnlength'
327
+ },
328
+
329
+ // Extra field length (m)
330
+ {
331
+ offset: 30,
332
+ size: 2,
333
+ default: 0,
334
+ name: 'extraLength'
335
+ },
336
+
337
+ // File comment length (k)
338
+ {
339
+ offset: 32,
340
+ size: 2,
341
+ default: 0
342
+ },
343
+
344
+ // Disk number where file starts (or 0xffff for ZIP64)
345
+ {
346
+ offset: 34,
347
+ size: 2,
348
+ default: 0
349
+ },
350
+
351
+ // Internal file attributes
352
+ {
353
+ offset: 36,
354
+ size: 2,
355
+ default: 0
356
+ },
357
+
358
+ // External file attributes
359
+ {
360
+ offset: 38,
361
+ size: 4,
362
+ default: 0
363
+ },
364
+
365
+ // Relative offset of local file header
366
+ {
367
+ offset: 42,
368
+ size: 4,
369
+ name: 'offset'
370
+ }
371
+ ];
@@ -1,3 +1,6 @@
1
+ // loaders.gl, MIT license
2
+ // Copyright (c) vis.gl contributors
3
+
1
4
  import {FileProvider, compareArrayBuffers} from '@loaders.gl/loader-utils';
2
5
  import {ZipSignature, searchFromTheEnd} from './search-from-the-end';
3
6
 
@@ -1,3 +1,6 @@
1
+ // loaders.gl, MIT license
2
+ // Copyright (c) vis.gl contributors
3
+
1
4
  import {FileProvider, compareArrayBuffers} from '@loaders.gl/loader-utils';
2
5
  import {ZipSignature} from './search-from-the-end';
3
6
 
@@ -1,3 +1,6 @@
1
+ // loaders.gl, MIT license
2
+ // Copyright (c) vis.gl contributors
3
+
1
4
  import {FileProvider} from '@loaders.gl/loader-utils';
2
5
 
3
6
  /** Description of zip signature type */
@@ -0,0 +1,90 @@
1
+ import {concatenateArrayBuffers} from '@loaders.gl/loader-utils';
2
+
3
+ export const signature = new Uint8Array([0x01, 0x00]);
4
+
5
+ /** info that can be placed into zip64 field, doc: https://en.wikipedia.org/wiki/ZIP_(file_format)#ZIP64 */
6
+ type Zip64Options = {
7
+ /** Original uncompressed file size and Size of compressed data */
8
+ size?: number;
9
+ /** Offset of local header record */
10
+ offset?: number;
11
+ };
12
+
13
+ /**
14
+ * creates zip64 extra field
15
+ * @param options info that can be placed into zip64 field
16
+ * @returns buffer with field
17
+ */
18
+ export function createZip64Info(options: Zip64Options): ArrayBuffer {
19
+ const optionsToUse = {
20
+ ...options,
21
+ zip64Length: (options.offset ? 1 : 0) * 8 + (options.size ? 1 : 0) * 16
22
+ };
23
+
24
+ const arraysToConcat: ArrayBuffer[] = [];
25
+
26
+ for (const field of ZIP64_FIELDS) {
27
+ if (!optionsToUse[field.name ?? ''] && !field.default) {
28
+ continue;
29
+ }
30
+ const newValue = new DataView(new ArrayBuffer(field.size));
31
+ NUMBER_SETTERS[field.size](newValue, 0, optionsToUse[field.name ?? ''] ?? field.default);
32
+ arraysToConcat.push(newValue.buffer);
33
+ }
34
+
35
+ return concatenateArrayBuffers(...arraysToConcat);
36
+ }
37
+
38
+ /**
39
+ * Function to write values into buffer
40
+ * @param header buffer where to write a value
41
+ * @param offset offset of the writing start
42
+ * @param value value to be written
43
+ */
44
+ type NumberSetter = (header: DataView, offset: number, value: number) => void;
45
+
46
+ /** functions to write values into buffer according to the bytes amount */
47
+ export const NUMBER_SETTERS: {[key: number]: NumberSetter} = {
48
+ 2: (header, offset, value) => {
49
+ header.setUint16(offset, value, true);
50
+ },
51
+ 4: (header, offset, value) => {
52
+ header.setUint32(offset, value, true);
53
+ },
54
+ 8: (header, offset, value) => {
55
+ header.setBigUint64(offset, BigInt(value), true);
56
+ }
57
+ };
58
+
59
+ /** zip64 info fields description, we need it as a pattern to build a zip64 info */
60
+ const ZIP64_FIELDS = [
61
+ // Header ID 0x0001
62
+ {
63
+ size: 2,
64
+ default: new DataView(signature.buffer).getUint16(0, true)
65
+ },
66
+
67
+ // Size of the extra field chunk (8, 16, 24 or 28)
68
+ {
69
+ size: 2,
70
+ name: 'zip64Length'
71
+ },
72
+
73
+ // Original uncompressed file size
74
+ {
75
+ size: 8,
76
+ name: 'size'
77
+ },
78
+
79
+ // Size of compressed data
80
+ {
81
+ size: 8,
82
+ name: 'size'
83
+ },
84
+
85
+ // Offset of local header record
86
+ {
87
+ size: 8,
88
+ name: 'offset'
89
+ }
90
+ ];
@@ -1,3 +1,6 @@
1
+ // loaders.gl, MIT license
2
+ // Copyright (c) vis.gl contributors
3
+
1
4
  import Tar from './lib/tar/tar';
2
5
 
3
6
  const TAR_BUILDER_OPTIONS = {