@loaders.gl/zip 4.1.0-alpha.1 → 4.1.0-alpha.10
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.
- package/dist/dist.dev.js +313 -51
- package/dist/filesystems/zip-filesystem.d.ts.map +1 -1
- package/dist/filesystems/zip-filesystem.js.map +1 -1
- package/dist/hash-file-utility.d.ts +6 -0
- package/dist/hash-file-utility.d.ts.map +1 -1
- package/dist/hash-file-utility.js +22 -0
- package/dist/hash-file-utility.js.map +1 -1
- package/dist/index.cjs +290 -56
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/lib/tar/header.d.ts.map +1 -1
- package/dist/lib/tar/header.js.map +1 -1
- package/dist/lib/tar/tar.d.ts.map +1 -1
- package/dist/lib/tar/tar.js.map +1 -1
- package/dist/lib/tar/types.d.ts.map +1 -1
- package/dist/lib/tar/types.js.map +1 -1
- package/dist/lib/tar/utils.d.ts.map +1 -1
- package/dist/lib/tar/utils.js.map +1 -1
- package/dist/parse-zip/cd-file-header.d.ts +1 -1
- package/dist/parse-zip/cd-file-header.d.ts.map +1 -1
- package/dist/parse-zip/cd-file-header.js +4 -4
- package/dist/parse-zip/cd-file-header.js.map +1 -1
- package/dist/parse-zip/end-of-central-directory.d.ts +19 -0
- package/dist/parse-zip/end-of-central-directory.d.ts.map +1 -1
- package/dist/parse-zip/end-of-central-directory.js +41 -8
- package/dist/parse-zip/end-of-central-directory.js.map +1 -1
- package/dist/parse-zip/local-file-header.d.ts +16 -0
- package/dist/parse-zip/local-file-header.d.ts.map +1 -1
- package/dist/parse-zip/local-file-header.js +73 -1
- package/dist/parse-zip/local-file-header.js.map +1 -1
- package/dist/parse-zip/search-from-the-end.d.ts.map +1 -1
- package/dist/parse-zip/search-from-the-end.js.map +1 -1
- package/dist/parse-zip/zip-compozition.d.ts +8 -0
- package/dist/parse-zip/zip-compozition.d.ts.map +1 -0
- package/dist/parse-zip/zip-compozition.js +43 -0
- package/dist/parse-zip/zip-compozition.js.map +1 -0
- package/dist/parse-zip/zip64-info-generation.d.ts +5 -8
- package/dist/parse-zip/zip64-info-generation.d.ts.map +1 -1
- package/dist/parse-zip/zip64-info-generation.js +6 -3
- package/dist/parse-zip/zip64-info-generation.js.map +1 -1
- package/dist/tar-builder.d.ts.map +1 -1
- package/dist/tar-builder.js.map +1 -1
- package/dist/zip-loader.d.ts.map +1 -1
- package/dist/zip-loader.js +1 -1
- package/dist/zip-loader.js.map +1 -1
- package/dist/zip-writer.d.ts +2 -2
- package/dist/zip-writer.d.ts.map +1 -1
- package/dist/zip-writer.js +22 -7
- package/dist/zip-writer.js.map +1 -1
- package/package.json +7 -7
- package/src/filesystems/zip-filesystem.ts +2 -1
- package/src/hash-file-utility.ts +52 -2
- package/src/index.ts +6 -3
- package/src/lib/tar/header.ts +2 -1
- package/src/lib/tar/tar.ts +2 -1
- package/src/lib/tar/types.ts +2 -1
- package/src/lib/tar/utils.ts +2 -1
- package/src/parse-zip/cd-file-header.ts +8 -6
- package/src/parse-zip/end-of-central-directory.ts +99 -9
- package/src/parse-zip/local-file-header.ts +128 -2
- package/src/parse-zip/search-from-the-end.ts +2 -1
- package/src/parse-zip/zip-compozition.ts +113 -0
- package/src/parse-zip/zip64-info-generation.ts +20 -4
- package/src/tar-builder.ts +2 -1
- package/src/zip-loader.ts +2 -1
- package/src/zip-writer.ts +24 -10
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import {FileHandleFile, concatenateArrayBuffers} from '@loaders.gl/loader-utils';
|
|
2
|
+
import {ZipEoCDRecord, parseEoCDRecord, updateEoCD} from './end-of-central-directory';
|
|
3
|
+
import {CRC32Hash} from '@loaders.gl/crypto';
|
|
4
|
+
import {generateLocalHeader} from './local-file-header';
|
|
5
|
+
import {generateCDHeader} from './cd-file-header';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* cut off CD and EoCD records from zip file
|
|
9
|
+
* @param provider zip file
|
|
10
|
+
* @returns tuple with three values: CD, EoCD record, EoCD information
|
|
11
|
+
*/
|
|
12
|
+
async function cutTheTailOff(
|
|
13
|
+
provider: FileHandleFile
|
|
14
|
+
): Promise<[ArrayBuffer, ArrayBuffer, ZipEoCDRecord]> {
|
|
15
|
+
// define where the body ends
|
|
16
|
+
const oldEoCDinfo = await parseEoCDRecord(provider);
|
|
17
|
+
const oldCDStartOffset = oldEoCDinfo.cdStartOffset;
|
|
18
|
+
|
|
19
|
+
// define cd length
|
|
20
|
+
const oldCDLength = Number(
|
|
21
|
+
oldEoCDinfo.offsets.zip64EoCDOffset
|
|
22
|
+
? oldEoCDinfo.offsets.zip64EoCDOffset - oldCDStartOffset
|
|
23
|
+
: oldEoCDinfo.offsets.zipEoCDOffset - oldCDStartOffset
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
// cut off everything except of archieve body
|
|
27
|
+
const zipEnding = await provider.slice(oldCDStartOffset, provider.length);
|
|
28
|
+
await provider.truncate(Number(oldCDStartOffset));
|
|
29
|
+
|
|
30
|
+
// divide cd body and eocd record
|
|
31
|
+
const oldCDBody = zipEnding.slice(0, oldCDLength);
|
|
32
|
+
const eocdBody = zipEnding.slice(oldCDLength, zipEnding.byteLength);
|
|
33
|
+
|
|
34
|
+
return [oldCDBody, eocdBody, oldEoCDinfo];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* generates CD and local headers for the file
|
|
39
|
+
* @param fileName name of the file
|
|
40
|
+
* @param fileToAdd buffer with the file
|
|
41
|
+
* @param localFileHeaderOffset offset of the file local header
|
|
42
|
+
* @returns tuple with two values: local header and file body, cd header
|
|
43
|
+
*/
|
|
44
|
+
async function generateFileHeaders(
|
|
45
|
+
fileName: string,
|
|
46
|
+
fileToAdd: ArrayBuffer,
|
|
47
|
+
localFileHeaderOffset: bigint
|
|
48
|
+
): Promise<[Uint8Array, Uint8Array]> {
|
|
49
|
+
// generating CRC32 of the content
|
|
50
|
+
const newFileCRC322 = parseInt(await new CRC32Hash().hash(fileToAdd, 'hex'), 16);
|
|
51
|
+
|
|
52
|
+
// generate local header for the file
|
|
53
|
+
const newFileLocalHeader = generateLocalHeader({
|
|
54
|
+
crc32: newFileCRC322,
|
|
55
|
+
fileName,
|
|
56
|
+
length: fileToAdd.byteLength
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// generate hash file cd header
|
|
60
|
+
const newFileCDHeader = generateCDHeader({
|
|
61
|
+
crc32: newFileCRC322,
|
|
62
|
+
fileName,
|
|
63
|
+
offset: localFileHeaderOffset,
|
|
64
|
+
length: fileToAdd.byteLength
|
|
65
|
+
});
|
|
66
|
+
return [
|
|
67
|
+
new Uint8Array(concatenateArrayBuffers(newFileLocalHeader, fileToAdd)),
|
|
68
|
+
new Uint8Array(newFileCDHeader)
|
|
69
|
+
];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* adds one file in the end of the archieve
|
|
74
|
+
* @param zipUrl path to the file
|
|
75
|
+
* @param fileToAdd new file body
|
|
76
|
+
* @param fileName new file name
|
|
77
|
+
*/
|
|
78
|
+
export async function addOneFile(zipUrl: string, fileToAdd: ArrayBuffer, fileName: string) {
|
|
79
|
+
// init file handler
|
|
80
|
+
const provider = new FileHandleFile(zipUrl, true);
|
|
81
|
+
|
|
82
|
+
const [oldCDBody, eocdBody, oldEoCDinfo] = await cutTheTailOff(provider);
|
|
83
|
+
|
|
84
|
+
// remember the new file local header start offset
|
|
85
|
+
const newFileOffset = provider.length;
|
|
86
|
+
|
|
87
|
+
const [localPart, cdHeaderPart] = await generateFileHeaders(fileName, fileToAdd, newFileOffset);
|
|
88
|
+
|
|
89
|
+
// write down the file local header
|
|
90
|
+
await provider.append(localPart);
|
|
91
|
+
|
|
92
|
+
// add the file CD header to the CD
|
|
93
|
+
const newCDBody = concatenateArrayBuffers(oldCDBody, cdHeaderPart);
|
|
94
|
+
|
|
95
|
+
// remember the CD start offset
|
|
96
|
+
const newCDStartOffset = provider.length;
|
|
97
|
+
|
|
98
|
+
// write down new CD
|
|
99
|
+
await provider.append(new Uint8Array(newCDBody));
|
|
100
|
+
|
|
101
|
+
// remember where eocd starts
|
|
102
|
+
const eocdOffset = provider.length;
|
|
103
|
+
|
|
104
|
+
await provider.append(
|
|
105
|
+
await updateEoCD(
|
|
106
|
+
eocdBody,
|
|
107
|
+
oldEoCDinfo.offsets,
|
|
108
|
+
newCDStartOffset,
|
|
109
|
+
eocdOffset,
|
|
110
|
+
oldEoCDinfo.cdRecordsNumber + 1n
|
|
111
|
+
)
|
|
112
|
+
);
|
|
113
|
+
}
|
|
@@ -41,15 +41,31 @@ export function createZip64Info(options: Zip64Options): ArrayBuffer {
|
|
|
41
41
|
* @param offset offset of the writing start
|
|
42
42
|
* @param value value to be written
|
|
43
43
|
*/
|
|
44
|
-
type NumberSetter = (header: DataView, offset: number, value: number) => void;
|
|
44
|
+
type NumberSetter = (header: DataView, offset: number, value: number | bigint) => void;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Writes values into buffer according to the bytes amount
|
|
48
|
+
* @param header header where to write the data
|
|
49
|
+
* @param fieldSize size of the field in bytes
|
|
50
|
+
* @param fieldOffset offset of the field
|
|
51
|
+
* @param value value to be written
|
|
52
|
+
*/
|
|
53
|
+
export function setFieldToNumber(
|
|
54
|
+
header: DataView,
|
|
55
|
+
fieldSize: number,
|
|
56
|
+
fieldOffset: number | bigint,
|
|
57
|
+
value: number | bigint
|
|
58
|
+
): void {
|
|
59
|
+
NUMBER_SETTERS[fieldSize](header, Number(fieldOffset), value);
|
|
60
|
+
}
|
|
45
61
|
|
|
46
62
|
/** functions to write values into buffer according to the bytes amount */
|
|
47
|
-
|
|
63
|
+
const NUMBER_SETTERS: {[key: number]: NumberSetter} = {
|
|
48
64
|
2: (header, offset, value) => {
|
|
49
|
-
header.setUint16(offset, value, true);
|
|
65
|
+
header.setUint16(offset, Number(value), true);
|
|
50
66
|
},
|
|
51
67
|
4: (header, offset, value) => {
|
|
52
|
-
header.setUint32(offset, value, true);
|
|
68
|
+
header.setUint32(offset, Number(value), true);
|
|
53
69
|
},
|
|
54
70
|
8: (header, offset, value) => {
|
|
55
71
|
header.setBigUint64(offset, BigInt(value), true);
|
package/src/tar-builder.ts
CHANGED
package/src/zip-loader.ts
CHANGED
package/src/zip-writer.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
// loaders.gl
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
2
3
|
// Copyright (c) vis.gl contributors
|
|
3
4
|
|
|
4
|
-
import type {
|
|
5
|
+
import type {WriterWithEncoder, WriterOptions} from '@loaders.gl/loader-utils';
|
|
5
6
|
import JSZip, {JSZipGeneratorOptions} from 'jszip';
|
|
6
7
|
|
|
8
|
+
// @ts-ignore TS2304: Cannot find name '__VERSION__'.
|
|
9
|
+
const VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'latest';
|
|
10
|
+
|
|
7
11
|
export type ZipWriterOptions = WriterOptions & {
|
|
8
12
|
zip?: {
|
|
9
13
|
onUpdate?: (metadata: {percent: number}) => void;
|
|
@@ -15,19 +19,27 @@ export type ZipWriterOptions = WriterOptions & {
|
|
|
15
19
|
/**
|
|
16
20
|
* Zip exporter
|
|
17
21
|
*/
|
|
18
|
-
export const ZipWriter:
|
|
22
|
+
export const ZipWriter: WriterWithEncoder<Record<string, ArrayBuffer>, never, ZipWriterOptions> = {
|
|
19
23
|
name: 'Zip Archive',
|
|
24
|
+
id: 'zip',
|
|
25
|
+
module: 'zip',
|
|
26
|
+
version: VERSION,
|
|
20
27
|
extensions: ['zip'],
|
|
21
28
|
category: 'archive',
|
|
22
29
|
mimeTypes: ['application/zip'],
|
|
23
|
-
|
|
30
|
+
options: {
|
|
31
|
+
zip: {
|
|
32
|
+
onUpdate: () => {}
|
|
33
|
+
},
|
|
34
|
+
jszip: {}
|
|
35
|
+
},
|
|
24
36
|
encode: encodeZipAsync
|
|
25
37
|
};
|
|
26
38
|
|
|
27
39
|
async function encodeZipAsync(
|
|
28
40
|
fileMap: Record<string, ArrayBuffer>,
|
|
29
41
|
options: ZipWriterOptions = {}
|
|
30
|
-
) {
|
|
42
|
+
): Promise<ArrayBuffer> {
|
|
31
43
|
const jsZip = new JSZip();
|
|
32
44
|
// add files to the zip
|
|
33
45
|
for (const subFileName in fileMap) {
|
|
@@ -38,14 +50,16 @@ async function encodeZipAsync(
|
|
|
38
50
|
jsZip.file(subFileName, subFileData, options?.jszip || {});
|
|
39
51
|
}
|
|
40
52
|
|
|
41
|
-
|
|
42
|
-
const jszipOptions: JSZipGeneratorOptions = {...options?.jszip,
|
|
43
|
-
const {onUpdate = () => {}} = options;
|
|
53
|
+
const zipOptions = {...ZipWriter.options.zip, ...options?.zip};
|
|
54
|
+
const jszipOptions: JSZipGeneratorOptions = {...ZipWriter.options?.jszip, ...options.jszip};
|
|
44
55
|
|
|
45
56
|
try {
|
|
46
|
-
return await jsZip.generateAsync(
|
|
57
|
+
return await jsZip.generateAsync(
|
|
58
|
+
{...jszipOptions, type: 'arraybuffer'}, // generate an arraybuffer
|
|
59
|
+
zipOptions.onUpdate
|
|
60
|
+
);
|
|
47
61
|
} catch (error) {
|
|
48
|
-
options.log.error(`Unable to
|
|
62
|
+
options.log.error(`Unable to encode zip archive: ${error}`);
|
|
49
63
|
throw error;
|
|
50
64
|
}
|
|
51
65
|
}
|