@neoware_inc/neozipkit 0.5.0

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 (171) hide show
  1. package/README.md +134 -0
  2. package/dist/browser/ZipkitBrowser.d.ts +27 -0
  3. package/dist/browser/ZipkitBrowser.d.ts.map +1 -0
  4. package/dist/browser/ZipkitBrowser.js +303 -0
  5. package/dist/browser/ZipkitBrowser.js.map +1 -0
  6. package/dist/browser/index.d.ts +9 -0
  7. package/dist/browser/index.d.ts.map +1 -0
  8. package/dist/browser/index.esm.d.ts +12 -0
  9. package/dist/browser/index.esm.d.ts.map +1 -0
  10. package/dist/browser/index.esm.js +46 -0
  11. package/dist/browser/index.esm.js.map +1 -0
  12. package/dist/browser/index.js +38 -0
  13. package/dist/browser/index.js.map +1 -0
  14. package/dist/browser-esm/index.d.ts +9 -0
  15. package/dist/browser-esm/index.js +50211 -0
  16. package/dist/browser-esm/index.js.map +7 -0
  17. package/dist/browser-umd/index.d.ts +9 -0
  18. package/dist/browser-umd/index.js +50221 -0
  19. package/dist/browser-umd/index.js.map +7 -0
  20. package/dist/browser-umd/index.min.js +39 -0
  21. package/dist/browser.d.ts +9 -0
  22. package/dist/browser.js +38 -0
  23. package/dist/core/ZipCompress.d.ts +99 -0
  24. package/dist/core/ZipCompress.d.ts.map +1 -0
  25. package/dist/core/ZipCompress.js +287 -0
  26. package/dist/core/ZipCompress.js.map +1 -0
  27. package/dist/core/ZipCopy.d.ts +175 -0
  28. package/dist/core/ZipCopy.d.ts.map +1 -0
  29. package/dist/core/ZipCopy.js +310 -0
  30. package/dist/core/ZipCopy.js.map +1 -0
  31. package/dist/core/ZipDecompress.d.ts +57 -0
  32. package/dist/core/ZipDecompress.d.ts.map +1 -0
  33. package/dist/core/ZipDecompress.js +155 -0
  34. package/dist/core/ZipDecompress.js.map +1 -0
  35. package/dist/core/ZipEntry.d.ts +138 -0
  36. package/dist/core/ZipEntry.d.ts.map +1 -0
  37. package/dist/core/ZipEntry.js +829 -0
  38. package/dist/core/ZipEntry.js.map +1 -0
  39. package/dist/core/Zipkit.d.ts +315 -0
  40. package/dist/core/Zipkit.d.ts.map +1 -0
  41. package/dist/core/Zipkit.js +647 -0
  42. package/dist/core/Zipkit.js.map +1 -0
  43. package/dist/core/ZstdManager.d.ts +56 -0
  44. package/dist/core/ZstdManager.d.ts.map +1 -0
  45. package/dist/core/ZstdManager.js +144 -0
  46. package/dist/core/ZstdManager.js.map +1 -0
  47. package/dist/core/components/HashCalculator.d.ts +138 -0
  48. package/dist/core/components/HashCalculator.d.ts.map +1 -0
  49. package/dist/core/components/HashCalculator.js +360 -0
  50. package/dist/core/components/HashCalculator.js.map +1 -0
  51. package/dist/core/components/Logger.d.ts +73 -0
  52. package/dist/core/components/Logger.d.ts.map +1 -0
  53. package/dist/core/components/Logger.js +156 -0
  54. package/dist/core/components/Logger.js.map +1 -0
  55. package/dist/core/components/ProgressTracker.d.ts +43 -0
  56. package/dist/core/components/ProgressTracker.d.ts.map +1 -0
  57. package/dist/core/components/ProgressTracker.js +112 -0
  58. package/dist/core/components/ProgressTracker.js.map +1 -0
  59. package/dist/core/components/Support.d.ts +64 -0
  60. package/dist/core/components/Support.d.ts.map +1 -0
  61. package/dist/core/components/Support.js +71 -0
  62. package/dist/core/components/Support.js.map +1 -0
  63. package/dist/core/components/Util.d.ts +26 -0
  64. package/dist/core/components/Util.d.ts.map +1 -0
  65. package/dist/core/components/Util.js +95 -0
  66. package/dist/core/components/Util.js.map +1 -0
  67. package/dist/core/constants/Errors.d.ts +52 -0
  68. package/dist/core/constants/Errors.d.ts.map +1 -0
  69. package/dist/core/constants/Errors.js +67 -0
  70. package/dist/core/constants/Errors.js.map +1 -0
  71. package/dist/core/constants/Headers.d.ts +170 -0
  72. package/dist/core/constants/Headers.d.ts.map +1 -0
  73. package/dist/core/constants/Headers.js +194 -0
  74. package/dist/core/constants/Headers.js.map +1 -0
  75. package/dist/core/encryption/Manager.d.ts +58 -0
  76. package/dist/core/encryption/Manager.d.ts.map +1 -0
  77. package/dist/core/encryption/Manager.js +121 -0
  78. package/dist/core/encryption/Manager.js.map +1 -0
  79. package/dist/core/encryption/ZipCrypto.d.ts +172 -0
  80. package/dist/core/encryption/ZipCrypto.d.ts.map +1 -0
  81. package/dist/core/encryption/ZipCrypto.js +554 -0
  82. package/dist/core/encryption/ZipCrypto.js.map +1 -0
  83. package/dist/core/encryption/index.d.ts +9 -0
  84. package/dist/core/encryption/index.d.ts.map +1 -0
  85. package/dist/core/encryption/index.js +17 -0
  86. package/dist/core/encryption/index.js.map +1 -0
  87. package/dist/core/encryption/types.d.ts +29 -0
  88. package/dist/core/encryption/types.d.ts.map +1 -0
  89. package/dist/core/encryption/types.js +12 -0
  90. package/dist/core/encryption/types.js.map +1 -0
  91. package/dist/core/index.d.ts +27 -0
  92. package/dist/core/index.d.ts.map +1 -0
  93. package/dist/core/index.js +59 -0
  94. package/dist/core/index.js.map +1 -0
  95. package/dist/core/version.d.ts +5 -0
  96. package/dist/core/version.d.ts.map +1 -0
  97. package/dist/core/version.js +31 -0
  98. package/dist/core/version.js.map +1 -0
  99. package/dist/index.d.ts +9 -0
  100. package/dist/index.d.ts.map +1 -0
  101. package/dist/index.js +38 -0
  102. package/dist/index.js.map +1 -0
  103. package/dist/node/ZipCompressNode.d.ts +123 -0
  104. package/dist/node/ZipCompressNode.d.ts.map +1 -0
  105. package/dist/node/ZipCompressNode.js +565 -0
  106. package/dist/node/ZipCompressNode.js.map +1 -0
  107. package/dist/node/ZipCopyNode.d.ts +165 -0
  108. package/dist/node/ZipCopyNode.d.ts.map +1 -0
  109. package/dist/node/ZipCopyNode.js +347 -0
  110. package/dist/node/ZipCopyNode.js.map +1 -0
  111. package/dist/node/ZipDecompressNode.d.ts +197 -0
  112. package/dist/node/ZipDecompressNode.d.ts.map +1 -0
  113. package/dist/node/ZipDecompressNode.js +678 -0
  114. package/dist/node/ZipDecompressNode.js.map +1 -0
  115. package/dist/node/ZipkitNode.d.ts +466 -0
  116. package/dist/node/ZipkitNode.d.ts.map +1 -0
  117. package/dist/node/ZipkitNode.js +1426 -0
  118. package/dist/node/ZipkitNode.js.map +1 -0
  119. package/dist/node/index.d.ts +25 -0
  120. package/dist/node/index.d.ts.map +1 -0
  121. package/dist/node/index.js +54 -0
  122. package/dist/node/index.js.map +1 -0
  123. package/dist/types/index.d.ts +45 -0
  124. package/dist/types/index.d.ts.map +1 -0
  125. package/dist/types/index.js +11 -0
  126. package/dist/types/index.js.map +1 -0
  127. package/examples/README.md +261 -0
  128. package/examples/append-data.json +44 -0
  129. package/examples/copy-zip-append.ts +139 -0
  130. package/examples/copy-zip.ts +152 -0
  131. package/examples/create-zip.ts +172 -0
  132. package/examples/extract-zip.ts +118 -0
  133. package/examples/list-zip.ts +161 -0
  134. package/examples/test-files/data.json +116 -0
  135. package/examples/test-files/document.md +80 -0
  136. package/examples/test-files/document.txt +6 -0
  137. package/examples/test-files/file1.txt +48 -0
  138. package/examples/test-files/file2.txt +80 -0
  139. package/examples/tsconfig.json +44 -0
  140. package/package.json +167 -0
  141. package/src/browser/ZipkitBrowser.ts +305 -0
  142. package/src/browser/index.esm.ts +32 -0
  143. package/src/browser/index.ts +19 -0
  144. package/src/core/ZipCompress.ts +370 -0
  145. package/src/core/ZipCopy.ts +434 -0
  146. package/src/core/ZipDecompress.ts +191 -0
  147. package/src/core/ZipEntry.ts +917 -0
  148. package/src/core/Zipkit.ts +794 -0
  149. package/src/core/ZstdManager.ts +165 -0
  150. package/src/core/components/HashCalculator.ts +384 -0
  151. package/src/core/components/Logger.ts +180 -0
  152. package/src/core/components/ProgressTracker.ts +134 -0
  153. package/src/core/components/Support.ts +77 -0
  154. package/src/core/components/Util.ts +91 -0
  155. package/src/core/constants/Errors.ts +78 -0
  156. package/src/core/constants/Headers.ts +205 -0
  157. package/src/core/encryption/Manager.ts +137 -0
  158. package/src/core/encryption/ZipCrypto.ts +650 -0
  159. package/src/core/encryption/index.ts +15 -0
  160. package/src/core/encryption/types.ts +33 -0
  161. package/src/core/index.ts +42 -0
  162. package/src/core/version.ts +33 -0
  163. package/src/index.ts +19 -0
  164. package/src/node/ZipCompressNode.ts +618 -0
  165. package/src/node/ZipCopyNode.ts +437 -0
  166. package/src/node/ZipDecompressNode.ts +793 -0
  167. package/src/node/ZipkitNode.ts +1706 -0
  168. package/src/node/index.ts +40 -0
  169. package/src/types/index.ts +68 -0
  170. package/src/types/modules.d.ts +22 -0
  171. package/src/types/opentimestamps.d.ts +1 -0
