UNPKG

14.3 kBTypeScriptView Raw
1/// <reference types="node" />
2
3import * as FS from "fs";
4import { Constants } from "./util";
5
6declare class AdmZip {
7 /**
8 * @param fileNameOrRawData If provided, reads an existing archive. Otherwise creates a new, empty archive.
9 * @param options Options when initializing the ZIP file
10 */
11 constructor(fileNameOrRawData?: string | Buffer, options?: Partial<AdmZip.InitOptions>);
12 /**
13 * Extracts the given entry from the archive and returns the content as a Buffer object
14 * @param entry ZipEntry object or String with the full path of the entry
15 * @param pass Password used for decrypting the file
16 * @return Buffer or Null in case of error
17 */
18 readFile(entry: string | AdmZip.IZipEntry, pass?: string | Buffer): Buffer | null;
19 /**
20 * Asynchronous `readFile`.
21 * @param entry The full path of the entry or a `IZipEntry` object.
22 * @param callback Called with a `Buffer` or `null` in case of error.
23 */
24 readFileAsync(entry: string | AdmZip.IZipEntry, callback: (data: Buffer | null, err: string) => void): void;
25 /**
26 * Extracts the given entry from the archive and returns the content as
27 * plain text in the given encoding.
28 * @param entry The full path of the entry or a `IZipEntry` object.
29 * @param encoding If no encoding is specified `"utf8"` is used.
30 */
31 readAsText(fileName: string | AdmZip.IZipEntry, encoding?: string): string;
32 /**
33 * Asynchronous `readAsText`.
34 * @param entry The full path of the entry or a `IZipEntry` object.
35 * @param callback Called with the resulting string.
36 * @param encoding If no encoding is specified `"utf8"` is used.
37 */
38 readAsTextAsync(
39 fileName: string | AdmZip.IZipEntry,
40 callback: (data: string, err: string) => void,
41 encoding?: string,
42 ): void;
43 /**
44 * Remove the entry from the file or the entry and all its nested directories
45 * and files if the given entry is a directory.
46 * @param entry The full path of the entry or a `IZipEntry` object.
47 */
48 deleteFile(entry: string | AdmZip.IZipEntry): void;
49 /**
50 * Adds a comment to the zip. The zip must be rewritten after
51 * adding the comment.
52 * @param comment Content of the comment.
53 */
54 addZipComment(comment: string): void;
55 /**
56 * @return The zip comment.
57 */
58 getZipComment(): string;
59 /**
60 * Adds a comment to a specified file or `IZipEntry`. The zip must be rewritten after
61 * adding the comment.
62 * The comment cannot exceed 65535 characters in length.
63 * @param entry The full path of the entry or a `IZipEntry` object.
64 * @param comment The comment to add to the entry.
65 */
66 addZipEntryComment(entry: string | AdmZip.IZipEntry, comment: string): void;
67 /**
68 * Returns the comment of the specified entry.
69 * @param entry The full path of the entry or a `IZipEntry` object.
70 * @return The comment of the specified entry.
71 */
72 getZipEntryComment(entry: string | AdmZip.IZipEntry): string;
73 /**
74 * Updates the content of an existing entry inside the archive. The zip
75 * must be rewritten after updating the content.
76 * @param entry The full path of the entry or a `IZipEntry` object.
77 * @param content The entry's new contents.
78 */
79 updateFile(entry: string | AdmZip.IZipEntry, content: Buffer): void;
80 /**
81 * Adds a file from the disk to the archive.
82 * @param localPath Path to a file on disk.
83 * @param zipPath Path to a directory in the archive. Defaults to the empty
84 * string.
85 * @param zipName Name for the file.
86 * @param comment Comment to be attached to the file
87 */
88 addLocalFile(localPath: string, zipPath?: string, zipName?: string, comment?: string): void;
89 /**
90 * Adds a local directory and all its nested files and directories to the
91 * archive.
92 * @param localPath Path to a folder on disk.
93 * @param zipPath Path to a folder in the archive. Default: `""`.
94 * @param filter RegExp or Function if files match will be included.
95 */
96 addLocalFolder(localPath: string, zipPath?: string, filter?: RegExp | ((filename: string) => boolean)): void;
97 /**
98 * Asynchronous addLocalFile
99 * @param localPath
100 * @param callback
101 * @param zipPath optional path inside zip
102 * @param filter optional RegExp or Function if files match will
103 * be included.
104 */
105 addLocalFolderAsync(
106 localPath: string,
107 callback: (success?: boolean, err?: string) => void,
108 zipPath?: string,
109 filter?: RegExp | ((filename: string) => boolean),
110 ): void;
111 /**
112 * @param localPath - path where files will be extracted
113 * @param props - optional properties
114 * @param props.zipPath - optional path inside zip
115 * @param props.filter - RegExp or Function if files match will be included.
116 */
117 addLocalFolderPromise(
118 localPath: string,
119 props: { zipPath?: string; filter?: RegExp | ((filename: string) => boolean) },
120 ): Promise<void>;
121 /**
122 * Allows you to create a entry (file or directory) in the zip file.
123 * If you want to create a directory the `entryName` must end in `"/"` and a `null`
124 * buffer should be provided.
125 * @param entryName Entry path.
126 * @param content Content to add to the entry; must be a 0-length buffer
127 * for a directory.
128 * @param comment Comment to add to the entry.
129 * @param attr Attribute to add to the entry.
130 * @return The entry corresponding to one which was just added.
131 */
132 addFile(entryName: string, content: Buffer, comment?: string, attr?: number): AdmZip.IZipEntry;
133 /**
134 * Returns an array of `IZipEntry` objects representing the files and folders
135 * inside the archive.
136 */
137 getEntries(): AdmZip.IZipEntry[];
138 /**
139 * Returns a `IZipEntry` object representing the file or folder specified by `name`.
140 * @param name Name of the file or folder to retrieve.
141 * @return The entry corresponding to the `name`.
142 */
143 getEntry(name: string): AdmZip.IZipEntry | null;
144 /**
145 * Returns the number of entries in the ZIP
146 * @return The amount of entries in the ZIP
147 */
148 getEntryCount(): number;
149 /**
150 * Loop through each entry in the ZIP
151 * @param callback The callback that receives each individual entry
152 */
153 forEach(callback: (entry: AdmZip.IZipEntry) => void): void;
154 /**
155 * Extracts the given entry to the given `targetPath`.
156 * If the entry is a directory inside the archive, the entire directory and
157 * its subdirectories will be extracted.
158 * @param entry The full path of the entry or a `IZipEntry` object.
159 * @param targetPath Target folder where to write the file.
160 * @param maintainEntryPath If maintainEntryPath is `true` and the entry is
161 * inside a folder, the entry folder will be created in `targetPath` as
162 * well. Default: `true`.
163 * @param overwrite If the file already exists at the target path, the file
164 * will be overwriten if this is `true`. Default: `false`.
165 * @param keepOriginalPermission The file will be set as the permission from
166 * the entry if this is true. Default: `false`.
167 * @param outFileName String If set will override the filename of the
168 * extracted file (Only works if the entry is a file)
169 * @return Boolean
170 */
171 extractEntryTo(
172 entryPath: string | AdmZip.IZipEntry,
173 targetPath: string,
174 maintainEntryPath?: boolean,
175 overwrite?: boolean,
176 keepOriginalPermission?: boolean,
177 outFileName?: string,
178 ): boolean;
179 /**
180 * Test the archive
181 * @param password The password for the archive
182 */
183 test(password?: string | Buffer): boolean;
184 /**
185 * Extracts the entire archive to the given location.
186 * @param targetPath Target location.
187 * @param overwrite If the file already exists at the target path, the file
188 * will be overwriten if this is `true`. Default: `false`.
189 * @param keepOriginalPermission The file will be set as the permission from
190 * the entry if this is true. Default: `false`.
191 * @param password The password for the archive
192 */
193 extractAllTo(
194 targetPath: string,
195 overwrite?: boolean,
196 keepOriginalPermission?: boolean,
197 password?: string | Buffer,
198 ): void;
199 /**
200 * Extracts the entire archive to the given location.
201 * @param targetPath Target location.
202 * @param overwrite If the file already exists at the target path, the file
203 * will be overwriten if this is `true`. Default: `false`.
204 * @param keepOriginalPermission The file will be set as the permission from
205 * the entry if this is true. Default: `false`.
206 * @param callback The callback function will be called after extraction.
207 */
208 extractAllToAsync(
209 targetPath: string,
210 overwrite?: boolean,
211 keepOriginalPermission?: boolean,
212 callback?: (error?: Error) => void,
213 ): void;
214 /**
215 * Writes the newly created zip file to disk at the specified location or
216 * if a zip was opened and no `targetFileName` is provided, it will
217 * overwrite the opened zip.
218 */
219 writeZip(targetFileName?: string, callback?: (error: Error | null) => void): void;
220 /**
221 * Writes the newly created zip file to disk at the specified location or
222 * if a zip was opened and no `targetFileName` is provided, it will
223 * overwrite the opened zip.
224 */
225 writeZipPromise(targetFileName?: string, props?: { overwrite?: boolean; perm?: number }): Promise<boolean>;
226 /**
227 * Returns the content of the entire zip file.
228 */
229 toBuffer(): Buffer;
230 /**
231 * Asynchronously returns the content of the entire zip file.
232 * @param onSuccess called with the content of the zip file, once it has been generated.
233 * @param onFail unused.
234 * @param onItemStart called before an entry is compressed.
235 * @param onItemEnd called after an entry is compressed.
236 */
237 toBuffer(
238 onSuccess: (buffer: Buffer) => void,
239 onFail?: (...args: any[]) => void,
240 onItemStart?: (name: string) => void,
241 onItemEnd?: (name: string) => void,
242 ): void;
243 /**
244 * Asynchronously convert the promise to a Buffer
245 */
246 toBufferPromise(): Promise<Buffer>;
247}
248
249declare namespace AdmZip {
250 /**
251 * The `IZipEntry` is more than a structure representing the entry inside the
252 * zip file. Beside the normal attributes and headers a entry can have, the
253 * class contains a reference to the part of the file where the compressed
254 * data resides and decompresses it when requested. It also compresses the
255 * data and creates the headers required to write in the zip file.
256 */
257 // disable warning about the I-prefix in interface name to prevent breaking stuff for users without a major bump
258 // eslint-disable-next-line @typescript-eslint/naming-convention
259 interface IZipEntry {
260 /**
261 * Represents the full name and path of the file
262 */
263 entryName: string;
264 readonly rawEntryName: Buffer;
265 /**
266 * Extra data associated with this entry.
267 */
268 extra: Buffer;
269 /**
270 * Entry comment.
271 */
272 comment: string;
273 readonly name: string;
274 /**
275 * Read-Only property that indicates the type of the entry.
276 */
277 readonly isDirectory: boolean;
278 /**
279 * Get the header associated with this ZipEntry.
280 */
281 readonly header: EntryHeader;
282 attr: number;
283 /**
284 * Retrieve the compressed data for this entry. Note that this may trigger
285 * compression if any properties were modified.
286 */
287 getCompressedData(): Buffer;
288 /**
289 * Asynchronously retrieve the compressed data for this entry. Note that
290 * this may trigger compression if any properties were modified.
291 */
292 getCompressedDataAsync(callback: (data: Buffer) => void): void;
293 /**
294 * Set the (uncompressed) data to be associated with this entry.
295 */
296 setData(value: string | Buffer): void;
297 /**
298 * Get the decompressed data associated with this entry.
299 */
300 getData(): Buffer;
301 /**
302 * Asynchronously get the decompressed data associated with this entry.
303 */
304 getDataAsync(callback: (data: Buffer, err: string) => void): void;
305 /**
306 * Returns the CEN Entry Header to be written to the output zip file, plus
307 * the extra data and the entry comment.
308 */
309 packHeader(): Buffer;
310 /**
311 * Returns a nicely formatted string with the most important properties of
312 * the ZipEntry.
313 */
314 toString(): string;
315 }
316
317 interface EntryHeader {
318 made: number;
319 version: number;
320 flags: number;
321 method: number;
322 time: Date;
323 crc: number;
324 compressedSize: number;
325 size: number;
326 fileNameLength: number;
327 extraLength: number;
328 commentLength: number;
329 diskNumStart: number;
330 inAttr: number;
331 attr: number;
332 offset: number;
333 readonly encripted: boolean;
334 readonly entryHeaderSize: number;
335 readonly realDataOffset: number;
336 readonly dataHeader: DataHeader;
337 loadDataHeaderFromBinary(data: Buffer): void;
338 loadFromBinary(data: Buffer): void;
339 dataHeaderToBinary(): Buffer;
340 entryHeaderToBinary(): Buffer;
341 toString(): string;
342 }
343
344 interface DataHeader {
345 version: number;
346 flags: number;
347 method: number;
348 time: number;
349 crc: number;
350 compressedSize: number;
351 size: number;
352 fnameLen: number;
353 extraLen: number;
354 }
355
356 interface InitOptions {
357 /* If true it disables files sorting */
358 noSort: boolean;
359 /* Read entries during load (initial loading may be slower) */
360 readEntries: boolean;
361 /* Read method */
362 method: (typeof Constants)[keyof typeof Constants] | number;
363 /* file system */
364 fs: null | typeof FS;
365 }
366}
367
368export = AdmZip;