UNPKG

11.4 kBTypeScriptView Raw
1// Type definitions for adm-zip 0.4
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// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
8
9/// <reference types="node" />
10
11declare class AdmZip {
12 /**
13 * @param fileNameOrRawData If provided, reads an existing archive. Otherwise creates a new, empty archive.
14 */
15 constructor(fileNameOrRawData?: string | Buffer);
16 /**
17 * Extracts the given entry from the archive and returns the content.
18 * @param entry The full path of the entry or a `IZipEntry` object.
19 * @return `Buffer` or `null` in case of error.
20 */
21 readFile(entry: string | AdmZip.IZipEntry): Buffer | null;
22 /**
23 * Asynchronous `readFile`.
24 * @param entry The full path of the entry or a `IZipEntry` object.
25 * @param callback Called with a `Buffer` or `null` in case of error.
26 */
27 readFileAsync(entry: string | AdmZip.IZipEntry, callback: (data: Buffer | null, err: string) => any): void;
28 /**
29 * Extracts the given entry from the archive and returns the content as
30 * plain text in the given encoding.
31 * @param entry The full path of the entry or a `IZipEntry` object.
32 * @param encoding If no encoding is specified `"utf8"` is used.
33 */
34 readAsText(fileName: string | AdmZip.IZipEntry, encoding?: string): string;
35 /**
36 * Asynchronous `readAsText`.
37 * @param entry The full path of the entry or a `IZipEntry` object.
38 * @param callback Called with the resulting string.
39 * @param encoding If no encoding is specified `"utf8"` is used.
40 */
41 readAsTextAsync(fileName: string | AdmZip.IZipEntry, callback: (data: string, err: string) => any, encoding?: string): void;
42 /**
43 * Remove the entry from the file or the entry and all its nested directories
44 * and files if the given entry is a directory.
45 * @param entry The full path of the entry or a `IZipEntry` object.
46 */
47 deleteFile(entry: string | AdmZip.IZipEntry): void;
48 /**
49 * Adds a comment to the zip. The zip must be rewritten after
50 * adding the comment.
51 * @param comment Content of the comment.
52 */
53 addZipComment(comment: string): void;
54 /**
55 * @return The zip comment.
56 */
57 getZipComment(): string;
58 /**
59 * Adds a comment to a specified file or `IZipEntry`. The zip must be rewritten after
60 * adding the comment.
61 * The comment cannot exceed 65535 characters in length.
62 * @param entry The full path of the entry or a `IZipEntry` object.
63 * @param comment The comment to add to the entry.
64 */
65 addZipEntryComment(entry: string | AdmZip.IZipEntry, comment: string): void;
66 /**
67 * Returns the comment of the specified entry.
68 * @param entry The full path of the entry or a `IZipEntry` object.
69 * @return The comment of the specified entry.
70 */
71 getZipEntryComment(entry: string | AdmZip.IZipEntry): string;
72 /**
73 * Updates the content of an existing entry inside the archive. The zip
74 * must be rewritten after updating the content.
75 * @param entry The full path of the entry or a `IZipEntry` object.
76 * @param content The entry's new contents.
77 */
78 updateFile(entry: string | AdmZip.IZipEntry, content: Buffer): void;
79 /**
80 * Adds a file from the disk to the archive.
81 * @param localPath Path to a file on disk.
82 * @param zipPath Path to a directory in the archive. Defaults to the empty
83 * string.
84 * @param zipName Name for the file.
85 */
86 addLocalFile(localPath: string, zipPath?: string, zipName?: string): void;
87 /**
88 * Adds a local directory and all its nested files and directories to the
89 * archive.
90 * @param localPath Path to a folder on disk.
91 * @param zipPath Path to a folder in the archive. Default: `""`.
92 * @param filter RegExp or Function if files match will be included.
93 */
94 addLocalFolder(localPath: string, zipPath?: string, filter?: RegExp | ((filename: string) => boolean)): void;
95 /**
96 * Allows you to create a entry (file or directory) in the zip file.
97 * If you want to create a directory the `entryName` must end in `"/"` and a `null`
98 * buffer should be provided.
99 * @param entryName Entry path.
100 * @param content Content to add to the entry; must be a 0-length buffer
101 * for a directory.
102 * @param comment Comment to add to the entry.
103 * @param attr Attribute to add to the entry.
104 */
105 addFile(entryName: string, data: Buffer, comment?: string, attr?: number): void;
106 /**
107 * Returns an array of `IZipEntry` objects representing the files and folders
108 * inside the archive.
109 */
110 getEntries(): AdmZip.IZipEntry[];
111 /**
112 * Returns a `IZipEntry` object representing the file or folder specified by `name`.
113 * @param name Name of the file or folder to retrieve.
114 * @return The entry corresponding to the `name`.
115 */
116 getEntry(name: string): AdmZip.IZipEntry | null;
117 /**
118 * Extracts the given entry to the given `targetPath`.
119 * If the entry is a directory inside the archive, the entire directory and
120 * its subdirectories will be extracted.
121 * @param entry The full path of the entry or a `IZipEntry` object.
122 * @param targetPath Target folder where to write the file.
123 * @param maintainEntryPath If maintainEntryPath is `true` and the entry is
124 * inside a folder, the entry folder will be created in `targetPath` as
125 * well. Default: `true`.
126 * @param overwrite If the file already exists at the target path, the file
127 * will be overwriten if this is `true`. Default: `false`.
128 */
129 extractEntryTo(
130 entryPath: string | AdmZip.IZipEntry,
131 targetPath: string,
132 maintainEntryPath?: boolean,
133 overwrite?: boolean,
134 ): boolean;
135 /**
136 * Extracts the entire archive to the given location.
137 * @param targetPath Target location.
138 * @param overwrite If the file already exists at the target path, the file
139 * will be overwriten if this is `true`. Default: `false`.
140 */
141 extractAllTo(targetPath: string, overwrite?: boolean): void;
142 /**
143 * Extracts the entire archive to the given location.
144 * @param targetPath Target location.
145 * @param overwrite If the file already exists at the target path, the file
146 * will be overwriten if this is `true`. Default: `false`.
147 * @param callback The callback function will be called after extraction.
148 */
149 extractAllToAsync(targetPath: string, overwrite?: boolean, callback?: (error: Error) => void): void;
150 /**
151 * Writes the newly created zip file to disk at the specified location or
152 * if a zip was opened and no `targetFileName` is provided, it will
153 * overwrite the opened zip.
154 */
155 writeZip(targetFileName?: string, callback?: (error: Error | null) => void): void;
156 /**
157 * Returns the content of the entire zip file.
158 */
159 toBuffer(): Buffer;
160 /**
161 * Asynchronously returns the content of the entire zip file.
162 * @param onSuccess called with the content of the zip file, once it has been generated.
163 * @param onFail unused.
164 * @param onItemStart called before an entry is compressed.
165 * @param onItemEnd called after an entry is compressed.
166 */
167 toBuffer(
168 onSuccess: (buffer: Buffer) => void,
169 onFail?: (...args: any[]) => void,
170 onItemStart?: (name: string) => void,
171 onItemEnd?: (name: string) => void,
172 ): void;
173 /**
174 * Test the archive.
175 */
176 test(): boolean;
177}
178
179declare namespace AdmZip {
180 /**
181 * The `IZipEntry` is more than a structure representing the entry inside the
182 * zip file. Beside the normal attributes and headers a entry can have, the
183 * class contains a reference to the part of the file where the compressed
184 * data resides and decompresses it when requested. It also compresses the
185 * data and creates the headers required to write in the zip file.
186 */
187 // disable warning about the I-prefix in interface name to prevent breaking stuff for users without a major bump
188 // tslint:disable-next-line:interface-name
189 interface IZipEntry {
190 /**
191 * Represents the full name and path of the file
192 */
193 entryName: string;
194 readonly rawEntryName: Buffer;
195 /**
196 * Extra data associated with this entry.
197 */
198 extra: Buffer;
199 /**
200 * Entry comment.
201 */
202 comment: string;
203 readonly name: string;
204 /**
205 * Read-Only property that indicates the type of the entry.
206 */
207 readonly isDirectory: boolean;
208 /**
209 * Get the header associated with this ZipEntry.
210 */
211 readonly header: EntryHeader;
212 attr: number;
213 /**
214 * Retrieve the compressed data for this entry. Note that this may trigger
215 * compression if any properties were modified.
216 */
217 getCompressedData(): Buffer;
218 /**
219 * Asynchronously retrieve the compressed data for this entry. Note that
220 * this may trigger compression if any properties were modified.
221 */
222 getCompressedDataAsync(callback: (data: Buffer) => void): void;
223 /**
224 * Set the (uncompressed) data to be associated with this entry.
225 */
226 setData(value: string | Buffer): void;
227 /**
228 * Get the decompressed data associated with this entry.
229 */
230 getData(): Buffer;
231 /**
232 * Asynchronously get the decompressed data associated with this entry.
233 */
234 getDataAsync(callback: (data: Buffer, err: string) => any): void;
235 /**
236 * Returns the CEN Entry Header to be written to the output zip file, plus
237 * the extra data and the entry comment.
238 */
239 packHeader(): Buffer;
240 /**
241 * Returns a nicely formatted string with the most important properties of
242 * the ZipEntry.
243 */
244 toString(): string;
245 }
246
247 interface EntryHeader {
248 made: number;
249 version: number;
250 flags: number;
251 method: number;
252 time: Date;
253 crc: number;
254 compressedSize: number;
255 size: number;
256 fileNameLength: number;
257 extraLength: number;
258 commentLength: number;
259 diskNumStart: number;
260 inAttr: number;
261 attr: number;
262 offset: number;
263 readonly encripted: boolean;
264 readonly entryHeaderSize: number;
265 readonly realDataOffset: number;
266 readonly dataHeader: DataHeader;
267 loadDataHeaderFromBinary(data: Buffer): void;
268 loadFromBinary(data: Buffer): void;
269 dataHeaderToBinary(): Buffer;
270 entryHeaderToBinary(): Buffer;
271 toString(): string;
272 }
273
274 interface DataHeader {
275 version: number;
276 flags: number;
277 method: number;
278 time: number;
279 crc: number;
280 compressedSize: number;
281 size: number;
282 fnameLen: number;
283 extraLen: number;
284 }
285}
286
287export = AdmZip;