@loaders.gl/zip 4.0.0-alpha.19 → 4.0.0-alpha.21

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 (92) hide show
  1. package/dist/dist.min.js +638 -1
  2. package/dist/es5/file-provider/data-view-file.js +146 -0
  3. package/dist/es5/file-provider/data-view-file.js.map +1 -0
  4. package/dist/es5/file-provider/file-handle-file.js +234 -0
  5. package/dist/es5/file-provider/file-handle-file.js.map +1 -0
  6. package/dist/es5/file-provider/file-handle.js +101 -0
  7. package/dist/es5/file-provider/file-handle.js.map +1 -0
  8. package/dist/es5/file-provider/file-provider.js +11 -0
  9. package/dist/es5/file-provider/file-provider.js.map +1 -0
  10. package/dist/es5/filesystems/zip-filesystem.js +340 -0
  11. package/dist/es5/filesystems/zip-filesystem.js.map +1 -0
  12. package/dist/es5/hash-file-utility.js +130 -0
  13. package/dist/es5/hash-file-utility.js.map +1 -0
  14. package/dist/es5/index.js +85 -0
  15. package/dist/es5/index.js.map +1 -1
  16. package/dist/es5/parse-zip/cd-file-header.js +163 -0
  17. package/dist/es5/parse-zip/cd-file-header.js.map +1 -0
  18. package/dist/es5/parse-zip/end-of-central-directory.js +98 -0
  19. package/dist/es5/parse-zip/end-of-central-directory.js.map +1 -0
  20. package/dist/es5/parse-zip/local-file-header.js +117 -0
  21. package/dist/es5/parse-zip/local-file-header.js.map +1 -0
  22. package/dist/es5/parse-zip/search-from-the-end.js +69 -0
  23. package/dist/es5/parse-zip/search-from-the-end.js.map +1 -0
  24. package/dist/es5/zip-loader.js +1 -1
  25. package/dist/esm/file-provider/data-view-file.js +33 -0
  26. package/dist/esm/file-provider/data-view-file.js.map +1 -0
  27. package/dist/esm/file-provider/file-handle-file.js +57 -0
  28. package/dist/esm/file-provider/file-handle-file.js.map +1 -0
  29. package/dist/esm/file-provider/file-handle.js +37 -0
  30. package/dist/esm/file-provider/file-handle.js.map +1 -0
  31. package/dist/esm/file-provider/file-provider.js +4 -0
  32. package/dist/esm/file-provider/file-provider.js.map +1 -0
  33. package/dist/esm/filesystems/zip-filesystem.js +86 -0
  34. package/dist/esm/filesystems/zip-filesystem.js.map +1 -0
  35. package/dist/esm/hash-file-utility.js +55 -0
  36. package/dist/esm/hash-file-utility.js.map +1 -0
  37. package/dist/esm/index.js +7 -0
  38. package/dist/esm/index.js.map +1 -1
  39. package/dist/esm/parse-zip/cd-file-header.js +54 -0
  40. package/dist/esm/parse-zip/cd-file-header.js.map +1 -0
  41. package/dist/esm/parse-zip/end-of-central-directory.js +31 -0
  42. package/dist/esm/parse-zip/end-of-central-directory.js.map +1 -0
  43. package/dist/esm/parse-zip/local-file-header.js +41 -0
  44. package/dist/esm/parse-zip/local-file-header.js.map +1 -0
  45. package/dist/esm/parse-zip/search-from-the-end.js +16 -0
  46. package/dist/esm/parse-zip/search-from-the-end.js.map +1 -0
  47. package/dist/esm/zip-loader.js +1 -1
  48. package/dist/file-provider/data-view-file.d.ts +37 -0
  49. package/dist/file-provider/data-view-file.d.ts.map +1 -0
  50. package/dist/file-provider/data-view-file.js +63 -0
  51. package/dist/file-provider/file-handle-file.d.ts +53 -0
  52. package/dist/file-provider/file-handle-file.d.ts.map +1 -0
  53. package/dist/file-provider/file-handle-file.js +90 -0
  54. package/dist/file-provider/file-handle.d.ts +40 -0
  55. package/dist/file-provider/file-handle.d.ts.map +1 -0
  56. package/dist/file-provider/file-handle.js +57 -0
  57. package/dist/file-provider/file-provider.d.ts +45 -0
  58. package/dist/file-provider/file-provider.d.ts.map +1 -0
  59. package/dist/file-provider/file-provider.js +13 -0
  60. package/dist/filesystems/zip-filesystem.d.ts +44 -0
  61. package/dist/filesystems/zip-filesystem.d.ts.map +1 -0
  62. package/dist/filesystems/zip-filesystem.js +119 -0
  63. package/dist/hash-file-utility.d.ts +35 -0
  64. package/dist/hash-file-utility.d.ts.map +1 -0
  65. package/dist/hash-file-utility.js +88 -0
  66. package/dist/index.d.ts +9 -0
  67. package/dist/index.d.ts.map +1 -1
  68. package/dist/index.js +21 -1
  69. package/dist/parse-zip/cd-file-header.d.ts +36 -0
  70. package/dist/parse-zip/cd-file-header.d.ts.map +1 -0
  71. package/dist/parse-zip/cd-file-header.js +68 -0
  72. package/dist/parse-zip/end-of-central-directory.d.ts +18 -0
  73. package/dist/parse-zip/end-of-central-directory.d.ts.map +1 -0
  74. package/dist/parse-zip/end-of-central-directory.js +40 -0
  75. package/dist/parse-zip/local-file-header.d.ts +29 -0
  76. package/dist/parse-zip/local-file-header.d.ts.map +1 -0
  77. package/dist/parse-zip/local-file-header.js +55 -0
  78. package/dist/parse-zip/search-from-the-end.d.ts +11 -0
  79. package/dist/parse-zip/search-from-the-end.d.ts.map +1 -0
  80. package/dist/parse-zip/search-from-the-end.js +31 -0
  81. package/package.json +9 -2
  82. package/src/file-provider/data-view-file.ts +72 -0
  83. package/src/file-provider/file-handle-file.ts +114 -0
  84. package/src/file-provider/file-handle.ts +73 -0
  85. package/src/file-provider/file-provider.ts +56 -0
  86. package/src/filesystems/zip-filesystem.ts +132 -0
  87. package/src/hash-file-utility.ts +101 -0
  88. package/src/index.ts +19 -0
  89. package/src/parse-zip/cd-file-header.ts +114 -0
  90. package/src/parse-zip/end-of-central-directory.ts +71 -0
  91. package/src/parse-zip/local-file-header.ts +91 -0
  92. package/src/parse-zip/search-from-the-end.ts +38 -0
