UNPKG

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