@@ -0,0 +1,434 @@
1
+ // ======================================
2
+ // ZipCopy.ts - Efficient ZIP Entry Copying (Buffer-based)
3
+ // Copyright (c) 2025 NeoWare, Inc. All rights reserved.
4
+ // ======================================
5
+ //
6
+ // Efficient ZIP entry copying using ZipEntry instances directly.
7
+ // Leverages existing offsets and sizes to copy raw bytes without
8
+ // unnecessary parsing and reconstruction.
9
+ // Buffer-based implementation for in-memory ZIP operations.
10
+
11
+ import ZipEntry from './ZipEntry';
12
+ import Zipkit from './Zipkit';
13
+ import { LOCAL_HDR, CENTRAL_END, GP_FLAG } from './constants/Headers';
14
+
15
+ /**
16
+ * Options for copying ZIP files
17
+ */
18
+ export interface CopyOptions {
19
+ /** Filter function to determine which entries to copy */
20
+ entryFilter?: (entry: ZipEntry) => boolean;
21
+ /** Sort function to reorder entries before copying */
22
+ entrySorter?: (a: ZipEntry, b: ZipEntry) => number;
23
+ /** Whether to preserve the ZIP file comment (default: true) */
24
+ preserveComments?: boolean;
25
+ }
26
+
27
+ /**
28
+ * Result of copying a ZIP file
29
+ */
30
+ export interface CopyResult {
31
+ /** Information about copied entries */
32
+ entries: Array<{
33
+ filename: string;
34
+ localHeaderOffset: number;
35
+ compressedSize: number;
36
+ }>;
37
+ /** Offset to the start of the central directory */
38
+ centralDirOffset: number;
39
+ /** Total number of entries copied */
40
+ totalEntries: number;
41
+ /** Buffer containing the complete copied ZIP file */
42
+ zipBuffer: Buffer;
43
+ }
44
+
45
+ /**
46
+ * Result of copying only ZIP entry data (no central directory or EOCD).
47
+ * Use with writeCentralDirectoryAndEOCD after optionally appending more entry data
48
+ * to build a ZIP that includes both copied and new entries.
49
+ */
50
+ export interface CopyEntriesOnlyResult {
51
+ /** Buffer containing only entry data (local headers + compressed data); not yet a valid ZIP */
52
+ entryDataBuffer: Buffer;
53
+ /** Offset at which entry data ends; central directory should start here after any new entries are appended (equals entryDataBuffer.length) */
54
+ dataEndOffset: number;
55
+ /** Copied entries with localHdrOffset set for the destination buffer */
56
+ copiedEntries: ZipEntry[];
57
+ }
58
+
59
+ /**
60
+ * Options for finalizing a ZIP (writing central directory and EOCD)
61
+ */
62
+ export interface FinalizeZipOptions {
63
+ /** ZIP file comment (default: empty) */
64
+ zipComment?: string;
65
+ /** Offset at which the central directory will start in the final buffer (required for buffer-based finalization; e.g. entryDataBuffer.length) */
66
+ centralDirOffset?: number;
67
+ }
68
+
69
+ /**
70
+ * Efficient ZIP file copying class (Buffer-based)
71
+ *
72
+ * Uses ZipEntry instances directly to copy entries without decompression/recompression.
73
+ * Supports filtering and reordering entries while maintaining ZIP file validity.
74
+ * Works entirely with buffers for in-memory operations.
75
+ *
76
+ * Entry data and the central directory / EOCD are separated so you can append
77
+ * more entry data before finalizing: use copyZipEntriesOnly, append new entry
78
+ * data to the buffer, then call writeCentralDirectoryAndEOCD with all entries
79
+ * and concatenate to form the final ZIP buffer.
80
+ *
81
+ * @example Full copy (one shot)
82
+ * ```typescript
83
+ * const zipCopy = new ZipCopy(zipkit);
84
+ * const result = await zipCopy.copyZipBuffer(sourceZipBuffer, {
85
+ * entryFilter: (entry) => !entry.filename.startsWith('.'),
86
+ * entrySorter: (a, b) => a.filename.localeCompare(b.filename)
87
+ * });
88
+ * // result.zipBuffer contains the complete copied ZIP file
89
+ * ```
90
+ *
91
+ * @example Copy then append entries before finalizing
92
+ * ```typescript
93
+ * const { entryDataBuffer, dataEndOffset, copiedEntries } = await zipCopy.copyZipEntriesOnly(sourceZipBuffer);
94
+ * // ... append new entry data (local header + data), collect new ZipEntry[] with localHdrOffset set ...
95
+ * const allEntryData = Buffer.concat([entryDataBuffer, newEntryDataBuffer]);
96
+ * const allEntries = [...copiedEntries, ...newEntries];
97
+ * const centralAndEocd = zipCopy.writeCentralDirectoryAndEOCD(allEntries, {
98
+ * zipComment: '',
99
+ * centralDirOffset: allEntryData.length
100
+ * });
101
+ * const zipBuffer = Buffer.concat([allEntryData, centralAndEocd]);
102
+ * ```
103
+ */
104
+ export class ZipCopy {
105
+ private zipkit: Zipkit;
106
+
107
+ /**
108
+ * Creates a new ZipCopy instance
109
+ * @param zipkit - Zipkit instance to use for ZIP operations
110
+ */
111
+ constructor(zipkit: Zipkit) {
112
+ this.zipkit = zipkit;
113
+ }
114
+
115
+ /**
116
+ * Calculate local header size by reading from buffer
117
+ *
118
+ * Reads the first 30 bytes of the local header to get the exact
119
+ * filename and extra field lengths, ensuring accuracy even when
120
+ * local header differs from central directory.
121
+ *
122
+ * @param sourceBuffer - Buffer containing the source ZIP file
123
+ * @param entry - ZipEntry with localHdrOffset
124
+ * @returns Size of the local header in bytes
125
+ */
126
+ private calculateLocalHeaderSize(sourceBuffer: Buffer, entry: ZipEntry): number {
127
+ // Verify offset is within buffer bounds
128
+ if (entry.localHdrOffset + LOCAL_HDR.SIZE > sourceBuffer.length) {
129
+ throw new Error(
130
+ `Local header offset ${entry.localHdrOffset} is beyond buffer size ${sourceBuffer.length} for entry ${entry.filename}`
131
+ );
132
+ }
133
+
134
+ // Read the fixed 30-byte local header
135
+ const headerBuffer = sourceBuffer.subarray(entry.localHdrOffset, entry.localHdrOffset + LOCAL_HDR.SIZE);
136
+
137
+ // Verify signature
138
+ if (headerBuffer.readUInt32LE(0) !== LOCAL_HDR.SIGNATURE) {
139
+ throw new Error(
140
+ `Invalid local file header signature for entry ${entry.filename} at offset ${entry.localHdrOffset}`
141
+ );
142
+ }
143
+
144
+ // Read filename and extra field lengths from the actual header
145
+ const filenameLength = headerBuffer.readUInt16LE(LOCAL_HDR.FNAME_LEN);
146
+ const extraFieldLength = headerBuffer.readUInt16LE(LOCAL_HDR.EXTRA_LEN);
147
+
148
+ // Calculate total local header size
149
+ return LOCAL_HDR.SIZE + filenameLength + extraFieldLength;
150
+ }
151
+
152
+ /**
153
+ * Copy entry bytes directly from source buffer
154
+ *
155
+ * Copies the local header and compressed data as a single operation.
156
+ *
157
+ * Handles various entry types:
158
+ * - Normal entries: [local header][filename][extra][compressed data]
159
+ * - Encrypted entries: compressedSize includes 12-byte encryption header
160
+ * - Data descriptor entries: [local header][filename][extra][data][data descriptor (16 bytes)]
161
+ *
162
+ * For data descriptor entries, the local header has compressed size = 0,
163
+ * but the actual size is in the central directory (entry.compressedSize).
164
+ *
165
+ * @param sourceBuffer - Buffer containing the source ZIP file
166
+ * @param entry - ZipEntry with source offset information
167
+ * @returns Buffer containing the copied entry (local header + compressed data + data descriptor if present)
168
+ */
169
+ private copyEntryBytes(
170
+ sourceBuffer: Buffer,
171
+ entry: ZipEntry
172
+ ): Buffer {
173
+ // Calculate local header size by reading from buffer
174
+ const localHeaderSize = this.calculateLocalHeaderSize(sourceBuffer, entry);
175
+
176
+ // Determine total entry size
177
+ // For data descriptor entries, add 16 bytes for the data descriptor
178
+ const hasDataDescriptor = (entry.bitFlags & GP_FLAG.DATA_DESC) !== 0;
179
+ const totalEntrySize = localHeaderSize + entry.compressedSize + (hasDataDescriptor ? 16 : 0);
180
+
181
+ // Verify we have enough data in the buffer
182
+ if (entry.localHdrOffset + totalEntrySize > sourceBuffer.length) {
183
+ throw new Error(
184
+ `Entry ${entry.filename} extends beyond buffer size: ` +
185
+ `offset ${entry.localHdrOffset} + size ${totalEntrySize} > buffer length ${sourceBuffer.length}`
186
+ );
187
+ }
188
+
189
+ // Extract the entire entry (local header + compressed data + data descriptor if present) from source
190
+ const entryBuffer = sourceBuffer.subarray(entry.localHdrOffset, entry.localHdrOffset + totalEntrySize);
191
+
192
+ // Verify data descriptor signature if present
193
+ if (hasDataDescriptor) {
194
+ const dataDescOffset = localHeaderSize + entry.compressedSize;
195
+ const dataDescSig = entryBuffer.readUInt32LE(dataDescOffset);
196
+ if (dataDescSig !== 0x08074b50) { // DATA_DESCRIPTOR signature
197
+ throw new Error(
198
+ `Invalid data descriptor signature for entry ${entry.filename} ` +
199
+ `(expected 0x08074b50, got 0x${dataDescSig.toString(16)})`
200
+ );
201
+ }
202
+ }
203
+
204
+ // Return a copy of the buffer slice (so modifications don't affect source)
205
+ return Buffer.from(entryBuffer);
206
+ }
207
+
208
+ /**
209
+ * Clone a ZipEntry with a new local header offset
210
+ *
211
+ * Creates a new ZipEntry instance with all properties copied from the source,
212
+ * but with an updated localHdrOffset for the destination buffer.
213
+ *
214
+ * @param entry - Source ZipEntry to clone
215
+ * @param newLocalHdrOffset - New local header offset for the destination
216
+ * @returns New ZipEntry instance with updated offset
217
+ */
218
+ private cloneEntryWithOffset(
219
+ entry: ZipEntry,
220
+ newLocalHdrOffset: number
221
+ ): ZipEntry {
222
+ // Create a new ZipEntry with the same filename and comment
223
+ const cloned = new ZipEntry(entry.filename, entry.comment || null, entry.debug);
224
+
225
+ // Copy all properties
226
+ cloned.verMadeBy = entry.verMadeBy;
227
+ cloned.verExtract = entry.verExtract;
228
+ cloned.bitFlags = entry.bitFlags;
229
+ cloned.cmpMethod = entry.cmpMethod;
230
+ cloned.timeDateDOS = entry.timeDateDOS;
231
+ cloned.crc = entry.crc;
232
+ cloned.compressedSize = entry.compressedSize;
233
+ cloned.uncompressedSize = entry.uncompressedSize;
234
+ cloned.volNumber = entry.volNumber;
235
+ cloned.intFileAttr = entry.intFileAttr;
236
+ cloned.extFileAttr = entry.extFileAttr;
237
+ cloned.localHdrOffset = newLocalHdrOffset; // Update offset
238
+
239
+ // Copy extra field if present
240
+ if (entry.extraField) {
241
+ cloned.extraField = Buffer.from(entry.extraField);
242
+ }
243
+
244
+ // Copy metadata
245
+ cloned.isEncrypted = entry.isEncrypted;
246
+ cloned.isStrongEncrypt = entry.isStrongEncrypt;
247
+ cloned.isDirectory = entry.isDirectory;
248
+ cloned.isMetaData = entry.isMetaData;
249
+ cloned.isUpdated = entry.isUpdated;
250
+
251
+ // Copy platform-specific data
252
+ cloned.platform = entry.platform;
253
+ cloned.universalTime = entry.universalTime;
254
+ cloned.uid = entry.uid;
255
+ cloned.gid = entry.gid;
256
+ cloned.sha256 = entry.sha256;
257
+
258
+ // Copy symlink data
259
+ cloned.isSymlink = entry.isSymlink;
260
+ cloned.linkTarget = entry.linkTarget;
261
+
262
+ // Copy hardlink data
263
+ cloned.isHardLink = entry.isHardLink;
264
+ cloned.originalEntry = entry.originalEntry;
265
+ cloned.inode = entry.inode;
266
+
267
+ return cloned;
268
+ }
269
+
270
+ /**
271
+ * Build central directory and End of Central Directory buffer.
272
+ * Used internally by copyZipBuffer and by writeCentralDirectoryAndEOCD.
273
+ */
274
+ private buildCentralDirectoryAndEOCDBuffer(
275
+ entries: ZipEntry[],
276
+ zipComment: string,
277
+ centralDirOffset: number
278
+ ): Buffer {
279
+ const centralDirChunks: Buffer[] = [];
280
+ for (const entry of entries) {
281
+ centralDirChunks.push(entry.centralDirEntry());
282
+ }
283
+ const centralDirBuffer = Buffer.concat(centralDirChunks);
284
+ const centralDirSize = centralDirBuffer.length;
285
+
286
+ const commentBytes = Buffer.from(zipComment, 'utf8');
287
+ const commentLength = Math.min(commentBytes.length, 0xffff);
288
+
289
+ const eocdBuffer = Buffer.alloc(22 + commentLength);
290
+ let pos = 0;
291
+ eocdBuffer.writeUInt32LE(CENTRAL_END.SIGNATURE, pos);
292
+ pos += 4;
293
+ eocdBuffer.writeUInt16LE(0, pos);
294
+ pos += 2;
295
+ eocdBuffer.writeUInt16LE(0, pos);
296
+ pos += 2;
297
+ eocdBuffer.writeUInt16LE(entries.length, pos);
298
+ pos += 2;
299
+ eocdBuffer.writeUInt16LE(entries.length, pos);
300
+ pos += 2;
301
+ eocdBuffer.writeUInt32LE(centralDirSize, pos);
302
+ pos += 4;
303
+ eocdBuffer.writeUInt32LE(centralDirOffset, pos);
304
+ pos += 4;
305
+ eocdBuffer.writeUInt16LE(commentLength, pos);
306
+ pos += 2;
307
+ if (commentLength > 0) {
308
+ commentBytes.copy(eocdBuffer, pos, 0, commentLength);
309
+ }
310
+
311
+ return Buffer.concat([centralDirBuffer, eocdBuffer]);
312
+ }
313
+
314
+ /**
315
+ * Copy only ZIP entry data from the source buffer (no central directory or EOCD).
316
+ * Use when you want to append more entry data before finalizing. Then call
317
+ * writeCentralDirectoryAndEOCD with all entries (copied + new) and concatenate
318
+ * to form the final ZIP buffer.
319
+ *
320
+ * @param sourceZipBuffer - Buffer containing the source ZIP file
321
+ * @param options - Optional copy options (filtering, sorting)
322
+ * @returns Result with entryDataBuffer and copiedEntries for use when appending and finalizing
323
+ */
324
+ async copyZipEntriesOnly(
325
+ sourceZipBuffer: Buffer,
326
+ options?: CopyOptions
327
+ ): Promise<CopyEntriesOnlyResult> {
328
+ const sourceEntries = this.zipkit.loadZip(sourceZipBuffer);
329
+
330
+ let entriesToCopy = options?.entryFilter
331
+ ? sourceEntries.filter(options.entryFilter)
332
+ : sourceEntries;
333
+
334
+ if (options?.entrySorter) {
335
+ entriesToCopy = [...entriesToCopy].sort(options.entrySorter);
336
+ }
337
+
338
+ if (entriesToCopy.length === 0) {
339
+ throw new Error('No entries to copy after filtering');
340
+ }
341
+
342
+ const outputChunks: Buffer[] = [];
343
+ let destOffset = 0;
344
+ const copiedEntries: ZipEntry[] = [];
345
+
346
+ for (const sourceEntry of entriesToCopy) {
347
+ const newLocalHdrOffset = destOffset;
348
+ const entryBuffer = this.copyEntryBytes(sourceZipBuffer, sourceEntry);
349
+ outputChunks.push(entryBuffer);
350
+ destOffset += entryBuffer.length;
351
+ const clonedEntry = this.cloneEntryWithOffset(sourceEntry, newLocalHdrOffset);
352
+ copiedEntries.push(clonedEntry);
353
+ }
354
+
355
+ const entryDataBuffer = Buffer.concat(outputChunks);
356
+
357
+ return {
358
+ entryDataBuffer,
359
+ dataEndOffset: entryDataBuffer.length,
360
+ copiedEntries,
361
+ };
362
+ }
363
+
364
+ /**
365
+ * Build central directory and End of Central Directory buffer for the given entries.
366
+ * Concatenate this with entry data (e.g. from copyZipEntriesOnly + any appended entries)
367
+ * to form a valid ZIP buffer. When concatenating, pass centralDirOffset equal to the
368
+ * length of the entry data buffer that will precede this (so EOCD has the correct offset).
369
+ *
370
+ * @param entries - All entries in order (copied + any new), each with localHdrOffset set
371
+ * @param options - Optional finalize options (zipComment; centralDirOffset for correct EOCD when concatenating)
372
+ * @returns Buffer containing central directory + EOCD
373
+ */
374
+ writeCentralDirectoryAndEOCD(
375
+ entries: ZipEntry[],
376
+ options?: FinalizeZipOptions
377
+ ): Buffer {
378
+ if (entries.length === 0) {
379
+ throw new Error('At least one entry is required to finalize');
380
+ }
381
+
382
+ const zipComment = options?.zipComment ?? '';
383
+ const centralDirOffset = options?.centralDirOffset ?? 0;
384
+ const result: Buffer = this.buildCentralDirectoryAndEOCDBuffer(
385
+ entries,
386
+ zipComment,
387
+ centralDirOffset
388
+ );
389
+ return result;
390
+ }
391
+
392
+ /**
393
+ * Copy ZIP file entries efficiently from buffer to buffer
394
+ *
395
+ * Main method that copies entries from source ZIP buffer to destination ZIP buffer.
396
+ * Uses ZipEntry instances directly to avoid unnecessary parsing.
397
+ *
398
+ * @param sourceZipBuffer - Buffer containing the source ZIP file
399
+ * @param options - Optional copy options (filtering, sorting, etc.)
400
+ * @returns Copy result with entry information and the complete ZIP buffer
401
+ */
402
+ async copyZipBuffer(
403
+ sourceZipBuffer: Buffer,
404
+ options?: CopyOptions
405
+ ): Promise<CopyResult> {
406
+ const { entryDataBuffer, dataEndOffset, copiedEntries } = await this.copyZipEntriesOnly(
407
+ sourceZipBuffer,
408
+ options
409
+ );
410
+
411
+ const zipComment =
412
+ options?.preserveComments !== false && this.zipkit.getZipComment()
413
+ ? this.zipkit.getZipComment()!
414
+ : '';
415
+
416
+ const centralAndEocd = this.writeCentralDirectoryAndEOCD(copiedEntries, {
417
+ zipComment,
418
+ centralDirOffset: dataEndOffset,
419
+ });
420
+
421
+ const zipBuffer = Buffer.concat([entryDataBuffer, centralAndEocd]);
422
+
423
+ return {
424
+ entries: copiedEntries.map(entry => ({
425
+ filename: entry.filename,
426
+ localHeaderOffset: entry.localHdrOffset,
427
+ compressedSize: entry.compressedSize,
428
+ })),
429
+ centralDirOffset: dataEndOffset,
430
+ totalEntries: copiedEntries.length,
431
+ zipBuffer,
432
+ };
433
+ }
434
+ }
@@ -0,0 +1,191 @@
1
+ // ======================================
2
+ // ZipDecompress.ts - Decompression Module
3
+ // Copyright (c) 2025 NeoWare, Inc. All rights reserved.
4
+ // ======================================
5
+ //
6
+ // LOGGING INSTRUCTIONS:
7
+ // ---------------------
8
+ // To enable/disable logging, set loggingEnabled to true/false in the class:
9
+ // private static loggingEnabled: boolean = true; // Enable logging
10
+ // private static loggingEnabled: boolean = false; // Disable logging
11
+ //
12
+ // Logging respects the global Logger level (debug, info, warn, error, silent).
13
+ // Logger level is automatically set to 'debug' when loggingEnabled is true.
14
+ //
15
+
16
+ const pako = require('pako');
17
+ import { ZstdManager } from './ZstdManager';
18
+ import Zipkit from './Zipkit';
19
+ import { Logger } from './components/Logger';
20
+ import ZipEntry from './ZipEntry';
21
+ import Errors from './constants/Errors';
22
+ import { CMP_METHOD } from './constants/Headers';
23
+ import { ZipCrypto } from './encryption/ZipCrypto';
24
+
25
+ export interface DecompressionResult {
26
+ success: boolean;
27
+ data?: Buffer;
28
+ error?: string;
29
+ }
30
+
31
+ export interface DecompressionOptions {
32
+ method: number; // Compression method code (0=stored, 8=deflate, 93=zstd)
33
+ debug?: boolean; // Enable debug logging
34
+ }
35
+
36
+ /**
37
+ * ZipDecompress - Unified Decompression Module (Internal to Zipkit)
38
+ * Consolidates decompression functionality from ZipCompress
39
+ * Supports deflate, zstd, and stored compression methods with chunked processing
40
+ *
41
+ * This class is internal to Zipkit and should not be used directly.
42
+ * Use Zipkit methods instead.
43
+ */
44
+ class ZipDecompress {
45
+ private zipkit: Zipkit;
46
+ private debug: boolean;
47
+
48
+ // Class-level logging control - set to true to enable logging
49
+ private static loggingEnabled: boolean = false;
50
+
51
+ /**
52
+ * Internal logging method - only logs if class logging is enabled
53
+ */
54
+ private log(...args: any[]): void {
55
+ if (ZipDecompress.loggingEnabled) {
56
+ Logger.debug(`[ZipDecompress]`, ...args);
57
+ }
58
+ }
59
+
60
+ constructor(zipkit: Zipkit) {
61
+ this.zipkit = zipkit;
62
+ // Debug disabled by default
63
+ this.debug = false;
64
+ // If logging is enabled, ensure Logger level is set to debug
65
+ if (ZipDecompress.loggingEnabled) {
66
+ Logger.setLevel('debug');
67
+ }
68
+ }
69
+
70
+ /**
71
+ * Extract file data (Buffer-based ZIP only)
72
+ * Public method that validates buffer mode and extracts entry
73
+ *
74
+ * @param entry ZIP entry to extract
75
+ * @param skipHashCheck Skip hash verification (CRC-32 or SHA-256)
76
+ * @returns Promise resolving to extracted data as Buffer, or null if failed
77
+ * @throws Error if not a Buffer-based ZIP
78
+ */
79
+ async extract(entry: ZipEntry, skipHashCheck?: boolean): Promise<Buffer | null> {
80
+ if (!this.zipkit.hasInBuffer()) {
81
+ throw new Error('extract() requires Buffer-based ZIP. Use ZipkitNode.extractToFile() for file-based ZIP or call loadZip() first.');
82
+ }
83
+
84
+ this.log(`extract() called for entry: ${entry.filename}, method: ${entry.cmpMethod}, skipHashCheck: ${skipHashCheck}`);
85
+
86
+ const buffer = this.zipkit.ensureBuffer();
87
+
88
+ // Decrypt if needed using password on zipkit instance
89
+ let fdata = this.zipkit.parseLocalHeader(entry, buffer);
90
+
91
+ if ((entry as any).isEncrypted && (this.zipkit as any)?.password) {
92
+ this.log(`Starting in-memory decryption for entry: ${entry.filename}`);
93
+
94
+ // Use ZipCrypto's decryptBuffer method which handles all the header parsing and decryption
95
+ const zipCrypto = new ZipCrypto();
96
+
97
+ fdata = zipCrypto.decryptBuffer(entry, buffer, fdata, (this.zipkit as any).password);
98
+
99
+ this.log(`Decryption successful, decrypted compressed data length: ${fdata.length}`);
100
+ }
101
+
102
+ if (fdata.length === 0) {
103
+ return null;
104
+ }
105
+
106
+ // Use the unCompress method
107
+ return this.unCompress(fdata, entry, skipHashCheck);
108
+ }
109
+
110
+
111
+
112
+ /**
113
+ * Inflate data using pako (internal use only)
114
+ */
115
+ private inflate(data: Buffer): Buffer {
116
+ this.log(`inflate() called with ${data.length} bytes`);
117
+ const result = pako.inflateRaw(data);
118
+ return Buffer.from(result.buffer, result.byteOffset, result.byteLength);
119
+ }
120
+
121
+
122
+ /**
123
+ * Synchronous zstd decompress method for in-memory mode
124
+ * ZSTD codec is guaranteed to be initialized via factory method
125
+ * Internal method only
126
+ */
127
+ private async zstdDecompressSync(data: Buffer): Promise<Buffer> {
128
+ this.log(`zstdDecompressSync() called with ${data.length} bytes`);
129
+
130
+ try {
131
+ // Use global ZstdManager for decompression (handles queuing and initialization)
132
+ const decompressed = await ZstdManager.decompress(data);
133
+ this.log(`ZSTD decompression successful: ${data.length} bytes -> ${decompressed.length} bytes`);
134
+ return Buffer.from(decompressed);
135
+ } catch (error) {
136
+ this.log(`ZSTD decompression failed: ${error}`);
137
+ throw new Error(`ZSTD decompression failed: ${error instanceof Error ? error.message : String(error)}`);
138
+ }
139
+ }
140
+
141
+ /**
142
+ * Uncompress compressed data buffer (synchronous for in-memory mode)
143
+ * Handles decompression and hash verification
144
+ * Internal method only
145
+ */
146
+ private async unCompress(
147
+ compressedData: Buffer,
148
+ entry: ZipEntry,
149
+ skipHashCheck?: boolean
150
+ ): Promise<Buffer> {
151
+ this.log(`unCompress() called for entry: ${entry.filename}, method: ${entry.cmpMethod}, data length: ${compressedData.length}`);
152
+
153
+ if (compressedData.length === 0) {
154
+ return Buffer.alloc(0);
155
+ }
156
+
157
+ let outBuf: Buffer;
158
+ if (entry.cmpMethod === CMP_METHOD.STORED) {
159
+ outBuf = compressedData;
160
+ } else if (entry.cmpMethod === CMP_METHOD.DEFLATED) {
161
+ // Use synchronous inflate for deflate
162
+ outBuf = this.inflate(compressedData);
163
+ } else if (entry.cmpMethod === CMP_METHOD.ZSTD) {
164
+ // Use ZSTD decompression (now async with ZstdManager)
165
+ outBuf = await this.zstdDecompressSync(compressedData);
166
+ } else {
167
+ throw new Error(`Unsupported compression method: ${entry.cmpMethod}`);
168
+ }
169
+
170
+ // Verify hash
171
+ if (!skipHashCheck) {
172
+ if (entry.sha256) {
173
+ const isValid = this.zipkit.testSHA256(entry, outBuf);
174
+ if (!isValid) {
175
+ throw new Error(Errors.INVALID_SHA256);
176
+ }
177
+ } else {
178
+ const isValid = this.zipkit.testCRC32(entry, outBuf);
179
+ if (!isValid) {
180
+ throw new Error(Errors.INVALID_CRC);
181
+ }
182
+ }
183
+ }
184
+
185
+ return outBuf;
186
+ }
187
+
188
+ }
189
+
190
+ // Default export for internal use by Zipkit only
191
+ export default ZipDecompress;