@@ -0,0 +1,132 @@
1
+ import {FileSystem, isBrowser} from '@loaders.gl/core';
2
+ import {FileProvider, isFileProvider} from '../file-provider/file-provider';
3
+ import {FileHandleFile} from '../file-provider/file-handle-file';
4
+ import {ZipCDFileHeader, zipCDFileHeaderGenerator} from '../parse-zip/cd-file-header';
5
+ import {parseZipLocalFileHeader} from '../parse-zip/local-file-header';
6
+
7
+ type CompressionHandler = (compressedFile: ArrayBuffer) => Promise<ArrayBuffer>;
8
+ /** Handling different compression types in zip */
9
+ const COMPRESSION_METHODS: {[key: number]: CompressionHandler} = {
10
+ /** No compression */
11
+ 0: async (compressedFile) => compressedFile
12
+ };
13
+
14
+ /**
15
+ * FileSystem adapter for a ZIP file
16
+ * Holds FileProvider object that provides random access to archived files
17
+ */
18
+ export class ZipFileSystem implements FileSystem {
19
+ /** FileProvider instance promise */
20
+ private fileProvider: Promise<FileProvider | null> = Promise.resolve(null);
21
+
22
+ /**
23
+ * Constructor
24
+ * @param file - instance of FileProvider or file path string
25
+ */
26
+ constructor(file: FileProvider | string) {
27
+ // Try to open file in NodeJS
28
+ if (typeof file === 'string') {
29
+ if (!isBrowser) {
30
+ this.fileProvider = FileHandleFile.from(file);
31
+ } else {
32
+ throw new Error('Cannot open file for random access in a WEB browser');
33
+ }
34
+ } else if (isFileProvider(file)) {
35
+ this.fileProvider = Promise.resolve(file);
36
+ }
37
+ }
38
+
39
+ /** Clean up resources */
40
+ async destroy() {
41
+ const fileProvider = await this.fileProvider;
42
+ if (fileProvider) {
43
+ fileProvider.destroy();
44
+ }
45
+ }
46
+
47
+ /**
48
+ * Get file names list from zip archive
49
+ * @returns array of file names
50
+ */
51
+ async readdir(): Promise<string[]> {
52
+ const fileProvider = await this.fileProvider;
53
+ if (!fileProvider) {
54
+ throw new Error('No data detected in the zip archive');
55
+ }
56
+ const fileNames: string[] = [];
57
+ const zipCDIterator = zipCDFileHeaderGenerator(fileProvider);
58
+ for await (const cdHeader of zipCDIterator) {
59
+ fileNames.push(cdHeader.fileName);
60
+ }
61
+ return fileNames;
62
+ }
63
+
64
+ /**
65
+ * Get file metadata
66
+ * @param filename - name of a file
67
+ * @returns central directory data
68
+ */
69
+ async stat(filename: string): Promise<ZipCDFileHeader & {size: number}> {
70
+ const cdFileHeader = await this.getCDFileHeader(filename);
71
+ return {...cdFileHeader, size: Number(cdFileHeader.uncompressedSize)};
72
+ }
73
+
74
+ /**
75
+ * Implementation of fetch against this file system
76
+ * @param filename - name of a file
77
+ * @returns - Response with file data
78
+ */
79
+ async fetch(filename: string): Promise<Response> {
80
+ const fileProvider = await this.fileProvider;
81
+ if (!fileProvider) {
82
+ throw new Error('No data detected in the zip archive');
83
+ }
84
+ const cdFileHeader = await this.getCDFileHeader(filename);
85
+ const localFileHeader = await parseZipLocalFileHeader(
86
+ cdFileHeader.localHeaderOffset,
87
+ fileProvider
88
+ );
89
+ if (!localFileHeader) {
90
+ throw new Error('Local file header has not been found in the zip archive`');
91
+ }
92
+
93
+ const compressionHandler = COMPRESSION_METHODS[localFileHeader.compressionMethod.toString()];
94
+ if (!compressionHandler) {
95
+ throw Error('Only Deflation compression is supported');
96
+ }
97
+
98
+ const compressedFile = await fileProvider.slice(
99
+ localFileHeader.fileDataOffset,
100
+ localFileHeader.fileDataOffset + localFileHeader.compressedSize
101
+ );
102
+
103
+ const uncompressedFile = await compressionHandler(compressedFile);
104
+
105
+ const response = new Response(uncompressedFile);
106
+ return response;
107
+ }
108
+
109
+ /**
110
+ * Get central directory file header
111
+ * @param filename - name of a file
112
+ * @returns central directory file header
113
+ */
114
+ private async getCDFileHeader(filename: string): Promise<ZipCDFileHeader> {
115
+ const fileProvider = await this.fileProvider;
116
+ if (!fileProvider) {
117
+ throw new Error('No data detected in the zip archive');
118
+ }
119
+ const zipCDIterator = zipCDFileHeaderGenerator(fileProvider);
120
+ let result: ZipCDFileHeader | null = null;
121
+ for await (const cdHeader of zipCDIterator) {
122
+ if (cdHeader.fileName === filename) {
123
+ result = cdHeader;
124
+ break;
125
+ }
126
+ }
127
+ if (!result) {
128
+ throw new Error('File has not been found in the zip archive');
129
+ }
130
+ return result;
131
+ }
132
+ }
@@ -0,0 +1,101 @@
1
+ import md5 from 'md5';
2
+ import {zipCDFileHeaderGenerator} from './parse-zip/cd-file-header';
3
+ import {FileProvider} from './file-provider/file-provider';
4
+
5
+ /** Element of hash array */
6
+ export type HashElement = {
7
+ /** File name hash */
8
+ hash: Buffer;
9
+ /** File offset in the archive */
10
+ offset: bigint;
11
+ };
12
+
13
+ /**
14
+ * Comparing md5 hashes according to https://github.com/Esri/i3s-spec/blob/master/docs/2.0/slpk_hashtable.pcsl.md step 5
15
+ * @param hash1 hash to compare
16
+ * @param hash2 hash to compare
17
+ * @returns -1 if hash1 < hash2, 0 of hash1 === hash2, 1 if hash1 > hash2
18
+ */
19
+ export const compareHashes = (hash1: Buffer, hash2: Buffer): number => {
20
+ const h1 = new BigUint64Array(hash1.buffer, hash1.byteOffset, 2);
21
+ const h2 = new BigUint64Array(hash2.buffer, hash2.byteOffset, 2);
22
+
23
+ const diff = h1[0] === h2[0] ? h1[1] - h2[1] : h1[0] - h2[0];
24
+
25
+ if (diff < 0n) {
26
+ return -1;
27
+ } else if (diff === 0n) {
28
+ return 0;
29
+ }
30
+ return 1;
31
+ };
32
+
33
+ /**
34
+ * Reads hash file from buffer and returns it in ready-to-use form
35
+ * @param hashFile - bufer containing hash file
36
+ * @returns Array containing file info
37
+ */
38
+ export const parseHashFile = (hashFile: ArrayBuffer): HashElement[] => {
39
+ const hashFileBuffer = Buffer.from(hashFile);
40
+ const hashArray: HashElement[] = [];
41
+ for (let i = 0; i < hashFileBuffer.buffer.byteLength; i = i + 24) {
42
+ const offsetBuffer = new DataView(
43
+ hashFileBuffer.buffer.slice(
44
+ hashFileBuffer.byteOffset + i + 16,
45
+ hashFileBuffer.byteOffset + i + 24
46
+ )
47
+ );
48
+ const offset = offsetBuffer.getBigUint64(offsetBuffer.byteOffset, true);
49
+ hashArray.push({
50
+ hash: Buffer.from(
51
+ hashFileBuffer.subarray(hashFileBuffer.byteOffset + i, hashFileBuffer.byteOffset + i + 16)
52
+ ),
53
+ offset
54
+ });
55
+ }
56
+ return hashArray;
57
+ };
58
+
59
+ /**
60
+ * Binary search in the hash info
61
+ * @param hashToSearch hash that we need to find
62
+ * @returns required hash element or undefined if not found
63
+ */
64
+ export const findBin = (
65
+ hashToSearch: Buffer,
66
+ hashArray: HashElement[]
67
+ ): HashElement | undefined => {
68
+ let lowerBorder = 0;
69
+ let upperBorder = hashArray.length;
70
+
71
+ while (upperBorder - lowerBorder > 1) {
72
+ const middle = lowerBorder + Math.floor((upperBorder - lowerBorder) / 2);
73
+ const value = compareHashes(hashArray[middle].hash, hashToSearch);
74
+ if (value === 0) {
75
+ return hashArray[middle];
76
+ } else if (value < 0) {
77
+ lowerBorder = middle;
78
+ } else {
79
+ upperBorder = middle;
80
+ }
81
+ }
82
+ return undefined;
83
+ };
84
+
85
+ /**
86
+ * generates hash info from central directory
87
+ * @param fileProvider - provider of the archive
88
+ * @returns ready to use hash info
89
+ */
90
+ export const generateHashInfo = async (fileProvider: FileProvider): Promise<HashElement[]> => {
91
+ const zipCDIterator = zipCDFileHeaderGenerator(fileProvider);
92
+ const hashInfo: HashElement[] = [];
93
+ for await (const cdHeader of zipCDIterator) {
94
+ hashInfo.push({
95
+ hash: Buffer.from(md5(cdHeader.fileName.split('\\').join('/').toLocaleLowerCase()), 'hex'),
96
+ offset: cdHeader.localHeaderOffset
97
+ });
98
+ }
99
+ hashInfo.sort((a, b) => compareHashes(a.hash, b.hash));
100
+ return hashInfo;
101
+ };
package/src/index.ts CHANGED
@@ -1,3 +1,22 @@
1
1
  export {ZipLoader} from './zip-loader';
