@loaders.gl/zip 4.1.0-alpha.10 → 4.1.0-alpha.11
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 +572 -23
- package/dist/hash-file-utility.d.ts +15 -2
- package/dist/hash-file-utility.d.ts.map +1 -1
- package/dist/hash-file-utility.js +14 -6
- package/dist/hash-file-utility.js.map +1 -1
- package/dist/index.cjs +260 -8
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/parse-zip/end-of-central-directory.d.ts +17 -1
- package/dist/parse-zip/end-of-central-directory.d.ts.map +1 -1
- package/dist/parse-zip/end-of-central-directory.js +121 -2
- package/dist/parse-zip/end-of-central-directory.js.map +1 -1
- package/dist/parse-zip/zip-composition.d.ts +38 -0
- package/dist/parse-zip/zip-composition.d.ts.map +1 -0
- package/dist/parse-zip/zip-composition.js +115 -0
- package/dist/parse-zip/zip-composition.js.map +1 -0
- package/dist/parse-zip/zip64-info-generation.js +2 -2
- package/dist/parse-zip/zip64-info-generation.js.map +1 -1
- package/dist/zip-loader.js +1 -1
- package/dist/zip-writer.js +1 -1
- package/package.json +5 -5
- package/src/hash-file-utility.ts +38 -7
- package/src/index.ts +1 -1
- package/src/parse-zip/end-of-central-directory.ts +241 -3
- package/src/parse-zip/zip-composition.ts +235 -0
- package/src/parse-zip/zip64-info-generation.ts +3 -3
- package/dist/parse-zip/zip-compozition.d.ts +0 -8
- package/dist/parse-zip/zip-compozition.d.ts.map +0 -1
- package/dist/parse-zip/zip-compozition.js +0 -43
- package/dist/parse-zip/zip-compozition.js.map +0 -1
- package/src/parse-zip/zip-compozition.ts +0 -113
|
@@ -1,113 +0,0 @@
|
|
|
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
|
-
}
|