智慧水务管理系统 - 精河县供水工程综合管理平台

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355
  1. /**
  2. * zip.js is a JavaScript open-source library (BSD-3-Clause license) for
  3. * compressing and decompressing zip files. It has been designed to handle large amounts
  4. * of data. It supports notably multi-core compression, native compression with
  5. * compression streams, archives larger than 4GB with Zip64, split zip files, data
  6. * encryption, and Deflate64 decompression.
  7. *
  8. * @author Gildas Lormeau
  9. * @license BSD-3-Clause
  10. *
  11. * @example
  12. * Hello world
  13. * ```js
  14. * import {
  15. * BlobReader,
  16. * BlobWriter,
  17. * TextReader,
  18. * TextWriter,
  19. * ZipReader,
  20. * ZipWriter,
  21. * } from from "@zip-js/zip-js";
  22. *
  23. * // ----
  24. * // Write the zip file
  25. * // ----
  26. *
  27. * // Creates a BlobWriter object where the zip content will be written.
  28. * const zipFileWriter = new BlobWriter();
  29. *
  30. * // Creates a TextReader object storing the text of the entry to add in the zip
  31. * // (i.e. "Hello world!").
  32. * const helloWorldReader = new TextReader("Hello world!");
  33. *
  34. * // Creates a ZipWriter object writing data via `zipFileWriter`, adds the entry
  35. * // "hello.txt" containing the text "Hello world!" via `helloWorldReader`, and
  36. * // closes the writer.
  37. * const zipWriter = new ZipWriter(zipFileWriter);
  38. * await zipWriter.add("hello.txt", helloWorldReader);
  39. * await zipWriter.close();
  40. *
  41. * // Retrieves the Blob object containing the zip content into `zipFileBlob`. It
  42. * // is also returned by zipWriter.close() for more convenience.
  43. * const zipFileBlob = await zipFileWriter.getData();
  44. *
  45. * // ----
  46. * // Read the zip file
  47. * // ----
  48. *
  49. * // Creates a BlobReader object used to read `zipFileBlob`.
  50. * const zipFileReader = new BlobReader(zipFileBlob);
  51. * // Creates a TextWriter object where the content of the first entry in the zip
  52. * // will be written.
  53. * const helloWorldWriter = new TextWriter();
  54. *
  55. * // Creates a ZipReader object reading the zip content via `zipFileReader`,
  56. * // retrieves metadata (name, dates, etc.) of the first entry, retrieves its
  57. * // content via `helloWorldWriter`, and closes the reader.
  58. * const zipReader = new ZipReader(zipFileReader);
  59. * const firstEntry = (await zipReader.getEntries()).shift();
  60. * const helloWorldText = await firstEntry.getData(helloWorldWriter);
  61. * await zipReader.close();
  62. *
  63. * // Displays "Hello world!".
  64. * console.log(helloWorldText);
  65. * ```
  66. *
  67. * @example
  68. * Hello world with Streams
  69. * ```js
  70. * import {
  71. * BlobReader,
  72. * ZipReader,
  73. * ZipWriter,
  74. * } from "@zip-js/zip-js";
  75. *
  76. * // ----
  77. * // Write the zip file
  78. * // ----
  79. *
  80. * // Creates a TransformStream object, the zip content will be written in the
  81. * // `writable` property.
  82. * const zipFileStream = new TransformStream();
  83. * // Creates a Promise object resolved to the zip content returned as a Blob
  84. * // object retrieved from `zipFileStream.readable`.
  85. * const zipFileBlobPromise = new Response(zipFileStream.readable).blob();
  86. * // Creates a ReadableStream object storing the text of the entry to add in the
  87. * // zip (i.e. "Hello world!").
  88. * const helloWorldReadable = new Blob(["Hello world!"]).stream();
  89. *
  90. * // Creates a ZipWriter object writing data into `zipFileStream.writable`, adds
  91. * // the entry "hello.txt" containing the text "Hello world!" retrieved from
  92. * // `helloWorldReadable`, and closes the writer.
  93. * const zipWriter = new ZipWriter(zipFileStream.writable);
  94. * await zipWriter.add("hello.txt", helloWorldReadable);
  95. * await zipWriter.close();
  96. *
  97. * // Retrieves the Blob object containing the zip content into `zipFileBlob`.
  98. * const zipFileBlob = await zipFileBlobPromise;
  99. *
  100. * // ----
  101. * // Read the zip file
  102. * // ----
  103. *
  104. * // Creates a BlobReader object used to read `zipFileBlob`.
  105. * const zipFileReader = new BlobReader(zipFileBlob);
  106. * // Creates a TransformStream object, the content of the first entry in the zip
  107. * // will be written in the `writable` property.
  108. * const helloWorldStream = new TransformStream();
  109. * // Creates a Promise object resolved to the content of the first entry returned
  110. * // as text from `helloWorldStream.readable`.
  111. * const helloWorldTextPromise = new Response(helloWorldStream.readable).text();
  112. *
  113. * // Creates a ZipReader object reading the zip content via `zipFileReader`,
  114. * // retrieves metadata (name, dates, etc.) of the first entry, retrieves its
  115. * // content into `helloWorldStream.writable`, and closes the reader.
  116. * const zipReader = new ZipReader(zipFileReader);
  117. * const firstEntry = (await zipReader.getEntries()).shift();
  118. * await firstEntry.getData(helloWorldStream.writable);
  119. * await zipReader.close();
  120. *
  121. * // Displays "Hello world!".
  122. * const helloWorldText = await helloWorldTextPromise;
  123. * console.log(helloWorldText);
  124. * ```
  125. *
  126. * @example
  127. * Adding concurrently multiple entries in a zip file
  128. * ```js
  129. * import {
  130. * BlobWriter,
  131. * HttpReader,
  132. * TextReader,
  133. * ZipWriter,
  134. * } from "@zip-js/zip-js";
  135. *
  136. * const README_URL = "https://unpkg.com/@zip.js/zip.js/README.md";
  137. * getZipFileBlob()
  138. * .then(downloadFile);
  139. *
  140. * async function getZipFileBlob() {
  141. * const zipWriter = new ZipWriter(new BlobWriter("application/zip"));
  142. * await Promise.all([
  143. * zipWriter.add("hello.txt", new TextReader("Hello world!")),
  144. * zipWriter.add("README.md", new HttpReader(README_URL)),
  145. * ]);
  146. * return zipWriter.close();
  147. * }
  148. *
  149. * function downloadFile(blob) {
  150. * document.body.appendChild(Object.assign(document.createElement("a"), {
  151. * download: "hello.zip",
  152. * href: URL.createObjectURL(blob),
  153. * textContent: "Download zip file",
  154. * }));
  155. * }
  156. * ```
  157. *
  158. * @module
  159. */
  160. /**
  161. * Represents the `FileSystemEntry` class.
  162. *
  163. * @see {@link https://wicg.github.io/entries-api/#api-entry|specification}
  164. */
  165. // deno-lint-ignore no-empty-interface
  166. interface FileSystemEntryLike {}
  167. /**
  168. * Represents the `FileSystemHandle` class.
  169. *
  170. * @see {@link https://fs.spec.whatwg.org/#api-filesystemhandle}
  171. */
  172. // deno-lint-ignore no-empty-interface
  173. interface FileSystemHandleLike {}
  174. /**
  175. * Represents a generic `TransformStream` class.
  176. *
  177. * @see {@link https://streams.spec.whatwg.org/#generictransformstream|specification}
  178. */
  179. declare class TransformStreamLike {
  180. /**
  181. * The readable stream.
  182. */
  183. readable: ReadableStream;
  184. /**
  185. * The writable stream.
  186. */
  187. writable: WritableStream;
  188. }
  189. /**
  190. * Configures zip.js
  191. *
  192. * @param configuration The configuration.
  193. */
  194. export function configure(configuration: Configuration): void;
  195. /**
  196. * Represents the configuration passed to {@link configure}.
  197. */
  198. export interface Configuration extends WorkerConfiguration {
  199. /**
  200. * The maximum number of web workers used to compress/decompress data simultaneously.
  201. *
  202. * @defaultValue `navigator.hardwareConcurrency`
  203. */
  204. maxWorkers?: number;
  205. /**
  206. * The delay in milliseconds before idle web workers are automatically terminated. You can call `terminateWorkers()` to terminate idle workers.
  207. *
  208. * @defaultValue 5000
  209. */
  210. terminateWorkerTimeout?: number;
  211. /**
  212. * The URI of the web worker.
  213. *
  214. * It allows using alternative deflate implementations or specifying a URL to the worker script if the CSP of the page blocks scripts imported from a Data URI.
  215. *
  216. * Here is an example to import the worker module as a URL (see `?url`) and avoid CSP issues:
  217. * ```
  218. * import workerURI from "@zip.js/zip.js/dist/zip-web-worker.js?url";
  219. *
  220. * configure({
  221. * workerURI
  222. * });
  223. * ```
  224. *
  225. * @defaultValue "./core/web-worker.js"
  226. */
  227. workerURI?: string;
  228. /**
  229. * The URI of the WebAssembly module used by default implementations to compress/decompress data. It is ignored if `useCompressionStream` is set to `true` and `CompressionStream`/`DecompressionStream` are supported by the environment.
  230. *
  231. * Here is an example to import the WASM module as a URL (see `?url`) and avoid CSP issues:
  232. * ```
  233. * import wasmURI from "@zip.js/zip.js/dist/zip-module.wasm?url";
  234. *
  235. * configure({
  236. * wasmURI
  237. * });
  238. * ```
  239. *
  240. * @defaultValue "./core/streams/zlib-wasm/zlib-streams.wasm"
  241. */
  242. wasmURI?: string;
  243. /**
  244. * The size of the chunks in bytes during data compression/decompression.
  245. *
  246. * @defaultValue 65536
  247. */
  248. chunkSize?: number;
  249. /**
  250. * The stream implementation used to compress data when `useCompressionStream` is set to `true`.
  251. *
  252. * @defaultValue {@link CodecStream}
  253. */
  254. CompressionStream?: typeof TransformStreamLike;
  255. /**
  256. * The stream implementation used to decompress data when `useCompressionStream` is set to `true`.
  257. *
  258. * @defaultValue {@link CodecStream}
  259. */
  260. DecompressionStream?: typeof TransformStreamLike;
  261. /**
  262. * The stream implementation used to compress data when `useCompressionStream` is set to `false`.
  263. *
  264. * @defaultValue {@link CodecStream}
  265. */
  266. CompressionStreamZlib?: typeof TransformStreamLike;
  267. /**
  268. * The stream implementation used to decompress data when `useCompressionStream` is set to `false`.
  269. *
  270. * @defaultValue {@link CodecStream}
  271. */
  272. DecompressionStreamZlib?: typeof TransformStreamLike;
  273. }
  274. /**
  275. * Represents configuration passed to {@link configure}, the constructor of {@link ZipReader}, {@link FileEntry#getData}, the constructor of {@link ZipWriter}, and {@link ZipWriter#add}.
  276. */
  277. export interface WorkerConfiguration {
  278. /**
  279. * `true` to use web workers to compress/decompress data in non-blocking background processes.
  280. *
  281. * @defaultValue true
  282. */
  283. useWebWorkers?: boolean;
  284. /**
  285. * `true` to use the native API `CompressionStream`/`DecompressionStream` to compress/decompress data.
  286. *
  287. * @defaultValue true
  288. */
  289. useCompressionStream?: boolean;
  290. /**
  291. * `true` to transfer stream ownership to web workers.
  292. *
  293. * @defaultValue true
  294. */
  295. transferStreams?: boolean;
  296. }
  297. /**
  298. * Terminates all the web workers
  299. */
  300. export function terminateWorkers(): Promise<void>;
  301. /**
  302. * Represents a class implementing `CompressionStream` or `DecompressionStream` interfaces.
  303. */
  304. declare class CodecStream extends TransformStream {}
  305. /**
  306. * Returns the MIME type corresponding to a filename extension.
  307. *
  308. * @param fileExtension the extension of the filename.
  309. * @returns The corresponding MIME type.
  310. */
  311. export function getMimeType(fileExtension: string): string;
  312. /**
  313. * Represents an instance used to read or write unknown type of data.
  314. *
  315. * zip.js can handle multiple types of data thanks to a generic API. This feature is based on 2 abstract constructors: {@link Reader} and {@link Writer}.
  316. * The classes inheriting from {@link Reader} help to read data from a source of data. The classes inheriting from {@link Writer} help to write data into a destination.
  317. */
  318. export interface Initializable {
  319. /**
  320. * Initializes the instance asynchronously
  321. */
  322. init?(): Promise<void>;
  323. }
  324. /**
  325. * Represents an instance used to read data from a `ReadableStream` instance.
  326. */
  327. export interface ReadableReader {
  328. /**
  329. * The `ReadableStream` instance.
  330. */
  331. readable: ReadableStream;
  332. }
  333. /**
  334. * Represents an instance used to read unknown type of data.
  335. *
  336. * @example
  337. * Here is an example of custom {@link Reader} class used to read binary strings:
  338. * ```
  339. * class BinaryStringReader extends Reader {
  340. *
  341. * constructor(binaryString) {
  342. * super();
  343. * this.binaryString = binaryString;
  344. * }
  345. *
  346. * init() {
  347. * super.init();
  348. * this.size = this.binaryString.length;
  349. * }
  350. *
  351. * readUint8Array(offset, length) {
  352. * const result = new Uint8Array(length);
  353. * for (let indexCharacter = 0; indexCharacter < length; indexCharacter++) {
  354. * result[indexCharacter] = this.binaryString.charCodeAt(indexCharacter + offset) & 0xFF;
  355. * }
  356. * return result;
  357. * }
  358. * }
  359. * ```
  360. */
  361. export class Reader<Type> implements Initializable, ReadableReader {
  362. /**
  363. * Creates the {@link Reader} instance
  364. *
  365. * @param value The data to read.
  366. */
  367. constructor(value: Type);
  368. /**
  369. * The `ReadableStream` instance.
  370. */
  371. readable: ReadableStream;
  372. /**
  373. * The total size of the data in bytes.
  374. */
  375. size: number;
  376. /**
  377. * Initializes the instance asynchronously
  378. */
  379. init?(): Promise<void>;
  380. /**
  381. * Reads a chunk of data
  382. *
  383. * @param index The byte index of the data to read.
  384. * @param length The length of the data to read in bytes.
  385. * @returns A promise resolving to a chunk of data. The data must be trucated to the remaining size if the requested length is larger than the remaining size.
  386. */
  387. readUint8Array(index: number, length: number): Promise<Uint8Array>;
  388. }
  389. /**
  390. * Represents a {@link Reader} instance used to read data provided as a `string`.
  391. */
  392. export class TextReader extends Reader<string> {}
  393. /**
  394. * Represents a {@link Reader} instance used to read data provided as a `Blob` instance.
  395. */
  396. export class BlobReader extends Reader<Blob> {}
  397. /**
  398. * Represents a {@link Reader} instance used to read data provided as a Data URI `string` encoded in Base64.
  399. */
  400. export class Data64URIReader extends Reader<string> {}
  401. /**
  402. * Represents a {@link Reader} instance used to read data provided as a `Uint8Array` instance.
  403. */
  404. export class Uint8ArrayReader extends Reader<Uint8Array> {}
  405. /**
  406. * Represents a {@link Reader} instance used to read data provided as an array of {@link ReadableReader} instances (e.g. split zip files).
  407. */
  408. export class SplitDataReader extends Reader<
  409. Reader<unknown>[] | ReadableReader[] | ReadableStream[]
  410. > {}
  411. /**
  412. * Represents a URL stored into a `string`.
  413. */
  414. type URLString = string;
  415. /**
  416. * Represents a {@link Reader} instance used to fetch data from a URL.
  417. */
  418. export class HttpReader extends Reader<URLString> {
  419. /**
  420. * Creates the {@link HttpReader} instance
  421. *
  422. * @param url The URL of the data.
  423. * @param options The options.
  424. */
  425. constructor(url: URLString | URL, options?: HttpOptions);
  426. }
  427. /**
  428. * Represents a {@link Reader} instance used to fetch data from servers returning `Accept-Ranges` headers.
  429. */
  430. export class HttpRangeReader extends HttpReader {
  431. /**
  432. * Creates the {@link HttpRangeReader} instance. It is particularly useful for reading ZIP files via HTTP.
  433. * If you just want to add content retrieved via HTTP to a ZIP file, you can simply use
  434. * `Response#body` {@link https://developer.mozilla.org/en-US/docs/Web/API/Response/body} instead.
  435. *
  436. * @param url The URL of the data.
  437. * @param options The options.
  438. */
  439. constructor(url: URLString | URL, options?: HttpRangeOptions);
  440. }
  441. /**
  442. * Represents the options passed to the constructor of {@link HttpReader}.
  443. */
  444. export interface HttpOptions extends HttpRangeOptions {
  445. /**
  446. * `true` to use `Range` headers when fetching data from servers returning `Accept-Ranges` headers.
  447. *
  448. * @defaultValue false
  449. */
  450. useRangeHeader?: boolean;
  451. /**
  452. * `true` to always use `Range` headers when fetching data.
  453. *
  454. * @defaultValue false
  455. */
  456. forceRangeRequests?: boolean;
  457. /**
  458. * `true` to prevent using `HEAD` HTTP request in order the get the size of the content.
  459. * `false` to explicitly use `HEAD`, this is useful in case of CORS where `Access-Control-Expose-Headers: Content-Range` is not returned by the server.
  460. *
  461. * @defaultValue false
  462. */
  463. preventHeadRequest?: boolean;
  464. /**
  465. * `true` to use `Range: bytes=-22` on the first request and cache the EOCD, make sure beforehand that the server supports a suffix range request.
  466. *
  467. * @defaultValue false
  468. */
  469. combineSizeEocd?: boolean;
  470. }
  471. /**
  472. * Represents options passed to the constructor of {@link HttpRangeReader} and {@link HttpReader}.
  473. */
  474. export interface HttpRangeOptions {
  475. /**
  476. * `true` to rely `XMLHttpRequest` instead of `fetch` to fetch data.
  477. *
  478. * @defaultValue false
  479. */
  480. useXHR?: boolean;
  481. /**
  482. * The HTTP headers.
  483. */
  484. headers?: Iterable<[string, string]> | Map<string, string>;
  485. }
  486. /**
  487. * Represents an instance used to write data into a `WritableStream` instance.
  488. */
  489. export interface WritableWriter {
  490. /**
  491. * The `WritableStream` instance.
  492. */
  493. writable: WritableStream;
  494. /**
  495. * The maximum size of split data when creating a {@link ZipWriter} instance or when calling {@link FileEntry#getData} with a generator of {@link WritableWriter} instances.
  496. */
  497. maxSize?: number;
  498. }
  499. /**
  500. * Represents an instance used to write unknown type of data.
  501. *
  502. * @example
  503. * Here is an example of custom {@link Writer} class used to write binary strings:
  504. * ```
  505. * class BinaryStringWriter extends Writer {
  506. *
  507. * constructor() {
  508. * super();
  509. * this.binaryString = "";
  510. * }
  511. *
  512. * writeUint8Array(array) {
  513. * for (let indexCharacter = 0; indexCharacter < array.length; indexCharacter++) {
  514. * this.binaryString += String.fromCharCode(array[indexCharacter]);
  515. * }
  516. * }
  517. *
  518. * getData() {
  519. * return this.binaryString;
  520. * }
  521. * }
  522. * ```
  523. */
  524. export class Writer<Type> implements Initializable, WritableWriter {
  525. /**
  526. * The `WritableStream` instance.
  527. */
  528. writable: WritableStream;
  529. /**
  530. * Initializes the instance asynchronously
  531. *
  532. * @param size the total size of the written data in bytes.
  533. */
  534. init?(size?: number): Promise<void>;
  535. /**
  536. * Appends a chunk of data
  537. *
  538. * @param array The chunk data to append.
  539. *
  540. * @virtual
  541. */
  542. writeUint8Array(array: Uint8Array): Promise<void>;
  543. /**
  544. * Retrieves all the written data
  545. *
  546. * @returns A promise resolving to the written data.
  547. */
  548. getData(): Promise<Type>;
  549. }
  550. /**
  551. * Represents a {@link Writer} instance used to retrieve the written data as a `string`.
  552. */
  553. export class TextWriter extends Writer<string> {
  554. /**
  555. * Creates the {@link TextWriter} instance
  556. *
  557. * @param encoding The encoding of the text.
  558. */
  559. constructor(encoding?: string);
  560. }
  561. /**
  562. * Represents a {@link WritableWriter} instance used to retrieve the written data as a `Blob` instance.
  563. */
  564. export class BlobWriter implements Initializable, WritableWriter {
  565. /**
  566. * The `WritableStream` instance.
  567. */
  568. writable: WritableStream;
  569. /**
  570. * Initializes the instance asynchronously
  571. */
  572. init(): Promise<void>;
  573. /**
  574. * Creates the {@link BlobWriter} instance
  575. *
  576. * @param mimeString The MIME type of the content.
  577. */
  578. constructor(mimeString?: string);
  579. /**
  580. * Retrieves all the written data
  581. *
  582. * @returns A promise resolving to the written data.
  583. */
  584. getData(): Promise<Blob>;
  585. }
  586. /**
  587. * Represents a {@link Writer} instance used to retrieve the written data as a Data URI `string` encoded in Base64.
  588. */
  589. export class Data64URIWriter extends Writer<string> {
  590. /**
  591. * Creates the {@link Data64URIWriter} instance
  592. *
  593. * @param mimeString The MIME type of the content.
  594. */
  595. constructor(mimeString?: string);
  596. }
  597. /**
  598. * Represents a {@link Writer} instance used to retrieve the written data from a generator of {@link WritableWriter} instances (i.e. split zip files).
  599. */
  600. export class SplitDataWriter implements Initializable, WritableWriter {
  601. /**
  602. * The `WritableStream` instance.
  603. */
  604. writable: WritableStream;
  605. /**
  606. * Initializes the instance asynchronously
  607. */
  608. init(): Promise<void>;
  609. /**
  610. * Creates the {@link SplitDataWriter} instance
  611. *
  612. * @param writerGenerator A generator of Writer instances.
  613. * @param maxSize The maximum size of the data written into {@link Writer} instances (default: 4GB).
  614. */
  615. constructor(
  616. writerGenerator: AsyncGenerator<
  617. Writer<unknown> | WritableWriter | WritableStream,
  618. boolean
  619. >,
  620. maxSize?: number
  621. );
  622. }
  623. /**
  624. * Represents a {@link Writer} instance used to retrieve the written data as a `Uint8Array` instance.
  625. */
  626. export class Uint8ArrayWriter extends Writer<Uint8Array<ArrayBuffer>> {
  627. /**
  628. * Creates the {@link Uint8ArrayWriter} instance
  629. *
  630. * @param defaultBufferSize The initial size of the internal buffer (default: 256KB).
  631. */
  632. constructor(defaultBufferSize?: number);
  633. }
  634. /**
  635. * Represents an instance used to create an unzipped stream.
  636. *
  637. * @example
  638. * This example will take a zip file, decompress it and then save its files and directories to disk.
  639. * ```
  640. * import {resolve} from "https://deno.land/std/path/mod.ts";
  641. * import {ensureDir, ensureFile} from "https://deno.land/std/fs/mod.ts";
  642. *
  643. * for await (const entry of (await fetch(urlToZippedFile)).body.pipeThrough(new ZipReaderStream())) {
  644. * const fullPath = resolve(destination, entry.filename);
  645. * if (entry.directory) {
  646. * await ensureDir(fullPath);
  647. * continue;
  648. * }
  649. *
  650. * await ensureFile(fullPath);
  651. * await entry.readable?.pipeTo((await Deno.create(fullPath)).writable);
  652. * }
  653. * ```
  654. */
  655. export class ZipReaderStream<T> {
  656. /**
  657. * Creates the stream.
  658. *
  659. * @param options The options.
  660. */
  661. constructor(options?: ZipReaderConstructorOptions);
  662. /**
  663. * The readable stream.
  664. */
  665. readable: ReadableStream<
  666. Omit<Entry, "getData"> & { readable?: ReadableStream<Uint8Array> }
  667. >;
  668. /**
  669. * The writable stream.
  670. */
  671. writable: WritableStream<T>;
  672. }
  673. /**
  674. * Represents an instance used to read a zip file.
  675. *
  676. * @example
  677. * Here is an example showing how to read the text data of the first entry from a zip file:
  678. * ```
  679. * // create a BlobReader to read with a ZipReader the zip from a Blob object
  680. * const reader = new zip.ZipReader(new zip.BlobReader(blob));
  681. *
  682. * // get all entries from the zip
  683. * const entries = await reader.getEntries();
  684. * if (entries.length) {
  685. *
  686. * // get first entry content as text by using a TextWriter
  687. * const text = await entries[0].getData(
  688. * // writer
  689. * new zip.TextWriter(),
  690. * // options
  691. * {
  692. * onprogress: (index, max) => {
  693. * // onprogress callback
  694. * }
  695. * }
  696. * );
  697. * // text contains the entry data as a String
  698. * console.log(text);
  699. * }
  700. *
  701. * // close the ZipReader
  702. * await reader.close();
  703. * ```
  704. */
  705. export class ZipReader<Type> {
  706. /**
  707. * Creates the instance
  708. *
  709. * @param reader The {@link Reader} instance used to read data.
  710. * @param options The options.
  711. */
  712. constructor(
  713. reader:
  714. | Reader<Type>
  715. | ReadableReader
  716. | ReadableStream
  717. | Reader<unknown>[]
  718. | ReadableReader[]
  719. | ReadableStream[],
  720. options?: ZipReaderConstructorOptions
  721. );
  722. /**
  723. * The global comment of the zip file.
  724. */
  725. comment: Uint8Array;
  726. /**
  727. * The data prepended before the zip file.
  728. */
  729. prependedData?: Uint8Array;
  730. /**
  731. * The data appended after the zip file.
  732. */
  733. appendedData?: Uint8Array;
  734. /**
  735. * Returns all the entries in the zip file
  736. *
  737. * @param options The options.
  738. * @returns A promise resolving to an `array` of {@link Entry} instances.
  739. */
  740. getEntries(options?: ZipReaderGetEntriesOptions): Promise<Entry[]>;
  741. /**
  742. * Returns a generator used to iterate on all the entries in the zip file
  743. *
  744. * @param options The options.
  745. * @returns An asynchronous generator of {@link Entry} instances.
  746. */
  747. getEntriesGenerator(
  748. options?: ZipReaderGetEntriesOptions
  749. ): AsyncGenerator<Entry, boolean>;
  750. /**
  751. * Closes the zip file
  752. */
  753. close(): Promise<void>;
  754. }
  755. /**
  756. * Represents the options passed to the constructor of {@link ZipReader}, and `{@link ZipDirectory}#import*`.
  757. */
  758. export interface ZipReaderConstructorOptions
  759. extends ZipReaderOptions,
  760. GetEntriesOptions,
  761. WorkerConfiguration {
  762. /**
  763. * `true` to extract the prepended data into {@link ZipReader#prependedData}.
  764. *
  765. * @defaultValue false
  766. */
  767. extractPrependedData?: boolean;
  768. /**
  769. * `true` to extract the appended data into {@link ZipReader#appendedData}.
  770. *
  771. * @defaultValue false
  772. */
  773. extractAppendedData?: boolean;
  774. }
  775. /**
  776. * Represents the options passed to {@link ZipReader#getEntries} and {@link ZipReader#getEntriesGenerator}.
  777. */
  778. export interface ZipReaderGetEntriesOptions
  779. extends GetEntriesOptions,
  780. EntryOnprogressOptions {}
  781. /**
  782. * Represents options passed to the constructor of {@link ZipReader}, {@link ZipReader#getEntries} and {@link ZipReader#getEntriesGenerator}.
  783. */
  784. export interface GetEntriesOptions {
  785. /**
  786. * The encoding of the filename of the entry.
  787. */
  788. filenameEncoding?: string;
  789. /**
  790. * The encoding of the comment of the entry.
  791. */
  792. commentEncoding?: string;
  793. /**
  794. * The function called for decoding the filename and the comment of the entry.
  795. *
  796. * @param value The raw text value.
  797. * @param encoding The encoding of the text.
  798. * @returns The decoded text value or `undefined` if the raw text value should be decoded by zip.js.
  799. */
  800. decodeText?(value: Uint8Array, encoding: string): string | undefined;
  801. }
  802. /**
  803. * Represents options passed to the constructor of {@link ZipReader} and {@link FileEntry#getData}.
  804. */
  805. export interface ZipReaderOptions {
  806. /**
  807. * `true` to check only if the password is valid.
  808. *
  809. * @defaultValue false
  810. */
  811. checkPasswordOnly?: boolean;
  812. /**
  813. * `true` to check the signature of the entry.
  814. *
  815. * @defaultValue false
  816. */
  817. checkSignature?: boolean;
  818. /**
  819. * `true` to throw an {@link ERR_OVERLAPPING_ENTRY} error when calling {@link FileEntry#getData} if the entry
  820. * overlaps with another entry on which {@link FileEntry#getData} has already been called (with the option
  821. * `checkOverlappingEntry` or `checkOverlappingEntryOnly` set to `true`).
  822. *
  823. * @defaultValue false
  824. */
  825. checkOverlappingEntry?: boolean;
  826. /**
  827. * `true` to throw an {@link ERR_OVERLAPPING_ENTRY} error when calling {@link FileEntry#getData} if the entry
  828. * overlaps with another entry on which {@link FileEntry#getData} has already been called (with the option
  829. * `checkOverlappingEntry` or `checkOverlappingEntryOnly` set to `true`) without trying to read the content of the
  830. * entry.
  831. *
  832. * @defaultValue false
  833. */
  834. checkOverlappingEntryOnly?: boolean;
  835. /**
  836. * The password used to decrypt the content of the entry.
  837. */
  838. password?: string;
  839. /**
  840. * `true` to read the data as-is without decompressing it and without decrypting it.
  841. */
  842. passThrough?: boolean;
  843. /**
  844. * The password used to encrypt the content of the entry (raw).
  845. */
  846. rawPassword?: Uint8Array;
  847. /**
  848. * The `AbortSignal` instance used to cancel the decompression.
  849. */
  850. signal?: AbortSignal;
  851. /**
  852. * `true` to prevent closing of {@link Writer#writable} when calling {@link FileEntry#getData}.
  853. *
  854. * @defaultValue false
  855. */
  856. preventClose?: boolean;
  857. }
  858. /**
  859. * Represents the metadata of an entry in a zip file (Core API).
  860. */
  861. export interface EntryMetaData {
  862. /**
  863. * The byte offset of the entry.
  864. */
  865. offset: number;
  866. /**
  867. * The filename of the entry.
  868. */
  869. filename: string;
  870. /**
  871. * The filename of the entry (raw).
  872. */
  873. rawFilename: Uint8Array;
  874. /**
  875. * `true` if the filename is encoded in UTF-8.
  876. */
  877. filenameUTF8: boolean;
  878. /**
  879. * `true` if the entry is an executable file
  880. */
  881. executable: boolean;
  882. /**
  883. * `true` if the content of the entry is encrypted.
  884. */
  885. encrypted: boolean;
  886. /**
  887. * `true` if the content of the entry is encrypted with the ZipCrypto algorithm.
  888. */
  889. zipCrypto: boolean;
  890. /**
  891. * The size of the compressed data in bytes.
  892. */
  893. compressedSize: number;
  894. /**
  895. * The size of the decompressed data in bytes.
  896. */
  897. uncompressedSize: number;
  898. /**
  899. * The last modification date.
  900. */
  901. lastModDate: Date;
  902. /**
  903. * The last access date.
  904. */
  905. lastAccessDate?: Date;
  906. /**
  907. * The creation date.
  908. */
  909. creationDate?: Date;
  910. /**
  911. * The last modification date (raw).
  912. */
  913. rawLastModDate: number | bigint;
  914. /**
  915. * The last access date (raw).
  916. */
  917. rawLastAccessDate?: number | bigint;
  918. /**
  919. * The creation date (raw).
  920. */
  921. rawCreationDate?: number | bigint;
  922. /**
  923. * The comment of the entry.
  924. */
  925. comment: string;
  926. /**
  927. * The comment of the entry (raw).
  928. */
  929. rawComment: Uint8Array;
  930. /**
  931. * `true` if the comment is encoded in UTF-8.
  932. */
  933. commentUTF8: boolean;
  934. /**
  935. * The signature (CRC32 checksum) of the content.
  936. */
  937. signature: number;
  938. /**
  939. * The extra field.
  940. */
  941. extraField?: Map<number, { type: number; data: Uint8Array }>;
  942. /**
  943. * The extra field (raw).
  944. */
  945. rawExtraField: Uint8Array;
  946. /**
  947. * `true` if the entry is using Zip64.
  948. */
  949. zip64: boolean;
  950. /**
  951. * The "Version" field.
  952. */
  953. version: number;
  954. /**
  955. * The "Version made by" field.
  956. */
  957. versionMadeBy: number;
  958. /**
  959. * `true` if `internalFileAttributes` and `externalFileAttributes` are compatible with MS-DOS format.
  960. */
  961. msDosCompatible: boolean;
  962. /**
  963. * Note (MS-DOS / Unix attributes):
  964. *
  965. * - The single source of truth for on-disk metadata is the 32-bit `externalFileAttributes` value stored in
  966. * the ZIP headers. The upper 16 bits are commonly used for Unix `st_mode` (type/permissions/special bits)
  967. * and the low 8 bits for MS-DOS attribute flags.
  968. *
  969. * - Writer vs Reader:
  970. * - The writer composes `externalFileAttributes` from the provided options (`externalFileAttributes`,
  971. * `unixMode`/special flags, `msdosAttributesRaw`/`msdosAttributes`).
  972. * - The reader decodes the stored `externalFileAttributes` and exposes convenience fields such as
  973. * `msdosAttributesRaw`, `msdosAttributes`, `unixExternalUpper`, and `unixMode`.
  974. *
  975. * - Practical rule: treat `externalFileAttributes` as authoritative; other fields are conveniences derived
  976. * from it. If you need a specific on-disk value, set `externalFileAttributes` explicitly.
  977. */
  978. /**
  979. * The MS-DOS attributes low byte (raw).
  980. * This is the low 8 bits of {@link EntryMetaData#externalFileAttributes} when present.
  981. */
  982. msdosAttributesRaw?: number;
  983. /**
  984. * The MS-DOS attribute flags exposed as booleans.
  985. */
  986. msdosAttributes?: {
  987. readOnly: boolean;
  988. hidden: boolean;
  989. system: boolean;
  990. directory: boolean;
  991. archive: boolean;
  992. };
  993. /**
  994. * Unix owner id when available.
  995. */
  996. uid?: number;
  997. /**
  998. * Unix group id when available.
  999. */
  1000. gid?: number;
  1001. /**
  1002. * Unix mode (st_mode) when available.
  1003. */
  1004. unixMode?: number;
  1005. /**
  1006. * `true` if the setuid bit is set on the entry.
  1007. */
  1008. setuid?: boolean;
  1009. /**
  1010. * `true` if the setgid bit is set on the entry.
  1011. */
  1012. setgid?: boolean;
  1013. /**
  1014. * `true` if the sticky bit is set on the entry.
  1015. */
  1016. sticky?: boolean;
  1017. /**
  1018. * The internal file attributes (raw).
  1019. */
  1020. internalFileAttributes: number;
  1021. /**
  1022. * The 32-bit `externalFileAttributes` field is the authoritative on-disk metadata for each entry.
  1023. * - Upper 16 bits: Unix mode/type (e.g., permissions, file type)
  1024. * - Low 8 bits: MS-DOS file attributes (e.g., directory, read-only)
  1025. *
  1026. * When writing, all provided options are merged into this field. When reading, convenience fields are decoded from it.
  1027. * For most use cases, prefer the high-level options and fields; only advanced users need to manipulate the raw value directly.
  1028. */
  1029. externalFileAttributes: number;
  1030. /**
  1031. * The upper 16-bit portion of {@link EntryMetaData#externalFileAttributes} when it represents Unix mode bits.
  1032. */
  1033. unixExternalUpper?: number;
  1034. /**
  1035. * The number of the disk where the entry data starts.
  1036. */
  1037. /**
  1038. * The internal file attribute (raw).
  1039. * @deprecated Use {@link EntryMetaData#internalFileAttributes} instead.
  1040. */
  1041. internalFileAttribute: number;
  1042. /**
  1043. * The external file attribute (raw).
  1044. * @deprecated Use {@link EntryMetaData#externalFileAttributes} instead.
  1045. */
  1046. externalFileAttribute: number;
  1047. /**
  1048. * The number of the disk where the entry data starts.
  1049. */
  1050. diskNumberStart: number;
  1051. /**
  1052. * The compression method.
  1053. */
  1054. compressionMethod: number;
  1055. }
  1056. export interface DirectoryEntry extends EntryMetaData {
  1057. /**
  1058. * `true` if the entry is a directory.
  1059. */
  1060. directory: true;
  1061. }
  1062. export interface FileEntry extends EntryMetaData {
  1063. /**
  1064. * `false` if the entry is a file.
  1065. */
  1066. directory: false;
  1067. /**
  1068. * Returns the content of the entry
  1069. *
  1070. * @param writer The {@link Writer} instance used to write the content of the entry.
  1071. * @param options The options.
  1072. * @returns A promise resolving to the type to data associated to `writer`.
  1073. */
  1074. getData<Type>(
  1075. writer:
  1076. | Writer<Type>
  1077. | WritableWriter
  1078. | WritableStream
  1079. | AsyncGenerator<
  1080. Writer<unknown> | WritableWriter | WritableStream,
  1081. boolean
  1082. >,
  1083. options?: EntryGetDataCheckPasswordOptions
  1084. ): Promise<Type>;
  1085. /**
  1086. * Retrieves the content of the entry as an `ArrayBuffer` instance
  1087. *
  1088. * @param options The options.
  1089. * @returns A promise resolving to an `ArrayBuffer` instance.
  1090. */
  1091. arrayBuffer(options?: EntryGetDataOptions): Promise<ArrayBuffer>;
  1092. }
  1093. /**
  1094. * Represents an entry with its data and metadata in a zip file (Core API).
  1095. * This is a union type of {@link DirectoryEntry} and {@link FileEntry}.
  1096. *
  1097. * Before using getData, you should check if the entry is a file.
  1098. *
  1099. * @example
  1100. *
  1101. * ```ts
  1102. * for await (const entry of reader.getEntriesGenerator()) {
  1103. * if (entry.directory) continue;
  1104. *
  1105. * // entry is a FileEntry
  1106. * const plainTextData = await entry.getData(new TextWriter());
  1107. *
  1108. * // Do something with the plainTextData
  1109. * }
  1110. * ```
  1111. */
  1112. export type Entry = DirectoryEntry | FileEntry;
  1113. /**
  1114. * Represents the options passed to {@link FileEntry#getData} and `{@link ZipFileEntry}.get*`.
  1115. */
  1116. export interface EntryGetDataOptions
  1117. extends EntryDataOnprogressOptions,
  1118. ZipReaderOptions,
  1119. WorkerConfiguration {}
  1120. /**
  1121. * Represents the options passed to {@link FileEntry#getData} and `{@link ZipFileEntry}.get*`.
  1122. */
  1123. export interface EntryGetDataCheckPasswordOptions extends EntryGetDataOptions {}
  1124. /**
  1125. * Represents an instance used to create a zipped stream.
  1126. *
  1127. * @example
  1128. * This example creates a zipped file called numbers.txt.zip containing the numbers 0 - 1000 each on their own line.
  1129. * ```
  1130. * const readable = ReadableStream.from((function* () {
  1131. * for (let i = 0; i < 1000; ++i)
  1132. * yield i + '\n'
  1133. * })())
  1134. *
  1135. * readable
  1136. * .pipeThrough(new ZipWriterStream().transform('numbers.txt'))
  1137. * .pipeTo((await Deno.create('numbers.txt.zip')).writable)
  1138. * ```
  1139. *
  1140. * @example
  1141. * This example creates a zipped file called Archive.zip containing two files called numbers.txt and letters.txt
  1142. * ```
  1143. * const readable1 = ReadableStream.from((function* () {
  1144. * for (let i = 0; i < 1000; ++i)
  1145. * yield i + '\n'
  1146. * })())
  1147. * const readable2 = ReadableStream.from((function* () {
  1148. * const letters = 'abcdefghijklmnopqrstuvwxyz'.split('')
  1149. * while (letters.length)
  1150. * yield letters.shift() + '\n'
  1151. * })())
  1152. *
  1153. * const zipper = new ZipWriterStream()
  1154. * zipper.readable.pipeTo((await Deno.create('Archive.zip')).writable)
  1155. * readable1.pipeTo(zipper.writable('numbers.txt'))
  1156. * readable2.pipeTo(zipper.writable('letters.txt'))
  1157. * zipper.close()
  1158. * ```
  1159. */
  1160. export class ZipWriterStream {
  1161. /**
  1162. * Creates the stream.
  1163. *
  1164. * @param options The options.
  1165. */
  1166. constructor(options?: ZipWriterConstructorOptions);
  1167. /**
  1168. * The readable stream.
  1169. */
  1170. readable: ReadableStream<Uint8Array>;
  1171. /**
  1172. * The ZipWriter property.
  1173. */
  1174. zipWriter: ZipWriter<unknown>;
  1175. /**
  1176. * Returns an object containing a readable and writable property for the .pipeThrough method
  1177. *
  1178. * @param path The name of the stream when unzipped.
  1179. * @returns An object containing readable and writable properties
  1180. */
  1181. transform<T>(path: string): {
  1182. readable: ReadableStream<T>;
  1183. writable: WritableStream<T>;
  1184. };
  1185. /**
  1186. * Returns a WritableStream for the .pipeTo method
  1187. *
  1188. * @param path The directory path of where the stream should exist in the zipped stream.
  1189. * @returns A WritableStream.
  1190. */
  1191. writable<T>(path: string): WritableStream<T>;
  1192. /**
  1193. * Writes the entries directory, writes the global comment, and returns the content of the zipped file.
  1194. *
  1195. * @param comment The global comment of the zip file.
  1196. * @param options The options.
  1197. * @returns The content of the zip file.
  1198. */
  1199. close(
  1200. comment?: Uint8Array,
  1201. options?: ZipWriterCloseOptions
  1202. ): Promise<unknown>;
  1203. }
  1204. /**
  1205. * Represents an instance used to create a zip file.
  1206. *
  1207. * @example
  1208. * Here is an example showing how to create a zip file containing a compressed text file:
  1209. * ```
  1210. * // use a BlobWriter to store with a ZipWriter the zip into a Blob object
  1211. * const blobWriter = new zip.BlobWriter("application/zip");
  1212. * const writer = new zip.ZipWriter(blobWriter);
  1213. *
  1214. * // use a TextReader to read the String to add
  1215. * await writer.add("filename.txt", new zip.TextReader("test!"));
  1216. *
  1217. * // close the ZipReader
  1218. * await writer.close();
  1219. *
  1220. * // get the zip file as a Blob
  1221. * const blob = await blobWriter.getData();
  1222. * ```
  1223. */
  1224. export class ZipWriter<Type> {
  1225. /**
  1226. * Creates the {@link ZipWriter} instance
  1227. *
  1228. * @param writer The {@link Writer} instance where the zip content will be written.
  1229. * @param options The options.
  1230. */
  1231. constructor(
  1232. writer:
  1233. | Writer<Type>
  1234. | WritableWriter
  1235. | WritableStream
  1236. | AsyncGenerator<
  1237. Writer<unknown> | WritableWriter | WritableStream,
  1238. boolean
  1239. >,
  1240. options?: ZipWriterConstructorOptions
  1241. );
  1242. /**
  1243. * `true` if the zip contains at least one entry that has been partially written.
  1244. */
  1245. readonly hasCorruptedEntries?: boolean;
  1246. /**
  1247. * Adds an existing zip file at the beginning of the current zip. This method
  1248. * cannot be called after the first call to {@link ZipWriter#add}.
  1249. *
  1250. * @param reader The {@link Reader} instance used to read the content of the zip file.
  1251. * @returns A promise resolving when the zip file has been added.
  1252. */
  1253. prependZip<ReaderType>(
  1254. reader:
  1255. | Reader<ReaderType>
  1256. | ReadableReader
  1257. | ReadableStream
  1258. | Reader<unknown>[]
  1259. | ReadableReader[]
  1260. | ReadableStream[]
  1261. ): Promise<void>;
  1262. /**
  1263. * Adds an entry into the zip file
  1264. *
  1265. * @param filename The filename of the entry.
  1266. * @param reader The {@link Reader} instance used to read the content of the entry.
  1267. * @param options The options.
  1268. * @returns A promise resolving to an {@link EntryMetaData} instance.
  1269. */
  1270. add<ReaderType>(
  1271. filename: string,
  1272. reader?:
  1273. | Reader<ReaderType>
  1274. | ReadableReader
  1275. | ReadableStream
  1276. | Reader<unknown>[]
  1277. | ReadableReader[]
  1278. | ReadableStream[],
  1279. options?: ZipWriterAddDataOptions
  1280. ): Promise<EntryMetaData>;
  1281. /**
  1282. * Removes an entry from the central directory that will be written for the zip file. The entry
  1283. * data itself cannot be removed because it has already been streamed to the output.
  1284. *
  1285. * @param entry The entry to remove. This can be an {@link Entry} instance or the filename of the entry.
  1286. * @returns `true` if the entry has been removed, `false` otherwise.
  1287. */
  1288. remove(entry: Entry | string): boolean;
  1289. /**
  1290. * Writes the entries directory, writes the global comment, and returns the content of the zip file
  1291. *
  1292. * @param comment The global comment of the zip file.
  1293. * @param options The options.
  1294. * @returns The content of the zip file.
  1295. */
  1296. close(comment?: Uint8Array, options?: ZipWriterCloseOptions): Promise<Type>;
  1297. }
  1298. /**
  1299. * Represents the options passed to {@link ZipWriter#add}.
  1300. */
  1301. export interface ZipWriterAddDataOptions
  1302. extends ZipWriterConstructorOptions,
  1303. EntryDataOnprogressOptions,
  1304. WorkerConfiguration {
  1305. /**
  1306. * `true` if the entry is a directory.
  1307. *
  1308. * @defaultValue false
  1309. */
  1310. directory?: boolean;
  1311. /**
  1312. * `true` if the entry is an executable file.
  1313. *
  1314. * @defaultValue false
  1315. */
  1316. executable?: boolean;
  1317. /**
  1318. * The comment of the entry.
  1319. */
  1320. comment?: string;
  1321. /**
  1322. * The extra field of the entry.
  1323. */
  1324. extraField?: Map<number, Uint8Array>;
  1325. /**
  1326. * The uncompressed size of the entry. This option is ignored if the {@link ZipWriterConstructorOptions#passThrough} option is not set to `true`.
  1327. */
  1328. uncompressedSize?: number;
  1329. /**
  1330. * The signature (CRC32 checksum) of the content. This option is ignored if the {@link ZipWriterConstructorOptions#passThrough} option is not set to `true`.
  1331. */
  1332. signature?: number;
  1333. }
  1334. /**
  1335. * Represents the options passed to {@link ZipWriter#close}.
  1336. */
  1337. export interface ZipWriterCloseOptions extends EntryOnprogressOptions {
  1338. /**
  1339. * `true` to use Zip64 to write the entries directory.
  1340. *
  1341. * @defaultValue false
  1342. */
  1343. zip64?: boolean;
  1344. /**
  1345. * `true` to prevent closing of {@link WritableWriter#writable}.
  1346. *
  1347. * @defaultValue false
  1348. */
  1349. preventClose?: boolean;
  1350. }
  1351. /**
  1352. * Represents options passed to the constructor of {@link ZipWriter}, {@link ZipWriter#add} and `{@link ZipDirectoryEntry}#export*`.
  1353. */
  1354. export interface ZipWriterConstructorOptions extends WorkerConfiguration {
  1355. /**
  1356. * `true` to use Zip64 to store the entry.
  1357. *
  1358. * `zip64` is automatically set to `true` when necessary (e.g. compressed data larger than 4GB or with unknown size).
  1359. *
  1360. * @defaultValue false
  1361. */
  1362. zip64?: boolean;
  1363. /**
  1364. * `true` to prevent closing of {@link WritableWriter#writable}.
  1365. *
  1366. * @defaultValue false
  1367. */
  1368. preventClose?: boolean;
  1369. /**
  1370. * The level of compression.
  1371. *
  1372. * The minimum value is 0 and means that no compression is applied. The maximum value is 9.
  1373. *
  1374. * @defaultValue 6
  1375. */
  1376. level?: number;
  1377. /**
  1378. * `true` to write entry data in a buffer before appending it to the zip file.
  1379. *
  1380. * `bufferedWrite` is automatically set to `true` when compressing more than one entry in parallel.
  1381. *
  1382. * @defaultValue false
  1383. */
  1384. bufferedWrite?: boolean;
  1385. /**
  1386. * An async factory function that returns a `TransformStream`-like object (`{ writable, readable }`) used as a temporary buffer when entries are written in parallel.
  1387. *
  1388. * When provided, this replaces the default in-memory `TransformStream` buffer, allowing data to be stored externally (e.g. filesystem, OPFS, network).
  1389. * The `writable` side receives compressed entry data. The `readable` side is consumed when the entry is replayed into the final zip stream.
  1390. */
  1391. createTempStream?: () => Promise<{ writable: WritableStream; readable: ReadableStream }>;
  1392. /**
  1393. * `true` to keep the order of the entry physically in the zip file.
  1394. *
  1395. * When set to `true`, the use of web workers will be improved.
  1396. *
  1397. * @defaultValue true
  1398. */
  1399. keepOrder?: boolean;
  1400. /**
  1401. * The password used to encrypt the content of the entry.
  1402. */
  1403. password?: string;
  1404. /**
  1405. * The password used to encrypt the content of the entry (raw).
  1406. */
  1407. rawPassword?: Uint8Array;
  1408. /**
  1409. * The encryption strength (AES):
  1410. * - 1: 128-bit encryption key
  1411. * - 2: 192-bit encryption key
  1412. * - 3: 256-bit encryption key
  1413. *
  1414. * @defaultValue 3
  1415. */
  1416. encryptionStrength?: 1 | 2 | 3;
  1417. /**
  1418. * The `AbortSignal` instance used to cancel the compression.
  1419. */
  1420. signal?: AbortSignal;
  1421. /**
  1422. * The last modification date.
  1423. *
  1424. * @defaultValue The current date.
  1425. */
  1426. lastModDate?: Date;
  1427. /**
  1428. * The last access date.
  1429. *
  1430. * This option is ignored if the {@link ZipWriterConstructorOptions#extendedTimestamp} option is set to `false`.
  1431. *
  1432. * @defaultValue The current date.
  1433. */
  1434. lastAccessDate?: Date;
  1435. /**
  1436. * The creation date.
  1437. *
  1438. * This option is ignored if the {@link ZipWriterConstructorOptions#extendedTimestamp} option is set to `false`.
  1439. *
  1440. * @defaultValue The current date.
  1441. */
  1442. creationDate?: Date;
  1443. /**
  1444. * `true` to store extended timestamp extra fields.
  1445. *
  1446. * When set to `false`, the maximum last modification date cannot exceed November 31, 2107 and the maximum accuracy is 2 seconds.
  1447. *
  1448. * @defaultValue true
  1449. */
  1450. extendedTimestamp?: boolean;
  1451. /**
  1452. * `true` to use the ZipCrypto algorithm to encrypt the content of the entry. Setting it to `true` will also
  1453. * set the {@link ZipWriterConstructorOptions#dataDescriptor} to `true`.
  1454. *
  1455. * It is not recommended to set `zipCrypto` to `true` because the ZipCrypto encryption can be easily broken.
  1456. *
  1457. * @defaultValue false
  1458. */
  1459. zipCrypto?: boolean;
  1460. /**
  1461. * The "Version" field.
  1462. */
  1463. version?: number;
  1464. /**
  1465. * The "Version made by" field.
  1466. *
  1467. * @defaultValue 20
  1468. */
  1469. versionMadeBy?: number;
  1470. /**
  1471. * `true` to mark the file names as UTF-8 setting the general purpose bit 11 in the header (see Appendix D -
  1472. * Language Encoding (EFS)), `false` to mark the names as compliant with the original IBM Code Page 437.
  1473. *
  1474. * Note that this does not ensure that the file names are in the correct encoding.
  1475. *
  1476. * @defaultValue true
  1477. */
  1478. useUnicodeFileNames?: boolean;
  1479. /**
  1480. * `true` to add a data descriptor.
  1481. *
  1482. * When set to `false`, the {@link ZipWriterConstructorOptions#bufferedWrite} option will automatically be
  1483. * set to `true`. It will be automatically set to `false` when it is `undefined` and the
  1484. * {@link ZipWriterConstructorOptions#bufferedWrite} option is set to `true`, or when the
  1485. * {@link ZipWriterConstructorOptions#zipCrypto} option is set to `true`. Otherwise, the default value is `true`.
  1486. */
  1487. dataDescriptor?: boolean;
  1488. /**
  1489. * `true` to add the signature of the data descriptor.
  1490. *
  1491. * @defaultValue false
  1492. */
  1493. dataDescriptorSignature?: boolean;
  1494. /**
  1495. * `true` to write {@link EntryMetaData#externalFileAttributes} in MS-DOS format for folder entries.
  1496. *
  1497. * @defaultValue false
  1498. */
  1499. msDosCompatible?: boolean;
  1500. /**
  1501. * The external file attribute.
  1502. *
  1503. * @defaultValue 0
  1504. */
  1505. externalFileAttributes?: number;
  1506. /**
  1507. * The Unix owner id to write in the Unix extra field or as part of the external attributes.
  1508. */
  1509. uid?: number;
  1510. /**
  1511. * The Unix group id to write in the Unix extra field or as part of the external attributes.
  1512. */
  1513. gid?: number;
  1514. /**
  1515. * The Unix mode (st_mode bits) to use when writing external attributes.
  1516. */
  1517. unixMode?: number;
  1518. /**
  1519. * `true` to set the setuid bit when writing the Unix mode.
  1520. */
  1521. setuid?: boolean;
  1522. /**
  1523. * `true` to set the setgid bit when writing the Unix mode.
  1524. */
  1525. setgid?: boolean;
  1526. /**
  1527. * `true` to set the sticky bit when writing the Unix mode.
  1528. */
  1529. sticky?: boolean;
  1530. /**
  1531. * Which Unix extra field format to write when creating entries that include Unix metadata.
  1532. * - "infozip": use Info-ZIP New Unix extra field
  1533. * - "unix": use the traditional Unix extra field format
  1534. */
  1535. unixExtraFieldType?: "infozip" | "unix";
  1536. /**
  1537. * The internal file attribute.
  1538. *
  1539. * @defaultValue 0
  1540. */
  1541. internalFileAttributes?: number;
  1542. /**
  1543. * When provided, the low 8-bit MS-DOS attributes to write into external file attributes.
  1544. * Must be an integer between 0 and 255.
  1545. */
  1546. msdosAttributesRaw?: number;
  1547. /**
  1548. * When provided, MS-DOS attribute flags (boolean object) to write into external file attributes low byte.
  1549. */
  1550. msdosAttributes?: {
  1551. readOnly?: boolean;
  1552. hidden?: boolean;
  1553. system?: boolean;
  1554. directory?: boolean;
  1555. archive?: boolean;
  1556. };
  1557. /**
  1558. * `false` to never write disk numbers in zip64 data.
  1559. *
  1560. * @defaultValue true
  1561. */
  1562. supportZip64SplitFile?: boolean;
  1563. /**
  1564. * `true`to produce zip files compatible with the USDZ specification.
  1565. *
  1566. * @defaultValue false
  1567. */
  1568. usdz?: boolean;
  1569. /**
  1570. * `true` to write the data as-is without compressing it and without crypting it.
  1571. */
  1572. passThrough?: boolean;
  1573. /**
  1574. * `true` to write encrypted data when `passThrough` is set to `true`.
  1575. */
  1576. encrypted?: boolean;
  1577. /**
  1578. * The offset of the first entry in the zip file.
  1579. */
  1580. offset?: number;
  1581. /**
  1582. * The compression method (e.g. 8 for DEFLATE, 0 for STORE).
  1583. */
  1584. compressionMethod?: number;
  1585. /**
  1586. * The function called for encoding the filename and the comment of the entry.
  1587. *
  1588. * @param text The text to encode.
  1589. * @returns The encoded text or `undefined` if the text should be encoded by zip.js.
  1590. */
  1591. encodeText?(text: string): Uint8Array | undefined;
  1592. }
  1593. /**
  1594. * Represents options passed to {@link FileEntry#getData}, {@link ZipWriter.add} and `{@link ZipDirectory}.export*`.
  1595. */
  1596. export interface EntryDataOnprogressOptions {
  1597. /**
  1598. * The function called when starting compression/decompression.
  1599. *
  1600. * @param total The total number of bytes.
  1601. * @returns An empty promise or `undefined`.
  1602. */
  1603. onstart?(total: number): Promise<void> | void;
  1604. /**
  1605. * The function called during compression/decompression.
  1606. *
  1607. * @param progress The current progress in bytes.
  1608. * @param total The total number of bytes.
  1609. * @returns An empty promise or `undefined`.
  1610. */
  1611. onprogress?(progress: number, total: number): Promise<void> | void;
  1612. /**
  1613. * The function called when ending compression/decompression.
  1614. *
  1615. * @param computedSize The total number of bytes (computed).
  1616. * @returns An empty promise or `undefined`.
  1617. */
  1618. onend?(computedSize: number): Promise<void> | void;
  1619. }
  1620. /**
  1621. * Represents options passed to {@link ZipReader#getEntries}, {@link ZipReader#getEntriesGenerator}, and {@link ZipWriter#close}.
  1622. */
  1623. export interface EntryOnprogressOptions {
  1624. /**
  1625. * The function called each time an entry is read/written.
  1626. *
  1627. * @param progress The entry index.
  1628. * @param total The total number of entries.
  1629. * @param entry The entry being read/written.
  1630. * @returns An empty promise or `undefined`.
  1631. */
  1632. onprogress?(
  1633. progress: number,
  1634. total: number,
  1635. entry: EntryMetaData
  1636. ): Promise<void> | void;
  1637. }
  1638. /**
  1639. * Represents an entry in a zip file (Filesystem API).
  1640. */
  1641. declare class ZipEntry {
  1642. /**
  1643. * The relative filename of the entry.
  1644. */
  1645. name: string;
  1646. /**
  1647. * The underlying {@link EntryMetaData} instance.
  1648. */
  1649. data?: EntryMetaData;
  1650. /**
  1651. * The ID of the instance.
  1652. */
  1653. id: number;
  1654. /**
  1655. * The parent directory of the entry.
  1656. */
  1657. parent?: ZipEntry;
  1658. /**
  1659. * The uncompressed size of the content.
  1660. */
  1661. uncompressedSize: number;
  1662. /**
  1663. * The children of the entry.
  1664. */
  1665. children: ZipEntry[];
  1666. /**
  1667. * Clones the entry
  1668. *
  1669. * @param deepClone `true` to clone all the descendants.
  1670. */
  1671. clone(deepClone?: boolean): ZipEntry;
  1672. /**
  1673. * Returns the full filename of the entry
  1674. */
  1675. getFullname(): string;
  1676. /**
  1677. * Returns the filename of the entry relative to a parent directory
  1678. */
  1679. getRelativeName(ancestor: ZipDirectoryEntry): string;
  1680. /**
  1681. * Tests if a {@link ZipDirectoryEntry} instance is an ancestor of the entry
  1682. *
  1683. * @param ancestor The {@link ZipDirectoryEntry} instance.
  1684. */
  1685. isDescendantOf(ancestor: ZipDirectoryEntry): boolean;
  1686. /**
  1687. * Tests if the entry or any of its children is password protected
  1688. */
  1689. isPasswordProtected(): boolean;
  1690. /**
  1691. * Tests the password on the entry and all children if any, returns `true` if the entry is not password protected
  1692. */
  1693. checkPassword(
  1694. password: string,
  1695. options?: EntryGetDataOptions
  1696. ): Promise<boolean>;
  1697. /**
  1698. * Set the name of the entry
  1699. *
  1700. * @param name The new name of the entry.
  1701. */
  1702. rename(name: string): void;
  1703. }
  1704. /**
  1705. * Represents a file entry in the zip (Filesystem API).
  1706. */
  1707. export class ZipFileEntry<ReaderType, WriterType> extends ZipEntry {
  1708. /**
  1709. * `void` for {@link ZipFileEntry} instances.
  1710. */
  1711. directory: void;
  1712. /**
  1713. * The {@link Reader} instance used to read the content of the entry.
  1714. */
  1715. reader:
  1716. | Reader<ReaderType>
  1717. | ReadableReader
  1718. | ReadableStream
  1719. | Reader<unknown>[]
  1720. | ReadableReader[]
  1721. | ReadableStream[];
  1722. /**
  1723. * The {@link Writer} instance used to write the content of the entry.
  1724. */
  1725. writer:
  1726. | Writer<WriterType>
  1727. | WritableWriter
  1728. | WritableStream
  1729. | AsyncGenerator<Writer<unknown> | WritableWriter | WritableStream>;
  1730. /**
  1731. * Retrieves the text content of the entry as a `string`
  1732. *
  1733. * @param encoding The encoding of the text.
  1734. * @param options The options.
  1735. * @returns A promise resolving to a `string`.
  1736. */
  1737. getText(encoding?: string, options?: EntryGetDataOptions): Promise<string>;
  1738. /**
  1739. * Retrieves the content of the entry as a `Blob` instance
  1740. *
  1741. * @param mimeType The MIME type of the content.
  1742. * @param options The options.
  1743. * @returns A promise resolving to a `Blob` instance.
  1744. */
  1745. getBlob(mimeType?: string, options?: EntryGetDataOptions): Promise<Blob>;
  1746. /**
  1747. * Retrieves the content of the entry as as a Data URI `string` encoded in Base64
  1748. *
  1749. * @param mimeType The MIME type of the content.
  1750. * @param options The options.
  1751. * @returns A promise resolving to a Data URI `string` encoded in Base64.
  1752. */
  1753. getData64URI(
  1754. mimeType?: string,
  1755. options?: EntryGetDataOptions
  1756. ): Promise<string>;
  1757. /**
  1758. * Retrieves the content of the entry as a `Uint8Array` instance
  1759. *
  1760. * @param options The options.
  1761. * @returns A promise resolving to a `Uint8Array` instance.
  1762. */
  1763. getUint8Array(options?: EntryGetDataOptions): Promise<Uint8Array>;
  1764. /**
  1765. * Retrieves the content of the entry via a `WritableStream` instance
  1766. *
  1767. * @param writable The `WritableStream` instance.
  1768. * @param options The options.
  1769. * @returns A promise resolving to the `WritableStream` instance.
  1770. */
  1771. getWritable(
  1772. writable?: WritableStream,
  1773. options?: EntryGetDataOptions
  1774. ): Promise<WritableStream>;
  1775. /**
  1776. * Retrieves the content of the entry via a {@link Writer} instance
  1777. *
  1778. * @param writer The {@link Writer} instance.
  1779. * @param options The options.
  1780. * @returns A promise resolving to data associated to the {@link Writer} instance.
  1781. */
  1782. getData<Type>(
  1783. writer:
  1784. | Writer<unknown>
  1785. | WritableWriter
  1786. | WritableStream
  1787. | AsyncGenerator<Writer<unknown> | WritableWriter | WritableStream>,
  1788. options?: EntryGetDataOptions
  1789. ): Promise<Type>;
  1790. /**
  1791. * Retrieves the content of the entry as an `ArrayBuffer` instance
  1792. *
  1793. * @param options The options.
  1794. * @returns A promise resolving to an `ArrayBuffer` instance.
  1795. */
  1796. getArrayBuffer(options?: EntryGetDataOptions): Promise<ArrayBuffer>;
  1797. /**
  1798. * Replaces the content of the entry with a `Blob` instance
  1799. *
  1800. * @param blob The `Blob` instance.
  1801. */
  1802. replaceBlob(blob: Blob): void;
  1803. /**
  1804. * Replaces the content of the entry with a `string`
  1805. *
  1806. * @param text The `string`.
  1807. */
  1808. replaceText(text: string): void;
  1809. /**
  1810. * Replaces the content of the entry with a Data URI `string` encoded in Base64
  1811. *
  1812. * @param dataURI The Data URI `string` encoded in Base64.
  1813. */
  1814. replaceData64URI(dataURI: string): void;
  1815. /**
  1816. * Replaces the content of the entry with a `Uint8Array` instance
  1817. *
  1818. * @param array The `Uint8Array` instance.
  1819. */
  1820. replaceUint8Array(array: Uint8Array): void;
  1821. /**
  1822. * Replaces the content of the entry with a `ReadableStream` instance
  1823. *
  1824. * @param readable The `ReadableStream` instance.
  1825. */
  1826. replaceReadable(readable: ReadableStream): void;
  1827. }
  1828. /**
  1829. * Represents a directory entry in the zip (Filesystem API).
  1830. */
  1831. export class ZipDirectoryEntry extends ZipEntry {
  1832. /**
  1833. * `true` for {@link ZipDirectoryEntry} instances.
  1834. */
  1835. directory: true;
  1836. /**
  1837. * Gets a {@link ZipEntry} child instance from its relative filename
  1838. *
  1839. * @param name The relative filename.
  1840. * @returns A {@link ZipFileEntry} or a {@link ZipDirectoryEntry} instance (use the {@link ZipFileEntry#directory} and {@link ZipDirectoryEntry#directory} properties to differentiate entries).
  1841. */
  1842. getChildByName(name: string): ZipEntry | undefined;
  1843. /**
  1844. * Adds a directory
  1845. *
  1846. * @param name The relative filename of the directory.
  1847. * @param options The options.
  1848. * @returns A {@link ZipDirectoryEntry} instance.
  1849. */
  1850. addDirectory(
  1851. name: string,
  1852. options?: ZipWriterAddDataOptions
  1853. ): ZipDirectoryEntry;
  1854. /**
  1855. * Adds an entry with content provided as text
  1856. *
  1857. * @param name The relative filename of the entry.
  1858. * @param text The text.
  1859. * @param options The options.
  1860. * @returns A {@link ZipFileEntry} instance.
  1861. */
  1862. addText(
  1863. name: string,
  1864. text: string,
  1865. options?: ZipWriterAddDataOptions
  1866. ): ZipFileEntry<string, string>;
  1867. /**
  1868. * Adds a entry entry with content provided as a `Blob` instance
  1869. *
  1870. * @param name The relative filename of the entry.
  1871. * @param blob The `Blob` instance.
  1872. * @param options The options.
  1873. * @returns A {@link ZipFileEntry} instance.
  1874. */
  1875. addBlob(
  1876. name: string,
  1877. blob: Blob,
  1878. options?: ZipWriterAddDataOptions
  1879. ): ZipFileEntry<Blob, Blob>;
  1880. /**
  1881. * Adds a entry entry with content provided as a Data URI `string` encoded in Base64
  1882. *
  1883. * @param name The relative filename of the entry.
  1884. * @param dataURI The Data URI `string` encoded in Base64.
  1885. * @param options The options.
  1886. * @returns A {@link ZipFileEntry} instance.
  1887. */
  1888. addData64URI(
  1889. name: string,
  1890. dataURI: string,
  1891. options?: ZipWriterAddDataOptions
  1892. ): ZipFileEntry<string, string>;
  1893. /**
  1894. * Adds an entry with content provided as a `Uint8Array` instance
  1895. *
  1896. * @param name The relative filename of the entry.
  1897. * @param array The `Uint8Array` instance.
  1898. * @param options The options.
  1899. * @returns A {@link ZipFileEntry} instance.
  1900. */
  1901. addUint8Array(
  1902. name: string,
  1903. array: Uint8Array,
  1904. options?: ZipWriterAddDataOptions
  1905. ): ZipFileEntry<Uint8Array, Uint8Array>;
  1906. /**
  1907. * Adds an entry with content fetched from a URL
  1908. *
  1909. * @param name The relative filename of the entry.
  1910. * @param url The URL.
  1911. * @param options The options.
  1912. * @returns A {@link ZipFileEntry} instance.
  1913. */
  1914. addHttpContent(
  1915. name: string,
  1916. url: string,
  1917. options?: HttpOptions & ZipWriterAddDataOptions
  1918. ): ZipFileEntry<string, void>;
  1919. /**
  1920. * Adds a entry entry with content provided via a `ReadableStream` instance
  1921. *
  1922. * @param name The relative filename of the entry.
  1923. * @param readable The `ReadableStream` instance.
  1924. * @param options The options.
  1925. * @returns A {@link ZipFileEntry} instance.
  1926. */
  1927. addReadable(
  1928. name: string,
  1929. readable: ReadableStream,
  1930. options?: ZipWriterAddDataOptions
  1931. ): ZipFileEntry<ReadableStream, void>;
  1932. /**
  1933. * Adds an entry with content provided via a `File` instance
  1934. *
  1935. * @param file The `File` instance.
  1936. * @param options The options.
  1937. * @returns A promise resolving to a {@link ZipFileEntry} or a {@link ZipDirectoryEntry} instance.
  1938. */
  1939. addFile(file: File, options?: ZipWriterAddDataOptions): Promise<ZipEntry>;
  1940. /**
  1941. * Adds an entry with content provided via a `FileSystemEntry` instance
  1942. *
  1943. * @param fileSystemEntry The `FileSystemEntry` instance.
  1944. * @param options The options.
  1945. * @returns A promise resolving to an array of {@link ZipFileEntry} or a {@link ZipDirectoryEntry} instances.
  1946. */
  1947. addFileSystemEntry(
  1948. fileSystemEntry: FileSystemEntryLike,
  1949. options?: ZipWriterAddDataOptions
  1950. ): Promise<ZipEntry[]>;
  1951. /**
  1952. * Adds an entry with content provided via a `FileSystemHandle` instance
  1953. *
  1954. * @param fileSystemHandle The `fileSystemHandle` instance.
  1955. * @param options The options.
  1956. * @returns A promise resolving to an array of {@link ZipFileEntry} or a {@link ZipDirectoryEntry} instances.
  1957. */
  1958. addFileSystemHandle(
  1959. fileSystemHandle: FileSystemHandleLike,
  1960. options?: ZipWriterAddDataOptions
  1961. ): Promise<ZipEntry[]>;
  1962. /**
  1963. * Extracts a zip file provided as a `Blob` instance into the entry
  1964. *
  1965. * @param blob The `Blob` instance.
  1966. * @param options The options.
  1967. */
  1968. importBlob(
  1969. blob: Blob,
  1970. options?: ZipReaderConstructorOptions
  1971. ): Promise<[ZipEntry]>;
  1972. /**
  1973. * Extracts a zip file provided as a Data URI `string` encoded in Base64 into the entry
  1974. *
  1975. * @param dataURI The Data URI `string` encoded in Base64.
  1976. * @param options The options.
  1977. */
  1978. importData64URI(
  1979. dataURI: string,
  1980. options?: ZipReaderConstructorOptions
  1981. ): Promise<[ZipEntry]>;
  1982. /**
  1983. * Extracts a zip file provided as a `Uint8Array` instance into the entry
  1984. *
  1985. * @param array The `Uint8Array` instance.
  1986. * @param options The options.
  1987. */
  1988. importUint8Array(
  1989. array: Uint8Array,
  1990. options?: ZipReaderConstructorOptions
  1991. ): Promise<[ZipEntry]>;
  1992. /**
  1993. * Extracts a zip file fetched from a URL into the entry
  1994. *
  1995. * @param url The URL.
  1996. * @param options The options.
  1997. */
  1998. importHttpContent(
  1999. url: string,
  2000. options?: ZipDirectoryEntryImportHttpOptions
  2001. ): Promise<[ZipEntry]>;
  2002. /**
  2003. * Extracts a zip file provided via a `ReadableStream` instance into the entry
  2004. *
  2005. * @param readable The `ReadableStream` instance.
  2006. * @param options The options.
  2007. */
  2008. importReadable(
  2009. readable: ReadableStream,
  2010. options?: ZipReaderConstructorOptions
  2011. ): Promise<[ZipEntry]>;
  2012. /**
  2013. * Extracts a zip file provided via a custom {@link Reader} instance into the entry
  2014. *
  2015. * @param reader The {@link Reader} instance.
  2016. * @param options The options.
  2017. */
  2018. importZip(
  2019. reader:
  2020. | Reader<unknown>
  2021. | ReadableReader
  2022. | ReadableStream
  2023. | Reader<unknown>[]
  2024. | ReadableReader[]
  2025. | ReadableStream[],
  2026. options?: ZipReaderConstructorOptions
  2027. ): Promise<[ZipEntry]>;
  2028. /**
  2029. * Returns a `Blob` instance containing a zip file of the entry and its descendants
  2030. *
  2031. * @param options The options.
  2032. * @returns A promise resolving to the `Blob` instance.
  2033. */
  2034. exportBlob(options?: ZipDirectoryEntryExportOptions): Promise<Blob>;
  2035. /**
  2036. * Returns a Data URI `string` encoded in Base64 containing a zip file of the entry and its descendants
  2037. *
  2038. * @param options The options.
  2039. * @returns A promise resolving to the Data URI `string` encoded in Base64.
  2040. */
  2041. exportData64URI(options?: ZipDirectoryEntryExportOptions): Promise<string>;
  2042. /**
  2043. * Returns a `Uint8Array` instance containing a zip file of the entry and its descendants
  2044. *
  2045. * @param options The options.
  2046. * @returns A promise resolving to the `Uint8Array` instance.
  2047. */
  2048. exportUint8Array(
  2049. options?: ZipDirectoryEntryExportOptions
  2050. ): Promise<Uint8Array>;
  2051. /**
  2052. * Creates a zip file via a `WritableStream` instance containing the entry and its descendants
  2053. *
  2054. * @param writable The `WritableStream` instance.
  2055. * @param options The options.
  2056. * @returns A promise resolving to the `Uint8Array` instance.
  2057. */
  2058. exportWritable(
  2059. writable?: WritableStream,
  2060. options?: ZipDirectoryEntryExportOptions
  2061. ): Promise<WritableStream>;
  2062. /**
  2063. * Creates a zip file via a custom {@link Writer} instance containing the entry and its descendants
  2064. *
  2065. * @param writer The {@link Writer} instance.
  2066. * @param options The options.
  2067. * @returns A promise resolving to the data.
  2068. */
  2069. exportZip(
  2070. writer:
  2071. | Writer<unknown>
  2072. | WritableWriter
  2073. | WritableStream
  2074. | AsyncGenerator<Writer<unknown> | WritableWriter | WritableStream>,
  2075. options?: ZipDirectoryEntryExportOptions
  2076. ): Promise<unknown>;
  2077. }
  2078. /**
  2079. * Represents the options passed to {@link ZipDirectoryEntry#importHttpContent}.
  2080. */
  2081. export interface ZipDirectoryEntryImportHttpOptions
  2082. extends ZipReaderConstructorOptions,
  2083. HttpOptions {}
  2084. /**
  2085. * Represents the options passed to `{@link ZipDirectoryEntry}#export*()`.
  2086. */
  2087. export interface ZipDirectoryEntryExportOptions
  2088. extends ZipWriterConstructorOptions,
  2089. EntryDataOnprogressOptions {
  2090. /**
  2091. * `true` to use filenames relative to the entry instead of full filenames.
  2092. */
  2093. relativePath?: boolean;
  2094. /**
  2095. * The MIME type of the exported data when relevant.
  2096. */
  2097. mimeType?: string;
  2098. /**
  2099. * The options passed to the Reader instances
  2100. */
  2101. readerOptions?: ZipReaderConstructorOptions;
  2102. }
  2103. /**
  2104. * Represents a Filesystem instance.
  2105. *
  2106. * @example
  2107. * Here is an example showing how to create and read a zip file containing a compressed text file:
  2108. * ```
  2109. * const TEXT_CONTENT = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.";
  2110. * const FILENAME = "lorem.txt";
  2111. * const BLOB = new Blob([TEXT_CONTENT], { type: zip.getMimeType(FILENAME) });
  2112. * let zipFs = new zip.fs.FS();
  2113. * zipFs.addBlob("lorem.txt", BLOB);
  2114. * const zippedBlob = await zipFs.exportBlob();
  2115. * zipFs = new zip.fs.FS();
  2116. * await zipFs.importBlob(zippedBlob);
  2117. * const firstEntry = zipFs.children[0];
  2118. * const unzippedBlob = await firstEntry.getBlob(zip.getMimeType(firstEntry.name));
  2119. * ```
  2120. */
  2121. export class FS extends ZipDirectoryEntry {
  2122. /**
  2123. * The root directory.
  2124. */
  2125. root: ZipDirectoryEntry;
  2126. /**
  2127. * Removes a {@link ZipEntry} instance and its children
  2128. *
  2129. * @param entry The {@link ZipEntry} instance to remove.
  2130. */
  2131. remove(entry: ZipEntry): void;
  2132. /**
  2133. * Moves a {@link ZipEntry} instance and its children into a {@link ZipDirectoryEntry} instance
  2134. *
  2135. * @param entry The {@link ZipEntry} instance to move.
  2136. * @param destination The {@link ZipDirectoryEntry} instance.
  2137. */
  2138. move(entry: ZipEntry, destination: ZipDirectoryEntry): void;
  2139. /**
  2140. * Returns a {@link ZipEntry} instance from its full filename
  2141. *
  2142. * @param fullname The full filename.
  2143. * @returns The {@link ZipEntry} instance.
  2144. */
  2145. find(fullname: string): ZipEntry | undefined;
  2146. /**
  2147. * Returns a {@link ZipEntry} instance from the value of {@link ZipEntry#id}
  2148. *
  2149. * @param id The id of the {@link ZipEntry} instance.
  2150. * @returns The {@link ZipEntry} instance.
  2151. */
  2152. getById(id: number): ZipEntry | undefined;
  2153. }
  2154. /**
  2155. * The Filesystem API.
  2156. */
  2157. export const fs: {
  2158. /**
  2159. * The Filesystem constructor.
  2160. *
  2161. * @defaultValue {@link FS}
  2162. */
  2163. FS: typeof FS;
  2164. /**
  2165. * The {@link ZipDirectoryEntry} constructor.
  2166. *
  2167. * @defaultValue {@link ZipDirectoryEntry}
  2168. */
  2169. ZipDirectoryEntry: typeof ZipDirectoryEntry;
  2170. /**
  2171. * The {@link ZipFileEntry} constructor.
  2172. *
  2173. * @defaultValue {@link ZipFileEntry}
  2174. */
  2175. ZipFileEntry: typeof ZipFileEntry;
  2176. };
  2177. // The error messages.
  2178. /**
  2179. * HTTP range error
  2180. */
  2181. export const ERR_HTTP_RANGE: string;
  2182. /**
  2183. * Zip format error
  2184. */
  2185. export const ERR_BAD_FORMAT: string;
  2186. /**
  2187. * End of Central Directory Record not found error
  2188. */
  2189. export const ERR_EOCDR_NOT_FOUND: string;
  2190. /**
  2191. * Zip64 End of Central Directory Locator not found error
  2192. */
  2193. export const ERR_EOCDR_LOCATOR_ZIP64_NOT_FOUND: string;
  2194. /**
  2195. * Central Directory not found error
  2196. */
  2197. export const ERR_CENTRAL_DIRECTORY_NOT_FOUND: string;
  2198. /**
  2199. * Local file header not found error
  2200. */
  2201. export const ERR_LOCAL_FILE_HEADER_NOT_FOUND: string;
  2202. /**
  2203. * Extra field Zip64 not found error
  2204. */
  2205. export const ERR_EXTRAFIELD_ZIP64_NOT_FOUND: string;
  2206. /**
  2207. * Encrypted entry error
  2208. */
  2209. export const ERR_ENCRYPTED: string;
  2210. /**
  2211. * Unsupported encryption error
  2212. */
  2213. export const ERR_UNSUPPORTED_ENCRYPTION: string;
  2214. /**
  2215. * Unsupported compression error
  2216. */
  2217. export const ERR_UNSUPPORTED_COMPRESSION: string;
  2218. /**
  2219. * Invalid signature error
  2220. */
  2221. export const ERR_INVALID_SIGNATURE: string;
  2222. /**
  2223. * Invalid uncompressed size error
  2224. */
  2225. export const ERR_INVALID_UNCOMPRESSED_SIZE: string;
  2226. /**
  2227. * Invalid password error
  2228. */
  2229. export const ERR_INVALID_PASSWORD: string;
  2230. /**
  2231. * Duplicate entry error
  2232. */
  2233. export const ERR_DUPLICATED_NAME: string;
  2234. /**
  2235. * Invalid comment error
  2236. */
  2237. export const ERR_INVALID_COMMENT: string;
  2238. /**
  2239. * Invalid entry name error
  2240. */
  2241. export const ERR_INVALID_ENTRY_NAME: string;
  2242. /**
  2243. * Invalid entry comment error
  2244. */
  2245. export const ERR_INVALID_ENTRY_COMMENT: string;
  2246. /**
  2247. * Invalid version error
  2248. */
  2249. export const ERR_INVALID_VERSION: string;
  2250. /**
  2251. * Invalid extra field type error
  2252. */
  2253. export const ERR_INVALID_EXTRAFIELD_TYPE: string;
  2254. /**
  2255. * Invalid extra field data error
  2256. */
  2257. export const ERR_INVALID_EXTRAFIELD_DATA: string;
  2258. /**
  2259. * Invalid encryption strength error
  2260. */
  2261. export const ERR_INVALID_ENCRYPTION_STRENGTH: string;
  2262. /**
  2263. * Invalid format error
  2264. */
  2265. export const ERR_UNSUPPORTED_FORMAT: string;
  2266. /**
  2267. * Split zip file error
  2268. */
  2269. export const ERR_SPLIT_ZIP_FILE: string;
  2270. /**
  2271. * Overlapping entry error
  2272. */
  2273. export const ERR_OVERLAPPING_ENTRY: string;
  2274. /**
  2275. * Iteration completed too soon error
  2276. */
  2277. export const ERR_ITERATOR_COMPLETED_TOO_SOON: string;
  2278. /**
  2279. * Undefined uncompressed size error
  2280. */
  2281. export const ERR_UNDEFINED_UNCOMPRESSED_SIZE: string;
  2282. /**
  2283. * Writer not initialized error
  2284. */
  2285. export const ERR_WRITER_NOT_INITIALIZED: string;
  2286. /**
  2287. * Zip file not empty error
  2288. */
  2289. export const ERR_ZIP_NOT_EMPTY: string;