2
2
  export {ZipWriter} from './zip-writer';
3
3
  export {default as TarBuilder} from './tar-builder';
4
+
5
+ export type {FileProvider} from './file-provider/file-provider';
6
+ export {FileHandleFile} from './file-provider/file-handle-file';
7
+ export {DataViewFile} from './file-provider/data-view-file';
8
+
9
+ export {
10
+ parseZipCDFileHeader,
11
+ zipCDFileHeaderGenerator,
12
+ signature as cdSignature
13
+ } from './parse-zip/cd-file-header';
14
+ export {
15
+ parseZipLocalFileHeader,
16
+ signature as localHeaderSignature
17
+ } from './parse-zip/local-file-header';
18
+ export {parseEoCDRecord} from './parse-zip/end-of-central-directory';
19
+ export {searchFromTheEnd} from './parse-zip/search-from-the-end';
20
+
21
+ export type {HashElement} from './hash-file-utility';
22
+ export {compareHashes, parseHashFile, findBin, generateHashInfo} from './hash-file-utility';
@@ -0,0 +1,114 @@
1
+ import {FileProvider} from '../file-provider/file-provider';
2
+ import {parseEoCDRecord} from './end-of-central-directory';
3
+ import {ZipSignature} from './search-from-the-end';
4
+
5
+ /**
6
+ * zip central directory file header info
7
+ * according to https://en.wikipedia.org/wiki/ZIP_(file_format)
8
+ */
9
+ export type ZipCDFileHeader = {
10
+ /** Compressed size */
11
+ compressedSize: bigint;
12
+ /** Uncompressed size */
13
+ uncompressedSize: bigint;
14
+ /** Extra field size */
15
+ extraFieldLength: number;
16
+ /** File name length */
17
+ fileNameLength: number;
18
+ /** File name */
19
+ fileName: string;
20
+ /** Extra field offset */
21
+ extraOffset: bigint;
22
+ /** Relative offset of local file header */
23
+ localHeaderOffset: bigint;
24
+ };
25
+
26
+ // offsets accroding to https://en.wikipedia.org/wiki/ZIP_(file_format)
27
+ const CD_COMPRESSED_SIZE_OFFSET = 20n;
28
+ const CD_UNCOMPRESSED_SIZE_OFFSET = 24n;
29
+ const CD_FILE_NAME_LENGTH_OFFSET = 28n;
30
+ const CD_EXTRA_FIELD_LENGTH_OFFSET = 30n;
31
+ const CD_LOCAL_HEADER_OFFSET_OFFSET = 42n;
32
+ const CD_FILE_NAME_OFFSET = 46n;
33
+
34
+ export const signature: ZipSignature = [0x50, 0x4b, 0x01, 0x02];
35
+
36
+ /**
37
+ * Parses central directory file header of zip file
38
+ * @param headerOffset - offset in the archive where header starts
39
+ * @param buffer - buffer containing whole array
40
+ * @returns Info from the header
41
+ */
42
+ export const parseZipCDFileHeader = async (
43
+ headerOffset: bigint,
44
+ buffer: FileProvider
45
+ ): Promise<ZipCDFileHeader | null> => {
46
+ if (
47
+ Buffer.from(await buffer.slice(headerOffset, headerOffset + 4n)).compare(
48
+ Buffer.from(signature)
49
+ ) !== 0
50
+ ) {
51
+ return null;
52
+ }
53
+
54
+ let compressedSize = BigInt(await buffer.getUint32(headerOffset + CD_COMPRESSED_SIZE_OFFSET));
55
+
56
+ let uncompressedSize = BigInt(await buffer.getUint32(headerOffset + CD_UNCOMPRESSED_SIZE_OFFSET));
57
+
58
+ const extraFieldLength = await buffer.getUint16(headerOffset + CD_EXTRA_FIELD_LENGTH_OFFSET);
59
+
60
+ const fileNameLength = await buffer.getUint16(headerOffset + CD_FILE_NAME_LENGTH_OFFSET);
61
+
62
+ const fileName = new TextDecoder().decode(
63
+ await buffer.slice(
64
+ headerOffset + CD_FILE_NAME_OFFSET,
65
+ headerOffset + CD_FILE_NAME_OFFSET + BigInt(fileNameLength)
66
+ )
67
+ );
68
+
69
+ const extraOffset = headerOffset + CD_FILE_NAME_OFFSET + BigInt(fileNameLength);
70
+
71
+ const oldFormatOffset = await buffer.getUint32(headerOffset + CD_LOCAL_HEADER_OFFSET_OFFSET);
72
+
73
+ let fileDataOffset = BigInt(oldFormatOffset);
74
+ let offsetInZip64Data = 4n;
75
+ // looking for info that might be also be in zip64 extra field
76
+ if (uncompressedSize === BigInt(0xffffffff)) {
77
+ uncompressedSize = await buffer.getBigUint64(extraOffset + offsetInZip64Data);
78
+ offsetInZip64Data += 8n;
79
+ }
80
+ if (compressedSize === BigInt(0xffffffff)) {
81
+ compressedSize = await buffer.getBigUint64(extraOffset + offsetInZip64Data);
82
+ offsetInZip64Data += 8n;
83
+ }
84
+ if (fileDataOffset === BigInt(0xffffffff)) {
85
+ fileDataOffset = await buffer.getBigUint64(extraOffset + offsetInZip64Data); // setting it to the one from zip64
86
+ }
87
+ const localHeaderOffset = fileDataOffset;
88
+
89
+ return {
90
+ compressedSize,
91
+ uncompressedSize,
92
+ extraFieldLength,
93
+ fileNameLength,
94
+ fileName,
95
+ extraOffset,
96
+ localHeaderOffset
97
+ };
98
+ };
99
+
100
+ /**
101
+ * Create iterator over files of zip archive
102
+ * @param fileProvider - file provider that provider random access to the file
103
+ */
104
+ export async function* zipCDFileHeaderGenerator(fileProvider: FileProvider) {
105
+ const {cdStartOffset} = await parseEoCDRecord(fileProvider);
106
+ let cdHeader = await parseZipCDFileHeader(cdStartOffset, fileProvider);
107
+ while (cdHeader) {
108
+ yield cdHeader;
109
+ cdHeader = await parseZipCDFileHeader(
110
+ cdHeader.extraOffset + BigInt(cdHeader.extraFieldLength),
111
+ fileProvider
112
+ );
113
+ }
114
+ }
@@ -0,0 +1,71 @@
1
+ import {FileProvider} from '../file-provider/file-provider';
2
+ import {ZipSignature, searchFromTheEnd} from './search-from-the-end';
3
+
4
+ /**
5
+ * End of central directory info
6
+ * according to https://en.wikipedia.org/wiki/ZIP_(file_format)
7
+ */
8
+ export type ZipEoCDRecord = {
9
+ /** Relative offset of local file header */
10
+ cdStartOffset: bigint;
11
+ /** Relative offset of local file header */
12
+ cdRecordsNumber: bigint;
13
+ };
14
+
15
+ const eoCDSignature: ZipSignature = [0x50, 0x4b, 0x05, 0x06];
16
+ const zip64EoCDLocatorSignature = Buffer.from([0x50, 0x4b, 0x06, 0x07]);
17
+ const zip64EoCDSignature = Buffer.from([0x50, 0x4b, 0x06, 0x06]);
18
+
19
+ // offsets accroding to https://en.wikipedia.org/wiki/ZIP_(file_format)
20
+ const CD_RECORDS_NUMBER_OFFSET = 8n;
21
+ const CD_START_OFFSET_OFFSET = 16n;
22
+ const ZIP64_EOCD_START_OFFSET_OFFSET = 8n;
23
+ const ZIP64_CD_RECORDS_NUMBER_OFFSET = 24n;
24
+ const ZIP64_CD_START_OFFSET_OFFSET = 48n;
25
+
26
+ /**
27
+ * Parses end of central directory record of zip file
28
+ * @param fileProvider - FileProvider instance
29
+ * @returns Info from the header
30
+ */
31
+ export const parseEoCDRecord = async (fileProvider: FileProvider): Promise<ZipEoCDRecord> => {
32
+ const zipEoCDOffset = await searchFromTheEnd(fileProvider, eoCDSignature);
33
+
34
+ let cdRecordsNumber = BigInt(
35
+ await fileProvider.getUint16(zipEoCDOffset + CD_RECORDS_NUMBER_OFFSET)
36
+ );
37
+ let cdStartOffset = BigInt(await fileProvider.getUint32(zipEoCDOffset + CD_START_OFFSET_OFFSET));
38
+
39
+ if (cdStartOffset === BigInt(0xffffffff) || cdRecordsNumber === BigInt(0xffffffff)) {
40
+ const zip64EoCDLocatorOffset = zipEoCDOffset - 20n;
41
+
42
+ if (
43
+ Buffer.from(
44
+ await fileProvider.slice(zip64EoCDLocatorOffset, zip64EoCDLocatorOffset + 4n)
45
+ ).compare(zip64EoCDLocatorSignature) !== 0
46
+ ) {
47
+ throw new Error('zip64 EoCD locator not found');
48
+ }
49
+ const zip64EoCDOffset = await fileProvider.getBigUint64(
50
+ zip64EoCDLocatorOffset + ZIP64_EOCD_START_OFFSET_OFFSET
51
+ );
52
+
53
+ if (
54
+ Buffer.from(await fileProvider.slice(zip64EoCDOffset, zip64EoCDOffset + 4n)).compare(
55
+ zip64EoCDSignature
56
+ ) !== 0
57
+ ) {
58
+ throw new Error('zip64 EoCD not found');
59
+ }
60
+
61
+ cdRecordsNumber = await fileProvider.getBigUint64(
62
+ zip64EoCDOffset + ZIP64_CD_RECORDS_NUMBER_OFFSET
63
+ );
64
+ cdStartOffset = await fileProvider.getBigUint64(zip64EoCDOffset + ZIP64_CD_START_OFFSET_OFFSET);
65
+ }
66
+
67
+ return {
68
+ cdRecordsNumber,
69
+ cdStartOffset
70
+ };
71
+ };
@@ -0,0 +1,91 @@
1
+ import {FileProvider} from '../file-provider/file-provider';
2
+
3
+ /**
4
+ * zip local file header info
5
+ * according to https://en.wikipedia.org/wiki/ZIP_(file_format)
6
+ */
7
+ export type ZipLocalFileHeader = {
8
+ /** File name length */
9
+ fileNameLength: number;
10
+ /** File name */
11
+ fileName: string;
12
+ /** Extra field length */
13
+ extraFieldLength: number;
14
+ /** Offset of the file data */
15
+ fileDataOffset: bigint;
16
+ /** Compressed size */
17
+ compressedSize: bigint;
18
+ /** Compression method */
19
+ compressionMethod: number;
20
+ };
21
+
22
+ // offsets accroding to https://en.wikipedia.org/wiki/ZIP_(file_format)
23
+ const COMPRESSION_METHOD_OFFSET = 8n;
24
+ const COMPRESSED_SIZE_OFFSET = 18n;
25
+ const UNCOMPRESSED_SIZE_OFFSET = 22n;
26
+ const FILE_NAME_LENGTH_OFFSET = 26n;
27
+ const EXTRA_FIELD_LENGTH_OFFSET = 28n;
28
+ const FILE_NAME_OFFSET = 30n;
29
+
30
+ export const signature = Buffer.from([0x50, 0x4b, 0x03, 0x04]);
31
+
32
+ /**
33
+ * Parses local file header of zip file
34
+ * @param headerOffset - offset in the archive where header starts
35
+ * @param buffer - buffer containing whole array
36
+ * @returns Info from the header
37
+ */
38
+ export const parseZipLocalFileHeader = async (
39
+ headerOffset: bigint,
40
+ buffer: FileProvider
41
+ ): Promise<ZipLocalFileHeader | null> => {
42
+ if (Buffer.from(await buffer.slice(headerOffset, headerOffset + 4n)).compare(signature) !== 0) {
43
+ return null;
44
+ }
45
+
46
+ const fileNameLength = await buffer.getUint16(headerOffset + FILE_NAME_LENGTH_OFFSET);
47
+
48
+ const fileName = new TextDecoder()
49
+ .decode(
50
+ await buffer.slice(
51
+ headerOffset + FILE_NAME_OFFSET,
52
+ headerOffset + FILE_NAME_OFFSET + BigInt(fileNameLength)
53
+ )
54
+ )
55
+ .split('\\')
56
+ .join('/');
57
+ const extraFieldLength = await buffer.getUint16(headerOffset + EXTRA_FIELD_LENGTH_OFFSET);
58
+
59
+ let fileDataOffset = headerOffset + FILE_NAME_OFFSET + BigInt(fileNameLength + extraFieldLength);
60
+
61
+ const compressionMethod = await buffer.getUint16(headerOffset + COMPRESSION_METHOD_OFFSET);
62
+
63
+ let compressedSize = BigInt(await buffer.getUint32(headerOffset + COMPRESSED_SIZE_OFFSET)); // add zip 64 logic
64
+
65
+ let uncompressedSize = BigInt(await buffer.getUint32(headerOffset + UNCOMPRESSED_SIZE_OFFSET)); // add zip 64 logic
66
+
67
+ const extraOffset = headerOffset + FILE_NAME_OFFSET + BigInt(fileNameLength);
68
+
69
+ let offsetInZip64Data = 4n;
70
+ // looking for info that might be also be in zip64 extra field
71
+ if (uncompressedSize === BigInt(0xffffffff)) {
72
+ uncompressedSize = await buffer.getBigUint64(extraOffset + offsetInZip64Data);
73
+ offsetInZip64Data += 8n;
74
+ }
75
+ if (compressedSize === BigInt(0xffffffff)) {
76
+ compressedSize = await buffer.getBigUint64(extraOffset + offsetInZip64Data);
77
+ offsetInZip64Data += 8n;
78
+ }
79
+ if (fileDataOffset === BigInt(0xffffffff)) {
80
+ fileDataOffset = await buffer.getBigUint64(extraOffset + offsetInZip64Data); // setting it to the one from zip64
81
+ }
82
+
83
+ return {
84
+ fileNameLength,
85
+ fileName,
86
+ extraFieldLength,
87
+ fileDataOffset,
88
+ compressedSize,
89
+ compressionMethod
90
+ };
91
+ };
@@ -0,0 +1,38 @@
1
+ import {FileProvider} from '../file-provider/file-provider';
2
+
3
+ /** Description of zip signature type */
4
+ export type ZipSignature = [number, number, number, number];
5
+
6
+ /**
7
+ * looking for the last occurrence of the provided
8
+ * @param file
9
+ * @param target
10
+ * @returns
11
+ */
12
+ export const searchFromTheEnd = async (
13
+ file: FileProvider,
14
+ target: ZipSignature
15
+ ): Promise<bigint> => {
16
+ const searchWindow = [
17
+ await file.getUint8(file.length - 1n),
18
+ await file.getUint8(file.length - 2n),
19
+ await file.getUint8(file.length - 3n),
20
+ undefined
21
+ ];
22
+
23
+ let targetOffset = 0n;
24
+
25
+ // looking for the last record in the central directory
26
+ for (let i = file.length - 4n; i > -1; i--) {
27
+ searchWindow[3] = searchWindow[2];
28
+ searchWindow[2] = searchWindow[1];
29
+ searchWindow[1] = searchWindow[0];
30
+ searchWindow[0] = await file.getUint8(i);
31
+ if (searchWindow.every((val, index) => val === target[index])) {
32
+ targetOffset = i;
33
+ break;
34
+ }
35
+ }
36
+
37
+ return targetOffset;
38
+ };