Repository: Stuk/jszip Branch: main Commit: 643714aa770a Files: 145 Total size: 979.1 KB Directory structure: gitextract_lj84rw3_/ ├── .codeclimate.yml ├── .editorconfig ├── .eslintrc.js ├── .github/ │ ├── FUNDING.yml │ ├── actions/ │ │ └── setup/ │ │ └── action.yml │ └── workflows/ │ ├── benchmark.yml │ └── main.yml ├── .gitignore ├── .npmignore ├── CHANGES.md ├── Gruntfile.js ├── LICENSE.markdown ├── README.markdown ├── _config.yml ├── bower.json ├── component.json ├── dist/ │ └── jszip.js ├── docs/ │ ├── APPNOTE.TXT │ ├── ZIP spec.txt │ └── references.txt ├── documentation/ │ ├── .eslintrc.js │ ├── _layouts/ │ │ └── default.html │ ├── api_jszip/ │ │ ├── constructor.md │ │ ├── external.md │ │ ├── file_data.md │ │ ├── file_name.md │ │ ├── file_regex.md │ │ ├── filter.md │ │ ├── folder_name.md │ │ ├── folder_regex.md │ │ ├── for_each.md │ │ ├── generate_async.md │ │ ├── generate_internal_stream.md │ │ ├── generate_node_stream.md │ │ ├── load_async.md │ │ ├── load_async_object.md │ │ ├── remove.md │ │ ├── support.md │ │ └── version.md │ ├── api_jszip.md │ ├── api_streamhelper/ │ │ ├── accumulate.md │ │ ├── on.md │ │ ├── pause.md │ │ └── resume.md │ ├── api_streamhelper.md │ ├── api_zipobject/ │ │ ├── async.md │ │ ├── internal_stream.md │ │ └── node_stream.md │ ├── api_zipobject.md │ ├── contributing.md │ ├── css/ │ │ ├── main.css │ │ └── pygments.css │ ├── examples/ │ │ ├── download-zip-file.html │ │ ├── download-zip-file.inc/ │ │ │ ├── blob.html │ │ │ ├── blob.js │ │ │ ├── data_uri.html │ │ │ └── data_uri.js │ │ ├── downloader.html │ │ ├── downloader.inc/ │ │ │ ├── downloader.html │ │ │ ├── downloader.js │ │ │ └── helpers.js │ │ ├── get-binary-files-ajax.html │ │ ├── get-binary-files-ajax.inc/ │ │ │ ├── fetch_api.html │ │ │ ├── fetch_api.js │ │ │ ├── jszip_utils.html │ │ │ └── jszip_utils.js │ │ ├── read-local-file-api.html │ │ └── read-local-file-api.inc/ │ │ ├── read.html │ │ └── read.js │ ├── examples.md │ ├── faq.md │ ├── howto/ │ │ ├── read_zip.md │ │ └── write_zip.md │ ├── limitations.md │ └── upgrade_guide.md ├── index.d.ts ├── index.html ├── lib/ │ ├── base64.js │ ├── compressedObject.js │ ├── compressions.js │ ├── crc32.js │ ├── defaults.js │ ├── external.js │ ├── flate.js │ ├── generate/ │ │ ├── ZipFileWorker.js │ │ └── index.js │ ├── index.js │ ├── license_header.js │ ├── load.js │ ├── nodejs/ │ │ ├── NodejsStreamInputAdapter.js │ │ └── NodejsStreamOutputAdapter.js │ ├── nodejsUtils.js │ ├── object.js │ ├── readable-stream-browser.js │ ├── reader/ │ │ ├── ArrayReader.js │ │ ├── DataReader.js │ │ ├── NodeBufferReader.js │ │ ├── StringReader.js │ │ ├── Uint8ArrayReader.js │ │ └── readerFor.js │ ├── signature.js │ ├── stream/ │ │ ├── ConvertWorker.js │ │ ├── Crc32Probe.js │ │ ├── DataLengthProbe.js │ │ ├── DataWorker.js │ │ ├── GenericWorker.js │ │ └── StreamHelper.js │ ├── support.js │ ├── utf8.js │ ├── utils.js │ ├── zipEntries.js │ ├── zipEntry.js │ └── zipObject.js ├── package.json ├── sponsors.md ├── test/ │ ├── .eslintrc.js │ ├── asserts/ │ │ ├── constructor.js │ │ ├── delete.js │ │ ├── deprecated.js │ │ ├── external.js │ │ ├── file.js │ │ ├── filter.js │ │ ├── foreach.js │ │ ├── generate.js │ │ ├── load.js │ │ ├── permissions.js │ │ ├── stream.js │ │ ├── unicode.js │ │ ├── utils.js │ │ └── version.js │ ├── benchmark/ │ │ ├── .eslintrc.js │ │ ├── benchmark.js │ │ ├── index.html │ │ └── node.js │ ├── helpers/ │ │ ├── browser-test-utils.js │ │ ├── node-test-utils.js │ │ └── test-utils.js │ ├── index.html │ ├── ref/ │ │ └── complex_files/ │ │ ├── AntarcticaTemps.ods │ │ ├── AntarcticaTemps.xlsx │ │ ├── Franz Kafka - The Metamorphosis.epub │ │ └── Outlook2007_Calendar.xps │ └── run.js ├── tsconfig.json └── vendor/ └── FileSaver.js ================================================ FILE CONTENTS ================================================ ================================================ FILE: .codeclimate.yml ================================================ --- engines: duplication: enabled: true config: languages: - javascript eslint: enabled: true fixme: enabled: true ratings: paths: - "lib/*.js" exclude_paths: - "dist/*" ================================================ FILE: .editorconfig ================================================ root = true [*] end_of_line = lf insert_final_newline = true charset = utf-8 indent_style = space indent_size = 4 ================================================ FILE: .eslintrc.js ================================================ "use strict"; module.exports = { "env": { "browser": true, "commonjs": true, "es2021": true, "node": true }, "extends": "eslint:recommended", "parserOptions": { "ecmaVersion": "latest" }, "ignorePatterns": ["vendor/*.js", "dist/*.js", "test/jquery-1.8.3.min.js"], "rules": { "indent": [ "error", 4 ], "linebreak-style": [ "error", "unix" ], "quotes": [ "error", "double" ], "semi": [ "error", "always" ], "curly": "error", "eqeqeq": "error", "no-new": "error", "no-caller": "error", "guard-for-in": "error", "no-extend-native": "error", "strict": [ "error", "global" ], } }; ================================================ FILE: .github/FUNDING.yml ================================================ github: Stuk ================================================ FILE: .github/actions/setup/action.yml ================================================ name: Set up workspace description: Sets up the workspace by installing Node.js and dependencies. inputs: node-version: description: The Node.js version to install. required: false default: "lts/*" runs: using: composite steps: - name: Install Node.js uses: actions/setup-node@v4 with: node-version: ${{ inputs.node-version }} cache: npm - name: Install dependencies # Because the lockfile is not compatible between all supported Node.js versions we need to run `npm install`. # When support for older versions of Node.js is dropped, this can be changed to `npm ci`. run: npm install shell: bash ================================================ FILE: .github/workflows/benchmark.yml ================================================ name: Benchmark on: push: branches: [main] jobs: benchmark: name: Run benchmark runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v4 - name: Set up workspace uses: ./.github/actions/setup - name: Install browsers run: npx playwright install --with-deps - name: Run benchmark run: npm run benchmark | tee benchmark.txt - name: Download previous benchmark data uses: actions/cache@v4 with: path: ./cache key: ${{ runner.os }}-benchmark - name: Store benchmark result uses: benchmark-action/github-action-benchmark@v1 with: tool: benchmarkjs output-file-path: benchmark.txt external-data-json-path: ./cache/benchmark-data.json github-token: ${{ secrets.GITHUB_TOKEN }} alert-threshold: 150% comment-on-alert: true fail-on-alert: true alert-comment-cc-users: "@Stuk" ================================================ FILE: .github/workflows/main.yml ================================================ name: CI on: push: branches: [main] pull_request: branches: [main] jobs: lint: name: Lint runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v4 - name: Set up workspace uses: ./.github/actions/setup - name: Run linter run: npm run lint test-node: name: Test on Node.js ${{ matrix.node-version }} runs-on: ubuntu-latest strategy: fail-fast: false matrix: node-version: [6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17] steps: - name: Checkout repository uses: actions/checkout@v4 - name: Set up workspace uses: ./.github/actions/setup with: node-version: ${{ matrix.node-version }} - name: Run tests run: npm run test-node test-browser: name: Test in browser runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v4 - name: Set up workspace uses: ./.github/actions/setup - name: Install browsers run: npx playwright install --with-deps - name: Run tests run: npm run test-browser ================================================ FILE: .gitignore ================================================ _site .c9revisions .DS_Store .jekyll-metadata *~ node_modules sauce_connect.log ================================================ FILE: .npmignore ================================================ _config.yml bower.json component.json docs documentation Gruntfile.js index.html test ================================================ FILE: CHANGES.md ================================================ --- title: Changelog layout: default section: main --- ### v3.10.1 2022-08-02 - Add sponsorship files. + If you appreciate the time spent maintaining JSZip then I would really appreciate [your sponsorship](https://github.com/sponsors/Stuk). - Consolidate metadata types and expose OnUpdateCallback [#851](https://github.com/Stuk/jszip/pull/851) and [#852](https://github.com/Stuk/jszip/pull/852) - use `const` instead `var` in example from README.markdown [#828](https://github.com/Stuk/jszip/pull/828) - Switch manual download link to HTTPS [#839](https://github.com/Stuk/jszip/pull/839) Internals: - Replace jshint with eslint [#842](https://github.com/Stuk/jszip/pull/842) - Add performance tests [#834](https://github.com/Stuk/jszip/pull/834) ### v3.10.0 2022-05-20 - Change setimmediate dependency to more efficient one. Fixes https://github.com/Stuk/jszip/issues/617 (see [#829](https://github.com/Stuk/jszip/pull/829)) - Update types of `currentFile` metadata to include `null` (see [#826](https://github.com/Stuk/jszip/pull/826)) ### v3.9.1 2022-04-06 - Fix recursive definition of `InputFileFormat` introduced in 3.9.0. ### v3.9.0 2022-04-04 - Update types JSZip#loadAsync to accept a promise for data, and remove arguments from `new JSZip()` (see [#752](https://github.com/Stuk/jszip/pull/752)) - Update types for `compressionOptions` to JSZipFileOptions and JSZipGeneratorOptions (see [#722](https://github.com/Stuk/jszip/pull/722)) - Add types for `generateInternalStream` (see [#774](https://github.com/Stuk/jszip/pull/774)) ### v3.8.0 2022-03-30 - Santize filenames when files are loaded with `loadAsync`, to avoid ["zip slip" attacks](https://snyk.io/research/zip-slip-vulnerability). The original filename is available on each zip entry as `unsafeOriginalName`. See the [documentation](https://stuk.github.io/jszip/documentation/api_jszip/load_async.html). Many thanks to McCaulay Hudson for reporting. ### v3.7.1 2021-08-05 - Fix build of `dist` files. + Note: this version ensures the changes from 3.7.0 are actually included in the `dist` files. Thanks to Evan W for reporting. ### v3.7.0 2021-07-23 - Fix: Use a null prototype object for this.files (see [#766](https://github.com/Stuk/jszip/pull/766)) + This change might break existing code if it uses prototype methods on the `.files` property of a zip object, for example `zip.files.toString()`. This approach is taken to prevent files in the zip overriding object methods that would exist on a normal object. ### v3.6.0 2021-02-09 - Fix: redirect main to dist on browsers (see [#742](https://github.com/Stuk/jszip/pull/742)) - Fix duplicate require DataLengthProbe, utils (see [#734](https://github.com/Stuk/jszip/pull/734)) - Fix small error in read_zip.md (see [#703](https://github.com/Stuk/jszip/pull/703)) ### v3.5.0 2020-05-31 - Fix 'End of data reached' error when file extra field is invalid (see [#544](https://github.com/Stuk/jszip/pull/544)). - Typescript definitions: Add null to return types of functions that may return null (see [#669](https://github.com/Stuk/jszip/pull/669)). - Typescript definitions: Correct nodeStream's type (see [#682](https://github.com/Stuk/jszip/pull/682)) - Typescript definitions: Add string output type (see [#666](https://github.com/Stuk/jszip/pull/666)) ### v3.4.0 2020-04-19 - Add Typescript type definitions (see [#601](https://github.com/Stuk/jszip/pull/601)). ### v3.3.0 2020-04-1 - Change browser module resolution to support Angular packager (see [#614](https://github.com/Stuk/jszip/pull/614)). ### v3.2.2 2019-07-04 - No public changes, but a number of testing dependencies have been updated. - Tested browsers are now: Internet Explorer 11, Chrome (most recent) and Firefox (most recent). Other browsers (specifically Safari) are still supported however testing them on Saucelabs is broken and so they were removed from the test matrix. ### v3.2.1 2019-03-22 - Corrected built dist files ### v3.2.0 2019-02-21 - Update dependencies to reduce bundle size (see [#532](https://github.com/Stuk/jszip/pull/532)). - Fix deprecated Buffer constructor usage and add safeguards (see [#506](https://github.com/Stuk/jszip/pull/506)). ### v3.1.5 2017-11-09 - Fix IE11 memory leak (see [#429](https://github.com/Stuk/jszip/pull/429)). - Handle 2 nodejs deprecations (see [#459](https://github.com/Stuk/jszip/pull/459)). - Improve the "unsupported format" error message (see [#461](https://github.com/Stuk/jszip/pull/461)). - Improve webworker compatibility (see [#468](https://github.com/Stuk/jszip/pull/468)). - Fix nodejs 0.10 compatibility (see [#480](https://github.com/Stuk/jszip/pull/480)). - Improve the error without type in async() (see [#481](https://github.com/Stuk/jszip/pull/481)). ### v3.1.4 2017-08-24 - consistently use our own utils object for inheritance (see [#395](https://github.com/Stuk/jszip/pull/395)). - lower the memory consumption in `generate*` with a lot of files (see [#449](https://github.com/Stuk/jszip/pull/449)). ### v3.1.3 2016-10-06 - instanceof failing in window / iframe contexts (see [#350](https://github.com/Stuk/jszip/pull/350)). - remove a copy with blob output (see [#357](https://github.com/Stuk/jszip/pull/357)). - fix crc32 check for empty entries (see [#358](https://github.com/Stuk/jszip/pull/358)). - fix the base64 error message with data uri (see [#359](https://github.com/Stuk/jszip/pull/359)). ### v3.1.2 2016-08-23 - fix support of nodejs `process.platform` in `generate*` methods (see [#335](https://github.com/Stuk/jszip/pull/335)). - improve browserify/webpack support (see [#333](https://github.com/Stuk/jszip/pull/333)). - partial support of a promise of text (see [#337](https://github.com/Stuk/jszip/pull/337)). - fix streamed zip files containing folders (see [#342](https://github.com/Stuk/jszip/pull/342)). ### v3.1.1 2016-08-08 - Use a hard-coded JSZip.version, fix an issue with webpack (see [#328](https://github.com/Stuk/jszip/pull/328)). ### v3.1.0 2016-08-03 - utils.delay: use macro tasks instead of micro tasks (see [#288](https://github.com/Stuk/jszip/pull/288)). - Harden base64 decode (see [#316](https://github.com/Stuk/jszip/pull/316)). - Add JSZip.version and the version in the header (see [#317](https://github.com/Stuk/jszip/pull/317)). - Support Promise(Blob) (see [#318](https://github.com/Stuk/jszip/pull/318)). - Change JSZip.external.Promise implementation (see [#321](https://github.com/Stuk/jszip/pull/321)). - Update pako to v1.0.2 to fix a DEFLATE bug (see [#322](https://github.com/Stuk/jszip/pull/322)). ### v3.0.0 2016-04-13 This release changes a lot of methods, please see [the upgrade guide](http://stuk.github.io/jszip/documentation/upgrade_guide.html). - replace sync getters and `generate()` with async methods (see [#195](https://github.com/Stuk/jszip/pull/195)). - support nodejs streams (in `file()` and `generateAsync()`). - support Blob and Promise in `file()` and `loadAsync()` (see [#275](https://github.com/Stuk/jszip/pull/275)). - add `support.nodestream`. - zip.filter: remove the defensive copy. - remove the deprecated API (see [#253](https://github.com/Stuk/jszip/pull/253)). - `type` is now mandatory in `generateAsync()`. - change the createFolders default value (now `true`). - Dates: use UTC instead of the local timezone. - Add `base64` and `array` as possible output type. - Add a forEach method. - Drop node 0.8 support (see [#270](https://github.com/Stuk/jszip/pull/270)). ### v2.6.1 2016-07-28 - update pako to v1.0.2 to fix a DEFLATE bug (see [#322](https://github.com/Stuk/jszip/pull/322)). ### v2.6.0 2016-03-23 - publish `dist/` files in the npm package (see [#225](https://github.com/Stuk/jszip/pull/225)). - update pako to v1.0.0 (see [#261](https://github.com/Stuk/jszip/pull/261)). - add support of Array in JSZip#load (see [#252](https://github.com/Stuk/jszip/pull/252)). - improve file name / comment encoding support (see [#211](https://github.com/Stuk/jszip/pull/211)). - handle prepended data (see [#266](https://github.com/Stuk/jszip/pull/266)). - improve platform coverage in tests (see [#233](https://github.com/Stuk/jszip/pull/233) and [#269](https://github.com/Stuk/jszip/pull/269)). ### v2.5.0 2015-03-10 - add support for custom mime-types (see [#199](https://github.com/Stuk/jszip/issues/199)). - add an option to set the DEFLATE level (see [#201](https://github.com/Stuk/jszip/issues/201)). - improve the error message with corrupted zip (see [#202](https://github.com/Stuk/jszip/issues/202)). - add support for UNIX / DOS permissions (see [#200](https://github.com/Stuk/jszip/issues/200) and [#205](https://github.com/Stuk/jszip/issues/205)). ### v2.4.0 2014-07-24 - update pako to 0.2.5 (see [#156](https://github.com/Stuk/jszip/issues/156)). - make JSZip work in a Firefox addon context (see [#151](https://github.com/Stuk/jszip/issues/151)). - add an option (`createFolders`) to control the subfolder generation (see [#154](https://github.com/Stuk/jszip/issues/154)). - allow `Buffer` polyfill in the browser (see [#139](https://github.com/Stuk/jszip/issues/139)). ### v2.3.0 2014-06-18 - don't generate subfolders (see [#130](https://github.com/Stuk/jszip/issues/130)). - add comment support (see [#134](https://github.com/Stuk/jszip/issues/134)). - on `ZipObject#options`, the attributes `date` and `dir` have been deprecated and are now on `ZipObject` (see [the upgrade guide](http://stuk.github.io/jszip/documentation/upgrade_guide.html)). - on `ZipObject#options`, the attributes `base64` and `binary` have been deprecated (see [the upgrade guide](http://stuk.github.io/jszip/documentation/upgrade_guide.html)). - deprecate internal functions exposed in the public API (see [#123](https://github.com/Stuk/jszip/issues/123)). - improve UTF-8 support (see [#142](https://github.com/Stuk/jszip/issues/142)). ### v2.2.2, 2014-05-01 - update pako to v0.2.1, fix an error when decompressing some files (see [#126](https://github.com/Stuk/jszip/issues/126)). ### v2.2.1, 2014-04-23 - fix unreadable generated file on Windows 8 (see [#112](https://github.com/Stuk/jszip/issues/112)). - replace zlibjs with pako. ### v2.2.0, 2014-02-25 - make the `new` operator optional before the `JSZip` constructor (see [#93](https://github.com/Stuk/jszip/pull/93)). - update zlibjs to v0.2.0. ### v2.1.1, 2014-02-13 - use the npm package for zlib.js instead of the github url. ### v2.1.0, 2014-02-06 - split the files and use Browserify to generate the final file (see [#74](https://github.com/Stuk/jszip/pull/74)) - packaging change : instead of 4 files (jszip.js, jszip-load.js, jszip-inflate.js, jszip-deflate.js) we now have 2 files : dist/jszip.js and dist/jszip.min.js - add component/bower support - rename variable: 'byte' is a reserved word (see [#76](https://github.com/Stuk/jszip/pull/76)) - add support for the unicode path extra field (see [#82](https://github.com/Stuk/jszip/pull/82)) - ensure that the generated files have a header with the licenses (see [#80](https://github.com/Stuk/jszip/pull/80)) # v2.0.0, 2013-10-20 - `JSZipBase64` has been renamed to `JSZip.base64`. - The `data` attribute on the object returned by `zip.file(name)` has been removed. Use `asText()`, `asBinary()`, `asUint8Array()`, `asArrayBuffer()` or `asNodeBuffer()`. - [Fix issue with Android browser](https://github.com/Stuk/jszip/pull/60) - The compression/decompression methods now give their input type with the `compressInputType` and `uncompressInputType` attributes. - Lazily decompress data when needed and [improve performance in general](https://github.com/Stuk/jszip/pull/56) - [Add support for `Buffer` in Node.js](https://github.com/Stuk/jszip/pull/57). - Package for CommonJS/npm. ### v1.0.1, 2013-03-04 - Fixed an issue when generating a compressed zip file with empty files or folders, see #33. - With bad data (null or undefined), asText/asBinary/asUint8Array/asArrayBuffer methods now return an empty string, see #36. # v1.0.0, 2013-02-14 - First release after a long period without version. ================================================ FILE: Gruntfile.js ================================================ "use strict"; module.exports = function(grunt) { var version = require("./package.json").version; grunt.initConfig({ browserify: { all: { files: { "dist/jszip.js": ["lib/index.js"] }, options: { browserifyOptions: { standalone: "JSZip", transform: ["package-json-versionify"], insertGlobalVars: { process: undefined, Buffer: undefined, __filename: undefined, __dirname: undefined }, builtins: false }, banner: grunt.file.read("lib/license_header.js").replace(/__VERSION__/, version) } } }, uglify: { options: { mangle: true, preserveComments: false, banner: grunt.file.read("lib/license_header.js").replace(/__VERSION__/, version) }, all: { src: "dist/jszip.js", dest: "dist/jszip.min.js" } } }); grunt.loadNpmTasks("grunt-browserify"); grunt.loadNpmTasks("grunt-contrib-uglify"); grunt.registerTask("build", ["browserify", "uglify"]); grunt.registerTask("default", ["build"]); }; ================================================ FILE: LICENSE.markdown ================================================ JSZip is dual licensed. At your choice you may use it under the MIT license *or* the GPLv3 license. The MIT License =============== Copyright (c) 2009-2016 Stuart Knightley, David Duponchel, Franz Buchinger, António Afonso Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. GPL version 3 ============= GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The GNU General Public License is a free, copyleft license for software and other kinds of works. The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. To protect your rights, we need to prevent others from denying you these rights or asking you to surrender the rights. Therefore, you have certain responsibilities if you distribute copies of the software, or if you modify it: responsibilities to respect the freedom of others. For example, if you distribute copies of such a program, whether gratis or for a fee, you must pass on to the recipients the same freedoms that you received. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. Developers that use the GNU GPL protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License giving you legal permission to copy, distribute and/or modify it. For the developers' and authors' protection, the GPL clearly explains that there is no warranty for this free software. For both users' and authors' sake, the GPL requires that modified versions be marked as changed, so that their problems will not be attributed erroneously to authors of previous versions. Some devices are designed to deny users access to install or run modified versions of the software inside them, although the manufacturer can do so. This is fundamentally incompatible with the aim of protecting users' freedom to change the software. The systematic pattern of such abuse occurs in the area of products for individuals to use, which is precisely where it is most unacceptable. Therefore, we have designed this version of the GPL to prohibit the practice for those products. If such problems arise substantially in other domains, we stand ready to extend this provision to those domains in future versions of the GPL, as needed to protect the freedom of users. Finally, every program is threatened constantly by software patents. States should not allow patents to restrict development and use of software on general-purpose computers, but in those that do, we wish to avoid the special danger that patents applied to a free program could make it effectively proprietary. To prevent this, the GPL assures that patents cannot be used to render the program non-free. The precise terms and conditions for copying, distribution and modification follow. TERMS AND CONDITIONS 0. Definitions. "This License" refers to version 3 of the GNU General Public License. "Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. "The Program" refers to any copyrightable work licensed under this License. Each licensee is addressed as "you". "Licensees" and "recipients" may be individuals or organizations. To "modify" a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a "modified version" of the earlier work or a work "based on" the earlier work. A "covered work" means either the unmodified Program or a work based on the Program. To "propagate" a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well. To "convey" a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying. An interactive user interface displays "Appropriate Legal Notices" to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion. 1. Source Code. The "source code" for a work means the preferred form of the work for making modifications to it. "Object code" means any non-source form of a work. A "Standard Interface" means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language. The "System Libraries" of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A "Major Component", in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it. The "Corresponding Source" for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work. The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source. The Corresponding Source for a work in source code form is that same work. 2. Basic Permissions. All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law. You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you. Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary. 3. Protecting Users' Legal Rights From Anti-Circumvention Law. No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures. When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures. 4. Conveying Verbatim Copies. You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program. You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee. 5. Conveying Modified Source Versions. You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions: a) The work must carry prominent notices stating that you modified it, and giving a relevant date. b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to "keep intact all notices". c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it. d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so. A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an "aggregate" if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate. 6. Conveying Non-Source Forms. You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways: a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange. b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge. c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b. d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements. e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d. A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work. A "User Product" is either (1) a "consumer product", which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, "normally used" refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product. "Installation Information" for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made. If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM). The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network. Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying. 7. Additional Terms. "Additional permissions" are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions. When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission. Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms: a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or d) Limiting the use for publicity purposes of names of licensors or authors of the material; or e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors. All other non-permissive additional terms are considered "further restrictions" within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying. If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms. Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way. 8. Termination. You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11). However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10. 9. Acceptance Not Required for Having Copies. You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so. 10. Automatic Licensing of Downstream Recipients. Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License. An "entity transaction" is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts. You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it. 11. Patents. A "contributor" is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's "contributor version". A contributor's "essential patent claims" are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, "control" includes the right to grant patent sublicenses in a manner consistent with the requirements of this License. Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version. In the following three paragraphs, a "patent license" is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To "grant" such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party. If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. "Knowingly relying" means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid. If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it. A patent license is "discriminatory" if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007. Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law. 12. No Surrender of Others' Freedom. If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. 13. Use with the GNU Affero General Public License. Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such. 14. Revised Versions of this License. The Free Software Foundation may publish revised and/or new versions of the GNU General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation. If the Program specifies that a proxy can decide which future versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version. 15. Disclaimer of Warranty. THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16. Limitation of Liability. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 17. Interpretation of Sections 15 and 16. If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee. END OF TERMS AND CONDITIONS ================================================ FILE: README.markdown ================================================ JSZip ===== A library for creating, reading and editing .zip files with JavaScript, with a lovely and simple API. See https://stuk.github.io/jszip for all the documentation. ```javascript const zip = new JSZip(); zip.file("Hello.txt", "Hello World\n"); const img = zip.folder("images"); img.file("smile.gif", imgData, {base64: true}); zip.generateAsync({type:"blob"}).then(function(content) { // see FileSaver.js saveAs(content, "example.zip"); }); /* Results in a zip containing Hello.txt images/ smile.gif */ ``` License ------- JSZip is dual-licensed. You may use it under the MIT license *or* the GPLv3 license. See [LICENSE.markdown](LICENSE.markdown). ================================================ FILE: _config.yml ================================================ # will be overwritten by github, see https://help.github.com/articles/using-jekyll-with-pages lsi: false safe: true source: ./ incremental: false highlighter: rouge gist: noscript: false # /overwritten baseurl: /jszip layouts_dir: ./documentation/_layouts permalink: none exclude: ['bin', 'README.md', 'node_modules'] kramdown: input: GFM hard_wrap: false gems: - jekyll-coffeescript - jekyll-paginate ================================================ FILE: bower.json ================================================ { "name": "jszip", "homepage": "http://stuartk.com/jszip", "authors": [ "Stuart Knightley " ], "description": "Create, read and edit .zip files with JavaScript http://stuartk.com/jszip", "main": "dist/jszip.js", "keywords": [ "zip", "deflate", "inflate" ], "license": "MIT or GPLv3", "ignore": [ "**/.*", "node_modules", "bower_components", "test", "tests" ] } ================================================ FILE: component.json ================================================ { "name": "jszip", "repo": "Stuk/jszip", "description": "Create, read and edit .zip files with JavaScript http://stuartk.com/jszip", "version": "3.2.0", "keywords": [ "zip", "deflate", "inflate" ], "main": "dist/jszip.js", "license": "MIT or GPLv3", "scripts": [ "dist/jszip.js" ] } ================================================ FILE: dist/jszip.js ================================================ /*! JSZip v3.10.1 - A JavaScript class for generating and reading zip files (c) 2009-2016 Stuart Knightley Dual licenced under the MIT license or GPLv3. See https://raw.github.com/Stuk/jszip/main/LICENSE.markdown. JSZip uses the library pako released under the MIT license : https://github.com/nodeca/pako/blob/main/LICENSE */ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.JSZip = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o> 2; enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); enc3 = remainingBytes > 1 ? (((chr2 & 15) << 2) | (chr3 >> 6)) : 64; enc4 = remainingBytes > 2 ? (chr3 & 63) : 64; output.push(_keyStr.charAt(enc1) + _keyStr.charAt(enc2) + _keyStr.charAt(enc3) + _keyStr.charAt(enc4)); } return output.join(""); }; // public method for decoding exports.decode = function(input) { var chr1, chr2, chr3; var enc1, enc2, enc3, enc4; var i = 0, resultIndex = 0; var dataUrlPrefix = "data:"; if (input.substr(0, dataUrlPrefix.length) === dataUrlPrefix) { // This is a common error: people give a data url // (data:image/png;base64,iVBOR...) with a {base64: true} and // wonders why things don't work. // We can detect that the string input looks like a data url but we // *can't* be sure it is one: removing everything up to the comma would // be too dangerous. throw new Error("Invalid base64 input, it looks like a data url."); } input = input.replace(/[^A-Za-z0-9+/=]/g, ""); var totalLength = input.length * 3 / 4; if(input.charAt(input.length - 1) === _keyStr.charAt(64)) { totalLength--; } if(input.charAt(input.length - 2) === _keyStr.charAt(64)) { totalLength--; } if (totalLength % 1 !== 0) { // totalLength is not an integer, the length does not match a valid // base64 content. That can happen if: // - the input is not a base64 content // - the input is *almost* a base64 content, with a extra chars at the // beginning or at the end // - the input uses a base64 variant (base64url for example) throw new Error("Invalid base64 input, bad content length."); } var output; if (support.uint8array) { output = new Uint8Array(totalLength|0); } else { output = new Array(totalLength|0); } while (i < input.length) { enc1 = _keyStr.indexOf(input.charAt(i++)); enc2 = _keyStr.indexOf(input.charAt(i++)); enc3 = _keyStr.indexOf(input.charAt(i++)); enc4 = _keyStr.indexOf(input.charAt(i++)); chr1 = (enc1 << 2) | (enc2 >> 4); chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); chr3 = ((enc3 & 3) << 6) | enc4; output[resultIndex++] = chr1; if (enc3 !== 64) { output[resultIndex++] = chr2; } if (enc4 !== 64) { output[resultIndex++] = chr3; } } return output; }; },{"./support":30,"./utils":32}],2:[function(require,module,exports){ "use strict"; var external = require("./external"); var DataWorker = require("./stream/DataWorker"); var Crc32Probe = require("./stream/Crc32Probe"); var DataLengthProbe = require("./stream/DataLengthProbe"); /** * Represent a compressed object, with everything needed to decompress it. * @constructor * @param {number} compressedSize the size of the data compressed. * @param {number} uncompressedSize the size of the data after decompression. * @param {number} crc32 the crc32 of the decompressed file. * @param {object} compression the type of compression, see lib/compressions.js. * @param {String|ArrayBuffer|Uint8Array|Buffer} data the compressed data. */ function CompressedObject(compressedSize, uncompressedSize, crc32, compression, data) { this.compressedSize = compressedSize; this.uncompressedSize = uncompressedSize; this.crc32 = crc32; this.compression = compression; this.compressedContent = data; } CompressedObject.prototype = { /** * Create a worker to get the uncompressed content. * @return {GenericWorker} the worker. */ getContentWorker: function () { var worker = new DataWorker(external.Promise.resolve(this.compressedContent)) .pipe(this.compression.uncompressWorker()) .pipe(new DataLengthProbe("data_length")); var that = this; worker.on("end", function () { if (this.streamInfo["data_length"] !== that.uncompressedSize) { throw new Error("Bug : uncompressed data size mismatch"); } }); return worker; }, /** * Create a worker to get the compressed content. * @return {GenericWorker} the worker. */ getCompressedWorker: function () { return new DataWorker(external.Promise.resolve(this.compressedContent)) .withStreamInfo("compressedSize", this.compressedSize) .withStreamInfo("uncompressedSize", this.uncompressedSize) .withStreamInfo("crc32", this.crc32) .withStreamInfo("compression", this.compression) ; } }; /** * Chain the given worker with other workers to compress the content with the * given compression. * @param {GenericWorker} uncompressedWorker the worker to pipe. * @param {Object} compression the compression object. * @param {Object} compressionOptions the options to use when compressing. * @return {GenericWorker} the new worker compressing the content. */ CompressedObject.createWorkerFrom = function (uncompressedWorker, compression, compressionOptions) { return uncompressedWorker .pipe(new Crc32Probe()) .pipe(new DataLengthProbe("uncompressedSize")) .pipe(compression.compressWorker(compressionOptions)) .pipe(new DataLengthProbe("compressedSize")) .withStreamInfo("compression", compression); }; module.exports = CompressedObject; },{"./external":6,"./stream/Crc32Probe":25,"./stream/DataLengthProbe":26,"./stream/DataWorker":27}],3:[function(require,module,exports){ "use strict"; var GenericWorker = require("./stream/GenericWorker"); exports.STORE = { magic: "\x00\x00", compressWorker : function () { return new GenericWorker("STORE compression"); }, uncompressWorker : function () { return new GenericWorker("STORE decompression"); } }; exports.DEFLATE = require("./flate"); },{"./flate":7,"./stream/GenericWorker":28}],4:[function(require,module,exports){ "use strict"; var utils = require("./utils"); /** * The following functions come from pako, from pako/lib/zlib/crc32.js * released under the MIT license, see pako https://github.com/nodeca/pako/ */ // Use ordinary array, since untyped makes no boost here function makeTable() { var c, table = []; for(var n =0; n < 256; n++){ c = n; for(var k =0; k < 8; k++){ c = ((c&1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1)); } table[n] = c; } return table; } // Create table on load. Just 255 signed longs. Not a problem. var crcTable = makeTable(); function crc32(crc, buf, len, pos) { var t = crcTable, end = pos + len; crc = crc ^ (-1); for (var i = pos; i < end; i++ ) { crc = (crc >>> 8) ^ t[(crc ^ buf[i]) & 0xFF]; } return (crc ^ (-1)); // >>> 0; } // That's all for the pako functions. /** * Compute the crc32 of a string. * This is almost the same as the function crc32, but for strings. Using the * same function for the two use cases leads to horrible performances. * @param {Number} crc the starting value of the crc. * @param {String} str the string to use. * @param {Number} len the length of the string. * @param {Number} pos the starting position for the crc32 computation. * @return {Number} the computed crc32. */ function crc32str(crc, str, len, pos) { var t = crcTable, end = pos + len; crc = crc ^ (-1); for (var i = pos; i < end; i++ ) { crc = (crc >>> 8) ^ t[(crc ^ str.charCodeAt(i)) & 0xFF]; } return (crc ^ (-1)); // >>> 0; } module.exports = function crc32wrapper(input, crc) { if (typeof input === "undefined" || !input.length) { return 0; } var isArray = utils.getTypeOf(input) !== "string"; if(isArray) { return crc32(crc|0, input, input.length, 0); } else { return crc32str(crc|0, input, input.length, 0); } }; },{"./utils":32}],5:[function(require,module,exports){ "use strict"; exports.base64 = false; exports.binary = false; exports.dir = false; exports.createFolders = true; exports.date = null; exports.compression = null; exports.compressionOptions = null; exports.comment = null; exports.unixPermissions = null; exports.dosPermissions = null; },{}],6:[function(require,module,exports){ "use strict"; // load the global object first: // - it should be better integrated in the system (unhandledRejection in node) // - the environment may have a custom Promise implementation (see zone.js) var ES6Promise = null; if (typeof Promise !== "undefined") { ES6Promise = Promise; } else { ES6Promise = require("lie"); } /** * Let the user use/change some implementations. */ module.exports = { Promise: ES6Promise }; },{"lie":37}],7:[function(require,module,exports){ "use strict"; var USE_TYPEDARRAY = (typeof Uint8Array !== "undefined") && (typeof Uint16Array !== "undefined") && (typeof Uint32Array !== "undefined"); var pako = require("pako"); var utils = require("./utils"); var GenericWorker = require("./stream/GenericWorker"); var ARRAY_TYPE = USE_TYPEDARRAY ? "uint8array" : "array"; exports.magic = "\x08\x00"; /** * Create a worker that uses pako to inflate/deflate. * @constructor * @param {String} action the name of the pako function to call : either "Deflate" or "Inflate". * @param {Object} options the options to use when (de)compressing. */ function FlateWorker(action, options) { GenericWorker.call(this, "FlateWorker/" + action); this._pako = null; this._pakoAction = action; this._pakoOptions = options; // the `meta` object from the last chunk received // this allow this worker to pass around metadata this.meta = {}; } utils.inherits(FlateWorker, GenericWorker); /** * @see GenericWorker.processChunk */ FlateWorker.prototype.processChunk = function (chunk) { this.meta = chunk.meta; if (this._pako === null) { this._createPako(); } this._pako.push(utils.transformTo(ARRAY_TYPE, chunk.data), false); }; /** * @see GenericWorker.flush */ FlateWorker.prototype.flush = function () { GenericWorker.prototype.flush.call(this); if (this._pako === null) { this._createPako(); } this._pako.push([], true); }; /** * @see GenericWorker.cleanUp */ FlateWorker.prototype.cleanUp = function () { GenericWorker.prototype.cleanUp.call(this); this._pako = null; }; /** * Create the _pako object. * TODO: lazy-loading this object isn't the best solution but it's the * quickest. The best solution is to lazy-load the worker list. See also the * issue #446. */ FlateWorker.prototype._createPako = function () { this._pako = new pako[this._pakoAction]({ raw: true, level: this._pakoOptions.level || -1 // default compression }); var self = this; this._pako.onData = function(data) { self.push({ data : data, meta : self.meta }); }; }; exports.compressWorker = function (compressionOptions) { return new FlateWorker("Deflate", compressionOptions); }; exports.uncompressWorker = function () { return new FlateWorker("Inflate", {}); }; },{"./stream/GenericWorker":28,"./utils":32,"pako":38}],8:[function(require,module,exports){ "use strict"; var utils = require("../utils"); var GenericWorker = require("../stream/GenericWorker"); var utf8 = require("../utf8"); var crc32 = require("../crc32"); var signature = require("../signature"); /** * Transform an integer into a string in hexadecimal. * @private * @param {number} dec the number to convert. * @param {number} bytes the number of bytes to generate. * @returns {string} the result. */ var decToHex = function(dec, bytes) { var hex = "", i; for (i = 0; i < bytes; i++) { hex += String.fromCharCode(dec & 0xff); dec = dec >>> 8; } return hex; }; /** * Generate the UNIX part of the external file attributes. * @param {Object} unixPermissions the unix permissions or null. * @param {Boolean} isDir true if the entry is a directory, false otherwise. * @return {Number} a 32 bit integer. * * adapted from http://unix.stackexchange.com/questions/14705/the-zip-formats-external-file-attribute : * * TTTTsstrwxrwxrwx0000000000ADVSHR * ^^^^____________________________ file type, see zipinfo.c (UNX_*) * ^^^_________________________ setuid, setgid, sticky * ^^^^^^^^^________________ permissions * ^^^^^^^^^^______ not used ? * ^^^^^^ DOS attribute bits : Archive, Directory, Volume label, System file, Hidden, Read only */ var generateUnixExternalFileAttr = function (unixPermissions, isDir) { var result = unixPermissions; if (!unixPermissions) { // I can't use octal values in strict mode, hence the hexa. // 040775 => 0x41fd // 0100664 => 0x81b4 result = isDir ? 0x41fd : 0x81b4; } return (result & 0xFFFF) << 16; }; /** * Generate the DOS part of the external file attributes. * @param {Object} dosPermissions the dos permissions or null. * @param {Boolean} isDir true if the entry is a directory, false otherwise. * @return {Number} a 32 bit integer. * * Bit 0 Read-Only * Bit 1 Hidden * Bit 2 System * Bit 3 Volume Label * Bit 4 Directory * Bit 5 Archive */ var generateDosExternalFileAttr = function (dosPermissions) { // the dir flag is already set for compatibility return (dosPermissions || 0) & 0x3F; }; /** * Generate the various parts used in the construction of the final zip file. * @param {Object} streamInfo the hash with information about the compressed file. * @param {Boolean} streamedContent is the content streamed ? * @param {Boolean} streamingEnded is the stream finished ? * @param {number} offset the current offset from the start of the zip file. * @param {String} platform let's pretend we are this platform (change platform dependents fields) * @param {Function} encodeFileName the function to encode the file name / comment. * @return {Object} the zip parts. */ var generateZipParts = function(streamInfo, streamedContent, streamingEnded, offset, platform, encodeFileName) { var file = streamInfo["file"], compression = streamInfo["compression"], useCustomEncoding = encodeFileName !== utf8.utf8encode, encodedFileName = utils.transformTo("string", encodeFileName(file.name)), utfEncodedFileName = utils.transformTo("string", utf8.utf8encode(file.name)), comment = file.comment, encodedComment = utils.transformTo("string", encodeFileName(comment)), utfEncodedComment = utils.transformTo("string", utf8.utf8encode(comment)), useUTF8ForFileName = utfEncodedFileName.length !== file.name.length, useUTF8ForComment = utfEncodedComment.length !== comment.length, dosTime, dosDate, extraFields = "", unicodePathExtraField = "", unicodeCommentExtraField = "", dir = file.dir, date = file.date; var dataInfo = { crc32 : 0, compressedSize : 0, uncompressedSize : 0 }; // if the content is streamed, the sizes/crc32 are only available AFTER // the end of the stream. if (!streamedContent || streamingEnded) { dataInfo.crc32 = streamInfo["crc32"]; dataInfo.compressedSize = streamInfo["compressedSize"]; dataInfo.uncompressedSize = streamInfo["uncompressedSize"]; } var bitflag = 0; if (streamedContent) { // Bit 3: the sizes/crc32 are set to zero in the local header. // The correct values are put in the data descriptor immediately // following the compressed data. bitflag |= 0x0008; } if (!useCustomEncoding && (useUTF8ForFileName || useUTF8ForComment)) { // Bit 11: Language encoding flag (EFS). bitflag |= 0x0800; } var extFileAttr = 0; var versionMadeBy = 0; if (dir) { // dos or unix, we set the dos dir flag extFileAttr |= 0x00010; } if(platform === "UNIX") { versionMadeBy = 0x031E; // UNIX, version 3.0 extFileAttr |= generateUnixExternalFileAttr(file.unixPermissions, dir); } else { // DOS or other, fallback to DOS versionMadeBy = 0x0014; // DOS, version 2.0 extFileAttr |= generateDosExternalFileAttr(file.dosPermissions, dir); } // date // @see http://www.delorie.com/djgpp/doc/rbinter/it/52/13.html // @see http://www.delorie.com/djgpp/doc/rbinter/it/65/16.html // @see http://www.delorie.com/djgpp/doc/rbinter/it/66/16.html dosTime = date.getUTCHours(); dosTime = dosTime << 6; dosTime = dosTime | date.getUTCMinutes(); dosTime = dosTime << 5; dosTime = dosTime | date.getUTCSeconds() / 2; dosDate = date.getUTCFullYear() - 1980; dosDate = dosDate << 4; dosDate = dosDate | (date.getUTCMonth() + 1); dosDate = dosDate << 5; dosDate = dosDate | date.getUTCDate(); if (useUTF8ForFileName) { // set the unicode path extra field. unzip needs at least one extra // field to correctly handle unicode path, so using the path is as good // as any other information. This could improve the situation with // other archive managers too. // This field is usually used without the utf8 flag, with a non // unicode path in the header (winrar, winzip). This helps (a bit) // with the messy Windows' default compressed folders feature but // breaks on p7zip which doesn't seek the unicode path extra field. // So for now, UTF-8 everywhere ! unicodePathExtraField = // Version decToHex(1, 1) + // NameCRC32 decToHex(crc32(encodedFileName), 4) + // UnicodeName utfEncodedFileName; extraFields += // Info-ZIP Unicode Path Extra Field "\x75\x70" + // size decToHex(unicodePathExtraField.length, 2) + // content unicodePathExtraField; } if(useUTF8ForComment) { unicodeCommentExtraField = // Version decToHex(1, 1) + // CommentCRC32 decToHex(crc32(encodedComment), 4) + // UnicodeName utfEncodedComment; extraFields += // Info-ZIP Unicode Path Extra Field "\x75\x63" + // size decToHex(unicodeCommentExtraField.length, 2) + // content unicodeCommentExtraField; } var header = ""; // version needed to extract header += "\x0A\x00"; // general purpose bit flag header += decToHex(bitflag, 2); // compression method header += compression.magic; // last mod file time header += decToHex(dosTime, 2); // last mod file date header += decToHex(dosDate, 2); // crc-32 header += decToHex(dataInfo.crc32, 4); // compressed size header += decToHex(dataInfo.compressedSize, 4); // uncompressed size header += decToHex(dataInfo.uncompressedSize, 4); // file name length header += decToHex(encodedFileName.length, 2); // extra field length header += decToHex(extraFields.length, 2); var fileRecord = signature.LOCAL_FILE_HEADER + header + encodedFileName + extraFields; var dirRecord = signature.CENTRAL_FILE_HEADER + // version made by (00: DOS) decToHex(versionMadeBy, 2) + // file header (common to file and central directory) header + // file comment length decToHex(encodedComment.length, 2) + // disk number start "\x00\x00" + // internal file attributes TODO "\x00\x00" + // external file attributes decToHex(extFileAttr, 4) + // relative offset of local header decToHex(offset, 4) + // file name encodedFileName + // extra field extraFields + // file comment encodedComment; return { fileRecord: fileRecord, dirRecord: dirRecord }; }; /** * Generate the EOCD record. * @param {Number} entriesCount the number of entries in the zip file. * @param {Number} centralDirLength the length (in bytes) of the central dir. * @param {Number} localDirLength the length (in bytes) of the local dir. * @param {String} comment the zip file comment as a binary string. * @param {Function} encodeFileName the function to encode the comment. * @return {String} the EOCD record. */ var generateCentralDirectoryEnd = function (entriesCount, centralDirLength, localDirLength, comment, encodeFileName) { var dirEnd = ""; var encodedComment = utils.transformTo("string", encodeFileName(comment)); // end of central dir signature dirEnd = signature.CENTRAL_DIRECTORY_END + // number of this disk "\x00\x00" + // number of the disk with the start of the central directory "\x00\x00" + // total number of entries in the central directory on this disk decToHex(entriesCount, 2) + // total number of entries in the central directory decToHex(entriesCount, 2) + // size of the central directory 4 bytes decToHex(centralDirLength, 4) + // offset of start of central directory with respect to the starting disk number decToHex(localDirLength, 4) + // .ZIP file comment length decToHex(encodedComment.length, 2) + // .ZIP file comment encodedComment; return dirEnd; }; /** * Generate data descriptors for a file entry. * @param {Object} streamInfo the hash generated by a worker, containing information * on the file entry. * @return {String} the data descriptors. */ var generateDataDescriptors = function (streamInfo) { var descriptor = ""; descriptor = signature.DATA_DESCRIPTOR + // crc-32 4 bytes decToHex(streamInfo["crc32"], 4) + // compressed size 4 bytes decToHex(streamInfo["compressedSize"], 4) + // uncompressed size 4 bytes decToHex(streamInfo["uncompressedSize"], 4); return descriptor; }; /** * A worker to concatenate other workers to create a zip file. * @param {Boolean} streamFiles `true` to stream the content of the files, * `false` to accumulate it. * @param {String} comment the comment to use. * @param {String} platform the platform to use, "UNIX" or "DOS". * @param {Function} encodeFileName the function to encode file names and comments. */ function ZipFileWorker(streamFiles, comment, platform, encodeFileName) { GenericWorker.call(this, "ZipFileWorker"); // The number of bytes written so far. This doesn't count accumulated chunks. this.bytesWritten = 0; // The comment of the zip file this.zipComment = comment; // The platform "generating" the zip file. this.zipPlatform = platform; // the function to encode file names and comments. this.encodeFileName = encodeFileName; // Should we stream the content of the files ? this.streamFiles = streamFiles; // If `streamFiles` is false, we will need to accumulate the content of the // files to calculate sizes / crc32 (and write them *before* the content). // This boolean indicates if we are accumulating chunks (it will change a lot // during the lifetime of this worker). this.accumulate = false; // The buffer receiving chunks when accumulating content. this.contentBuffer = []; // The list of generated directory records. this.dirRecords = []; // The offset (in bytes) from the beginning of the zip file for the current source. this.currentSourceOffset = 0; // The total number of entries in this zip file. this.entriesCount = 0; // the name of the file currently being added, null when handling the end of the zip file. // Used for the emitted metadata. this.currentFile = null; this._sources = []; } utils.inherits(ZipFileWorker, GenericWorker); /** * @see GenericWorker.push */ ZipFileWorker.prototype.push = function (chunk) { var currentFilePercent = chunk.meta.percent || 0; var entriesCount = this.entriesCount; var remainingFiles = this._sources.length; if(this.accumulate) { this.contentBuffer.push(chunk); } else { this.bytesWritten += chunk.data.length; GenericWorker.prototype.push.call(this, { data : chunk.data, meta : { currentFile : this.currentFile, percent : entriesCount ? (currentFilePercent + 100 * (entriesCount - remainingFiles - 1)) / entriesCount : 100 } }); } }; /** * The worker started a new source (an other worker). * @param {Object} streamInfo the streamInfo object from the new source. */ ZipFileWorker.prototype.openedSource = function (streamInfo) { this.currentSourceOffset = this.bytesWritten; this.currentFile = streamInfo["file"].name; var streamedContent = this.streamFiles && !streamInfo["file"].dir; // don't stream folders (because they don't have any content) if(streamedContent) { var record = generateZipParts(streamInfo, streamedContent, false, this.currentSourceOffset, this.zipPlatform, this.encodeFileName); this.push({ data : record.fileRecord, meta : {percent:0} }); } else { // we need to wait for the whole file before pushing anything this.accumulate = true; } }; /** * The worker finished a source (an other worker). * @param {Object} streamInfo the streamInfo object from the finished source. */ ZipFileWorker.prototype.closedSource = function (streamInfo) { this.accumulate = false; var streamedContent = this.streamFiles && !streamInfo["file"].dir; var record = generateZipParts(streamInfo, streamedContent, true, this.currentSourceOffset, this.zipPlatform, this.encodeFileName); this.dirRecords.push(record.dirRecord); if(streamedContent) { // after the streamed file, we put data descriptors this.push({ data : generateDataDescriptors(streamInfo), meta : {percent:100} }); } else { // the content wasn't streamed, we need to push everything now // first the file record, then the content this.push({ data : record.fileRecord, meta : {percent:0} }); while(this.contentBuffer.length) { this.push(this.contentBuffer.shift()); } } this.currentFile = null; }; /** * @see GenericWorker.flush */ ZipFileWorker.prototype.flush = function () { var localDirLength = this.bytesWritten; for(var i = 0; i < this.dirRecords.length; i++) { this.push({ data : this.dirRecords[i], meta : {percent:100} }); } var centralDirLength = this.bytesWritten - localDirLength; var dirEnd = generateCentralDirectoryEnd(this.dirRecords.length, centralDirLength, localDirLength, this.zipComment, this.encodeFileName); this.push({ data : dirEnd, meta : {percent:100} }); }; /** * Prepare the next source to be read. */ ZipFileWorker.prototype.prepareNextSource = function () { this.previous = this._sources.shift(); this.openedSource(this.previous.streamInfo); if (this.isPaused) { this.previous.pause(); } else { this.previous.resume(); } }; /** * @see GenericWorker.registerPrevious */ ZipFileWorker.prototype.registerPrevious = function (previous) { this._sources.push(previous); var self = this; previous.on("data", function (chunk) { self.processChunk(chunk); }); previous.on("end", function () { self.closedSource(self.previous.streamInfo); if(self._sources.length) { self.prepareNextSource(); } else { self.end(); } }); previous.on("error", function (e) { self.error(e); }); return this; }; /** * @see GenericWorker.resume */ ZipFileWorker.prototype.resume = function () { if(!GenericWorker.prototype.resume.call(this)) { return false; } if (!this.previous && this._sources.length) { this.prepareNextSource(); return true; } if (!this.previous && !this._sources.length && !this.generatedError) { this.end(); return true; } }; /** * @see GenericWorker.error */ ZipFileWorker.prototype.error = function (e) { var sources = this._sources; if(!GenericWorker.prototype.error.call(this, e)) { return false; } for(var i = 0; i < sources.length; i++) { try { sources[i].error(e); } catch(e) { // the `error` exploded, nothing to do } } return true; }; /** * @see GenericWorker.lock */ ZipFileWorker.prototype.lock = function () { GenericWorker.prototype.lock.call(this); var sources = this._sources; for(var i = 0; i < sources.length; i++) { sources[i].lock(); } }; module.exports = ZipFileWorker; },{"../crc32":4,"../signature":23,"../stream/GenericWorker":28,"../utf8":31,"../utils":32}],9:[function(require,module,exports){ "use strict"; var compressions = require("../compressions"); var ZipFileWorker = require("./ZipFileWorker"); /** * Find the compression to use. * @param {String} fileCompression the compression defined at the file level, if any. * @param {String} zipCompression the compression defined at the load() level. * @return {Object} the compression object to use. */ var getCompression = function (fileCompression, zipCompression) { var compressionName = fileCompression || zipCompression; var compression = compressions[compressionName]; if (!compression) { throw new Error(compressionName + " is not a valid compression method !"); } return compression; }; /** * Create a worker to generate a zip file. * @param {JSZip} zip the JSZip instance at the right root level. * @param {Object} options to generate the zip file. * @param {String} comment the comment to use. */ exports.generateWorker = function (zip, options, comment) { var zipFileWorker = new ZipFileWorker(options.streamFiles, comment, options.platform, options.encodeFileName); var entriesCount = 0; try { zip.forEach(function (relativePath, file) { entriesCount++; var compression = getCompression(file.options.compression, options.compression); var compressionOptions = file.options.compressionOptions || options.compressionOptions || {}; var dir = file.dir, date = file.date; file._compressWorker(compression, compressionOptions) .withStreamInfo("file", { name : relativePath, dir : dir, date : date, comment : file.comment || "", unixPermissions : file.unixPermissions, dosPermissions : file.dosPermissions }) .pipe(zipFileWorker); }); zipFileWorker.entriesCount = entriesCount; } catch (e) { zipFileWorker.error(e); } return zipFileWorker; }; },{"../compressions":3,"./ZipFileWorker":8}],10:[function(require,module,exports){ "use strict"; /** * Representation a of zip file in js * @constructor */ function JSZip() { // if this constructor is used without `new`, it adds `new` before itself: if(!(this instanceof JSZip)) { return new JSZip(); } if(arguments.length) { throw new Error("The constructor with parameters has been removed in JSZip 3.0, please check the upgrade guide."); } // object containing the files : // { // "folder/" : {...}, // "folder/data.txt" : {...} // } // NOTE: we use a null prototype because we do not // want filenames like "toString" coming from a zip file // to overwrite methods and attributes in a normal Object. this.files = Object.create(null); this.comment = null; // Where we are in the hierarchy this.root = ""; this.clone = function() { var newObj = new JSZip(); for (var i in this) { if (typeof this[i] !== "function") { newObj[i] = this[i]; } } return newObj; }; } JSZip.prototype = require("./object"); JSZip.prototype.loadAsync = require("./load"); JSZip.support = require("./support"); JSZip.defaults = require("./defaults"); // TODO find a better way to handle this version, // a require('package.json').version doesn't work with webpack, see #327 JSZip.version = "3.10.1"; JSZip.loadAsync = function (content, options) { return new JSZip().loadAsync(content, options); }; JSZip.external = require("./external"); module.exports = JSZip; },{"./defaults":5,"./external":6,"./load":11,"./object":15,"./support":30}],11:[function(require,module,exports){ "use strict"; var utils = require("./utils"); var external = require("./external"); var utf8 = require("./utf8"); var ZipEntries = require("./zipEntries"); var Crc32Probe = require("./stream/Crc32Probe"); var nodejsUtils = require("./nodejsUtils"); /** * Check the CRC32 of an entry. * @param {ZipEntry} zipEntry the zip entry to check. * @return {Promise} the result. */ function checkEntryCRC32(zipEntry) { return new external.Promise(function (resolve, reject) { var worker = zipEntry.decompressed.getContentWorker().pipe(new Crc32Probe()); worker.on("error", function (e) { reject(e); }) .on("end", function () { if (worker.streamInfo.crc32 !== zipEntry.decompressed.crc32) { reject(new Error("Corrupted zip : CRC32 mismatch")); } else { resolve(); } }) .resume(); }); } module.exports = function (data, options) { var zip = this; options = utils.extend(options || {}, { base64: false, checkCRC32: false, optimizedBinaryString: false, createFolders: false, decodeFileName: utf8.utf8decode }); if (nodejsUtils.isNode && nodejsUtils.isStream(data)) { return external.Promise.reject(new Error("JSZip can't accept a stream when loading a zip file.")); } return utils.prepareContent("the loaded zip file", data, true, options.optimizedBinaryString, options.base64) .then(function (data) { var zipEntries = new ZipEntries(options); zipEntries.load(data); return zipEntries; }).then(function checkCRC32(zipEntries) { var promises = [external.Promise.resolve(zipEntries)]; var files = zipEntries.files; if (options.checkCRC32) { for (var i = 0; i < files.length; i++) { promises.push(checkEntryCRC32(files[i])); } } return external.Promise.all(promises); }).then(function addFiles(results) { var zipEntries = results.shift(); var files = zipEntries.files; for (var i = 0; i < files.length; i++) { var input = files[i]; var unsafeName = input.fileNameStr; var safeName = utils.resolve(input.fileNameStr); zip.file(safeName, input.decompressed, { binary: true, optimizedBinaryString: true, date: input.date, dir: input.dir, comment: input.fileCommentStr.length ? input.fileCommentStr : null, unixPermissions: input.unixPermissions, dosPermissions: input.dosPermissions, createFolders: options.createFolders }); if (!input.dir) { zip.file(safeName).unsafeOriginalName = unsafeName; } } if (zipEntries.zipComment.length) { zip.comment = zipEntries.zipComment; } return zip; }); }; },{"./external":6,"./nodejsUtils":14,"./stream/Crc32Probe":25,"./utf8":31,"./utils":32,"./zipEntries":33}],12:[function(require,module,exports){ "use strict"; var utils = require("../utils"); var GenericWorker = require("../stream/GenericWorker"); /** * A worker that use a nodejs stream as source. * @constructor * @param {String} filename the name of the file entry for this stream. * @param {Readable} stream the nodejs stream. */ function NodejsStreamInputAdapter(filename, stream) { GenericWorker.call(this, "Nodejs stream input adapter for " + filename); this._upstreamEnded = false; this._bindStream(stream); } utils.inherits(NodejsStreamInputAdapter, GenericWorker); /** * Prepare the stream and bind the callbacks on it. * Do this ASAP on node 0.10 ! A lazy binding doesn't always work. * @param {Stream} stream the nodejs stream to use. */ NodejsStreamInputAdapter.prototype._bindStream = function (stream) { var self = this; this._stream = stream; stream.pause(); stream .on("data", function (chunk) { self.push({ data: chunk, meta : { percent : 0 } }); }) .on("error", function (e) { if(self.isPaused) { this.generatedError = e; } else { self.error(e); } }) .on("end", function () { if(self.isPaused) { self._upstreamEnded = true; } else { self.end(); } }); }; NodejsStreamInputAdapter.prototype.pause = function () { if(!GenericWorker.prototype.pause.call(this)) { return false; } this._stream.pause(); return true; }; NodejsStreamInputAdapter.prototype.resume = function () { if(!GenericWorker.prototype.resume.call(this)) { return false; } if(this._upstreamEnded) { this.end(); } else { this._stream.resume(); } return true; }; module.exports = NodejsStreamInputAdapter; },{"../stream/GenericWorker":28,"../utils":32}],13:[function(require,module,exports){ "use strict"; var Readable = require("readable-stream").Readable; var utils = require("../utils"); utils.inherits(NodejsStreamOutputAdapter, Readable); /** * A nodejs stream using a worker as source. * @see the SourceWrapper in http://nodejs.org/api/stream.html * @constructor * @param {StreamHelper} helper the helper wrapping the worker * @param {Object} options the nodejs stream options * @param {Function} updateCb the update callback. */ function NodejsStreamOutputAdapter(helper, options, updateCb) { Readable.call(this, options); this._helper = helper; var self = this; helper.on("data", function (data, meta) { if (!self.push(data)) { self._helper.pause(); } if(updateCb) { updateCb(meta); } }) .on("error", function(e) { self.emit("error", e); }) .on("end", function () { self.push(null); }); } NodejsStreamOutputAdapter.prototype._read = function() { this._helper.resume(); }; module.exports = NodejsStreamOutputAdapter; },{"../utils":32,"readable-stream":16}],14:[function(require,module,exports){ "use strict"; module.exports = { /** * True if this is running in Nodejs, will be undefined in a browser. * In a browser, browserify won't include this file and the whole module * will be resolved an empty object. */ isNode : typeof Buffer !== "undefined", /** * Create a new nodejs Buffer from an existing content. * @param {Object} data the data to pass to the constructor. * @param {String} encoding the encoding to use. * @return {Buffer} a new Buffer. */ newBufferFrom: function(data, encoding) { if (Buffer.from && Buffer.from !== Uint8Array.from) { return Buffer.from(data, encoding); } else { if (typeof data === "number") { // Safeguard for old Node.js versions. On newer versions, // Buffer.from(number) / Buffer(number, encoding) already throw. throw new Error("The \"data\" argument must not be a number"); } return new Buffer(data, encoding); } }, /** * Create a new nodejs Buffer with the specified size. * @param {Integer} size the size of the buffer. * @return {Buffer} a new Buffer. */ allocBuffer: function (size) { if (Buffer.alloc) { return Buffer.alloc(size); } else { var buf = new Buffer(size); buf.fill(0); return buf; } }, /** * Find out if an object is a Buffer. * @param {Object} b the object to test. * @return {Boolean} true if the object is a Buffer, false otherwise. */ isBuffer : function(b){ return Buffer.isBuffer(b); }, isStream : function (obj) { return obj && typeof obj.on === "function" && typeof obj.pause === "function" && typeof obj.resume === "function"; } }; },{}],15:[function(require,module,exports){ "use strict"; var utf8 = require("./utf8"); var utils = require("./utils"); var GenericWorker = require("./stream/GenericWorker"); var StreamHelper = require("./stream/StreamHelper"); var defaults = require("./defaults"); var CompressedObject = require("./compressedObject"); var ZipObject = require("./zipObject"); var generate = require("./generate"); var nodejsUtils = require("./nodejsUtils"); var NodejsStreamInputAdapter = require("./nodejs/NodejsStreamInputAdapter"); /** * Add a file in the current folder. * @private * @param {string} name the name of the file * @param {String|ArrayBuffer|Uint8Array|Buffer} data the data of the file * @param {Object} originalOptions the options of the file * @return {Object} the new file. */ var fileAdd = function(name, data, originalOptions) { // be sure sub folders exist var dataType = utils.getTypeOf(data), parent; /* * Correct options. */ var o = utils.extend(originalOptions || {}, defaults); o.date = o.date || new Date(); if (o.compression !== null) { o.compression = o.compression.toUpperCase(); } if (typeof o.unixPermissions === "string") { o.unixPermissions = parseInt(o.unixPermissions, 8); } // UNX_IFDIR 0040000 see zipinfo.c if (o.unixPermissions && (o.unixPermissions & 0x4000)) { o.dir = true; } // Bit 4 Directory if (o.dosPermissions && (o.dosPermissions & 0x0010)) { o.dir = true; } if (o.dir) { name = forceTrailingSlash(name); } if (o.createFolders && (parent = parentFolder(name))) { folderAdd.call(this, parent, true); } var isUnicodeString = dataType === "string" && o.binary === false && o.base64 === false; if (!originalOptions || typeof originalOptions.binary === "undefined") { o.binary = !isUnicodeString; } var isCompressedEmpty = (data instanceof CompressedObject) && data.uncompressedSize === 0; if (isCompressedEmpty || o.dir || !data || data.length === 0) { o.base64 = false; o.binary = true; data = ""; o.compression = "STORE"; dataType = "string"; } /* * Convert content to fit. */ var zipObjectContent = null; if (data instanceof CompressedObject || data instanceof GenericWorker) { zipObjectContent = data; } else if (nodejsUtils.isNode && nodejsUtils.isStream(data)) { zipObjectContent = new NodejsStreamInputAdapter(name, data); } else { zipObjectContent = utils.prepareContent(name, data, o.binary, o.optimizedBinaryString, o.base64); } var object = new ZipObject(name, zipObjectContent, o); this.files[name] = object; /* TODO: we can't throw an exception because we have async promises (we can have a promise of a Date() for example) but returning a promise is useless because file(name, data) returns the JSZip object for chaining. Should we break that to allow the user to catch the error ? return external.Promise.resolve(zipObjectContent) .then(function () { return object; }); */ }; /** * Find the parent folder of the path. * @private * @param {string} path the path to use * @return {string} the parent folder, or "" */ var parentFolder = function (path) { if (path.slice(-1) === "/") { path = path.substring(0, path.length - 1); } var lastSlash = path.lastIndexOf("/"); return (lastSlash > 0) ? path.substring(0, lastSlash) : ""; }; /** * Returns the path with a slash at the end. * @private * @param {String} path the path to check. * @return {String} the path with a trailing slash. */ var forceTrailingSlash = function(path) { // Check the name ends with a / if (path.slice(-1) !== "/") { path += "/"; // IE doesn't like substr(-1) } return path; }; /** * Add a (sub) folder in the current folder. * @private * @param {string} name the folder's name * @param {boolean=} [createFolders] If true, automatically create sub * folders. Defaults to false. * @return {Object} the new folder. */ var folderAdd = function(name, createFolders) { createFolders = (typeof createFolders !== "undefined") ? createFolders : defaults.createFolders; name = forceTrailingSlash(name); // Does this folder already exist? if (!this.files[name]) { fileAdd.call(this, name, null, { dir: true, createFolders: createFolders }); } return this.files[name]; }; /** * Cross-window, cross-Node-context regular expression detection * @param {Object} object Anything * @return {Boolean} true if the object is a regular expression, * false otherwise */ function isRegExp(object) { return Object.prototype.toString.call(object) === "[object RegExp]"; } // return the actual prototype of JSZip var out = { /** * @see loadAsync */ load: function() { throw new Error("This method has been removed in JSZip 3.0, please check the upgrade guide."); }, /** * Call a callback function for each entry at this folder level. * @param {Function} cb the callback function: * function (relativePath, file) {...} * It takes 2 arguments : the relative path and the file. */ forEach: function(cb) { var filename, relativePath, file; // ignore warning about unwanted properties because this.files is a null prototype object /* eslint-disable-next-line guard-for-in */ for (filename in this.files) { file = this.files[filename]; relativePath = filename.slice(this.root.length, filename.length); if (relativePath && filename.slice(0, this.root.length) === this.root) { // the file is in the current root cb(relativePath, file); // TODO reverse the parameters ? need to be clean AND consistent with the filter search fn... } } }, /** * Filter nested files/folders with the specified function. * @param {Function} search the predicate to use : * function (relativePath, file) {...} * It takes 2 arguments : the relative path and the file. * @return {Array} An array of matching elements. */ filter: function(search) { var result = []; this.forEach(function (relativePath, entry) { if (search(relativePath, entry)) { // the file matches the function result.push(entry); } }); return result; }, /** * Add a file to the zip file, or search a file. * @param {string|RegExp} name The name of the file to add (if data is defined), * the name of the file to find (if no data) or a regex to match files. * @param {String|ArrayBuffer|Uint8Array|Buffer} data The file data, either raw or base64 encoded * @param {Object} o File options * @return {JSZip|Object|Array} this JSZip object (when adding a file), * a file (when searching by string) or an array of files (when searching by regex). */ file: function(name, data, o) { if (arguments.length === 1) { if (isRegExp(name)) { var regexp = name; return this.filter(function(relativePath, file) { return !file.dir && regexp.test(relativePath); }); } else { // text var obj = this.files[this.root + name]; if (obj && !obj.dir) { return obj; } else { return null; } } } else { // more than one argument : we have data ! name = this.root + name; fileAdd.call(this, name, data, o); } return this; }, /** * Add a directory to the zip file, or search. * @param {String|RegExp} arg The name of the directory to add, or a regex to search folders. * @return {JSZip} an object with the new directory as the root, or an array containing matching folders. */ folder: function(arg) { if (!arg) { return this; } if (isRegExp(arg)) { return this.filter(function(relativePath, file) { return file.dir && arg.test(relativePath); }); } // else, name is a new folder var name = this.root + arg; var newFolder = folderAdd.call(this, name); // Allow chaining by returning a new object with this folder as the root var ret = this.clone(); ret.root = newFolder.name; return ret; }, /** * Delete a file, or a directory and all sub-files, from the zip * @param {string} name the name of the file to delete * @return {JSZip} this JSZip object */ remove: function(name) { name = this.root + name; var file = this.files[name]; if (!file) { // Look for any folders if (name.slice(-1) !== "/") { name += "/"; } file = this.files[name]; } if (file && !file.dir) { // file delete this.files[name]; } else { // maybe a folder, delete recursively var kids = this.filter(function(relativePath, file) { return file.name.slice(0, name.length) === name; }); for (var i = 0; i < kids.length; i++) { delete this.files[kids[i].name]; } } return this; }, /** * @deprecated This method has been removed in JSZip 3.0, please check the upgrade guide. */ generate: function() { throw new Error("This method has been removed in JSZip 3.0, please check the upgrade guide."); }, /** * Generate the complete zip file as an internal stream. * @param {Object} options the options to generate the zip file : * - compression, "STORE" by default. * - type, "base64" by default. Values are : string, base64, uint8array, arraybuffer, blob. * @return {StreamHelper} the streamed zip file. */ generateInternalStream: function(options) { var worker, opts = {}; try { opts = utils.extend(options || {}, { streamFiles: false, compression: "STORE", compressionOptions : null, type: "", platform: "DOS", comment: null, mimeType: "application/zip", encodeFileName: utf8.utf8encode }); opts.type = opts.type.toLowerCase(); opts.compression = opts.compression.toUpperCase(); // "binarystring" is preferred but the internals use "string". if(opts.type === "binarystring") { opts.type = "string"; } if (!opts.type) { throw new Error("No output type specified."); } utils.checkSupport(opts.type); // accept nodejs `process.platform` if( opts.platform === "darwin" || opts.platform === "freebsd" || opts.platform === "linux" || opts.platform === "sunos" ) { opts.platform = "UNIX"; } if (opts.platform === "win32") { opts.platform = "DOS"; } var comment = opts.comment || this.comment || ""; worker = generate.generateWorker(this, opts, comment); } catch (e) { worker = new GenericWorker("error"); worker.error(e); } return new StreamHelper(worker, opts.type || "string", opts.mimeType); }, /** * Generate the complete zip file asynchronously. * @see generateInternalStream */ generateAsync: function(options, onUpdate) { return this.generateInternalStream(options).accumulate(onUpdate); }, /** * Generate the complete zip file asynchronously. * @see generateInternalStream */ generateNodeStream: function(options, onUpdate) { options = options || {}; if (!options.type) { options.type = "nodebuffer"; } return this.generateInternalStream(options).toNodejsStream(onUpdate); } }; module.exports = out; },{"./compressedObject":2,"./defaults":5,"./generate":9,"./nodejs/NodejsStreamInputAdapter":12,"./nodejsUtils":14,"./stream/GenericWorker":28,"./stream/StreamHelper":29,"./utf8":31,"./utils":32,"./zipObject":35}],16:[function(require,module,exports){ "use strict"; /* * This file is used by module bundlers (browserify/webpack/etc) when * including a stream implementation. We use "readable-stream" to get a * consistent behavior between nodejs versions but bundlers often have a shim * for "stream". Using this shim greatly improve the compatibility and greatly * reduce the final size of the bundle (only one stream implementation, not * two). */ module.exports = require("stream"); },{"stream":undefined}],17:[function(require,module,exports){ "use strict"; var DataReader = require("./DataReader"); var utils = require("../utils"); function ArrayReader(data) { DataReader.call(this, data); for(var i = 0; i < this.data.length; i++) { data[i] = data[i] & 0xFF; } } utils.inherits(ArrayReader, DataReader); /** * @see DataReader.byteAt */ ArrayReader.prototype.byteAt = function(i) { return this.data[this.zero + i]; }; /** * @see DataReader.lastIndexOfSignature */ ArrayReader.prototype.lastIndexOfSignature = function(sig) { var sig0 = sig.charCodeAt(0), sig1 = sig.charCodeAt(1), sig2 = sig.charCodeAt(2), sig3 = sig.charCodeAt(3); for (var i = this.length - 4; i >= 0; --i) { if (this.data[i] === sig0 && this.data[i + 1] === sig1 && this.data[i + 2] === sig2 && this.data[i + 3] === sig3) { return i - this.zero; } } return -1; }; /** * @see DataReader.readAndCheckSignature */ ArrayReader.prototype.readAndCheckSignature = function (sig) { var sig0 = sig.charCodeAt(0), sig1 = sig.charCodeAt(1), sig2 = sig.charCodeAt(2), sig3 = sig.charCodeAt(3), data = this.readData(4); return sig0 === data[0] && sig1 === data[1] && sig2 === data[2] && sig3 === data[3]; }; /** * @see DataReader.readData */ ArrayReader.prototype.readData = function(size) { this.checkOffset(size); if(size === 0) { return []; } var result = this.data.slice(this.zero + this.index, this.zero + this.index + size); this.index += size; return result; }; module.exports = ArrayReader; },{"../utils":32,"./DataReader":18}],18:[function(require,module,exports){ "use strict"; var utils = require("../utils"); function DataReader(data) { this.data = data; // type : see implementation this.length = data.length; this.index = 0; this.zero = 0; } DataReader.prototype = { /** * Check that the offset will not go too far. * @param {string} offset the additional offset to check. * @throws {Error} an Error if the offset is out of bounds. */ checkOffset: function(offset) { this.checkIndex(this.index + offset); }, /** * Check that the specified index will not be too far. * @param {string} newIndex the index to check. * @throws {Error} an Error if the index is out of bounds. */ checkIndex: function(newIndex) { if (this.length < this.zero + newIndex || newIndex < 0) { throw new Error("End of data reached (data length = " + this.length + ", asked index = " + (newIndex) + "). Corrupted zip ?"); } }, /** * Change the index. * @param {number} newIndex The new index. * @throws {Error} if the new index is out of the data. */ setIndex: function(newIndex) { this.checkIndex(newIndex); this.index = newIndex; }, /** * Skip the next n bytes. * @param {number} n the number of bytes to skip. * @throws {Error} if the new index is out of the data. */ skip: function(n) { this.setIndex(this.index + n); }, /** * Get the byte at the specified index. * @param {number} i the index to use. * @return {number} a byte. */ byteAt: function() { // see implementations }, /** * Get the next number with a given byte size. * @param {number} size the number of bytes to read. * @return {number} the corresponding number. */ readInt: function(size) { var result = 0, i; this.checkOffset(size); for (i = this.index + size - 1; i >= this.index; i--) { result = (result << 8) + this.byteAt(i); } this.index += size; return result; }, /** * Get the next string with a given byte size. * @param {number} size the number of bytes to read. * @return {string} the corresponding string. */ readString: function(size) { return utils.transformTo("string", this.readData(size)); }, /** * Get raw data without conversion, bytes. * @param {number} size the number of bytes to read. * @return {Object} the raw data, implementation specific. */ readData: function() { // see implementations }, /** * Find the last occurrence of a zip signature (4 bytes). * @param {string} sig the signature to find. * @return {number} the index of the last occurrence, -1 if not found. */ lastIndexOfSignature: function() { // see implementations }, /** * Read the signature (4 bytes) at the current position and compare it with sig. * @param {string} sig the expected signature * @return {boolean} true if the signature matches, false otherwise. */ readAndCheckSignature: function() { // see implementations }, /** * Get the next date. * @return {Date} the date. */ readDate: function() { var dostime = this.readInt(4); return new Date(Date.UTC( ((dostime >> 25) & 0x7f) + 1980, // year ((dostime >> 21) & 0x0f) - 1, // month (dostime >> 16) & 0x1f, // day (dostime >> 11) & 0x1f, // hour (dostime >> 5) & 0x3f, // minute (dostime & 0x1f) << 1)); // second } }; module.exports = DataReader; },{"../utils":32}],19:[function(require,module,exports){ "use strict"; var Uint8ArrayReader = require("./Uint8ArrayReader"); var utils = require("../utils"); function NodeBufferReader(data) { Uint8ArrayReader.call(this, data); } utils.inherits(NodeBufferReader, Uint8ArrayReader); /** * @see DataReader.readData */ NodeBufferReader.prototype.readData = function(size) { this.checkOffset(size); var result = this.data.slice(this.zero + this.index, this.zero + this.index + size); this.index += size; return result; }; module.exports = NodeBufferReader; },{"../utils":32,"./Uint8ArrayReader":21}],20:[function(require,module,exports){ "use strict"; var DataReader = require("./DataReader"); var utils = require("../utils"); function StringReader(data) { DataReader.call(this, data); } utils.inherits(StringReader, DataReader); /** * @see DataReader.byteAt */ StringReader.prototype.byteAt = function(i) { return this.data.charCodeAt(this.zero + i); }; /** * @see DataReader.lastIndexOfSignature */ StringReader.prototype.lastIndexOfSignature = function(sig) { return this.data.lastIndexOf(sig) - this.zero; }; /** * @see DataReader.readAndCheckSignature */ StringReader.prototype.readAndCheckSignature = function (sig) { var data = this.readData(4); return sig === data; }; /** * @see DataReader.readData */ StringReader.prototype.readData = function(size) { this.checkOffset(size); // this will work because the constructor applied the "& 0xff" mask. var result = this.data.slice(this.zero + this.index, this.zero + this.index + size); this.index += size; return result; }; module.exports = StringReader; },{"../utils":32,"./DataReader":18}],21:[function(require,module,exports){ "use strict"; var ArrayReader = require("./ArrayReader"); var utils = require("../utils"); function Uint8ArrayReader(data) { ArrayReader.call(this, data); } utils.inherits(Uint8ArrayReader, ArrayReader); /** * @see DataReader.readData */ Uint8ArrayReader.prototype.readData = function(size) { this.checkOffset(size); if(size === 0) { // in IE10, when using subarray(idx, idx), we get the array [0x00] instead of []. return new Uint8Array(0); } var result = this.data.subarray(this.zero + this.index, this.zero + this.index + size); this.index += size; return result; }; module.exports = Uint8ArrayReader; },{"../utils":32,"./ArrayReader":17}],22:[function(require,module,exports){ "use strict"; var utils = require("../utils"); var support = require("../support"); var ArrayReader = require("./ArrayReader"); var StringReader = require("./StringReader"); var NodeBufferReader = require("./NodeBufferReader"); var Uint8ArrayReader = require("./Uint8ArrayReader"); /** * Create a reader adapted to the data. * @param {String|ArrayBuffer|Uint8Array|Buffer} data the data to read. * @return {DataReader} the data reader. */ module.exports = function (data) { var type = utils.getTypeOf(data); utils.checkSupport(type); if (type === "string" && !support.uint8array) { return new StringReader(data); } if (type === "nodebuffer") { return new NodeBufferReader(data); } if (support.uint8array) { return new Uint8ArrayReader(utils.transformTo("uint8array", data)); } return new ArrayReader(utils.transformTo("array", data)); }; },{"../support":30,"../utils":32,"./ArrayReader":17,"./NodeBufferReader":19,"./StringReader":20,"./Uint8ArrayReader":21}],23:[function(require,module,exports){ "use strict"; exports.LOCAL_FILE_HEADER = "PK\x03\x04"; exports.CENTRAL_FILE_HEADER = "PK\x01\x02"; exports.CENTRAL_DIRECTORY_END = "PK\x05\x06"; exports.ZIP64_CENTRAL_DIRECTORY_LOCATOR = "PK\x06\x07"; exports.ZIP64_CENTRAL_DIRECTORY_END = "PK\x06\x06"; exports.DATA_DESCRIPTOR = "PK\x07\x08"; },{}],24:[function(require,module,exports){ "use strict"; var GenericWorker = require("./GenericWorker"); var utils = require("../utils"); /** * A worker which convert chunks to a specified type. * @constructor * @param {String} destType the destination type. */ function ConvertWorker(destType) { GenericWorker.call(this, "ConvertWorker to " + destType); this.destType = destType; } utils.inherits(ConvertWorker, GenericWorker); /** * @see GenericWorker.processChunk */ ConvertWorker.prototype.processChunk = function (chunk) { this.push({ data : utils.transformTo(this.destType, chunk.data), meta : chunk.meta }); }; module.exports = ConvertWorker; },{"../utils":32,"./GenericWorker":28}],25:[function(require,module,exports){ "use strict"; var GenericWorker = require("./GenericWorker"); var crc32 = require("../crc32"); var utils = require("../utils"); /** * A worker which calculate the crc32 of the data flowing through. * @constructor */ function Crc32Probe() { GenericWorker.call(this, "Crc32Probe"); this.withStreamInfo("crc32", 0); } utils.inherits(Crc32Probe, GenericWorker); /** * @see GenericWorker.processChunk */ Crc32Probe.prototype.processChunk = function (chunk) { this.streamInfo.crc32 = crc32(chunk.data, this.streamInfo.crc32 || 0); this.push(chunk); }; module.exports = Crc32Probe; },{"../crc32":4,"../utils":32,"./GenericWorker":28}],26:[function(require,module,exports){ "use strict"; var utils = require("../utils"); var GenericWorker = require("./GenericWorker"); /** * A worker which calculate the total length of the data flowing through. * @constructor * @param {String} propName the name used to expose the length */ function DataLengthProbe(propName) { GenericWorker.call(this, "DataLengthProbe for " + propName); this.propName = propName; this.withStreamInfo(propName, 0); } utils.inherits(DataLengthProbe, GenericWorker); /** * @see GenericWorker.processChunk */ DataLengthProbe.prototype.processChunk = function (chunk) { if(chunk) { var length = this.streamInfo[this.propName] || 0; this.streamInfo[this.propName] = length + chunk.data.length; } GenericWorker.prototype.processChunk.call(this, chunk); }; module.exports = DataLengthProbe; },{"../utils":32,"./GenericWorker":28}],27:[function(require,module,exports){ "use strict"; var utils = require("../utils"); var GenericWorker = require("./GenericWorker"); // the size of the generated chunks // TODO expose this as a public variable var DEFAULT_BLOCK_SIZE = 16 * 1024; /** * A worker that reads a content and emits chunks. * @constructor * @param {Promise} dataP the promise of the data to split */ function DataWorker(dataP) { GenericWorker.call(this, "DataWorker"); var self = this; this.dataIsReady = false; this.index = 0; this.max = 0; this.data = null; this.type = ""; this._tickScheduled = false; dataP.then(function (data) { self.dataIsReady = true; self.data = data; self.max = data && data.length || 0; self.type = utils.getTypeOf(data); if(!self.isPaused) { self._tickAndRepeat(); } }, function (e) { self.error(e); }); } utils.inherits(DataWorker, GenericWorker); /** * @see GenericWorker.cleanUp */ DataWorker.prototype.cleanUp = function () { GenericWorker.prototype.cleanUp.call(this); this.data = null; }; /** * @see GenericWorker.resume */ DataWorker.prototype.resume = function () { if(!GenericWorker.prototype.resume.call(this)) { return false; } if (!this._tickScheduled && this.dataIsReady) { this._tickScheduled = true; utils.delay(this._tickAndRepeat, [], this); } return true; }; /** * Trigger a tick a schedule an other call to this function. */ DataWorker.prototype._tickAndRepeat = function() { this._tickScheduled = false; if(this.isPaused || this.isFinished) { return; } this._tick(); if(!this.isFinished) { utils.delay(this._tickAndRepeat, [], this); this._tickScheduled = true; } }; /** * Read and push a chunk. */ DataWorker.prototype._tick = function() { if(this.isPaused || this.isFinished) { return false; } var size = DEFAULT_BLOCK_SIZE; var data = null, nextIndex = Math.min(this.max, this.index + size); if (this.index >= this.max) { // EOF return this.end(); } else { switch(this.type) { case "string": data = this.data.substring(this.index, nextIndex); break; case "uint8array": data = this.data.subarray(this.index, nextIndex); break; case "array": case "nodebuffer": data = this.data.slice(this.index, nextIndex); break; } this.index = nextIndex; return this.push({ data : data, meta : { percent : this.max ? this.index / this.max * 100 : 0 } }); } }; module.exports = DataWorker; },{"../utils":32,"./GenericWorker":28}],28:[function(require,module,exports){ "use strict"; /** * A worker that does nothing but passing chunks to the next one. This is like * a nodejs stream but with some differences. On the good side : * - it works on IE 6-9 without any issue / polyfill * - it weights less than the full dependencies bundled with browserify * - it forwards errors (no need to declare an error handler EVERYWHERE) * * A chunk is an object with 2 attributes : `meta` and `data`. The former is an * object containing anything (`percent` for example), see each worker for more * details. The latter is the real data (String, Uint8Array, etc). * * @constructor * @param {String} name the name of the stream (mainly used for debugging purposes) */ function GenericWorker(name) { // the name of the worker this.name = name || "default"; // an object containing metadata about the workers chain this.streamInfo = {}; // an error which happened when the worker was paused this.generatedError = null; // an object containing metadata to be merged by this worker into the general metadata this.extraStreamInfo = {}; // true if the stream is paused (and should not do anything), false otherwise this.isPaused = true; // true if the stream is finished (and should not do anything), false otherwise this.isFinished = false; // true if the stream is locked to prevent further structure updates (pipe), false otherwise this.isLocked = false; // the event listeners this._listeners = { "data":[], "end":[], "error":[] }; // the previous worker, if any this.previous = null; } GenericWorker.prototype = { /** * Push a chunk to the next workers. * @param {Object} chunk the chunk to push */ push : function (chunk) { this.emit("data", chunk); }, /** * End the stream. * @return {Boolean} true if this call ended the worker, false otherwise. */ end : function () { if (this.isFinished) { return false; } this.flush(); try { this.emit("end"); this.cleanUp(); this.isFinished = true; } catch (e) { this.emit("error", e); } return true; }, /** * End the stream with an error. * @param {Error} e the error which caused the premature end. * @return {Boolean} true if this call ended the worker with an error, false otherwise. */ error : function (e) { if (this.isFinished) { return false; } if(this.isPaused) { this.generatedError = e; } else { this.isFinished = true; this.emit("error", e); // in the workers chain exploded in the middle of the chain, // the error event will go downward but we also need to notify // workers upward that there has been an error. if(this.previous) { this.previous.error(e); } this.cleanUp(); } return true; }, /** * Add a callback on an event. * @param {String} name the name of the event (data, end, error) * @param {Function} listener the function to call when the event is triggered * @return {GenericWorker} the current object for chainability */ on : function (name, listener) { this._listeners[name].push(listener); return this; }, /** * Clean any references when a worker is ending. */ cleanUp : function () { this.streamInfo = this.generatedError = this.extraStreamInfo = null; this._listeners = []; }, /** * Trigger an event. This will call registered callback with the provided arg. * @param {String} name the name of the event (data, end, error) * @param {Object} arg the argument to call the callback with. */ emit : function (name, arg) { if (this._listeners[name]) { for(var i = 0; i < this._listeners[name].length; i++) { this._listeners[name][i].call(this, arg); } } }, /** * Chain a worker with an other. * @param {Worker} next the worker receiving events from the current one. * @return {worker} the next worker for chainability */ pipe : function (next) { return next.registerPrevious(this); }, /** * Same as `pipe` in the other direction. * Using an API with `pipe(next)` is very easy. * Implementing the API with the point of view of the next one registering * a source is easier, see the ZipFileWorker. * @param {Worker} previous the previous worker, sending events to this one * @return {Worker} the current worker for chainability */ registerPrevious : function (previous) { if (this.isLocked) { throw new Error("The stream '" + this + "' has already been used."); } // sharing the streamInfo... this.streamInfo = previous.streamInfo; // ... and adding our own bits this.mergeStreamInfo(); this.previous = previous; var self = this; previous.on("data", function (chunk) { self.processChunk(chunk); }); previous.on("end", function () { self.end(); }); previous.on("error", function (e) { self.error(e); }); return this; }, /** * Pause the stream so it doesn't send events anymore. * @return {Boolean} true if this call paused the worker, false otherwise. */ pause : function () { if(this.isPaused || this.isFinished) { return false; } this.isPaused = true; if(this.previous) { this.previous.pause(); } return true; }, /** * Resume a paused stream. * @return {Boolean} true if this call resumed the worker, false otherwise. */ resume : function () { if(!this.isPaused || this.isFinished) { return false; } this.isPaused = false; // if true, the worker tried to resume but failed var withError = false; if(this.generatedError) { this.error(this.generatedError); withError = true; } if(this.previous) { this.previous.resume(); } return !withError; }, /** * Flush any remaining bytes as the stream is ending. */ flush : function () {}, /** * Process a chunk. This is usually the method overridden. * @param {Object} chunk the chunk to process. */ processChunk : function(chunk) { this.push(chunk); }, /** * Add a key/value to be added in the workers chain streamInfo once activated. * @param {String} key the key to use * @param {Object} value the associated value * @return {Worker} the current worker for chainability */ withStreamInfo : function (key, value) { this.extraStreamInfo[key] = value; this.mergeStreamInfo(); return this; }, /** * Merge this worker's streamInfo into the chain's streamInfo. */ mergeStreamInfo : function () { for(var key in this.extraStreamInfo) { if (!Object.prototype.hasOwnProperty.call(this.extraStreamInfo, key)) { continue; } this.streamInfo[key] = this.extraStreamInfo[key]; } }, /** * Lock the stream to prevent further updates on the workers chain. * After calling this method, all calls to pipe will fail. */ lock: function () { if (this.isLocked) { throw new Error("The stream '" + this + "' has already been used."); } this.isLocked = true; if (this.previous) { this.previous.lock(); } }, /** * * Pretty print the workers chain. */ toString : function () { var me = "Worker " + this.name; if (this.previous) { return this.previous + " -> " + me; } else { return me; } } }; module.exports = GenericWorker; },{}],29:[function(require,module,exports){ "use strict"; var utils = require("../utils"); var ConvertWorker = require("./ConvertWorker"); var GenericWorker = require("./GenericWorker"); var base64 = require("../base64"); var support = require("../support"); var external = require("../external"); var NodejsStreamOutputAdapter = null; if (support.nodestream) { try { NodejsStreamOutputAdapter = require("../nodejs/NodejsStreamOutputAdapter"); } catch(e) { // ignore } } /** * Apply the final transformation of the data. If the user wants a Blob for * example, it's easier to work with an U8intArray and finally do the * ArrayBuffer/Blob conversion. * @param {String} type the name of the final type * @param {String|Uint8Array|Buffer} content the content to transform * @param {String} mimeType the mime type of the content, if applicable. * @return {String|Uint8Array|ArrayBuffer|Buffer|Blob} the content in the right format. */ function transformZipOutput(type, content, mimeType) { switch(type) { case "blob" : return utils.newBlob(utils.transformTo("arraybuffer", content), mimeType); case "base64" : return base64.encode(content); default : return utils.transformTo(type, content); } } /** * Concatenate an array of data of the given type. * @param {String} type the type of the data in the given array. * @param {Array} dataArray the array containing the data chunks to concatenate * @return {String|Uint8Array|Buffer} the concatenated data * @throws Error if the asked type is unsupported */ function concat (type, dataArray) { var i, index = 0, res = null, totalLength = 0; for(i = 0; i < dataArray.length; i++) { totalLength += dataArray[i].length; } switch(type) { case "string": return dataArray.join(""); case "array": return Array.prototype.concat.apply([], dataArray); case "uint8array": res = new Uint8Array(totalLength); for(i = 0; i < dataArray.length; i++) { res.set(dataArray[i], index); index += dataArray[i].length; } return res; case "nodebuffer": return Buffer.concat(dataArray); default: throw new Error("concat : unsupported type '" + type + "'"); } } /** * Listen a StreamHelper, accumulate its content and concatenate it into a * complete block. * @param {StreamHelper} helper the helper to use. * @param {Function} updateCallback a callback called on each update. Called * with one arg : * - the metadata linked to the update received. * @return Promise the promise for the accumulation. */ function accumulate(helper, updateCallback) { return new external.Promise(function (resolve, reject){ var dataArray = []; var chunkType = helper._internalType, resultType = helper._outputType, mimeType = helper._mimeType; helper .on("data", function (data, meta) { dataArray.push(data); if(updateCallback) { updateCallback(meta); } }) .on("error", function(err) { dataArray = []; reject(err); }) .on("end", function (){ try { var result = transformZipOutput(resultType, concat(chunkType, dataArray), mimeType); resolve(result); } catch (e) { reject(e); } dataArray = []; }) .resume(); }); } /** * An helper to easily use workers outside of JSZip. * @constructor * @param {Worker} worker the worker to wrap * @param {String} outputType the type of data expected by the use * @param {String} mimeType the mime type of the content, if applicable. */ function StreamHelper(worker, outputType, mimeType) { var internalType = outputType; switch(outputType) { case "blob": case "arraybuffer": internalType = "uint8array"; break; case "base64": internalType = "string"; break; } try { // the type used internally this._internalType = internalType; // the type used to output results this._outputType = outputType; // the mime type this._mimeType = mimeType; utils.checkSupport(internalType); this._worker = worker.pipe(new ConvertWorker(internalType)); // the last workers can be rewired without issues but we need to // prevent any updates on previous workers. worker.lock(); } catch(e) { this._worker = new GenericWorker("error"); this._worker.error(e); } } StreamHelper.prototype = { /** * Listen a StreamHelper, accumulate its content and concatenate it into a * complete block. * @param {Function} updateCb the update callback. * @return Promise the promise for the accumulation. */ accumulate : function (updateCb) { return accumulate(this, updateCb); }, /** * Add a listener on an event triggered on a stream. * @param {String} evt the name of the event * @param {Function} fn the listener * @return {StreamHelper} the current helper. */ on : function (evt, fn) { var self = this; if(evt === "data") { this._worker.on(evt, function (chunk) { fn.call(self, chunk.data, chunk.meta); }); } else { this._worker.on(evt, function () { utils.delay(fn, arguments, self); }); } return this; }, /** * Resume the flow of chunks. * @return {StreamHelper} the current helper. */ resume : function () { utils.delay(this._worker.resume, [], this._worker); return this; }, /** * Pause the flow of chunks. * @return {StreamHelper} the current helper. */ pause : function () { this._worker.pause(); return this; }, /** * Return a nodejs stream for this helper. * @param {Function} updateCb the update callback. * @return {NodejsStreamOutputAdapter} the nodejs stream. */ toNodejsStream : function (updateCb) { utils.checkSupport("nodestream"); if (this._outputType !== "nodebuffer") { // an object stream containing blob/arraybuffer/uint8array/string // is strange and I don't know if it would be useful. // I you find this comment and have a good usecase, please open a // bug report ! throw new Error(this._outputType + " is not supported by this method"); } return new NodejsStreamOutputAdapter(this, { objectMode : this._outputType !== "nodebuffer" }, updateCb); } }; module.exports = StreamHelper; },{"../base64":1,"../external":6,"../nodejs/NodejsStreamOutputAdapter":13,"../support":30,"../utils":32,"./ConvertWorker":24,"./GenericWorker":28}],30:[function(require,module,exports){ "use strict"; exports.base64 = true; exports.array = true; exports.string = true; exports.arraybuffer = typeof ArrayBuffer !== "undefined" && typeof Uint8Array !== "undefined"; exports.nodebuffer = typeof Buffer !== "undefined"; // contains true if JSZip can read/generate Uint8Array, false otherwise. exports.uint8array = typeof Uint8Array !== "undefined"; if (typeof ArrayBuffer === "undefined") { exports.blob = false; } else { var buffer = new ArrayBuffer(0); try { exports.blob = new Blob([buffer], { type: "application/zip" }).size === 0; } catch (e) { try { var Builder = self.BlobBuilder || self.WebKitBlobBuilder || self.MozBlobBuilder || self.MSBlobBuilder; var builder = new Builder(); builder.append(buffer); exports.blob = builder.getBlob("application/zip").size === 0; } catch (e) { exports.blob = false; } } } try { exports.nodestream = !!require("readable-stream").Readable; } catch(e) { exports.nodestream = false; } },{"readable-stream":16}],31:[function(require,module,exports){ "use strict"; var utils = require("./utils"); var support = require("./support"); var nodejsUtils = require("./nodejsUtils"); var GenericWorker = require("./stream/GenericWorker"); /** * The following functions come from pako, from pako/lib/utils/strings * released under the MIT license, see pako https://github.com/nodeca/pako/ */ // Table with utf8 lengths (calculated by first byte of sequence) // Note, that 5 & 6-byte values and some 4-byte values can not be represented in JS, // because max possible codepoint is 0x10ffff var _utf8len = new Array(256); for (var i=0; i<256; i++) { _utf8len[i] = (i >= 252 ? 6 : i >= 248 ? 5 : i >= 240 ? 4 : i >= 224 ? 3 : i >= 192 ? 2 : 1); } _utf8len[254]=_utf8len[254]=1; // Invalid sequence start // convert string to array (typed, when possible) var string2buf = function (str) { var buf, c, c2, m_pos, i, str_len = str.length, buf_len = 0; // count binary size for (m_pos = 0; m_pos < str_len; m_pos++) { c = str.charCodeAt(m_pos); if ((c & 0xfc00) === 0xd800 && (m_pos+1 < str_len)) { c2 = str.charCodeAt(m_pos+1); if ((c2 & 0xfc00) === 0xdc00) { c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00); m_pos++; } } buf_len += c < 0x80 ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4; } // allocate buffer if (support.uint8array) { buf = new Uint8Array(buf_len); } else { buf = new Array(buf_len); } // convert for (i=0, m_pos = 0; i < buf_len; m_pos++) { c = str.charCodeAt(m_pos); if ((c & 0xfc00) === 0xd800 && (m_pos+1 < str_len)) { c2 = str.charCodeAt(m_pos+1); if ((c2 & 0xfc00) === 0xdc00) { c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00); m_pos++; } } if (c < 0x80) { /* one byte */ buf[i++] = c; } else if (c < 0x800) { /* two bytes */ buf[i++] = 0xC0 | (c >>> 6); buf[i++] = 0x80 | (c & 0x3f); } else if (c < 0x10000) { /* three bytes */ buf[i++] = 0xE0 | (c >>> 12); buf[i++] = 0x80 | (c >>> 6 & 0x3f); buf[i++] = 0x80 | (c & 0x3f); } else { /* four bytes */ buf[i++] = 0xf0 | (c >>> 18); buf[i++] = 0x80 | (c >>> 12 & 0x3f); buf[i++] = 0x80 | (c >>> 6 & 0x3f); buf[i++] = 0x80 | (c & 0x3f); } } return buf; }; // Calculate max possible position in utf8 buffer, // that will not break sequence. If that's not possible // - (very small limits) return max size as is. // // buf[] - utf8 bytes array // max - length limit (mandatory); var utf8border = function(buf, max) { var pos; max = max || buf.length; if (max > buf.length) { max = buf.length; } // go back from last position, until start of sequence found pos = max-1; while (pos >= 0 && (buf[pos] & 0xC0) === 0x80) { pos--; } // Fuckup - very small and broken sequence, // return max, because we should return something anyway. if (pos < 0) { return max; } // If we came to start of buffer - that means vuffer is too small, // return max too. if (pos === 0) { return max; } return (pos + _utf8len[buf[pos]] > max) ? pos : max; }; // convert array to string var buf2string = function (buf) { var i, out, c, c_len; var len = buf.length; // Reserve max possible length (2 words per char) // NB: by unknown reasons, Array is significantly faster for // String.fromCharCode.apply than Uint16Array. var utf16buf = new Array(len*2); for (out=0, i=0; i 4) { utf16buf[out++] = 0xfffd; i += c_len-1; continue; } // apply mask on first byte c &= c_len === 2 ? 0x1f : c_len === 3 ? 0x0f : 0x07; // join the rest while (c_len > 1 && i < len) { c = (c << 6) | (buf[i++] & 0x3f); c_len--; } // terminated by end of string? if (c_len > 1) { utf16buf[out++] = 0xfffd; continue; } if (c < 0x10000) { utf16buf[out++] = c; } else { c -= 0x10000; utf16buf[out++] = 0xd800 | ((c >> 10) & 0x3ff); utf16buf[out++] = 0xdc00 | (c & 0x3ff); } } // shrinkBuf(utf16buf, out) if (utf16buf.length !== out) { if(utf16buf.subarray) { utf16buf = utf16buf.subarray(0, out); } else { utf16buf.length = out; } } // return String.fromCharCode.apply(null, utf16buf); return utils.applyFromCharCode(utf16buf); }; // That's all for the pako functions. /** * Transform a javascript string into an array (typed if possible) of bytes, * UTF-8 encoded. * @param {String} str the string to encode * @return {Array|Uint8Array|Buffer} the UTF-8 encoded string. */ exports.utf8encode = function utf8encode(str) { if (support.nodebuffer) { return nodejsUtils.newBufferFrom(str, "utf-8"); } return string2buf(str); }; /** * Transform a bytes array (or a representation) representing an UTF-8 encoded * string into a javascript string. * @param {Array|Uint8Array|Buffer} buf the data de decode * @return {String} the decoded string. */ exports.utf8decode = function utf8decode(buf) { if (support.nodebuffer) { return utils.transformTo("nodebuffer", buf).toString("utf-8"); } buf = utils.transformTo(support.uint8array ? "uint8array" : "array", buf); return buf2string(buf); }; /** * A worker to decode utf8 encoded binary chunks into string chunks. * @constructor */ function Utf8DecodeWorker() { GenericWorker.call(this, "utf-8 decode"); // the last bytes if a chunk didn't end with a complete codepoint. this.leftOver = null; } utils.inherits(Utf8DecodeWorker, GenericWorker); /** * @see GenericWorker.processChunk */ Utf8DecodeWorker.prototype.processChunk = function (chunk) { var data = utils.transformTo(support.uint8array ? "uint8array" : "array", chunk.data); // 1st step, re-use what's left of the previous chunk if (this.leftOver && this.leftOver.length) { if(support.uint8array) { var previousData = data; data = new Uint8Array(previousData.length + this.leftOver.length); data.set(this.leftOver, 0); data.set(previousData, this.leftOver.length); } else { data = this.leftOver.concat(data); } this.leftOver = null; } var nextBoundary = utf8border(data); var usableData = data; if (nextBoundary !== data.length) { if (support.uint8array) { usableData = data.subarray(0, nextBoundary); this.leftOver = data.subarray(nextBoundary, data.length); } else { usableData = data.slice(0, nextBoundary); this.leftOver = data.slice(nextBoundary, data.length); } } this.push({ data : exports.utf8decode(usableData), meta : chunk.meta }); }; /** * @see GenericWorker.flush */ Utf8DecodeWorker.prototype.flush = function () { if(this.leftOver && this.leftOver.length) { this.push({ data : exports.utf8decode(this.leftOver), meta : {} }); this.leftOver = null; } }; exports.Utf8DecodeWorker = Utf8DecodeWorker; /** * A worker to endcode string chunks into utf8 encoded binary chunks. * @constructor */ function Utf8EncodeWorker() { GenericWorker.call(this, "utf-8 encode"); } utils.inherits(Utf8EncodeWorker, GenericWorker); /** * @see GenericWorker.processChunk */ Utf8EncodeWorker.prototype.processChunk = function (chunk) { this.push({ data : exports.utf8encode(chunk.data), meta : chunk.meta }); }; exports.Utf8EncodeWorker = Utf8EncodeWorker; },{"./nodejsUtils":14,"./stream/GenericWorker":28,"./support":30,"./utils":32}],32:[function(require,module,exports){ "use strict"; var support = require("./support"); var base64 = require("./base64"); var nodejsUtils = require("./nodejsUtils"); var external = require("./external"); require("setimmediate"); /** * Convert a string that pass as a "binary string": it should represent a byte * array but may have > 255 char codes. Be sure to take only the first byte * and returns the byte array. * @param {String} str the string to transform. * @return {Array|Uint8Array} the string in a binary format. */ function string2binary(str) { var result = null; if (support.uint8array) { result = new Uint8Array(str.length); } else { result = new Array(str.length); } return stringToArrayLike(str, result); } /** * Create a new blob with the given content and the given type. * @param {String|ArrayBuffer} part the content to put in the blob. DO NOT use * an Uint8Array because the stock browser of android 4 won't accept it (it * will be silently converted to a string, "[object Uint8Array]"). * * Use only ONE part to build the blob to avoid a memory leak in IE11 / Edge: * when a large amount of Array is used to create the Blob, the amount of * memory consumed is nearly 100 times the original data amount. * * @param {String} type the mime type of the blob. * @return {Blob} the created blob. */ exports.newBlob = function(part, type) { exports.checkSupport("blob"); try { // Blob constructor return new Blob([part], { type: type }); } catch (e) { try { // deprecated, browser only, old way var Builder = self.BlobBuilder || self.WebKitBlobBuilder || self.MozBlobBuilder || self.MSBlobBuilder; var builder = new Builder(); builder.append(part); return builder.getBlob(type); } catch (e) { // well, fuck ?! throw new Error("Bug : can't construct the Blob."); } } }; /** * The identity function. * @param {Object} input the input. * @return {Object} the same input. */ function identity(input) { return input; } /** * Fill in an array with a string. * @param {String} str the string to use. * @param {Array|ArrayBuffer|Uint8Array|Buffer} array the array to fill in (will be mutated). * @return {Array|ArrayBuffer|Uint8Array|Buffer} the updated array. */ function stringToArrayLike(str, array) { for (var i = 0; i < str.length; ++i) { array[i] = str.charCodeAt(i) & 0xFF; } return array; } /** * An helper for the function arrayLikeToString. * This contains static information and functions that * can be optimized by the browser JIT compiler. */ var arrayToStringHelper = { /** * Transform an array of int into a string, chunk by chunk. * See the performances notes on arrayLikeToString. * @param {Array|ArrayBuffer|Uint8Array|Buffer} array the array to transform. * @param {String} type the type of the array. * @param {Integer} chunk the chunk size. * @return {String} the resulting string. * @throws Error if the chunk is too big for the stack. */ stringifyByChunk: function(array, type, chunk) { var result = [], k = 0, len = array.length; // shortcut if (len <= chunk) { return String.fromCharCode.apply(null, array); } while (k < len) { if (type === "array" || type === "nodebuffer") { result.push(String.fromCharCode.apply(null, array.slice(k, Math.min(k + chunk, len)))); } else { result.push(String.fromCharCode.apply(null, array.subarray(k, Math.min(k + chunk, len)))); } k += chunk; } return result.join(""); }, /** * Call String.fromCharCode on every item in the array. * This is the naive implementation, which generate A LOT of intermediate string. * This should be used when everything else fail. * @param {Array|ArrayBuffer|Uint8Array|Buffer} array the array to transform. * @return {String} the result. */ stringifyByChar: function(array){ var resultStr = ""; for(var i = 0; i < array.length; i++) { resultStr += String.fromCharCode(array[i]); } return resultStr; }, applyCanBeUsed : { /** * true if the browser accepts to use String.fromCharCode on Uint8Array */ uint8array : (function () { try { return support.uint8array && String.fromCharCode.apply(null, new Uint8Array(1)).length === 1; } catch (e) { return false; } })(), /** * true if the browser accepts to use String.fromCharCode on nodejs Buffer. */ nodebuffer : (function () { try { return support.nodebuffer && String.fromCharCode.apply(null, nodejsUtils.allocBuffer(1)).length === 1; } catch (e) { return false; } })() } }; /** * Transform an array-like object to a string. * @param {Array|ArrayBuffer|Uint8Array|Buffer} array the array to transform. * @return {String} the result. */ function arrayLikeToString(array) { // Performances notes : // -------------------- // String.fromCharCode.apply(null, array) is the fastest, see // see http://jsperf.com/converting-a-uint8array-to-a-string/2 // but the stack is limited (and we can get huge arrays !). // // result += String.fromCharCode(array[i]); generate too many strings ! // // This code is inspired by http://jsperf.com/arraybuffer-to-string-apply-performance/2 // TODO : we now have workers that split the work. Do we still need that ? var chunk = 65536, type = exports.getTypeOf(array), canUseApply = true; if (type === "uint8array") { canUseApply = arrayToStringHelper.applyCanBeUsed.uint8array; } else if (type === "nodebuffer") { canUseApply = arrayToStringHelper.applyCanBeUsed.nodebuffer; } if (canUseApply) { while (chunk > 1) { try { return arrayToStringHelper.stringifyByChunk(array, type, chunk); } catch (e) { chunk = Math.floor(chunk / 2); } } } // no apply or chunk error : slow and painful algorithm // default browser on android 4.* return arrayToStringHelper.stringifyByChar(array); } exports.applyFromCharCode = arrayLikeToString; /** * Copy the data from an array-like to an other array-like. * @param {Array|ArrayBuffer|Uint8Array|Buffer} arrayFrom the origin array. * @param {Array|ArrayBuffer|Uint8Array|Buffer} arrayTo the destination array which will be mutated. * @return {Array|ArrayBuffer|Uint8Array|Buffer} the updated destination array. */ function arrayLikeToArrayLike(arrayFrom, arrayTo) { for (var i = 0; i < arrayFrom.length; i++) { arrayTo[i] = arrayFrom[i]; } return arrayTo; } // a matrix containing functions to transform everything into everything. var transform = {}; // string to ? transform["string"] = { "string": identity, "array": function(input) { return stringToArrayLike(input, new Array(input.length)); }, "arraybuffer": function(input) { return transform["string"]["uint8array"](input).buffer; }, "uint8array": function(input) { return stringToArrayLike(input, new Uint8Array(input.length)); }, "nodebuffer": function(input) { return stringToArrayLike(input, nodejsUtils.allocBuffer(input.length)); } }; // array to ? transform["array"] = { "string": arrayLikeToString, "array": identity, "arraybuffer": function(input) { return (new Uint8Array(input)).buffer; }, "uint8array": function(input) { return new Uint8Array(input); }, "nodebuffer": function(input) { return nodejsUtils.newBufferFrom(input); } }; // arraybuffer to ? transform["arraybuffer"] = { "string": function(input) { return arrayLikeToString(new Uint8Array(input)); }, "array": function(input) { return arrayLikeToArrayLike(new Uint8Array(input), new Array(input.byteLength)); }, "arraybuffer": identity, "uint8array": function(input) { return new Uint8Array(input); }, "nodebuffer": function(input) { return nodejsUtils.newBufferFrom(new Uint8Array(input)); } }; // uint8array to ? transform["uint8array"] = { "string": arrayLikeToString, "array": function(input) { return arrayLikeToArrayLike(input, new Array(input.length)); }, "arraybuffer": function(input) { return input.buffer; }, "uint8array": identity, "nodebuffer": function(input) { return nodejsUtils.newBufferFrom(input); } }; // nodebuffer to ? transform["nodebuffer"] = { "string": arrayLikeToString, "array": function(input) { return arrayLikeToArrayLike(input, new Array(input.length)); }, "arraybuffer": function(input) { return transform["nodebuffer"]["uint8array"](input).buffer; }, "uint8array": function(input) { return arrayLikeToArrayLike(input, new Uint8Array(input.length)); }, "nodebuffer": identity }; /** * Transform an input into any type. * The supported output type are : string, array, uint8array, arraybuffer, nodebuffer. * If no output type is specified, the unmodified input will be returned. * @param {String} outputType the output type. * @param {String|Array|ArrayBuffer|Uint8Array|Buffer} input the input to convert. * @throws {Error} an Error if the browser doesn't support the requested output type. */ exports.transformTo = function(outputType, input) { if (!input) { // undefined, null, etc // an empty string won't harm. input = ""; } if (!outputType) { return input; } exports.checkSupport(outputType); var inputType = exports.getTypeOf(input); var result = transform[inputType][outputType](input); return result; }; /** * Resolve all relative path components, "." and "..", in a path. If these relative components * traverse above the root then the resulting path will only contain the final path component. * * All empty components, e.g. "//", are removed. * @param {string} path A path with / or \ separators * @returns {string} The path with all relative path components resolved. */ exports.resolve = function(path) { var parts = path.split("/"); var result = []; for (var index = 0; index < parts.length; index++) { var part = parts[index]; // Allow the first and last component to be empty for trailing slashes. if (part === "." || (part === "" && index !== 0 && index !== parts.length - 1)) { continue; } else if (part === "..") { result.pop(); } else { result.push(part); } } return result.join("/"); }; /** * Return the type of the input. * The type will be in a format valid for JSZip.utils.transformTo : string, array, uint8array, arraybuffer. * @param {Object} input the input to identify. * @return {String} the (lowercase) type of the input. */ exports.getTypeOf = function(input) { if (typeof input === "string") { return "string"; } if (Object.prototype.toString.call(input) === "[object Array]") { return "array"; } if (support.nodebuffer && nodejsUtils.isBuffer(input)) { return "nodebuffer"; } if (support.uint8array && input instanceof Uint8Array) { return "uint8array"; } if (support.arraybuffer && input instanceof ArrayBuffer) { return "arraybuffer"; } }; /** * Throw an exception if the type is not supported. * @param {String} type the type to check. * @throws {Error} an Error if the browser doesn't support the requested type. */ exports.checkSupport = function(type) { var supported = support[type.toLowerCase()]; if (!supported) { throw new Error(type + " is not supported by this platform"); } }; exports.MAX_VALUE_16BITS = 65535; exports.MAX_VALUE_32BITS = -1; // well, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" is parsed as -1 /** * Prettify a string read as binary. * @param {string} str the string to prettify. * @return {string} a pretty string. */ exports.pretty = function(str) { var res = "", code, i; for (i = 0; i < (str || "").length; i++) { code = str.charCodeAt(i); res += "\\x" + (code < 16 ? "0" : "") + code.toString(16).toUpperCase(); } return res; }; /** * Defer the call of a function. * @param {Function} callback the function to call asynchronously. * @param {Array} args the arguments to give to the callback. */ exports.delay = function(callback, args, self) { setImmediate(function () { callback.apply(self || null, args || []); }); }; /** * Extends a prototype with an other, without calling a constructor with * side effects. Inspired by nodejs' `utils.inherits` * @param {Function} ctor the constructor to augment * @param {Function} superCtor the parent constructor to use */ exports.inherits = function (ctor, superCtor) { var Obj = function() {}; Obj.prototype = superCtor.prototype; ctor.prototype = new Obj(); }; /** * Merge the objects passed as parameters into a new one. * @private * @param {...Object} var_args All objects to merge. * @return {Object} a new object with the data of the others. */ exports.extend = function() { var result = {}, i, attr; for (i = 0; i < arguments.length; i++) { // arguments is not enumerable in some browsers for (attr in arguments[i]) { if (Object.prototype.hasOwnProperty.call(arguments[i], attr) && typeof result[attr] === "undefined") { result[attr] = arguments[i][attr]; } } } return result; }; /** * Transform arbitrary content into a Promise. * @param {String} name a name for the content being processed. * @param {Object} inputData the content to process. * @param {Boolean} isBinary true if the content is not an unicode string * @param {Boolean} isOptimizedBinaryString true if the string content only has one byte per character. * @param {Boolean} isBase64 true if the string content is encoded with base64. * @return {Promise} a promise in a format usable by JSZip. */ exports.prepareContent = function(name, inputData, isBinary, isOptimizedBinaryString, isBase64) { // if inputData is already a promise, this flatten it. var promise = external.Promise.resolve(inputData).then(function(data) { var isBlob = support.blob && (data instanceof Blob || ["[object File]", "[object Blob]"].indexOf(Object.prototype.toString.call(data)) !== -1); if (isBlob && typeof FileReader !== "undefined") { return new external.Promise(function (resolve, reject) { var reader = new FileReader(); reader.onload = function(e) { resolve(e.target.result); }; reader.onerror = function(e) { reject(e.target.error); }; reader.readAsArrayBuffer(data); }); } else { return data; } }); return promise.then(function(data) { var dataType = exports.getTypeOf(data); if (!dataType) { return external.Promise.reject( new Error("Can't read the data of '" + name + "'. Is it " + "in a supported JavaScript type (String, Blob, ArrayBuffer, etc) ?") ); } // special case : it's way easier to work with Uint8Array than with ArrayBuffer if (dataType === "arraybuffer") { data = exports.transformTo("uint8array", data); } else if (dataType === "string") { if (isBase64) { data = base64.decode(data); } else if (isBinary) { // optimizedBinaryString === true means that the file has already been filtered with a 0xFF mask if (isOptimizedBinaryString !== true) { // this is a string, not in a base64 format. // Be sure that this is a correct "binary string" data = string2binary(data); } } } return data; }); }; },{"./base64":1,"./external":6,"./nodejsUtils":14,"./support":30,"setimmediate":54}],33:[function(require,module,exports){ "use strict"; var readerFor = require("./reader/readerFor"); var utils = require("./utils"); var sig = require("./signature"); var ZipEntry = require("./zipEntry"); var support = require("./support"); // class ZipEntries {{{ /** * All the entries in the zip file. * @constructor * @param {Object} loadOptions Options for loading the stream. */ function ZipEntries(loadOptions) { this.files = []; this.loadOptions = loadOptions; } ZipEntries.prototype = { /** * Check that the reader is on the specified signature. * @param {string} expectedSignature the expected signature. * @throws {Error} if it is an other signature. */ checkSignature: function(expectedSignature) { if (!this.reader.readAndCheckSignature(expectedSignature)) { this.reader.index -= 4; var signature = this.reader.readString(4); throw new Error("Corrupted zip or bug: unexpected signature " + "(" + utils.pretty(signature) + ", expected " + utils.pretty(expectedSignature) + ")"); } }, /** * Check if the given signature is at the given index. * @param {number} askedIndex the index to check. * @param {string} expectedSignature the signature to expect. * @return {boolean} true if the signature is here, false otherwise. */ isSignature: function(askedIndex, expectedSignature) { var currentIndex = this.reader.index; this.reader.setIndex(askedIndex); var signature = this.reader.readString(4); var result = signature === expectedSignature; this.reader.setIndex(currentIndex); return result; }, /** * Read the end of the central directory. */ readBlockEndOfCentral: function() { this.diskNumber = this.reader.readInt(2); this.diskWithCentralDirStart = this.reader.readInt(2); this.centralDirRecordsOnThisDisk = this.reader.readInt(2); this.centralDirRecords = this.reader.readInt(2); this.centralDirSize = this.reader.readInt(4); this.centralDirOffset = this.reader.readInt(4); this.zipCommentLength = this.reader.readInt(2); // warning : the encoding depends of the system locale // On a linux machine with LANG=en_US.utf8, this field is utf8 encoded. // On a windows machine, this field is encoded with the localized windows code page. var zipComment = this.reader.readData(this.zipCommentLength); var decodeParamType = support.uint8array ? "uint8array" : "array"; // To get consistent behavior with the generation part, we will assume that // this is utf8 encoded unless specified otherwise. var decodeContent = utils.transformTo(decodeParamType, zipComment); this.zipComment = this.loadOptions.decodeFileName(decodeContent); }, /** * Read the end of the Zip 64 central directory. * Not merged with the method readEndOfCentral : * The end of central can coexist with its Zip64 brother, * I don't want to read the wrong number of bytes ! */ readBlockZip64EndOfCentral: function() { this.zip64EndOfCentralSize = this.reader.readInt(8); this.reader.skip(4); // this.versionMadeBy = this.reader.readString(2); // this.versionNeeded = this.reader.readInt(2); this.diskNumber = this.reader.readInt(4); this.diskWithCentralDirStart = this.reader.readInt(4); this.centralDirRecordsOnThisDisk = this.reader.readInt(8); this.centralDirRecords = this.reader.readInt(8); this.centralDirSize = this.reader.readInt(8); this.centralDirOffset = this.reader.readInt(8); this.zip64ExtensibleData = {}; var extraDataSize = this.zip64EndOfCentralSize - 44, index = 0, extraFieldId, extraFieldLength, extraFieldValue; while (index < extraDataSize) { extraFieldId = this.reader.readInt(2); extraFieldLength = this.reader.readInt(4); extraFieldValue = this.reader.readData(extraFieldLength); this.zip64ExtensibleData[extraFieldId] = { id: extraFieldId, length: extraFieldLength, value: extraFieldValue }; } }, /** * Read the end of the Zip 64 central directory locator. */ readBlockZip64EndOfCentralLocator: function() { this.diskWithZip64CentralDirStart = this.reader.readInt(4); this.relativeOffsetEndOfZip64CentralDir = this.reader.readInt(8); this.disksCount = this.reader.readInt(4); if (this.disksCount > 1) { throw new Error("Multi-volumes zip are not supported"); } }, /** * Read the local files, based on the offset read in the central part. */ readLocalFiles: function() { var i, file; for (i = 0; i < this.files.length; i++) { file = this.files[i]; this.reader.setIndex(file.localHeaderOffset); this.checkSignature(sig.LOCAL_FILE_HEADER); file.readLocalPart(this.reader); file.handleUTF8(); file.processAttributes(); } }, /** * Read the central directory. */ readCentralDir: function() { var file; this.reader.setIndex(this.centralDirOffset); while (this.reader.readAndCheckSignature(sig.CENTRAL_FILE_HEADER)) { file = new ZipEntry({ zip64: this.zip64 }, this.loadOptions); file.readCentralPart(this.reader); this.files.push(file); } if (this.centralDirRecords !== this.files.length) { if (this.centralDirRecords !== 0 && this.files.length === 0) { // We expected some records but couldn't find ANY. // This is really suspicious, as if something went wrong. throw new Error("Corrupted zip or bug: expected " + this.centralDirRecords + " records in central dir, got " + this.files.length); } else { // We found some records but not all. // Something is wrong but we got something for the user: no error here. // console.warn("expected", this.centralDirRecords, "records in central dir, got", this.files.length); } } }, /** * Read the end of central directory. */ readEndOfCentral: function() { var offset = this.reader.lastIndexOfSignature(sig.CENTRAL_DIRECTORY_END); if (offset < 0) { // Check if the content is a truncated zip or complete garbage. // A "LOCAL_FILE_HEADER" is not required at the beginning (auto // extractible zip for example) but it can give a good hint. // If an ajax request was used without responseType, we will also // get unreadable data. var isGarbage = !this.isSignature(0, sig.LOCAL_FILE_HEADER); if (isGarbage) { throw new Error("Can't find end of central directory : is this a zip file ? " + "If it is, see https://stuk.github.io/jszip/documentation/howto/read_zip.html"); } else { throw new Error("Corrupted zip: can't find end of central directory"); } } this.reader.setIndex(offset); var endOfCentralDirOffset = offset; this.checkSignature(sig.CENTRAL_DIRECTORY_END); this.readBlockEndOfCentral(); /* extract from the zip spec : 4) If one of the fields in the end of central directory record is too small to hold required data, the field should be set to -1 (0xFFFF or 0xFFFFFFFF) and the ZIP64 format record should be created. 5) The end of central directory record and the Zip64 end of central directory locator record must reside on the same disk when splitting or spanning an archive. */ if (this.diskNumber === utils.MAX_VALUE_16BITS || this.diskWithCentralDirStart === utils.MAX_VALUE_16BITS || this.centralDirRecordsOnThisDisk === utils.MAX_VALUE_16BITS || this.centralDirRecords === utils.MAX_VALUE_16BITS || this.centralDirSize === utils.MAX_VALUE_32BITS || this.centralDirOffset === utils.MAX_VALUE_32BITS) { this.zip64 = true; /* Warning : the zip64 extension is supported, but ONLY if the 64bits integer read from the zip file can fit into a 32bits integer. This cannot be solved : JavaScript represents all numbers as 64-bit double precision IEEE 754 floating point numbers. So, we have 53bits for integers and bitwise operations treat everything as 32bits. see https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Bitwise_Operators and http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf section 8.5 */ // should look for a zip64 EOCD locator offset = this.reader.lastIndexOfSignature(sig.ZIP64_CENTRAL_DIRECTORY_LOCATOR); if (offset < 0) { throw new Error("Corrupted zip: can't find the ZIP64 end of central directory locator"); } this.reader.setIndex(offset); this.checkSignature(sig.ZIP64_CENTRAL_DIRECTORY_LOCATOR); this.readBlockZip64EndOfCentralLocator(); // now the zip64 EOCD record if (!this.isSignature(this.relativeOffsetEndOfZip64CentralDir, sig.ZIP64_CENTRAL_DIRECTORY_END)) { // console.warn("ZIP64 end of central directory not where expected."); this.relativeOffsetEndOfZip64CentralDir = this.reader.lastIndexOfSignature(sig.ZIP64_CENTRAL_DIRECTORY_END); if (this.relativeOffsetEndOfZip64CentralDir < 0) { throw new Error("Corrupted zip: can't find the ZIP64 end of central directory"); } } this.reader.setIndex(this.relativeOffsetEndOfZip64CentralDir); this.checkSignature(sig.ZIP64_CENTRAL_DIRECTORY_END); this.readBlockZip64EndOfCentral(); } var expectedEndOfCentralDirOffset = this.centralDirOffset + this.centralDirSize; if (this.zip64) { expectedEndOfCentralDirOffset += 20; // end of central dir 64 locator expectedEndOfCentralDirOffset += 12 /* should not include the leading 12 bytes */ + this.zip64EndOfCentralSize; } var extraBytes = endOfCentralDirOffset - expectedEndOfCentralDirOffset; if (extraBytes > 0) { // console.warn(extraBytes, "extra bytes at beginning or within zipfile"); if (this.isSignature(endOfCentralDirOffset, sig.CENTRAL_FILE_HEADER)) { // The offsets seem wrong, but we have something at the specified offset. // So… we keep it. } else { // the offset is wrong, update the "zero" of the reader // this happens if data has been prepended (crx files for example) this.reader.zero = extraBytes; } } else if (extraBytes < 0) { throw new Error("Corrupted zip: missing " + Math.abs(extraBytes) + " bytes."); } }, prepareReader: function(data) { this.reader = readerFor(data); }, /** * Read a zip file and create ZipEntries. * @param {String|ArrayBuffer|Uint8Array|Buffer} data the binary string representing a zip file. */ load: function(data) { this.prepareReader(data); this.readEndOfCentral(); this.readCentralDir(); this.readLocalFiles(); } }; // }}} end of ZipEntries module.exports = ZipEntries; },{"./reader/readerFor":22,"./signature":23,"./support":30,"./utils":32,"./zipEntry":34}],34:[function(require,module,exports){ "use strict"; var readerFor = require("./reader/readerFor"); var utils = require("./utils"); var CompressedObject = require("./compressedObject"); var crc32fn = require("./crc32"); var utf8 = require("./utf8"); var compressions = require("./compressions"); var support = require("./support"); var MADE_BY_DOS = 0x00; var MADE_BY_UNIX = 0x03; /** * Find a compression registered in JSZip. * @param {string} compressionMethod the method magic to find. * @return {Object|null} the JSZip compression object, null if none found. */ var findCompression = function(compressionMethod) { for (var method in compressions) { if (!Object.prototype.hasOwnProperty.call(compressions, method)) { continue; } if (compressions[method].magic === compressionMethod) { return compressions[method]; } } return null; }; // class ZipEntry {{{ /** * An entry in the zip file. * @constructor * @param {Object} options Options of the current file. * @param {Object} loadOptions Options for loading the stream. */ function ZipEntry(options, loadOptions) { this.options = options; this.loadOptions = loadOptions; } ZipEntry.prototype = { /** * say if the file is encrypted. * @return {boolean} true if the file is encrypted, false otherwise. */ isEncrypted: function() { // bit 1 is set return (this.bitFlag & 0x0001) === 0x0001; }, /** * say if the file has utf-8 filename/comment. * @return {boolean} true if the filename/comment is in utf-8, false otherwise. */ useUTF8: function() { // bit 11 is set return (this.bitFlag & 0x0800) === 0x0800; }, /** * Read the local part of a zip file and add the info in this object. * @param {DataReader} reader the reader to use. */ readLocalPart: function(reader) { var compression, localExtraFieldsLength; // we already know everything from the central dir ! // If the central dir data are false, we are doomed. // On the bright side, the local part is scary : zip64, data descriptors, both, etc. // The less data we get here, the more reliable this should be. // Let's skip the whole header and dash to the data ! reader.skip(22); // in some zip created on windows, the filename stored in the central dir contains \ instead of /. // Strangely, the filename here is OK. // I would love to treat these zip files as corrupted (see http://www.info-zip.org/FAQ.html#backslashes // or APPNOTE#4.4.17.1, "All slashes MUST be forward slashes '/'") but there are a lot of bad zip generators... // Search "unzip mismatching "local" filename continuing with "central" filename version" on // the internet. // // I think I see the logic here : the central directory is used to display // content and the local directory is used to extract the files. Mixing / and \ // may be used to display \ to windows users and use / when extracting the files. // Unfortunately, this lead also to some issues : http://seclists.org/fulldisclosure/2009/Sep/394 this.fileNameLength = reader.readInt(2); localExtraFieldsLength = reader.readInt(2); // can't be sure this will be the same as the central dir // the fileName is stored as binary data, the handleUTF8 method will take care of the encoding. this.fileName = reader.readData(this.fileNameLength); reader.skip(localExtraFieldsLength); if (this.compressedSize === -1 || this.uncompressedSize === -1) { throw new Error("Bug or corrupted zip : didn't get enough information from the central directory " + "(compressedSize === -1 || uncompressedSize === -1)"); } compression = findCompression(this.compressionMethod); if (compression === null) { // no compression found throw new Error("Corrupted zip : compression " + utils.pretty(this.compressionMethod) + " unknown (inner file : " + utils.transformTo("string", this.fileName) + ")"); } this.decompressed = new CompressedObject(this.compressedSize, this.uncompressedSize, this.crc32, compression, reader.readData(this.compressedSize)); }, /** * Read the central part of a zip file and add the info in this object. * @param {DataReader} reader the reader to use. */ readCentralPart: function(reader) { this.versionMadeBy = reader.readInt(2); reader.skip(2); // this.versionNeeded = reader.readInt(2); this.bitFlag = reader.readInt(2); this.compressionMethod = reader.readString(2); this.date = reader.readDate(); this.crc32 = reader.readInt(4); this.compressedSize = reader.readInt(4); this.uncompressedSize = reader.readInt(4); var fileNameLength = reader.readInt(2); this.extraFieldsLength = reader.readInt(2); this.fileCommentLength = reader.readInt(2); this.diskNumberStart = reader.readInt(2); this.internalFileAttributes = reader.readInt(2); this.externalFileAttributes = reader.readInt(4); this.localHeaderOffset = reader.readInt(4); if (this.isEncrypted()) { throw new Error("Encrypted zip are not supported"); } // will be read in the local part, see the comments there reader.skip(fileNameLength); this.readExtraFields(reader); this.parseZIP64ExtraField(reader); this.fileComment = reader.readData(this.fileCommentLength); }, /** * Parse the external file attributes and get the unix/dos permissions. */ processAttributes: function () { this.unixPermissions = null; this.dosPermissions = null; var madeBy = this.versionMadeBy >> 8; // Check if we have the DOS directory flag set. // We look for it in the DOS and UNIX permissions // but some unknown platform could set it as a compatibility flag. this.dir = this.externalFileAttributes & 0x0010 ? true : false; if(madeBy === MADE_BY_DOS) { // first 6 bits (0 to 5) this.dosPermissions = this.externalFileAttributes & 0x3F; } if(madeBy === MADE_BY_UNIX) { this.unixPermissions = (this.externalFileAttributes >> 16) & 0xFFFF; // the octal permissions are in (this.unixPermissions & 0x01FF).toString(8); } // fail safe : if the name ends with a / it probably means a folder if (!this.dir && this.fileNameStr.slice(-1) === "/") { this.dir = true; } }, /** * Parse the ZIP64 extra field and merge the info in the current ZipEntry. * @param {DataReader} reader the reader to use. */ parseZIP64ExtraField: function() { if (!this.extraFields[0x0001]) { return; } // should be something, preparing the extra reader var extraReader = readerFor(this.extraFields[0x0001].value); // I really hope that these 64bits integer can fit in 32 bits integer, because js // won't let us have more. if (this.uncompressedSize === utils.MAX_VALUE_32BITS) { this.uncompressedSize = extraReader.readInt(8); } if (this.compressedSize === utils.MAX_VALUE_32BITS) { this.compressedSize = extraReader.readInt(8); } if (this.localHeaderOffset === utils.MAX_VALUE_32BITS) { this.localHeaderOffset = extraReader.readInt(8); } if (this.diskNumberStart === utils.MAX_VALUE_32BITS) { this.diskNumberStart = extraReader.readInt(4); } }, /** * Read the central part of a zip file and add the info in this object. * @param {DataReader} reader the reader to use. */ readExtraFields: function(reader) { var end = reader.index + this.extraFieldsLength, extraFieldId, extraFieldLength, extraFieldValue; if (!this.extraFields) { this.extraFields = {}; } while (reader.index + 4 < end) { extraFieldId = reader.readInt(2); extraFieldLength = reader.readInt(2); extraFieldValue = reader.readData(extraFieldLength); this.extraFields[extraFieldId] = { id: extraFieldId, length: extraFieldLength, value: extraFieldValue }; } reader.setIndex(end); }, /** * Apply an UTF8 transformation if needed. */ handleUTF8: function() { var decodeParamType = support.uint8array ? "uint8array" : "array"; if (this.useUTF8()) { this.fileNameStr = utf8.utf8decode(this.fileName); this.fileCommentStr = utf8.utf8decode(this.fileComment); } else { var upath = this.findExtraFieldUnicodePath(); if (upath !== null) { this.fileNameStr = upath; } else { // ASCII text or unsupported code page var fileNameByteArray = utils.transformTo(decodeParamType, this.fileName); this.fileNameStr = this.loadOptions.decodeFileName(fileNameByteArray); } var ucomment = this.findExtraFieldUnicodeComment(); if (ucomment !== null) { this.fileCommentStr = ucomment; } else { // ASCII text or unsupported code page var commentByteArray = utils.transformTo(decodeParamType, this.fileComment); this.fileCommentStr = this.loadOptions.decodeFileName(commentByteArray); } } }, /** * Find the unicode path declared in the extra field, if any. * @return {String} the unicode path, null otherwise. */ findExtraFieldUnicodePath: function() { var upathField = this.extraFields[0x7075]; if (upathField) { var extraReader = readerFor(upathField.value); // wrong version if (extraReader.readInt(1) !== 1) { return null; } // the crc of the filename changed, this field is out of date. if (crc32fn(this.fileName) !== extraReader.readInt(4)) { return null; } return utf8.utf8decode(extraReader.readData(upathField.length - 5)); } return null; }, /** * Find the unicode comment declared in the extra field, if any. * @return {String} the unicode comment, null otherwise. */ findExtraFieldUnicodeComment: function() { var ucommentField = this.extraFields[0x6375]; if (ucommentField) { var extraReader = readerFor(ucommentField.value); // wrong version if (extraReader.readInt(1) !== 1) { return null; } // the crc of the comment changed, this field is out of date. if (crc32fn(this.fileComment) !== extraReader.readInt(4)) { return null; } return utf8.utf8decode(extraReader.readData(ucommentField.length - 5)); } return null; } }; module.exports = ZipEntry; },{"./compressedObject":2,"./compressions":3,"./crc32":4,"./reader/readerFor":22,"./support":30,"./utf8":31,"./utils":32}],35:[function(require,module,exports){ "use strict"; var StreamHelper = require("./stream/StreamHelper"); var DataWorker = require("./stream/DataWorker"); var utf8 = require("./utf8"); var CompressedObject = require("./compressedObject"); var GenericWorker = require("./stream/GenericWorker"); /** * A simple object representing a file in the zip file. * @constructor * @param {string} name the name of the file * @param {String|ArrayBuffer|Uint8Array|Buffer} data the data * @param {Object} options the options of the file */ var ZipObject = function(name, data, options) { this.name = name; this.dir = options.dir; this.date = options.date; this.comment = options.comment; this.unixPermissions = options.unixPermissions; this.dosPermissions = options.dosPermissions; this._data = data; this._dataBinary = options.binary; // keep only the compression this.options = { compression : options.compression, compressionOptions : options.compressionOptions }; }; ZipObject.prototype = { /** * Create an internal stream for the content of this object. * @param {String} type the type of each chunk. * @return StreamHelper the stream. */ internalStream: function (type) { var result = null, outputType = "string"; try { if (!type) { throw new Error("No output type specified."); } outputType = type.toLowerCase(); var askUnicodeString = outputType === "string" || outputType === "text"; if (outputType === "binarystring" || outputType === "text") { outputType = "string"; } result = this._decompressWorker(); var isUnicodeString = !this._dataBinary; if (isUnicodeString && !askUnicodeString) { result = result.pipe(new utf8.Utf8EncodeWorker()); } if (!isUnicodeString && askUnicodeString) { result = result.pipe(new utf8.Utf8DecodeWorker()); } } catch (e) { result = new GenericWorker("error"); result.error(e); } return new StreamHelper(result, outputType, ""); }, /** * Prepare the content in the asked type. * @param {String} type the type of the result. * @param {Function} onUpdate a function to call on each internal update. * @return Promise the promise of the result. */ async: function (type, onUpdate) { return this.internalStream(type).accumulate(onUpdate); }, /** * Prepare the content as a nodejs stream. * @param {String} type the type of each chunk. * @param {Function} onUpdate a function to call on each internal update. * @return Stream the stream. */ nodeStream: function (type, onUpdate) { return this.internalStream(type || "nodebuffer").toNodejsStream(onUpdate); }, /** * Return a worker for the compressed content. * @private * @param {Object} compression the compression object to use. * @param {Object} compressionOptions the options to use when compressing. * @return Worker the worker. */ _compressWorker: function (compression, compressionOptions) { if ( this._data instanceof CompressedObject && this._data.compression.magic === compression.magic ) { return this._data.getCompressedWorker(); } else { var result = this._decompressWorker(); if(!this._dataBinary) { result = result.pipe(new utf8.Utf8EncodeWorker()); } return CompressedObject.createWorkerFrom(result, compression, compressionOptions); } }, /** * Return a worker for the decompressed content. * @private * @return Worker the worker. */ _decompressWorker : function () { if (this._data instanceof CompressedObject) { return this._data.getContentWorker(); } else if (this._data instanceof GenericWorker) { return this._data; } else { return new DataWorker(this._data); } } }; var removedMethods = ["asText", "asBinary", "asNodeBuffer", "asUint8Array", "asArrayBuffer"]; var removedFn = function () { throw new Error("This method has been removed in JSZip 3.0, please check the upgrade guide."); }; for(var i = 0; i < removedMethods.length; i++) { ZipObject.prototype[removedMethods[i]] = removedFn; } module.exports = ZipObject; },{"./compressedObject":2,"./stream/DataWorker":27,"./stream/GenericWorker":28,"./stream/StreamHelper":29,"./utf8":31}],36:[function(require,module,exports){ (function (global){ 'use strict'; var Mutation = global.MutationObserver || global.WebKitMutationObserver; var scheduleDrain; { if (Mutation) { var called = 0; var observer = new Mutation(nextTick); var element = global.document.createTextNode(''); observer.observe(element, { characterData: true }); scheduleDrain = function () { element.data = (called = ++called % 2); }; } else if (!global.setImmediate && typeof global.MessageChannel !== 'undefined') { var channel = new global.MessageChannel(); channel.port1.onmessage = nextTick; scheduleDrain = function () { channel.port2.postMessage(0); }; } else if ('document' in global && 'onreadystatechange' in global.document.createElement('script')) { scheduleDrain = function () { // Create a ================================================ FILE: documentation/api_jszip/constructor.md ================================================ --- title: "new JSZip() or JSZip()" layout: default section: api --- Create a new JSZip instance. __Returns__ : A new JSZip. __Since__: v1.0.0 ## Example ```js var zip = new JSZip(); // same as var zip = JSZip(); ``` ================================================ FILE: documentation/api_jszip/external.md ================================================ --- title: "JSZip.external" layout: default section: api --- JSZip uses objects that may not exist on every platform, in which case it uses a shim. Accessing or replacing these objects can sometimes be useful. JSZip.external contains the following properties : * `Promise` : the [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) implementation used. The global object is preferred when available. __Example__ ```js // use bluebird instead JSZip.external.Promise = Bluebird; // use the native Promise object: JSZip.external.Promise = Promise; ``` ================================================ FILE: documentation/api_jszip/file_data.md ================================================ --- title: "file(name, data [,options])" layout: default section: api --- Add (or update) a file to the zip file. If something goes wrong (the data is not in a supported format for example), an exception will be propagated when accessing the data. __Returns__ : The current JSZip object, for chaining. __Since__: v1.0.0 ## Arguments name | type | description --------------------|---------|------------ name | string | the name of the file. You can specify folders in the name : the folder separator is a forward slash ("/"). data | String/ArrayBuffer/Uint8Array/Buffer/Blob/Promise/Nodejs stream | the content of the file. options | object | the options. Content of `options` : name | type | default | description ------------|---------|---------|------------ base64 | boolean | `false` | set to `true` if the data is base64 encoded. For example image data from a `` element. Plain text and HTML do not need this option. [More](#base64-option). binary | boolean | `false` | set to `true` if the data should be treated as raw content, `false` if this is a text. If base64 is used, this defaults to `true`, if the data is not a string, this will be set to `true`. [More](#binary-option). date | date | the current date | the last modification date. [More](#date-option). compression | string | null | If set, specifies compression method to use for this specific file. If not, the default file compression will be used, see [generateAsync(options)]({{site.baseurl}}/documentation/api_jszip/generate_async.html). [More](#compression-and-compressionoptions-options). compressionOptions | object | `null` | the options to use when compressing the file, see [generateAsync(options)]({{site.baseurl}}/documentation/api_jszip/generate_async.html). [More](#compression-and-compressionoptions-options). comment | string | null | The comment for this file. [More](#comment-option). optimizedBinaryString | boolean | `false` | Set to true if (and only if) the input is a "binary string" and has already been prepared with a 0xFF mask. createFolders | boolean | `true` | Set to true if folders in the file path should be automatically created, otherwise there will only be virtual folders that represent the path to the file. [More](#createfolders-option). unixPermissions | 16 bits number | null | The UNIX permissions of the file, if any. [More](#unixpermissions-and-dospermissions-options). dosPermissions | 6 bits number | null | The DOS permissions of the file, if any. [More](#unixpermissions-and-dospermissions-options). dir | boolean | false | Set to true if this is a directory and content should be ignored. [More](#dir-option). ### data input You shouldn't update the data given to this method: it is kept as it so any update will impact the stored data. #### About Promise since v3.0.0 You can use a Promise of content directly to simplify async content handling. Let's use HTTP calls as examples: ```js /** with promises **/ // instead of $.get("url/to.file.txt") // jQuery v3 returns promises .then(function (content) { zip.file("file.txt", content); }) // you can do var promise = $.get("url/to.file.txt"); zip.file("file.txt", promise); ``` ```js /** with callbacks **/ // instead of request('url/to.file.txt', function (error, response, body) { zip.file("file.txt", body); }); // you can do var promise = new Promise(function (resolve, reject) { request('url/to.file.txt', function (error, response, body) { if (error) { reject(error); } else { resolve(body); } }); }); zip.file("file.txt", promise); ``` #### About Blob since v3.0.0 You can use directly [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) as input, no need to use a `FileReader`. [File](https://developer.mozilla.org/en-US/docs/Web/API/File) objects are Blobs so you can use them directly: ```js // in a change callback of a var files = evt.target.files; for (var i = 0; i < files.length; i++) { var f = files[i]; zip.file(f.name, f); } ``` #### About nodejs stream since v3.0.0 A stream can't be restarted: if it is used once, it can't be used again ( by [generateAsync()]({{site.baseurl}}/documentation/api_jszip/generate_async.html) or by [ZipObject methods]({{site.baseurl}}/documentation/api_zipobject.html)). In that case, the promise/stream (depending on the method called) will get an error. ### `base64` option ```js var zip = new JSZip(); zip.file("hello.txt", "aGVsbG8gd29ybGQK", {base64: true}); ``` ### `binary` option ```js var zip = new JSZip(); // here, we have a correct (unicode) string zip.file("hello.txt", "unicode ♥", {binary: false}); // here, we have a binary string: it can contain binary content, one byte // per character. zip.file("hello.txt", "unicode \xE2\x99\xA5", {binary: true}); ``` If you use a library that returns a binary string for example, you should use this option. Otherwise, you will get a corrupted result: JSZip will try to encode this string with UTF-8 when the content doesn't need to. ### `date` option ```js zip.file("Xmas.txt", "Ho ho ho !", { date: new Date("December 25, 2007 00:00:01") }); ``` ### `compression` and `compressionOptions` options See also the same options on [`JSZip#generateAsync()`]({{site.baseurl}}/documentation/api_jszip/generate_async.html#compression-and-compressionoptions-options). These options will be used when generating a zip file. They let you override entry per entry the compression / compression options to use. ```js zip.file("a.png", contentOfA, { compression: "STORE" // force a compression for this file }); zip.file("b.txt", contentOfA, { compression: "DEFLATE", compressionOptions: { level: 9 // force a compression and a compression level for this file } }); // don't force anything, use the generateAsync options zip.file("c.txt", contentOfB); // here: // - a.png will not be compressed (STORE) // - b.txt will be compressed at maximum level // - c.txt will be compressed with the default compression level zip.generateAsync({ type: "blob", compression: "DEFLATE" }); ``` ### `comment` option ```js zip.file("a.txt", "content", { comment: "comment of a.txt" }); ``` ### `createFolders` option ```js zip.file("a/b/c/d.txt", "content", { createFolders: true // default value }); console.log(zip.files); // will display: // - a/ // - a/b/ // - a/b/c/ // - a/b/c/d.txt zip.file("a/b/c/d.txt", "content", { createFolders: false }); console.log(zip.files); // will display: // - a/b/c/d.txt ``` ### `unixPermissions` and `dosPermissions` options Each permission will be used for matching [platform option of generateAsync()]({{site.baseurl}}/documentation/api_jszip/generate_async.html): on `DOS`, use `dosPermissions`, on `UNIX`, use `unixPermissions`. On nodejs you can use the `mode` attribute of [nodejs' fs.Stats](http://nodejs.org/api/fs.html#fs_class_fs_stats). When not set, a default value will be generated: - `0100664` or `040775` for `UNIX` - standard file or standard directory for `DOS` The field `unixPermissions` also accepts a **string** representing the octal value: "644", "755", etc. ```js zip.file("script.sh", "#!/bin/bash", { unixPermissions: "755" }); ``` ### `dir` option If `dir` is true or if a permission says it's a folder, this entry be flagged as a folder and the content will be ignored. See also [folder(name)]({{site.baseurl}}/documentation/api_jszip/folder_name.html). ```js zip.file("folder/", null, { dir: true }); ``` ## Other examples ```js zip.file("Hello.txt", "Hello World\n"); // base64 zip.file("smile.gif", "R0lGODdhBQAFAIACAAAAAP/eACwAAAAABQAFAAACCIwPkWerClIBADs=", {base64: true}); // from an ajax call with xhr.responseType = 'arraybuffer' zip.file("smile.gif", arraybufferFromXhr); // or on nodejs zip.file("smile.gif", fs.readFileSync("smile.gif")); zip.file("Xmas.txt", "Ho ho ho !", {date : new Date("December 25, 2007 00:00:01")}); zip.file("folder/file.txt", "file in folder"); zip.file("animals.txt", "dog,platypus\n").file("people.txt", "james,sebastian\n"); // result: // - Hello.txt // - smile.gif // - Xmas.txt // - animals.txt // - people.txt // - folder/ // - folder/file.txt ``` ================================================ FILE: documentation/api_jszip/file_name.md ================================================ --- title: "file(name)" layout: default section: api --- Get a file with the specified name. You can specify folders in the name : the folder separator is a forward slash ("/"). __Returns__ : An instance of [ZipObject]({{site.baseurl}}/documentation/api_zipobject.html) representing the file if any, `null` otherwise. __Since__: v1.0.0 ## Arguments name | type | description -----|--------|------------- name | string | the name of the file. __Throws__ : Nothing. ## Example ```js var zip = new JSZip(); zip.file("file.txt", "content"); zip.file("file.txt").name // "file.txt" zip.file("file.txt").async("string") // a promise of "content" zip.file("file.txt").dir // false // utf8 example var zip = new JSZip(); zip.file("amount.txt", "€15"); zip.file("amount.txt").async("string") // a promise of "€15" zip.file("amount.txt").async("arraybuffer") // a promise of an ArrayBuffer containing €15 encoded as utf8 zip.file("amount.txt").async("uint8array") // a promise of an Uint8Array containing €15 encoded as utf8 // with folders zip.folder("sub").file("file.txt", "content"); zip.file("sub/file.txt"); // the file // or zip.folder("sub").file("file.txt") // the file ``` ================================================ FILE: documentation/api_jszip/file_regex.md ================================================ --- title: "file(regex)" layout: default section: api --- Search a file in the current folder and subfolders with a [regular expression](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions). The regex is tested against the relative filename. __Returns__ : An array of matching files (an empty array if none matched). Each matching file is an instance of [ZipObject]({{site.baseurl}}/documentation/api_zipobject.html). __Since__: v1.0.0 ## Arguments name | type | description ------|--------|------------ regex | RegExp | the regex to use. ## Example ```js var zip = new JSZip(); zip.file("file1.txt", "content"); zip.file("file2.txt", "content"); zip.file(/file/); // array of size 2 // example with a relative path : var folder = zip.folder("sub"); folder .file("file3.txt", "content") // relative path from folder : file3.txt .file("file4.txt", "content"); // relative path from folder : file4.txt folder.file(/file/); // array of size 2 folder.file(/^file/); // array of size 2, the relative paths start with file // arrays contain objects in the form: // {name: "file2.txt", dir: false, async : function () {...}, ...} ``` ================================================ FILE: documentation/api_jszip/filter.md ================================================ --- title: "filter(predicate)" layout: default section: api --- Filter nested files/folders with the specified function. __Returns__ : An array of matching ZipObject. __Since__: v1.0.0 ## Arguments name | type | description ----------|----------|------------ predicate | function | the predicate to use. The predicate has the following signature : `function (relativePath, file) {...}` : name | type | description -------------|-----------|------------ relativePath | string | the filename and its path, relative to the current folder. file | ZipObject | the file being tested. See [ZipObject]({{site.baseurl}}/documentation/api_zipobject.html). The predicate must return true if the file should be included, false otherwise. ## Examples ```js var zip = new JSZip().folder("dir"); zip.file("readme.txt", "content"); zip.filter(function (relativePath, file){ // relativePath == "readme.txt" // file = {name:"dir/readme.txt",options:{...},async:function} return true/false; }); ``` ================================================ FILE: documentation/api_jszip/folder_name.md ================================================ --- title: "folder(name)" layout: default section: api --- Create a directory if it doesn't exist, return a new JSZip object with the new folder as root. See also [the `dir` option of file()]({{site.baseurl}}/documentation/api_jszip/file_data.html). __Returns__ : A new JSZip (for chaining), with the new folder as root. __Since__: v1.0.0 ## Arguments name | type | description -----|--------|------------ name | string | the name of the directory. ## Examples ```js zip.folder("images"); zip.folder("css").file("style.css", "body {background: #FF0000}"); // or specify an absolute path (using forward slashes) zip.file("css/font.css", "body {font-family: sans-serif}") // result : images/, css/, css/style.css, css/font.css ``` ================================================ FILE: documentation/api_jszip/folder_regex.md ================================================ --- title: "folder(regex)" layout: default section: api --- Search a subdirectory in the current directory with a [regular expression](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions). The regex is tested against the relative path. __Returns__ : An array of matching folders (an empty array if none matched). Each matching folder is an instance of [ZipObject]({{site.baseurl}}/documentation/api_zipobject.html). __Since__: v1.0.0 ## Arguments name | type | description ------|--------|------------ regex | RegExp | the regex to use. ## Examples ```js var zip = new JSZip(); zip.folder("home/Pierre/videos"); zip.folder("home/Pierre/photos"); zip.folder("home/Jean/videos"); zip.folder("home/Jean/photos"); zip.folder(/videos/); // array of size 2 zip.folder("home/Jean").folder(/^vid/); // array of 1 ``` ================================================ FILE: documentation/api_jszip/for_each.md ================================================ --- title: "forEach(callback)" layout: default section: api --- Call a callback function for each entry at this folder level. __Returns__ : Nothing. __Since__: v3.0.0 ## Arguments name | type | description ----------|----------|------------ callback | function | the callback to use. The callback has the following signature : `function (relativePath, file) {...}` : name | type | description -------------|-----------|------------ relativePath | string | the filename and its path, relative to the current folder. file | ZipObject | the current file. See [ZipObject]({{site.baseurl}}/documentation/api_zipobject.html). ## Examples ```js var zip = new JSZip(); zip.file("package.json", "..."); zip.file("lib/index.js", "..."); zip.file("test/index.html", "..."); zip.file("test/asserts/file.js", "..."); zip.file("test/asserts/generate.js", "..."); zip.folder("test").forEach(function (relativePath, file){ console.log("iterating over", relativePath); }); // will display: // iterating over index.html // iterating over asserts/ // iterating over asserts/file.js // iterating over asserts/generate.js ``` ================================================ FILE: documentation/api_jszip/generate_async.md ================================================ --- title: "generateAsync(options[, onUpdate])" layout: default section: api --- Generates the complete zip file at the current folder level. __Returns__ : A [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) of the generated zip file. An error will be propagated if the asked `type` is not available in the browser, see [JSZip.support]({{site.baseurl}}/documentation/api_jszip/support.html). __Since__: v3.0.0 ## Arguments name | type | default | description --------------------|----------|----------|------------ options | object | | the options to generate the zip file : options.type | string | | The type of zip to return, see below for the other types. **Required**. [More](#type-option). options.compression | string | `STORE` (no compression) | the default file compression method to use. [More](#compression-and-compressionoptions-options). options.compressionOptions | object | `null` | the options to use when compressing the file. [More](#compression-and-compressionoptions-options). options.comment | string | | The comment to use for the zip file. [More](#comment-option). options.mimeType | string | `application/zip` | mime-type for the generated file. [More](#mimetype-option). options.platform | string | `DOS` | The platform to use when generating the zip file. [More](#platform-option). options.encodeFileName | function | encode with UTF-8 | the function to encode the file name / comment. [More](#encodefilename-option). options.streamFiles | boolean | false | Stream the files and create file descriptors, see below. [More](#streamfiles-option). onUpdate | function | | The optional function called on each internal update with the metadata. [More](#onupdate-callback). ### `type` option Possible values for `type` : * `base64`: the result will be a string, the binary in a base64 form. * `binarystring` (or `string`, deprecated): the result will be a string in "binary" form, using 1 byte per char (2 bytes). * `array`: the result will be an Array of bytes (numbers between 0 and 255) containing the zip. * `uint8array`: the result will be a Uint8Array containing the zip. This requires a compatible browser. * `arraybuffer`: the result will be a ArrayBuffer containing the zip. This requires a compatible browser. * `blob`: the result will be a Blob containing the zip. This requires a compatible browser. * `nodebuffer`: the result will be a nodejs Buffer containing the zip. This requires nodejs. Note : when using type = "uint8array", "arraybuffer" or "blob", be sure to check if the browser supports it (you can use [`JSZip.support`]({{site.baseurl}}/documentation/api_jszip/support.html)). ```js zip.generateAsync({type: "uint8array"}).then(function (u8) { // ... }); ``` ### `compression` and `compressionOptions` options Available `compression` methods are `STORE` (no compression) and `DEFLATE`. The `compressionOptions` parameter depends on the compression type. With `STORE` (no compression), this parameter is ignored. With `DEFLATE`, you can give the compression level with `compressionOptions : {level:6}` (or any level between 1 (best speed) and 9 (best compression)). Note : if the entry is *already* compressed (coming from a compressed zip file), calling `generateAsync()` with a different compression level won't update the entry. The reason is simple : JSZip doesn't know how compressed the content was and how to match the compression level with the implementation we use. ```js zip.generateAsync({ type: "blob", compression: "DEFLATE", compressionOptions: { level: 9 } }); ``` ### `comment` option The zip format has no flag or field to give the encoding of this field and JSZip will use UTF-8. With non ASCII characters you might get encoding issues if the file archiver doesn't use UTF-8 (or the given encoding) to decode the comment. ```js zip.generateAsync({ type: "blob", comment: "The comment text for this zip file" }) ``` ### `mimeType` option This field is used when you generate a Blob and need to change [the mime type](https://developer.mozilla.org/en-US/docs/Web/API/Blob/type). Useful when you need to generate a file with a different extension, ie: ".ods". Note, this won't change the content of the file, only the other programs *may* see it. ```js //This example will Generate a Open Document Spreadsheet, with the correct mime type var zip = new JSZip(); zip.file("mimetype", "application/vnd.oasis.opendocument.spreadsheet"); var metaInf = zip.folder("META-INF"); metaInf.file("manifest.xml", "<..."); // ... //Generate the file zip.generateAsync({ type: "blob", mimeType: "application/ods", compression: "DEFLATE" }).then(function (odsFile) { // odsFile.type == "application/ods" }); ``` ### `platform` option Possible values for `platform` : `DOS` and `UNIX`. It also accepts nodejs `process.platform` values. When using `DOS`, the attribute `dosPermissions` of each file is used. When using `UNIX`, the attribute `unixPermissions` of each file is used. If you set the platform value on nodejs, be sure to use `process.platform`. `fs.stats` returns a non executable mode for folders on windows, if you force the platform to `UNIX` the generated zip file will have a strange behavior on UNIX platforms. ```js // on nodejs zip.file(pathname, content, { date: stat.mtime, unixPermissions: stat.mode }); // ... zip.generateAsync({ type: 'nodebuffer', platform: process.platform }); ``` ### `encodeFileName` option By default, JSZip uses UTF-8 to encode the file names / comments. You can use this method to force an other encoding. Note: the encoding used is not stored in a zip file, not using UTF-8 may lead to encoding issues. The function takes a string and returns a bytes array (Uint8Array or Array). See also [`decodeFileName` on `JSZip#loadAsync()`]({{site.baseurl}}/documentation/api_jszip/load_async.html#decodefilename-option). ```js // using iconv-lite for example var iconv = require('iconv-lite'); zip.generateAsync({ type: 'uint8array', encodeFileName: function (string) { return iconv.encode(string, 'your-encoding'); } }); ``` ### `streamFiles` option In a zip file, the size and the crc32 of the content are placed before the actual content: to write it we must process the whole file. When this option is `false` (the default) the processed file is held in memory. It takes more memory but generates a zip file which should be read by every program. When this options is `true`, we stream the file and use data descriptors at the end of the entry. This option uses less memory but some program might not support data descriptors (and won't accept the generated zip file). ```js zip.generateAsync({ type: 'uint8array', streamFiles: true }); ``` ### `onUpdate` callback If specified, this function will be called each time a chunk is pushed to the output stream (or internally accumulated). The function takes a `metadata` object which contains information about the ongoing process. __Metadata__ : the metadata are: name | type | description ------------|--------|------------ percent | number | the percent of completion (a double between 0 and 100) currentFile | string | the name of the current file being processed, if any. ```js zip.generateAsync({type:"blob"}, function updateCallback(metadata) { console.log("progression: " + metadata.percent.toFixed(2) + " %"); if(metadata.currentFile) { console.log("current file = " + metadata.currentFile); } }) ``` ## Other examples ```js zip.generateAsync({type:"blob"}) .then(function (content) { // see FileSaver.js saveAs(content, "hello.zip"); }); ``` ```js zip.generateAsync({type:"base64"}) .then(function (content) { location.href="data:application/zip;base64,"+content; }); ``` ```js zip.folder("folder_1").folder("folder_2").file("hello.txt", "hello"); // zip now contains: // folder_1/ // folder_1/folder_2/ // folder_1/folder_2/hello.txt zip.folder("folder_1").generateAsync({type:"nodebuffer"}) .then(function (content) { // relative to folder_1/, this file only contains: // folder_2/ // folder_2/hello.txt require("fs").writeFile("hello.zip", content, function(err){/*...*/}); }); ``` ================================================ FILE: documentation/api_jszip/generate_internal_stream.md ================================================ --- title: "generateInternalStream(options)" layout: default section: api --- Generates the complete zip file with the internal stream implementation. __Returns__ : a [StreamHelper]({{site.baseurl}}/documentation/api_streamhelper.html). __Since__: v3.0.0 ## Arguments name | type | default | description --------------------|----------|---------|------------ options | object | | the options to generate the zip file, see [the options of `generateAsync()`]({{site.baseurl}}/documentation/api_jszip/generate_async.html) __Metadata__ : see [the metadata of `generateAsync()`]({{site.baseurl}}/documentation/api_jszip/generate_async.html#onupdate-callback). ## Examples ```js zip .generateInternalStream({type:"uint8array"}) .accumulate() .then(function (data) { // data contains here the complete zip file as a uint8array (the type asked in generateInternalStream) }); ``` ================================================ FILE: documentation/api_jszip/generate_node_stream.md ================================================ --- title: "generateNodeStream(options[, onUpdate])" layout: default section: api --- Generates the complete zip file as a nodejs stream. __Returns__ : a [nodejs Streams3](https://github.com/nodejs/readable-stream). __Since__: v3.0.0 ## Arguments name | type | default | description --------------------|----------|---------|------------ options | object | | the options to generate the zip file, see [the options of `generateAsync()`]({{site.baseurl}}/documentation/api_jszip/generate_async.html) onUpdate | function | | The optional function called on each internal update with the metadata. The `type` parameter has here the default value of `nodebuffer`. Only `nodebuffer` is currently supported. __Metadata__ : see [the metadata of `generateAsync()`]({{site.baseurl}}/documentation/api_jszip/generate_async.html#onupdate-callback). ## Examples ```js zip .generateNodeStream({streamFiles:true}) .pipe(fs.createWriteStream('out.zip')) .on('finish', function () { // JSZip generates a readable stream with a "end" event, // but is piped here in a writable stream which emits a "finish" event. console.log("out.zip written."); }); ``` ================================================ FILE: documentation/api_jszip/load_async.md ================================================ --- title: "loadAsync(data [, options])" layout: default section: api --- Read an existing zip and merge the data in the current JSZip object at the current folder level. This technique has some limitations, see [here]({{site.baseurl}}/documentation/limitations.html). If the JSZip object already contains entries, new entries will be merged. If two have the same name, the loaded one will replace the other. Since v3.8.0 this method will santize relative path components (i.e. `..`) in loaded filenames to avoid ["zip slip" attacks](https://snyk.io/research/zip-slip-vulnerability). For example: `../../../example.txt` → `example.txt`, `src/images/../example.txt` → `src/example.txt`. The original filename is available on each zip entry as `unsafeOriginalName`. __Returns__ : A [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) with the updated zip object. The promise can fail if the loaded data is not valid zip data or if it uses unsupported features (multi volume, password protected, etc). __Since__: v3.0.0 ## Arguments name | type | description -------------------|--------|------------ data | String/Array of bytes/ArrayBuffer/Uint8Array/Buffer/Blob/Promise | the zip file options | object | the options to load the zip file Content of `options` : name | type | default | description ------------------------------|---------|---------|------------ options.base64 | boolean | false | set to `true` if the data is base64 encoded, `false` for binary. [More](#base64-option). options.checkCRC32 | boolean | false | set to `true` if the read data should be checked against its CRC32. [More](#checkcrc32-option). options.optimizedBinaryString | boolean | false | set to `true` if (and only if) the input is a string and has already been prepared with a 0xFF mask. options.createFolders | boolean | false | set to `true` to create folders in the file path automatically. Leaving it false will result in only virtual folders (i.e. folders that merely represent part of the file path) being created. [More](#createfolders-option). options.decodeFileName | function | decode from UTF-8 | the function to decode the file name / comment. [More](#decodefilename-option). You shouldn't update the data given to this method : it is kept as it so any update will impact the stored data. Zip features supported by this method : * Compression (DEFLATE supported) * zip with data descriptor * ZIP64 * UTF8 in file name, UTF8 in file content Zip features not (yet) supported : * password protected zip * multi-volume zip ### `base64` option ```js var zip = new JSZip(); zip.loadAsync("UEsDBAoDAAAAAJxs8T...AAAAAA==", {base64: true}); ``` ### `checkCRC32` option The `checkCRC32` option will load every files, compute the CRC32 value and compare it against the saved value. With larger zip files, this option can have a significant performance cost. ```js // here, "bin" is a corrupted zip file zip.loadAsync(bin) .then(function (zip) { // will be called, even if content is corrupted }, function (e) { // won't be called }); zip.loadAsync(bin, { checkCRC32: true }) .then(function (zip) { // won't be called }, function (e) { // Error: Corrupted zip : CRC32 mismatch }); ``` ### `createFolders` option ```js // here, "bin" is zip file containing: // folder1/folder2/folder3/file1.txt zip.loadAsync(bin) .then(function (zip) { console.log(zip.files); // folder1/folder2/folder3/file1.txt }); // with createFolders: true, all folders will be created zip.loadAsync(bin, {createFolders: true}) .then(function (zip) { console.log(zip.files); // folder1/ // folder1/folder2/ // folder1/folder2/folder3/ // folder1/folder2/folder3/file1.txt }); ``` ### `decodeFileName` option A zip file has a flag to say if the filename and comment are encoded with UTF-8. If it's not set, JSZip has **no way** to know the encoding used. It usually is the default encoding of the operating system. Some extra fields can give the unicode version of the filename/comment too (in that case, we use it). If we can't find an UTF-8 encoded filename/comment, we use the `decodeFileName` function (which is by default an UTF-8 decode). The function takes the bytes array (Uint8Array or Array) and returns the decoded string. ```js // here, "bin" is a russian zip file, using the cp866 encoding for file names // by default, using UTF-8 leads to wrong file names: zip.loadAsync(bin) .then(function (zip) { console.log(zip.files); // '����� �����/': ... // '����� �����/����� ⥪�⮢�� ���㬥��.txt': ... }); // using the correct encoding solve the issue: var iconv = require('iconv-lite'); zip.loadAsync(bin, { decodeFileName: function (bytes) { return iconv.decode(bytes, 'cp866'); } }) .then(function (zip) { console.log(zip.files); // 'Новая папка/': ... // 'Новая папка/Новый текстовый документ.txt': ... }); ``` ## Other examples ```js var zip = new JSZip(); zip.loadAsync(zipDataFromXHR); ``` ```js require("fs").readFile("hello.zip", function (err, data) { if (err) throw err; var zip = new JSZip(); zip.loadAsync(data); } ``` Using sub folders : ```js // here, "bin" is zip file containing: // file1.txt // folder1/file2.txt var zip = new JSZip(); zip.folder("subfolder").loadAsync(bin) .then(function (zip) { // "zip" is still in the "subfolder" folder console.log(zip.files); // subfolder/file1.txt // subfolder/folder1/file2.txt }); ``` Using `loadAsync` multiple times: ```js // here, "bin1" is zip file containing: // file1.txt // file2.txt // and "bin2" is zip file containing: // file2.txt // file3.txt var zip = new JSZip(); zip.loadAsync(bin1) .then(function (zip) { return zip.loadAsync(bin2); }).then(function (zip) { console.log(zip.files); // file1.txt, from bin1 // file2.txt, from bin2 // file3.txt, from bin2 }); ``` Reading a zip file with relative filenames: ```js // here, "unsafe.zip" is zip file containing: // src/images/../file.txt // ../../example.txt require("fs").readFile("unsafe.zip", function (err, data) { if (err) throw err; var zip = new JSZip(); zip.loadAsync(data) .then(function (zip) { console.log(zip.files); // src/file.txt // example.txt console.log(zip.files["example.txt"].unsafeOriginalName); // "../../example.txt" }); } ``` ================================================ FILE: documentation/api_jszip/load_async_object.md ================================================ --- title: "JSZip.loadAsync(data [, options])" layout: default section: api --- This is a shortcut for ```js var zip = new JSZip(); zip.loadAsync(data, options); ``` Please see the documentation of [loadAsync]({{site.baseurl}}/documentation/api_jszip/load_async.html). __Examples__ ```js dataAsPromise .then(JSZip.loadAsync) .then(function(zip) {...}) ``` same as: ```js JSZip.loadAsync(dataAsPromise) .then(function(zip) {...}) ``` ================================================ FILE: documentation/api_jszip/remove.md ================================================ --- title: "remove(name)" layout: default section: api --- Delete a file or folder (recursively). __Returns__ : The current JSZip object. __Since__: v1.0.0 ## Arguments name | type | description -----|--------|------------ name | string | the name of the file/folder to delete. ## Examples ```js var zip = new JSZip(); zip.file("Hello.txt", "Hello World\n"); zip.file("temp.txt", "nothing").remove("temp.txt"); // result : Hello.txt zip.folder("css").file("style.css", "body {background: #FF0000}"); zip.remove("css"); //result : empty zip ``` ================================================ FILE: documentation/api_jszip/support.md ================================================ --- title: "JSZip.support" layout: default section: api --- If the browser supports them, JSZip can take advantage of some "new" features : ArrayBuffer, Blob, Uint8Array. To know if JSZip can use them, you can check the JSZip.support object. It contains the following boolean properties : * `arraybuffer` : true if JSZip can read and generate ArrayBuffer, false otherwise. * `uint8array` : true if JSZip can read and generate Uint8Array, false otherwise. * `blob` : true if JSZip can generate Blob, false otherwise. * `nodebuffer` : true if JSZip can read and generate nodejs Buffer, false otherwise. * `nodestream` : true if JSZip can read and generate nodejs stream, false otherwise. ================================================ FILE: documentation/api_jszip/version.md ================================================ --- title: "JSZip.version" layout: default section: api --- The version of JSZip as a string. __Since__: v3.1.0 ## Example ```js JSZip.version == "3.1.0"; ``` ================================================ FILE: documentation/api_jszip.md ================================================ --- title: "JSZip API" layout: default section: api --- An instance of JSZip represents a set of files. You can add them, remove them, modify them. You can also import an existing zip file or generate one. ### Attributes attribute name | type | description ---------------------|-------------|------------- `files` | object | the [ZipObject]({{site.baseurl}}/documentation/api_zipobject.html)s inside the zip with the name as key. See [file(name)]({{site.baseurl}}/documentation/api_jszip/file_name.html). `comment` | string | the comment of the zip file. ================================================ FILE: documentation/api_streamhelper/accumulate.md ================================================ --- title: "accumulate([updateCallback])" layout: default section: api --- Read the whole stream and call a callback with the complete content. __Returns__ : A [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) of the full content. __Since__: v3.0.0 ## Arguments name | type | description ----------------|----------|------------ updateCallback | function | the function called every time the stream updates. This function is optional. The update callback function takes 1 parameter: the metadata (see the [`on` method]({{site.baseurl}}/documentation/api_streamhelper/on.html)). ## Example ```js zip .generateInternalStream({type:"uint8array"}) .accumulate(function updateCallback(metadata) { // metadata contains for example currentFile and percent, see the generateInternalStream doc. }).then(function (data) { // data contains here the complete zip file as a uint8array (the type asked in generateInternalStream) }); ``` ================================================ FILE: documentation/api_streamhelper/on.md ================================================ --- title: "on(event, callback)" layout: default section: api --- Register a listener on an event. __Returns__ : The current StreamHelper object, for chaining. __Throws__ : An exception if the event is unknown. ## Arguments name | type | description ----------|----------|------------ event | string | the name of the event. Only 3 events are supported : `data`, `end` and `error`. callback | function | the function called when the event occurs. See below for the arguments. The callbacks are executed in with the current `StreamHelper` as `this`. ### `data` callback It takes 2 parameters: - the current chunk of data (in a format specified by the method which generated this StreamHelper) - the metadata (see each method to know what's inside) ### `end` callback It does not take any parameter. ### `error` callback It takes an `Error` as parameter. ## Example ```js zip .generateInternalStream({type:"uint8array"}) .on('data', function (data, metadata) { // data is a Uint8Array because that's the type asked in generateInternalStream // metadata contains for example currentFile and percent, see the generateInternalStream doc. }) .on('error', function (e) { // e is the error }) .on('end', function () { // no parameter }) .resume(); ``` ================================================ FILE: documentation/api_streamhelper/pause.md ================================================ --- title: "pause()" layout: default section: api --- Pause the stream if the stream is running. Once paused, the stream stops sending `data` events. __Returns__ : The current StreamHelper object, for chaining. ## Example ```js zip .generateInternalStream({type:"uint8array"}) .on('data', function(chunk) { // if we push the chunk to an other service which is overloaded, we can // pause the stream as backpressure. this.pause(); }).resume(); // start the stream the first time ``` ================================================ FILE: documentation/api_streamhelper/resume.md ================================================ --- title: "resume()" layout: default section: api --- Resume the stream if the stream is paused. Once resumed, the stream starts sending `data` events again. __Returns__ : The current StreamHelper object, for chaining. __Since__: v3.0.0 ## Example ```js zip .generateInternalStream({type:"uint8array"}) .on('data', function() {...}) .resume(); ``` ================================================ FILE: documentation/api_streamhelper.md ================================================ --- title: "StreamHelper API" layout: default section: api --- A `StreamHelper` can be viewed as a pausable stream with some helper methods. It is not a full featured stream like in nodejs (and can't directly used as one) but the exposed methods should be enough to write the glue code with other async libraries : `on('data', function)`, `on('end', function)` and `on('error', function)`. It starts paused, be sure to `resume()` it when ready. If you are looking for an asynchronous helper without writing glue code, take a look at `accumulate(function)`. ================================================ FILE: documentation/api_zipobject/async.md ================================================ --- title: "async(type[, onUpdate])" layout: default section: api --- Return a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) of the content in the asked type. __Returns__ : A [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) of the content. __Since__: v3.0.0 ## Arguments name | type | description ---------|----------|------------ type | String | the type of the result. [More](#type-option). onUpdate | Function | an optional function called on each internal update with the metadata. [More](#onupdate-callback). ### `type` option Possible values for `type` : * `base64` : the result will be a string, the binary in a base64 form. * `text` (or `string`): the result will be an unicode string. * `binarystring`: the result will be a string in "binary" form, using 1 byte per char (2 bytes). * `array`: the result will be an Array of bytes (numbers between 0 and 255). * `uint8array` : the result will be a Uint8Array. This requires a compatible browser. * `arraybuffer` : the result will be a ArrayBuffer. This requires a compatible browser. * `blob` : the result will be a Blob. This requires a compatible browser. * `nodebuffer` : the result will be a nodejs Buffer. This requires nodejs. Note : when using type = "uint8array", "arraybuffer" or "blob", be sure to check if the browser supports it (you can use [`JSZip.support`]({{site.baseurl}}/documentation/api_jszip/support.html)). ```js zip.file("image.png").async("uint8array").then(function (u8) { // ... }); ``` ### `onUpdate` callback If specified, this function will be called each time a chunk is pushed to the output stream (or internally accumulated). The function takes a `metadata` object which contains information about the ongoing process. __Metadata__ : the metadata are : name | type | description ------------|--------|------------ percent | number | the percent of completion (a double between 0 and 100) ```js zip.file("image.png").async("uint8array", function updateCallback(metadata) { console.log("progression: " + metadata.percent.toFixed(2) + " %"); }).then(function (u8) { // ... }) ``` ## Other examples ```js zip .file("my_text.txt") .async("string") .then(function success(content) { // use the content }, function error(e) { // handle the error }); ``` ================================================ FILE: documentation/api_zipobject/internal_stream.md ================================================ --- title: "internalStream(type)" layout: default section: api --- Return a [StreamHelper]({{site.baseurl}}/documentation/api_streamhelper.html) of the content in the asked type. __Returns__ : a [StreamHelper]({{site.baseurl}}/documentation/api_streamhelper.html) of the content in the asked type. ## Arguments name | type | description ---------|----------|------------ type | String | the type of the result: `string`, `binarystring`, `uint8array`, `arraybuffer`, `nodebuffer`. ## Example ```js zip .file("my_text.txt") .internalStream("string") .on("data", function (data) {...}) .on("error", function (e) {...}) .on("end", function () {...}); ``` ================================================ FILE: documentation/api_zipobject/node_stream.md ================================================ --- title: "nodeStream(type[, onUpdate])" layout: default section: api --- Return a [nodejs Streams3](https://github.com/nodejs/readable-stream) of the content in the asked type. __Returns__ : a [nodejs Streams3](https://github.com/nodejs/readable-stream). ## Arguments name | type | default | description ---------|----------|--------------|------------ type | String | `nodebuffer` | only `nodebuffer` is currently supported. onUpdate | Function | | an optional function called on each internal update with the metadata. __Metadata__ : see [the metadata of `async()`]({{site.baseurl}}/documentation/api_zipobject/async.html#onupdate-callback). ## Example ```js zip .file("my_text.txt") .nodeStream() .pipe(fs.createWriteStream('/tmp/my_text.txt')) .on('finish', function () { // JSZip generates a readable stream with a "end" event, // but is piped here in a writable stream which emits a "finish" event. console.log("text file written."); }); ``` ================================================ FILE: documentation/api_zipobject.md ================================================ --- title: "ZipObject API" layout: default section: api --- This represents an entry in the zip file. If the entry comes from an existing archive previously [loaded]({{site.baseurl}}/documentation/api_jszip/load_async.html), the content will be automatically decompressed/converted first. ### Attributes attribute name | type | description ----------------------------|-------------|------------- `name` | string | the absolute path of the file `dir` | boolean | true if this is a directory `date` | date | the last modification date `comment` | string | the comment for this file `unixPermissions` | 16 bits number | The UNIX permissions of the file, if any. `dosPermissions` | 6 bits number | The DOS permissions of the file, if any. `options` | object | the options of the file. The available options are : `options.compression` | compression | see [file(name, data [,options])]({{site.baseurl}}/documentation/api_jszip/file_data.html) `options.compressionOptions`| object | see [file(name, data [,options])]({{site.baseurl}}/documentation/api_jszip/file_data.html) Example: ```js { name: 'docs/', dir: true, date: 2016-12-25T08:09:27.153Z, comment: null, unixPermissions: 16877, dosPermissions: null, options: { compression: 'STORE', compressionOptions: null } } ``` ```js { name: 'docs/list.txt', dir: false, date: 2016-12-25T08:09:27.152Z, comment: null, unixPermissions: 33206, dosPermissions: null, options: { compression: 'DEFLATE', compressionOptions: null } } ``` ================================================ FILE: documentation/contributing.md ================================================ --- title: Contributing layout: default section: main --- ### Download the sources You should create a [Github](https://github.com/) account and [fork the repository](https://help.github.com/articles/fork-a-repo) (you will need one to create the pull request). If you just want the get the source code, you can use git and do `git clone https://github.com/Stuk/jszip.git` to get the sources. You can also download the latest sources [here](https://github.com/Stuk/jszip/archive/main.zip). ### Building the project #### Code The dependencies are handled by npm, the first step is to run `npm install` to get the dependencies. JSZip uses Grunt to handle the build, [see here to install its CLI](http://gruntjs.com/getting-started). Here are the interesting build commands : * `grunt` will generate the final js file in dist/ and the minified version. * `npm run test-node` will run the tests in nodejs. * `npm run test-browser` will the tests in some browsers using SauceLabs, see below. * `npm run test` will run the tests in nodejs and in the browser. * `npm run lint` will use eslint the check the source code. #### Documentation The documentation uses jekyll on gh-pages. To render the documentation, you need to [install jekyll](http://jekyllrb.com/docs/installation/) and then run `jekyll serve --baseurl ''`. ### Testing the project To test JSZip in nodejs, use `npm run test-node`. To test JSZip in a browser, you can open the file `test/index.html` in the browser you want to test. Don't forget to update the dist/ files with `grunt`. You can also test JSZip in a lot of browsers at once with [SauceLabs](https://saucelabs.com/). You will need a SauceLabs account and two variables into your environment. On linux, just use ```bash export SAUCE_USERNAME=your-saucelabs-username export SAUCE_ACCESS_KEY=your-saucelabs-access-key ``` before running the `npm run test-browser` command. ### Merging the changes If you have tested bug fixes or new features, you can open a [pull request](https://help.github.com/articles/using-pull-requests) on Github. ## Releasing a new version 1. In `package.json` temporarily remove `browser["./lib/index"]`. 2. Run `npm test` * Locally open http://localhost:8080/test/ * Or use the SauceLabs configuration above 3. Update `JSZip.version` in `index.js`, `index.html` and in `package.json` 4. Run `grunt` to generate the new dist files * undo the package.json change, it was just needed to replace the `__VERSION__` in the header 5. Undo step 1. 6. Change version back in `package.json` (it will get update by the npm command below) 7. Update CHANGES.md 8. Commit the appropriate changes 9. Run `npm version ...` where `...` is `major`, `minor`, or `patch` 10. Run npm publish ================================================ FILE: documentation/css/main.css ================================================ ul.nav ul { list-style:none; margin: 0; padding: 0 0 0 25px; } #downloader_application form { margin-bottom: 10px; } #downloader_application ul { list-style-type: none; } .browser_support th { border-bottom-width: 3px !important; } .support_ie {border-bottom-color: #0275BA !important;} .support_ff {border-bottom-color: #DF7215 !important;} .support_sf {border-bottom-color: #43B3E9 !important;} .support_cr {border-bottom-color: #39B642 !important;} .support_op {border-bottom-color: #C42122 !important;} .support_nd {border-bottom-color: #8CC84B !important;} .show-example { padding: 10px; border: 1px solid #ccc; border-radius: 4px; margin: 0 0 10px; } .tab-pane > figure.highlight > pre, .tab-pane > .show-example { border-radius: 0 0 4px 4px; border-top: 0px; } ================================================ FILE: documentation/css/pygments.css ================================================ /* Generated with : * pygmentize -S default -f html > pygments.css */ .hll { background-color: #ffffcc } .c { color: #408080; font-style: italic } /* Comment */ .err { border: 1px solid #FF0000 } /* Error */ .k { color: #008000; font-weight: bold } /* Keyword */ .o { color: #666666 } /* Operator */ .cm { color: #408080; font-style: italic } /* Comment.Multiline */ .cp { color: #BC7A00 } /* Comment.Preproc */ .c1 { color: #408080; font-style: italic } /* Comment.Single */ .cs { color: #408080; font-style: italic } /* Comment.Special */ .gd { color: #A00000 } /* Generic.Deleted */ .ge { font-style: italic } /* Generic.Emph */ .gr { color: #FF0000 } /* Generic.Error */ .gh { color: #000080; font-weight: bold } /* Generic.Heading */ .gi { color: #00A000 } /* Generic.Inserted */ .go { color: #888888 } /* Generic.Output */ .gp { color: #000080; font-weight: bold } /* Generic.Prompt */ .gs { font-weight: bold } /* Generic.Strong */ .gu { color: #800080; font-weight: bold } /* Generic.Subheading */ .gt { color: #0044DD } /* Generic.Traceback */ .kc { color: #008000; font-weight: bold } /* Keyword.Constant */ .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */ .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */ .kp { color: #008000 } /* Keyword.Pseudo */ .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */ .kt { color: #B00040 } /* Keyword.Type */ .m { color: #666666 } /* Literal.Number */ .s { color: #BA2121 } /* Literal.String */ .na { color: #7D9029 } /* Name.Attribute */ .nb { color: #008000 } /* Name.Builtin */ .nc { color: #0000FF; font-weight: bold } /* Name.Class */ .no { color: #880000 } /* Name.Constant */ .nd { color: #AA22FF } /* Name.Decorator */ .ni { color: #999999; font-weight: bold } /* Name.Entity */ .ne { color: #D2413A; font-weight: bold } /* Name.Exception */ .nf { color: #0000FF } /* Name.Function */ .nl { color: #A0A000 } /* Name.Label */ .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */ .nt { color: #008000; font-weight: bold } /* Name.Tag */ .nv { color: #19177C } /* Name.Variable */ .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */ .w { color: #bbbbbb } /* Text.Whitespace */ .mf { color: #666666 } /* Literal.Number.Float */ .mh { color: #666666 } /* Literal.Number.Hex */ .mi { color: #666666 } /* Literal.Number.Integer */ .mo { color: #666666 } /* Literal.Number.Oct */ .sb { color: #BA2121 } /* Literal.String.Backtick */ .sc { color: #BA2121 } /* Literal.String.Char */ .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */ .s2 { color: #BA2121 } /* Literal.String.Double */ .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */ .sh { color: #BA2121 } /* Literal.String.Heredoc */ .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */ .sx { color: #008000 } /* Literal.String.Other */ .sr { color: #BB6688 } /* Literal.String.Regex */ .s1 { color: #BA2121 } /* Literal.String.Single */ .ss { color: #19177C } /* Literal.String.Symbol */ .bp { color: #008000 } /* Name.Builtin.Pseudo */ .vc { color: #19177C } /* Name.Variable.Class */ .vg { color: #19177C } /* Name.Variable.Global */ .vi { color: #19177C } /* Name.Variable.Instance */ .il { color: #666666 } /* Literal.Number.Integer.Long */ ================================================ FILE: documentation/examples/download-zip-file.html ================================================ --- title: "Download the generated zip file" layout: default section: example ---

The FileSaver API

{% include_relative download-zip-file.inc/blob.html %}
{% highlight js %} {% include_relative download-zip-file.inc/blob.js %} {% endhighlight %}
{% highlight html %} {% include_relative download-zip-file.inc/blob.html %} {% endhighlight %}

The data URL

{% include_relative download-zip-file.inc/data_uri.html %}
{% highlight js %} {% include_relative download-zip-file.inc/data_uri.js %} {% endhighlight %}
{% highlight html %} {% include_relative download-zip-file.inc/data_uri.html %} {% endhighlight %}
================================================ FILE: documentation/examples/download-zip-file.inc/blob.html ================================================

Works on firefox, chrome , opera >= 15 and IE >= 10 (but NOT in compatibility view).

================================================ FILE: documentation/examples/download-zip-file.inc/blob.js ================================================ "use strict"; var zip = new JSZip(); zip.file("Hello.txt", "Hello world\n"); jQuery("#blob").on("click", function () { zip.generateAsync({type:"blob"}).then(function (blob) { // 1) generate the zip file saveAs(blob, "hello.zip"); // 2) trigger the download }, function (err) { jQuery("#blob").text(err); }); }); ================================================ FILE: documentation/examples/download-zip-file.inc/data_uri.html ================================================

Does not work in IE, has restrictions on the length.

================================================ FILE: documentation/examples/download-zip-file.inc/data_uri.js ================================================ "use strict"; var zip = new JSZip(); zip.file("Hello.txt", "Hello world\n"); jQuery("#data_uri").on("click", function () { zip.generateAsync({type:"base64"}).then(function (base64) { window.location = "data:application/zip;base64," + base64; }, function (err) { jQuery("#data_uri").text(err); }); }); ================================================ FILE: documentation/examples/downloader.html ================================================ --- title: "Mini app : Downloader" layout: default section: example ---

This mini application let you choose the files you want in a list, download them, zip them and give the result to the user.

This demo requires a recent browser, see the howto.

This demo depends on the following libraries:

{% include_relative downloader.inc/downloader.html %}
{% highlight js %} {% include_relative downloader.inc/downloader.js %} {% endhighlight %}
{% highlight js %} {% include_relative downloader.inc/helpers.js %} {% endhighlight %}
{% highlight html %} {% include_relative downloader.inc/downloader.html %} {% endhighlight %}
================================================ FILE: documentation/examples/downloader.inc/downloader.html ================================================

Please select your files

================================================ FILE: documentation/examples/downloader.inc/downloader.js ================================================ "use strict"; // From helpers.js: /* global resetMessage, showMessage, showError, updatePercent */ /** * Fetch the content and return the associated promise. * @param {String} url the url of the content to fetch. * @return {Promise} the promise containing the data. */ function urlToPromise(url) { return new Promise(function(resolve, reject) { JSZipUtils.getBinaryContent(url, function (err, data) { if(err) { reject(err); } else { resolve(data); } }); }); } $("#download_form").on("submit", function () { resetMessage(); var zip = new JSZip(); // find every checked item $(this).find(":checked").each(function () { var $this = $(this); var url = $this.data("url"); var filename = url.replace(/.*\//g, ""); zip.file(filename, urlToPromise(url), {binary:true}); }); // when everything has been downloaded, we can trigger the dl zip.generateAsync({type:"blob"}, function updateCallback(metadata) { var msg = "progression : " + metadata.percent.toFixed(2) + " %"; if(metadata.currentFile) { msg += ", current file = " + metadata.currentFile; } showMessage(msg); updatePercent(metadata.percent|0); }) .then(function callback(blob) { // see FileSaver.js saveAs(blob, "example.zip"); showMessage("done !"); }, function (e) { showError(e); }); return false; }); ================================================ FILE: documentation/examples/downloader.inc/helpers.js ================================================ "use strict"; /** * Reset the message. */ function resetMessage () { $("#result") .removeClass() .text(""); } /** * show a successful message. * @param {String} text the text to show. */ // eslint-disable-next-line no-unused-vars function showMessage(text) { resetMessage(); $("#result") .addClass("alert alert-success") .text(text); } /** * show an error message. * @param {String} text the text to show. */ function showError(text) { resetMessage(); $("#result") .addClass("alert alert-danger") .text(text); } /** * Update the progress bar. * @param {Integer} percent the current percent */ // eslint-disable-next-line no-unused-vars function updatePercent(percent) { $("#progress_bar").removeClass("hide") .find(".progress-bar") .attr("aria-valuenow", percent) .css({ width : percent + "%" }); } if(!JSZip.support.blob) { showError("This demo works only with a recent browser !"); } ================================================ FILE: documentation/examples/get-binary-files-ajax.html ================================================ --- title: "Get a file with an ajax call" layout: default section: example ---

With JSZipUtils

Note: JSZipUtils is a library available here.
{% include_relative get-binary-files-ajax.inc/jszip_utils.html %}
{% highlight js %} {% include_relative get-binary-files-ajax.inc/jszip_utils.js %} {% endhighlight %}
{% highlight html %} {% include_relative get-binary-files-ajax.inc/jszip_utils.html %} {% endhighlight %}

With the Fetch API

Note: the Fetch API is a new javascript API which may not be available everywhere.
{% include_relative get-binary-files-ajax.inc/fetch_api.html %}
{% highlight js %} {% include_relative get-binary-files-ajax.inc/fetch_api.js %} {% endhighlight %}
{% highlight html %} {% include_relative get-binary-files-ajax.inc/fetch_api.html %} {% endhighlight %}
================================================ FILE: documentation/examples/get-binary-files-ajax.inc/fetch_api.html ================================================
================================================ FILE: documentation/examples/get-binary-files-ajax.inc/fetch_api.js ================================================ "use strict"; fetch("{{site.baseurl}}/test/ref/text.zip") // 1) fetch the url .then(function (response) { // 2) filter on 200 OK if (response.status === 200 || response.status === 0) { return Promise.resolve(response.blob()); } else { return Promise.reject(new Error(response.statusText)); } }) .then(JSZip.loadAsync) // 3) chain with the zip promise .then(function (zip) { return zip.file("Hello.txt").async("string"); // 4) chain with the text content promise }) .then(function success(text) { // 5) display the result $("#fetch").append($("

", { "class": "alert alert-success", text: "loaded, content = " + text })); }, function error(e) { $("#fetch").append($("

", { "class": "alert alert-danger", text: e })); }); ================================================ FILE: documentation/examples/get-binary-files-ajax.inc/jszip_utils.html ================================================

================================================ FILE: documentation/examples/get-binary-files-ajax.inc/jszip_utils.js ================================================ "use strict"; // 1) get a promise of the content var promise = new JSZip.external.Promise(function (resolve, reject) { JSZipUtils.getBinaryContent("{{site.baseurl}}/test/ref/text.zip", function(err, data) { if (err) { reject(err); } else { resolve(data); } }); }); promise.then(JSZip.loadAsync) // 2) chain with the zip promise .then(function(zip) { return zip.file("Hello.txt").async("string"); // 3) chain with the text content promise }) .then(function success(text) { // 4) display the result $("#jszip_utils").append($("

", { "class": "alert alert-success", text: "loaded, content = " + text })); }, function error(e) { $("#jszip_utils").append($("

", { "class": "alert alert-danger", text: e })); }); ================================================ FILE: documentation/examples/read-local-file-api.html ================================================ --- title: "Reading a local file with the File API" layout: default section: example ---

{% include_relative read-local-file-api.inc/read.html %}
{% highlight js %} {% include_relative read-local-file-api.inc/read.js %} {% endhighlight %}
{% highlight html %} {% include_relative read-local-file-api.inc/read.html %} {% endhighlight %}
================================================ FILE: documentation/examples/read-local-file-api.inc/read.html ================================================

Choose the local(s) zip file(s)

Note : your browser will process the zip file, don't choose a file too big !


================================================ FILE: documentation/examples/read-local-file-api.inc/read.js ================================================ "use strict"; var $result = $("#result"); $("#file").on("change", function(evt) { // remove content $result.html(""); // be sure to show the results $("#result_block").removeClass("hidden").addClass("show"); // Closure to capture the file information. function handleFile(f) { var $title = $("

", { text : f.name }); var $fileContent = $("
    "); $result.append($title); $result.append($fileContent); var dateBefore = new Date(); JSZip.loadAsync(f) // 1) read the Blob .then(function(zip) { var dateAfter = new Date(); $title.append($("", { "class": "small", text:" (loaded in " + (dateAfter - dateBefore) + "ms)" })); zip.forEach(function (relativePath, zipEntry) { // 2) print entries $fileContent.append($("
  • ", { text : zipEntry.name })); }); }, function (e) { $result.append($("
    ", { "class" : "alert alert-danger", text : "Error reading " + f.name + ": " + e.message })); }); } var files = evt.target.files; for (var i = 0; i < files.length; i++) { handleFile(files[i]); } }); ================================================ FILE: documentation/examples.md ================================================ --- title: "How to use JSZip" layout: default section: example --- An instance of JSZip represents a set of files. You can add them, remove them, modify them. You can also import an existing zip file or generate one. ### Getting the object #### In a browser For a browser, there are two interesting files : `dist/jszip.js` and `dist/jszip.min.js` (include just one). If you use an AMD loader (RequireJS for example) JSZip will register itself : you just have to put the js file at the right place, or configure the loader (see [here for RequireJS](http://requirejs.org/docs/api.html#config-paths)). Without any loader, JSZip will declare in the global scope a variable named `JSZip`. #### In nodejs In nodejs, you can `require` it : ```js var JSZip = require("jszip"); ``` ### Basic manipulations The first step is to create an instance of JSZip : ```js var zip = new JSZip(); ``` On this instance, we can add (and update) files and folders with `.file(name, content)` and `.folder(name)`. They return the current JSZip instance so you can chain the calls. ```js // create a file zip.file("hello.txt", "Hello[p my)6cxsw2q"); // oops, cat on keyboard. Fixing ! zip.file("hello.txt", "Hello World\n"); // create a file and a folder zip.file("nested/hello.txt", "Hello World\n"); // same as zip.folder("nested").file("hello.txt", "Hello World\n"); ``` With `.folder(name)`, the returned object has a different root : if you add files on this object, you will put them in the created subfolder. This is just a view, the added files will also be in the "root" object. ```js var photoZip = zip.folder("photos"); // this call will create photos/README photoZip.file("README", "a folder with photos"); ``` You can access the file content with `.file(name)` and [its getters]({{site.baseurl}}/documentation/api_zipobject.html) : ```js zip.file("hello.txt").async("string").then(function (data) { // data is "Hello World\n" }); if (JSZip.support.uint8array) { zip.file("hello.txt").async("uint8array").then(function (data) { // data is Uint8Array { 0=72, 1=101, 2=108, more...} }); } ``` You can also remove files or folders with `.remove(name)` : ```js zip.remove("photos/README"); zip.remove("photos"); // same as zip.remove("photos"); // by removing the folder, you also remove its content. ``` ### Generate a zip file With `.generateAsync(options)` or `.generateNodeStream(options)` you can generate a zip file (not a real file but its representation in memory). Check [this page]({{site.baseurl}}/documentation/howto/write_zip.html) for more information on how to write / give the file to the user. ```js var promise = null; if (JSZip.support.uint8array) { promise = zip.generateAsync({type : "uint8array"}); } else { promise = zip.generateAsync({type : "string"}); } ``` ### Read a zip file With `.loadAsync(data)` you can load a zip file. Check [this page]({{site.baseurl}}/documentation/howto/read_zip.html) to see how to do properly (it's more tricky that it seems). ```js var new_zip = new JSZip(); // more files ! new_zip.loadAsync(content) .then(function(zip) { // you now have every files contained in the loaded zip zip.file("hello.txt").async("string"); // a promise of "Hello World\n" }); ``` ================================================ FILE: documentation/faq.md ================================================ --- title: "Frequently Asked Questions" layout: default section: main --- ### "Corrupted zip or bug: unexpected signature" If you are sure that the zip file is correct, that error often comes from a corrupted content. An ajax request, if not prepared correctly, will try to decode the binary content as a text and corrupt it. See [this page]({{site.baseurl}}/documentation/howto/read_zip.html). ### My browser crashes / becomes unresponsive / never finish the execution That happens if you try to handle to much data with the synchronous API. If possible, try the asynchronous API, see [this page]({{site.baseurl}}/documentation/limitations.html) for more information. ### Can't read the data of [...]. Is it in a supported JavaScript type ? Or the old message: > The data of [...] is in an unsupported format The method [`file(name, data [,options])`]({{site.baseurl}}/documentation/api_jszip/file_data.html) accepts string and binary inputs for `data`. If you use an unsupported type, an object for example, you will get this error: ```js // WRONG var data = { content: new ArrayBuffer(...) }; zip.file("my.data", data); // won't work, data is an object // CORRECT var data = new ArrayBuffer(...); zip.file("my.data", data); // will work, JSZip accepts ArrayBuffer ``` ### My mac generates a `.cpgz` file when I try to extract the zip file MacOS Finder has a lot of bug related to zip files (the `unzip` command line tool is fine). When something goes wrong, Finder will generate this cpgz file instead of showing an error. To get a correct result, try to enable compression in `generateAsync`: ```js zip.generateAsync({ type:"...", compression: "DEFLATE" // <-- here }); ``` Using `platform: "UNIX"` may help too. ================================================ FILE: documentation/howto/read_zip.md ================================================ --- title: "How to read a file" layout: default section: example --- This page explains how to read an existing zip file or add a existing file into the zip file. ### In the browser #### AJAX request Getting binary data with an ajax request is hard (mainly because of IE <= 9). The easy way is to use [JSZipUtils.getBinaryContent](https://github.com/stuk/jszip-utils). With JSZipUtils.getBinaryContent, you can do the following (see the documentation for more examples) : ```js JSZipUtils.getBinaryContent('path/to/content.zip', function(err, data) { if(err) { throw err; // or handle err } JSZip.loadAsync(data).then(function () { // ... }); }); // or, with promises: new JSZip.external.Promise(function (resolve, reject) { JSZipUtils.getBinaryContent('path/to/content.zip', function(err, data) { if (err) { reject(err); } else { resolve(data); } }); }).then(function (data) { return JSZip.loadAsync(data); }) .then(...) ```
    If you need to adapt an existing solution to what getBinaryContent does, here are the details. When doing a XHR request (level 1, without setting the `responseType`) the browser will try to interpret the response as a string and decode it from its charset. To avoid this on Firefox/Chrome/Opera, you need to set mime type : `xhr.overrideMimeType("text/plain; charset=x-user-defined");`. On IE <= 9, this is harder. The overrideMimeType trick doesn't work so we need to use [vbscript](http://stackoverflow.com/questions/1095102/how-do-i-load-binary-image-data-using-javascript-and-xmlhttprequest) and non standard attributes. On IE > 9, overrideMimeType doesn't work but xhr2 does. With [xhr 2](http://caniuse.com/xhr2), you can just set the responseType attribute : `xhr.responseType = "arraybuffer";`. With this, the browser will return an ArrayBuffer. #### Local files If the browser supports the [FileReader API](http://caniuse.com/filereader), you can use it to read a zip file. JSZip can read ArrayBuffer, so you can use `FileReader.readAsArrayBuffer(Blob)`, see this [example]({{site.baseurl}}/documentation/examples/read-local-file-api.html). ### In nodejs JSZip can read Buffers so you can do the following : #### Local file ```js "use strict"; var fs = require("fs"); var JSZip = require("jszip"); // read a zip file fs.readFile("test.zip", function(err, data) { if (err) throw err; JSZip.loadAsync(data).then(function (zip) { // ... }); }); // or new JSZip.external.Promise(function (resolve, reject) { fs.readFile("test.zip", function(err, data) { if (err) { reject(e); } else { resolve(data); } }); }).then(function (data) { return JSZip.loadAsync(data); }) .then(...) // read a file and add it to a zip fs.readFile("picture.png", function(err, data) { if (err) throw err; var zip = new JSZip(); zip.file("picture.png", data); }); // or var contentPromise = new JSZip.external.Promise(function (resolve, reject) { fs.readFile("picture.png", function(err, data) { if (err) { reject(e); } else { resolve(data); } }); }); zip.file("picture.png", contentPromise); // read a file as a stream and add it to a zip var stream = fs.createReadStream("picture.png"); zip.file("picture.png", stream); ``` #### Remote file There are a lot of nodejs libraries doing http requests, from the built-in [http](http://nodejs.org/docs/latest/api/http.html) to the [npm packages](https://www.npmjs.org/browse/keyword/http). Here are two examples, one with the default http API, the other with [request](https://github.com/mikeal/request) (but you're free to use your favorite library !). If possible, download the file as a Buffer (you will get better performances). If it's not possible, you can fallback to a binary string (the option is likely to be `encoding : "binary"`). ##### With http : ```js "use strict"; var http = require("http"); var url = require("url"); var JSZip = require("jszip"); var req = http.get(url.parse("http://localhost/.../file.zip"), function (res) { if (res.statusCode !== 200) { console.log(res.statusCode); // handle error return; } var data = [], dataLen = 0; // don't set the encoding, it will break everything ! // or, if you must, set it to null. In that case the chunk will be a string. res.on("data", function (chunk) { data.push(chunk); dataLen += chunk.length; }); res.on("end", function () { var buf = Buffer.concat(data); // here we go ! JSZip.loadAsync(buf).then(function (zip) { return zip.file("content.txt").async("string"); }).then(function (text) { console.log(text); }); }); }); req.on("error", function(err){ // handle error }); ``` ##### With request : ```js "use strict"; var request = require('request'); var JSZip = require("jszip"); request({ method : "GET", url : "http://localhost/.../file.zip", encoding: null // <- this one is important ! }, function (error, response, body) { if(error || response.statusCode !== 200) { // handle error return; } JSZip.loadAsync(body).then(function (zip) { return zip.file("content.txt").async("string"); }).then(function (text) { console.log(text); }); }); ``` ================================================ FILE: documentation/howto/write_zip.md ================================================ --- title: "How to write a file / give it to the user" layout: default section: example --- ### In the browser With only javascript, this part won't work in old browsers, including IE < 10. For those browsers, you can use a flash polyfill, see below. You can also see this [example]({{site.baseurl}}/documentation/examples/download-zip-file.html). #### Blob URL / FileSaver With recent browsers, the easiest way is to use `saveAs` or a polyfill, see [FileSaver.js](https://github.com/eligrey/FileSaver.js) : ```js zip.generateAsync({type:"blob"}) .then(function (blob) { saveAs(blob, "hello.zip"); }); ``` Under the hood, the polyfill uses the native `saveAs` from the [FileSaver](http://www.w3.org/TR/file-writer-api/#the-filesaver-interface) API (on Chrome and IE10+) or use a [Blob URL](http://updates.html5rocks.com/2011/08/Downloading-resources-in-HTML5-a-download) (on Firefox). #### Data URI For older browsers that support [data URI](http://caniuse.com/datauri), you can also do the following : ```js zip.generateAsync({type:"base64"}).then(function (base64) { location.href="data:application/zip;base64," + base64; }); ``` The biggest issue here is that the filenames are very awkward, Firefox generates filenames such as `a5sZQRsx.zip.part` (see bugs [367231](https://bugzilla.mozilla.org/show_bug.cgi?id=367231) and [532230](https://bugzilla.mozilla.org/show_bug.cgi?id=532230), and Safari isn't much better with just `Unknown`. Browser support and resulting filename : Opera | Firefox | Safari | Chrome | Internet Explorer -------|---------|--------|--------|------------------ "default.zip" | random alphanumeric with ".part" extension | "Unknown" (no extension) | "download.zip" on OSX and Linux, just "download" on Windows | No #### Downloadify [Downloadify](https://github.com/dcneiner/downloadify) uses a small Flash SWF to download files to a user's computer with a filename that you can choose. Doug Neiner has added the `dataType` option to allow you to pass a zip for downloading. Follow the [Downloadify demo](http://pixelgraphics.us/downloadify/test.html) with the following changes: ```js zip = new JSZip(); zip.file("Hello.", "hello.txt"); zip.generateAsync({type:"base64"}).then(function (base64) { Downloadify.create('downloadify',{ ... data: function(){ return base64; }, ... dataType: 'base64' }); }); ``` #### Deprecated google gears [Franz Buchinger](http://www.picurl.org/blog/author/franz/) has written a brilliant tutorial on [using JSZip with Google Gears](http://www.picurl.org/blog/2009/11/22/creating-zip-archives-with-gears) ([part 2](http://www.picurl.org/blog/2009/11/29/gearszipper-part2-adding-support-for-real-files-and-canvas-elements/)). If you want to let your Gears users download several files at once I really recommend having a look at some of his [examples](http://picurl.org/gears/zipper/). ### In nodejs JSZip can generate Buffers so you can do the following : ```js var fs = require("fs"); var JSZip = require("jszip"); var zip = new JSZip(); // zip.file("file", content); // ... and other manipulations zip .generateNodeStream({type:'nodebuffer',streamFiles:true}) .pipe(fs.createWriteStream('out.zip')) .on('finish', function () { // JSZip generates a readable stream with a "end" event, // but is piped here in a writable stream which emits a "finish" event. console.log("out.zip written."); }); ``` ================================================ FILE: documentation/limitations.md ================================================ --- title: "Limitations of JSZip" layout: default section: limitations fullpage: true --- ### Not supported features Not all features of zip files are supported. Classic zip files will work but encrypted zip, multi-volume, etc are not supported and the loadAsync() method will return a failed promise. ### ZIP64 and 32bit integers ZIP64 files can be loaded, but only if the zip file is not "too big". ZIP64 uses 64bits integers but JavaScript represents all numbers as [64-bit double precision IEEE 754 floating point numbers](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf) (see section 8.5). So, we have 53bits for integers and [bitwise operations treat everything as 32bits](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators). So if all the 64bits integers can fit into 32 bits integers, everything will be fine. If it's not the case, you will have other problems anyway (see next limitation). ### Performance issues An other limitation comes from the browser (and the machine running the browser). A compressed zip file of 10MB is easily opened by firefox / chrome / opera / IE10+ but will crash older IE. Also keep in mind that strings in javascript are encoded in UTF-16 : a 10MB ascii text file will take 20MB of memory. The [`async` method]({{site.baseurl}}/documentation/api_zipobject/async.html) and the [`generateAsync` method]({{site.baseurl}}/documentation/api_jszip/generate_async.html) hold the full result in memory but doesn't freeze the browser. If the result is too big, and if you can't use the [`nodeStream` method]({{site.baseurl}}/documentation/api_zipobject/node_stream.html) or the [`generateNodeStream` method]({{site.baseurl}}/documentation/api_jszip/generate_node_stream.html) you need to use the underlying [`StreamHelper`]({{site.baseurl}}/documentation/api_streamhelper.html) to handle the result chunk by chunk and `pause()`/`resume()` to handle the backpressure. If you're having performance issues, please consider the following : * Don't use IE <= 9. Everything is better with typed arrays. * Use typed arrays (Uint8Array, ArrayBuffer, etc) if possible : * If you generate a zip file, you should use `type:"uint8array"` (or blob, arraybuffer, nodebuffer). * If you load the file from an ajax call, ask your XHR an ArrayBuffer. Loading a string is asking for troubles. Note about compression : When reading a file, JSZip will store the content without decompressing it. When generating a compressed file, JSZip will reuse if possible the compressed content : * If you read a zip file compressed with DEFLATE and call `generate` with the DEFLATE compression, JSZip won't call the compression algorithms (same with STORE everywhere.) * If you read a zip file compressed with DEFLATE and call `generate` with the STORE compression, JSZip will have to decompress everything. On IE <=9, typed arrays are not supported and the compression algorithm will fallback on arrays. In that case, JSZip needs to convert the binary string into an array, DEFLATE it and convert the result into a binary string. You don't want that to happen. ### The output zip will differ from the input zip Reading and generating a zip file won't give you back the same file. Some data are discarded (file metadata) and other are added (subfolders). ### Encodings support JSZip only supports UTF-8 natively. A zip file doesn't contain the name of the encoding used, you need to know it before doing anything. #### File name If the name of a file inside the zip is encoded with UTF-8 then JSZip can detect it (Language encoding flag, Unicode Path Extra Field). If not, JSZip can't detect the encoding used and will generate [Mojibake](https://en.wikipedia.org/wiki/Mojibake). You can use the [encodeFileName]({{site.baseurl}}/documentation/api_jszip/generate.html) option and the [decodeFileName]({{site.baseurl}}/documentation/api_jszip/load.html) option to encode/decode using a custom encoding. #### File content The `async("string")` method uses UTF-8 to decode the content. If you have a text in a different encoding, you can get the bytes array with `async("uint8array")` and decode it with a lib (iconv, iconv-lite, etc) on your side. To save a text using a non-UTF-8 encoding, do the same : encode it into a Uint8Array before adding it to JSZip. ================================================ FILE: documentation/upgrade_guide.md ================================================ --- title: Upgrade Guide layout: default section: main --- ### From 2.x to 3.0.0 * Deprecated objects/methods has been removed: * `options.base64` in `generate()` (the base64 type is still valid) * `options.base64`, `options.binary`, `options.dir`, `options.date` on `ZipObject` (see the [2.3 upgrade section](#from-222-to-230)) * `JSZip.utils` * `JSZip.prototype.crc32`, `JSZip.prototype.utf8encode`, `JSZip.prototype.utf8decode` * `JSZip.base64` (you can get the content of a file directly as a base64 string) * `JSZip.compressions` has been removed. * On `ZipObject`, the synchronous getters has been replaced by `async()` and `nodeStream()`. * The `generate()` method has been replaced by `generateAsync()` and `generateNodeStream()`. * The `type` option in `generate()` is now mandatory. * The "text" type has been replaced by the "string" type, a binary string is named "binarystring". * The `load()` method and the constructor with data (`new JSZip(data)`) have been replaced by `loadAsync()`. * When adding a file, the option `createFolders` now defaults to `true`. If you don't want to create sub folders, set it to false. * `zip.generateAsync()` and `zip.generateNodeStream()` now depend on the current folder level. ```js // 2.x zip.file("test.txt").asText(); // 3.x zip.file("test.txt").async("string") .then(function (content) { // use content }); // 2.x zip.generate(); // 3.x zip.generateAsync({type:"uint8array"}) .then(function (content) { // use content }); // 2.x new JSZip(data); zip.load(data); // zip.file(...) // 3.x JSZip.loadAsync(data).then(zip) {...}; zip.loadAsync(data).then(zip) {...}; // here, zip won't have (yet) the updated content // 2.x var data = zip.file("img.jpg").asBinary(); var dataURI = "data:image/jpeg;base64," + JSZip.base64.encode(data); // 3.x zip.file("img.jpg").async("base64") .then(function (data64) { var dataURI = "data:image/jpeg;base64," + data64; }); ``` `async` and `loadAsync` use (a polyfill of) promises, you can find the documentation [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) and a tutorial [here](http://www.html5rocks.com/en/tutorials/es6/promises/). It is worth noting that: ```js /* * JSZip accepts these promise as input */ // replace a content with JSZip v2 var content = zip.file("my_file").asText(); content = content.replace(/apples/, 'oranges'); zip.file("my_file", content); // replace a content with JSZip v3 var contentPromise = zip.file("my_file").async("text").then(function (content) { return content.replace(/apples/, 'oranges'); }); zip.file("my_file", contentPromise); /* * Promises are chainable */ // read, update, generate a zip file with JSZip v2 var zip = new JSZip(content); zip.file("new_file", "new_content"); var blob = zip.generate({type: "blob"}); saveAs(blob, "result.zip"); // read, update, generate a zip file with JSZip v3 JSZip.loadAsync(content) .then(function (zip) { zip.file("new_file", "new_content"); // if you return the zip object, it will be available in the next "then" return zip; .then(function (zip) { // if you return a promise of a blob, promises will "merge": the current // promise will wait for the other and the next "then" will get the // blob return zip.generateAsync({type: "blob"}); .then(function (blob) { saveAs(blob, "result.zip"); }); ``` ### From 2.2.2 to 2.3.0 * On `ZipObject#options`, the attributes `date` and `dir` have been deprecated and are now on `ZipObject`. * On `ZipObject#options`, the attributes `base64` and `binary` have been deprecated. * `JSZip.base64`, `JSZip.prototype.crc32`, `JSZip.prototype.utf8decode`, `JSZip.prototype.utf8encode` and `JSZip.utils` have been deprecated. ```js // deprecated zip.file("test.txt").options.date zip.file("test.txt").options.dir // new API zip.file("test.txt").date zip.file("test.txt").dir ``` ### From 2.0.0 to 2.1.0 * The packaging changed : instead of loading jszip.js, jszip-load.js, jszip-inflate.js, jszip-deflate.js, just include dist/jszip.js or dist/jszip.min.js. For AMD loader users : JSZip now registers itself. You just have to put the file at the right place or configure your loader. ### From 1.x to 2.x * `JSZipBase64` has been renamed to `JSZip.base64`. * The `data` attribute doesn't exist anymore : use the getters `asText()`, `asBinary()`, etc * The compression/decompression methods now give their input type with the `compressInputType` and `uncompressInputType` attributes. Example for the data attribute : ```js // before zip.file("test.txt").data; zip.files["test.txt"].data; zip.file("image.png").data; zip.files["image.png"].data; // after zip.file("test.txt").asText(); zip.files["test.txt"].asText(); zip.file("image.png").asBinary(); zip.files["image.png"].asBinary(); ``` ================================================ FILE: index.d.ts ================================================ // Type definitions for JSZip 3.1 // Project: http://stuk.github.com/jszip/, https://github.com/stuk/jszip // Definitions by: mzeiher , forabi // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 /// interface JSZipSupport { arraybuffer: boolean; uint8array: boolean; blob: boolean; nodebuffer: boolean; } type Compression = 'STORE' | 'DEFLATE'; /** * Depends on the compression type. With `STORE` (no compression), these options are ignored. With * `DEFLATE`, you can give the compression level between 1 (best speed) and 9 (best compression). */ interface CompressionOptions { level: number; } interface InputByType { base64: string; string: string; text: string; binarystring: string; array: number[]; uint8array: Uint8Array; arraybuffer: ArrayBuffer; blob: Blob; stream: NodeJS.ReadableStream; } interface OutputByType { base64: string; string: string; text: string; binarystring: string; array: number[]; uint8array: Uint8Array; arraybuffer: ArrayBuffer; blob: Blob; nodebuffer: Buffer; } // This private `_data` property on a JSZipObject uses this interface. // If/when it is made public this should be uncommented. // interface CompressedObject { // compressedSize: number; // uncompressedSize: number; // crc32: number; // compression: object; // compressedContent: string|ArrayBuffer|Uint8Array|Buffer; // } type InputFileFormat = InputByType[keyof InputByType] | Promise; declare namespace JSZip { type InputType = keyof InputByType; type OutputType = keyof OutputByType; interface JSZipMetadata { percent: number; currentFile: string | null; } type OnUpdateCallback = (metadata: JSZipMetadata) => void; interface JSZipObject { name: string; /** * Present for files loadded with `loadAsync`. May contain ".." path components that could * result in a zip-slip attack. See https://snyk.io/research/zip-slip-vulnerability */ unsafeOriginalName?: string; dir: boolean; date: Date; comment: string; /** The UNIX permissions of the file, if any. */ unixPermissions: number | string | null; /** The UNIX permissions of the file, if any. */ dosPermissions: number | null; options: JSZipObjectOptions; /** * Prepare the content in the asked type. * @param type the type of the result. * @param onUpdate a function to call on each internal update. * @return Promise the promise of the result. */ async(type: T, onUpdate?: OnUpdateCallback): Promise; nodeStream(type?: 'nodebuffer', onUpdate?: OnUpdateCallback): NodeJS.ReadableStream; } interface JSZipFileOptions { /** Set to `true` if the data is `base64` encoded. For example image data from a `` element. Plain text and HTML do not need this option. */ base64?: boolean; /** * Set to `true` if the data should be treated as raw content, `false` if this is a text. If `base64` is used, * this defaults to `true`, if the data is not a `string`, this will be set to `true`. */ binary?: boolean; /** * The last modification date, defaults to the current date. */ date?: Date; /** * Sets per file compression. The `compressionOptions` parameter depends on the compression type. */ compression?: Compression; /** * Sets per file compression level for `DEFLATE` compression. */ compressionOptions?: null | CompressionOptions; comment?: string; /** Set to `true` if (and only if) the input is a "binary string" and has already been prepared with a `0xFF` mask. */ optimizedBinaryString?: boolean; /** Set to `true` if folders in the file path should be automatically created, otherwise there will only be virtual folders that represent the path to the file. */ createFolders?: boolean; /** Set to `true` if this is a directory and content should be ignored. */ dir?: boolean; /** 6 bits number. The DOS permissions of the file, if any. */ dosPermissions?: number | null; /** * 16 bits number. The UNIX permissions of the file, if any. * Also accepts a `string` representing the octal value: `"644"`, `"755"`, etc. */ unixPermissions?: number | string | null; } interface JSZipObjectOptions { compression: Compression; } interface JSZipGeneratorOptions { /** * Sets compression option for all entries that have not specified their own `compression` option */ compression?: Compression; /** * Sets compression level for `DEFLATE` compression. */ compressionOptions?: null | CompressionOptions; type?: T; comment?: string; /** * mime-type for the generated file. * Useful when you need to generate a file with a different extension, ie: “.ods”. * @default 'application/zip' */ mimeType?: string; encodeFileName?(filename: string): string; /** Stream the files and create file descriptors */ streamFiles?: boolean; /** DOS (default) or UNIX */ platform?: 'DOS' | 'UNIX'; } interface JSZipLoadOptions { base64?: boolean; checkCRC32?: boolean; optimizedBinaryString?: boolean; createFolders?: boolean; decodeFileName?: (bytes: string[] | Uint8Array | Buffer) => string; } type DataEventCallback = (dataChunk: T, metadata: JSZipMetadata) => void type EndEventCallback = () => void type ErrorEventCallback = (error: Error) => void interface JSZipStreamHelper { /** * Register a listener on an event */ on(event: 'data', callback: DataEventCallback): this; on(event: 'end', callback: EndEventCallback): this; on(event: 'error', callback: ErrorEventCallback): this; /** * Read the whole stream and call a callback with the complete content * * @param updateCallback The function called every time the stream updates * @return A Promise of the full content */ accumulate(updateCallback?: (metadata: JSZipMetadata) => void): Promise; /** * Resume the stream if the stream is paused. Once resumed, the stream starts sending data events again * * @return The current StreamHelper object, for chaining */ resume(): this; /** * Pause the stream if the stream is running. Once paused, the stream stops sending data events * * @return The current StreamHelper object, for chaining */ pause(): this; } } interface JSZip { files: {[key: string]: JSZip.JSZipObject}; /** * Get a file from the archive * * @param Path relative path to file * @return File matching path, null if no file found */ file(path: string): JSZip.JSZipObject | null; /** * Get files matching a RegExp from archive * * @param path RegExp to match * @return Return all matching files or an empty array */ file(path: RegExp): JSZip.JSZipObject[]; /** * Add a file to the archive * * @param path Relative path to file * @param data Content of the file * @param options Optional information about the file * @return JSZip object */ file(path: string, data: InputByType[T] | Promise, options?: JSZip.JSZipFileOptions): this; file(path: string, data: null, options?: JSZip.JSZipFileOptions & { dir: true }): this; /** * Returns an new JSZip instance with the given folder as root * * @param name Name of the folder * @return New JSZip object with the given folder as root or null */ folder(name: string): JSZip | null; /** * Returns new JSZip instances with the matching folders as root * * @param name RegExp to match * @return New array of JSZipFile objects which match the RegExp */ folder(name: RegExp): JSZip.JSZipObject[]; /** * Call a callback function for each entry at this folder level. * * @param callback function */ forEach(callback: (relativePath: string, file: JSZip.JSZipObject) => void): void; /** * Get all files which match the given filter function * * @param predicate Filter function * @return Array of matched elements */ filter(predicate: (relativePath: string, file: JSZip.JSZipObject) => boolean): JSZip.JSZipObject[]; /** * Removes the file or folder from the archive * * @param path Relative path of file or folder * @return Returns the JSZip instance */ remove(path: string): JSZip; /** * Generates a new archive asynchronously * * @param options Optional options for the generator * @param onUpdate The optional function called on each internal update with the metadata. * @return The serialized archive */ generateAsync(options?: JSZip.JSZipGeneratorOptions, onUpdate?: JSZip.OnUpdateCallback): Promise; /** * Generates a new archive asynchronously * * @param options Optional options for the generator * @param onUpdate The optional function called on each internal update with the metadata. * @return A Node.js `ReadableStream` */ generateNodeStream(options?: JSZip.JSZipGeneratorOptions<'nodebuffer'>, onUpdate?: JSZip.OnUpdateCallback): NodeJS.ReadableStream; /** * Generates the complete zip file with the internal stream implementation * * @param options Optional options for the generator * @return a StreamHelper */ generateInternalStream(options?: JSZip.JSZipGeneratorOptions): JSZip.JSZipStreamHelper; /** * Deserialize zip file asynchronously * * @param data Serialized zip file * @param options Options for deserializing * @return Returns promise */ loadAsync(data: InputFileFormat, options?: JSZip.JSZipLoadOptions): Promise; /** * Create JSZip instance */ new(): this; (): JSZip; prototype: JSZip; support: JSZipSupport; external: { Promise: PromiseConstructorLike; }; version: string; } declare var JSZip: JSZip; export = JSZip; ================================================ FILE: index.html ================================================ --- title: JSZip layout: default section: main ---
    JSZip is a javascript library for creating, reading and editing .zip files, with a lovely and simple API.

    Current version : v3.10.1

    License : JSZip is dual-licensed. You may use it under the MIT license or the GPLv3 license. See LICENSE.markdown.

    Example

    Installation

    With npm : npm install jszip

    With bower : bower install Stuk/jszip

    With component : component install Stuk/jszip

    Manually : download JSZip and include the file dist/jszip.js or dist/jszip.min.js


    Installed ? Great ! You can now check our guides and examples !

    Support

    Opera Firefox Safari Chrome Internet Explorer Node.js
    Yes Yes Yes Yes Yes Yes
    Tested with the latest version Tested with 3.0 / 3.6 / latest version Tested with the latest version Tested with the latest version Tested with IE 6 / 7 / 8 / 9 / 10 Tested with node.js 0.10 / latest version

    Getting help

    Having trouble ? We'd like to help !

    • Try the FAQ, it has answers to common questions.
    • If you're looking for information about a specific method, try the documentation.
    • Check the examples.
    • Report bugs in our Bug tracker.

    Test status

    Live tests:
    See for yourself !
    ================================================ FILE: lib/base64.js ================================================ "use strict"; var utils = require("./utils"); var support = require("./support"); // private property var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; // public method for encoding exports.encode = function(input) { var output = []; var chr1, chr2, chr3, enc1, enc2, enc3, enc4; var i = 0, len = input.length, remainingBytes = len; var isArray = utils.getTypeOf(input) !== "string"; while (i < input.length) { remainingBytes = len - i; if (!isArray) { chr1 = input.charCodeAt(i++); chr2 = i < len ? input.charCodeAt(i++) : 0; chr3 = i < len ? input.charCodeAt(i++) : 0; } else { chr1 = input[i++]; chr2 = i < len ? input[i++] : 0; chr3 = i < len ? input[i++] : 0; } enc1 = chr1 >> 2; enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); enc3 = remainingBytes > 1 ? (((chr2 & 15) << 2) | (chr3 >> 6)) : 64; enc4 = remainingBytes > 2 ? (chr3 & 63) : 64; output.push(_keyStr.charAt(enc1) + _keyStr.charAt(enc2) + _keyStr.charAt(enc3) + _keyStr.charAt(enc4)); } return output.join(""); }; // public method for decoding exports.decode = function(input) { var chr1, chr2, chr3; var enc1, enc2, enc3, enc4; var i = 0, resultIndex = 0; var dataUrlPrefix = "data:"; if (input.substr(0, dataUrlPrefix.length) === dataUrlPrefix) { // This is a common error: people give a data url // (data:image/png;base64,iVBOR...) with a {base64: true} and // wonders why things don't work. // We can detect that the string input looks like a data url but we // *can't* be sure it is one: removing everything up to the comma would // be too dangerous. throw new Error("Invalid base64 input, it looks like a data url."); } input = input.replace(/[^A-Za-z0-9+/=]/g, ""); var totalLength = input.length * 3 / 4; if(input.charAt(input.length - 1) === _keyStr.charAt(64)) { totalLength--; } if(input.charAt(input.length - 2) === _keyStr.charAt(64)) { totalLength--; } if (totalLength % 1 !== 0) { // totalLength is not an integer, the length does not match a valid // base64 content. That can happen if: // - the input is not a base64 content // - the input is *almost* a base64 content, with a extra chars at the // beginning or at the end // - the input uses a base64 variant (base64url for example) throw new Error("Invalid base64 input, bad content length."); } var output; if (support.uint8array) { output = new Uint8Array(totalLength|0); } else { output = new Array(totalLength|0); } while (i < input.length) { enc1 = _keyStr.indexOf(input.charAt(i++)); enc2 = _keyStr.indexOf(input.charAt(i++)); enc3 = _keyStr.indexOf(input.charAt(i++)); enc4 = _keyStr.indexOf(input.charAt(i++)); chr1 = (enc1 << 2) | (enc2 >> 4); chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); chr3 = ((enc3 & 3) << 6) | enc4; output[resultIndex++] = chr1; if (enc3 !== 64) { output[resultIndex++] = chr2; } if (enc4 !== 64) { output[resultIndex++] = chr3; } } return output; }; ================================================ FILE: lib/compressedObject.js ================================================ "use strict"; var external = require("./external"); var DataWorker = require("./stream/DataWorker"); var Crc32Probe = require("./stream/Crc32Probe"); var DataLengthProbe = require("./stream/DataLengthProbe"); /** * Represent a compressed object, with everything needed to decompress it. * @constructor * @param {number} compressedSize the size of the data compressed. * @param {number} uncompressedSize the size of the data after decompression. * @param {number} crc32 the crc32 of the decompressed file. * @param {object} compression the type of compression, see lib/compressions.js. * @param {String|ArrayBuffer|Uint8Array|Buffer} data the compressed data. */ function CompressedObject(compressedSize, uncompressedSize, crc32, compression, data) { this.compressedSize = compressedSize; this.uncompressedSize = uncompressedSize; this.crc32 = crc32; this.compression = compression; this.compressedContent = data; } CompressedObject.prototype = { /** * Create a worker to get the uncompressed content. * @return {GenericWorker} the worker. */ getContentWorker: function () { var worker = new DataWorker(external.Promise.resolve(this.compressedContent)) .pipe(this.compression.uncompressWorker()) .pipe(new DataLengthProbe("data_length")); var that = this; worker.on("end", function () { if (this.streamInfo["data_length"] !== that.uncompressedSize) { throw new Error("Bug : uncompressed data size mismatch"); } }); return worker; }, /** * Create a worker to get the compressed content. * @return {GenericWorker} the worker. */ getCompressedWorker: function () { return new DataWorker(external.Promise.resolve(this.compressedContent)) .withStreamInfo("compressedSize", this.compressedSize) .withStreamInfo("uncompressedSize", this.uncompressedSize) .withStreamInfo("crc32", this.crc32) .withStreamInfo("compression", this.compression) ; } }; /** * Chain the given worker with other workers to compress the content with the * given compression. * @param {GenericWorker} uncompressedWorker the worker to pipe. * @param {Object} compression the compression object. * @param {Object} compressionOptions the options to use when compressing. * @return {GenericWorker} the new worker compressing the content. */ CompressedObject.createWorkerFrom = function (uncompressedWorker, compression, compressionOptions) { return uncompressedWorker .pipe(new Crc32Probe()) .pipe(new DataLengthProbe("uncompressedSize")) .pipe(compression.compressWorker(compressionOptions)) .pipe(new DataLengthProbe("compressedSize")) .withStreamInfo("compression", compression); }; module.exports = CompressedObject; ================================================ FILE: lib/compressions.js ================================================ "use strict"; var GenericWorker = require("./stream/GenericWorker"); exports.STORE = { magic: "\x00\x00", compressWorker : function () { return new GenericWorker("STORE compression"); }, uncompressWorker : function () { return new GenericWorker("STORE decompression"); } }; exports.DEFLATE = require("./flate"); ================================================ FILE: lib/crc32.js ================================================ "use strict"; var utils = require("./utils"); /** * The following functions come from pako, from pako/lib/zlib/crc32.js * released under the MIT license, see pako https://github.com/nodeca/pako/ */ // Use ordinary array, since untyped makes no boost here function makeTable() { var c, table = []; for(var n =0; n < 256; n++){ c = n; for(var k =0; k < 8; k++){ c = ((c&1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1)); } table[n] = c; } return table; } // Create table on load. Just 255 signed longs. Not a problem. var crcTable = makeTable(); function crc32(crc, buf, len, pos) { var t = crcTable, end = pos + len; crc = crc ^ (-1); for (var i = pos; i < end; i++ ) { crc = (crc >>> 8) ^ t[(crc ^ buf[i]) & 0xFF]; } return (crc ^ (-1)); // >>> 0; } // That's all for the pako functions. /** * Compute the crc32 of a string. * This is almost the same as the function crc32, but for strings. Using the * same function for the two use cases leads to horrible performances. * @param {Number} crc the starting value of the crc. * @param {String} str the string to use. * @param {Number} len the length of the string. * @param {Number} pos the starting position for the crc32 computation. * @return {Number} the computed crc32. */ function crc32str(crc, str, len, pos) { var t = crcTable, end = pos + len; crc = crc ^ (-1); for (var i = pos; i < end; i++ ) { crc = (crc >>> 8) ^ t[(crc ^ str.charCodeAt(i)) & 0xFF]; } return (crc ^ (-1)); // >>> 0; } module.exports = function crc32wrapper(input, crc) { if (typeof input === "undefined" || !input.length) { return 0; } var isArray = utils.getTypeOf(input) !== "string"; if(isArray) { return crc32(crc|0, input, input.length, 0); } else { return crc32str(crc|0, input, input.length, 0); } }; ================================================ FILE: lib/defaults.js ================================================ "use strict"; exports.base64 = false; exports.binary = false; exports.dir = false; exports.createFolders = true; exports.date = null; exports.compression = null; exports.compressionOptions = null; exports.comment = null; exports.unixPermissions = null; exports.dosPermissions = null; ================================================ FILE: lib/external.js ================================================ "use strict"; // load the global object first: // - it should be better integrated in the system (unhandledRejection in node) // - the environment may have a custom Promise implementation (see zone.js) var ES6Promise = null; if (typeof Promise !== "undefined") { ES6Promise = Promise; } else { ES6Promise = require("lie"); } /** * Let the user use/change some implementations. */ module.exports = { Promise: ES6Promise }; ================================================ FILE: lib/flate.js ================================================ "use strict"; var USE_TYPEDARRAY = (typeof Uint8Array !== "undefined") && (typeof Uint16Array !== "undefined") && (typeof Uint32Array !== "undefined"); var pako = require("pako"); var utils = require("./utils"); var GenericWorker = require("./stream/GenericWorker"); var ARRAY_TYPE = USE_TYPEDARRAY ? "uint8array" : "array"; exports.magic = "\x08\x00"; /** * Create a worker that uses pako to inflate/deflate. * @constructor * @param {String} action the name of the pako function to call : either "Deflate" or "Inflate". * @param {Object} options the options to use when (de)compressing. */ function FlateWorker(action, options) { GenericWorker.call(this, "FlateWorker/" + action); this._pako = null; this._pakoAction = action; this._pakoOptions = options; // the `meta` object from the last chunk received // this allow this worker to pass around metadata this.meta = {}; } utils.inherits(FlateWorker, GenericWorker); /** * @see GenericWorker.processChunk */ FlateWorker.prototype.processChunk = function (chunk) { this.meta = chunk.meta; if (this._pako === null) { this._createPako(); } this._pako.push(utils.transformTo(ARRAY_TYPE, chunk.data), false); }; /** * @see GenericWorker.flush */ FlateWorker.prototype.flush = function () { GenericWorker.prototype.flush.call(this); if (this._pako === null) { this._createPako(); } this._pako.push([], true); }; /** * @see GenericWorker.cleanUp */ FlateWorker.prototype.cleanUp = function () { GenericWorker.prototype.cleanUp.call(this); this._pako = null; }; /** * Create the _pako object. * TODO: lazy-loading this object isn't the best solution but it's the * quickest. The best solution is to lazy-load the worker list. See also the * issue #446. */ FlateWorker.prototype._createPako = function () { this._pako = new pako[this._pakoAction]({ raw: true, level: this._pakoOptions.level || -1 // default compression }); var self = this; this._pako.onData = function(data) { self.push({ data : data, meta : self.meta }); }; }; exports.compressWorker = function (compressionOptions) { return new FlateWorker("Deflate", compressionOptions); }; exports.uncompressWorker = function () { return new FlateWorker("Inflate", {}); }; ================================================ FILE: lib/generate/ZipFileWorker.js ================================================ "use strict"; var utils = require("../utils"); var GenericWorker = require("../stream/GenericWorker"); var utf8 = require("../utf8"); var crc32 = require("../crc32"); var signature = require("../signature"); /** * Transform an integer into a string in hexadecimal. * @private * @param {number} dec the number to convert. * @param {number} bytes the number of bytes to generate. * @returns {string} the result. */ var decToHex = function(dec, bytes) { var hex = "", i; for (i = 0; i < bytes; i++) { hex += String.fromCharCode(dec & 0xff); dec = dec >>> 8; } return hex; }; /** * Generate the UNIX part of the external file attributes. * @param {Object} unixPermissions the unix permissions or null. * @param {Boolean} isDir true if the entry is a directory, false otherwise. * @return {Number} a 32 bit integer. * * adapted from http://unix.stackexchange.com/questions/14705/the-zip-formats-external-file-attribute : * * TTTTsstrwxrwxrwx0000000000ADVSHR * ^^^^____________________________ file type, see zipinfo.c (UNX_*) * ^^^_________________________ setuid, setgid, sticky * ^^^^^^^^^________________ permissions * ^^^^^^^^^^______ not used ? * ^^^^^^ DOS attribute bits : Archive, Directory, Volume label, System file, Hidden, Read only */ var generateUnixExternalFileAttr = function (unixPermissions, isDir) { var result = unixPermissions; if (!unixPermissions) { // I can't use octal values in strict mode, hence the hexa. // 040775 => 0x41fd // 0100664 => 0x81b4 result = isDir ? 0x41fd : 0x81b4; } return (result & 0xFFFF) << 16; }; /** * Generate the DOS part of the external file attributes. * @param {Object} dosPermissions the dos permissions or null. * @param {Boolean} isDir true if the entry is a directory, false otherwise. * @return {Number} a 32 bit integer. * * Bit 0 Read-Only * Bit 1 Hidden * Bit 2 System * Bit 3 Volume Label * Bit 4 Directory * Bit 5 Archive */ var generateDosExternalFileAttr = function (dosPermissions) { // the dir flag is already set for compatibility return (dosPermissions || 0) & 0x3F; }; /** * Generate the various parts used in the construction of the final zip file. * @param {Object} streamInfo the hash with information about the compressed file. * @param {Boolean} streamedContent is the content streamed ? * @param {Boolean} streamingEnded is the stream finished ? * @param {number} offset the current offset from the start of the zip file. * @param {String} platform let's pretend we are this platform (change platform dependents fields) * @param {Function} encodeFileName the function to encode the file name / comment. * @return {Object} the zip parts. */ var generateZipParts = function(streamInfo, streamedContent, streamingEnded, offset, platform, encodeFileName) { var file = streamInfo["file"], compression = streamInfo["compression"], useCustomEncoding = encodeFileName !== utf8.utf8encode, encodedFileName = utils.transformTo("string", encodeFileName(file.name)), utfEncodedFileName = utils.transformTo("string", utf8.utf8encode(file.name)), comment = file.comment, encodedComment = utils.transformTo("string", encodeFileName(comment)), utfEncodedComment = utils.transformTo("string", utf8.utf8encode(comment)), useUTF8ForFileName = utfEncodedFileName.length !== file.name.length, useUTF8ForComment = utfEncodedComment.length !== comment.length, dosTime, dosDate, extraFields = "", unicodePathExtraField = "", unicodeCommentExtraField = "", dir = file.dir, date = file.date; var dataInfo = { crc32 : 0, compressedSize : 0, uncompressedSize : 0 }; // if the content is streamed, the sizes/crc32 are only available AFTER // the end of the stream. if (!streamedContent || streamingEnded) { dataInfo.crc32 = streamInfo["crc32"]; dataInfo.compressedSize = streamInfo["compressedSize"]; dataInfo.uncompressedSize = streamInfo["uncompressedSize"]; } var bitflag = 0; if (streamedContent) { // Bit 3: the sizes/crc32 are set to zero in the local header. // The correct values are put in the data descriptor immediately // following the compressed data. bitflag |= 0x0008; } if (!useCustomEncoding && (useUTF8ForFileName || useUTF8ForComment)) { // Bit 11: Language encoding flag (EFS). bitflag |= 0x0800; } var extFileAttr = 0; var versionMadeBy = 0; if (dir) { // dos or unix, we set the dos dir flag extFileAttr |= 0x00010; } if(platform === "UNIX") { versionMadeBy = 0x031E; // UNIX, version 3.0 extFileAttr |= generateUnixExternalFileAttr(file.unixPermissions, dir); } else { // DOS or other, fallback to DOS versionMadeBy = 0x0014; // DOS, version 2.0 extFileAttr |= generateDosExternalFileAttr(file.dosPermissions, dir); } // date // @see http://www.delorie.com/djgpp/doc/rbinter/it/52/13.html // @see http://www.delorie.com/djgpp/doc/rbinter/it/65/16.html // @see http://www.delorie.com/djgpp/doc/rbinter/it/66/16.html dosTime = date.getUTCHours(); dosTime = dosTime << 6; dosTime = dosTime | date.getUTCMinutes(); dosTime = dosTime << 5; dosTime = dosTime | date.getUTCSeconds() / 2; dosDate = date.getUTCFullYear() - 1980; dosDate = dosDate << 4; dosDate = dosDate | (date.getUTCMonth() + 1); dosDate = dosDate << 5; dosDate = dosDate | date.getUTCDate(); if (useUTF8ForFileName) { // set the unicode path extra field. unzip needs at least one extra // field to correctly handle unicode path, so using the path is as good // as any other information. This could improve the situation with // other archive managers too. // This field is usually used without the utf8 flag, with a non // unicode path in the header (winrar, winzip). This helps (a bit) // with the messy Windows' default compressed folders feature but // breaks on p7zip which doesn't seek the unicode path extra field. // So for now, UTF-8 everywhere ! unicodePathExtraField = // Version decToHex(1, 1) + // NameCRC32 decToHex(crc32(encodedFileName), 4) + // UnicodeName utfEncodedFileName; extraFields += // Info-ZIP Unicode Path Extra Field "\x75\x70" + // size decToHex(unicodePathExtraField.length, 2) + // content unicodePathExtraField; } if(useUTF8ForComment) { unicodeCommentExtraField = // Version decToHex(1, 1) + // CommentCRC32 decToHex(crc32(encodedComment), 4) + // UnicodeName utfEncodedComment; extraFields += // Info-ZIP Unicode Path Extra Field "\x75\x63" + // size decToHex(unicodeCommentExtraField.length, 2) + // content unicodeCommentExtraField; } var header = ""; // version needed to extract header += "\x0A\x00"; // general purpose bit flag header += decToHex(bitflag, 2); // compression method header += compression.magic; // last mod file time header += decToHex(dosTime, 2); // last mod file date header += decToHex(dosDate, 2); // crc-32 header += decToHex(dataInfo.crc32, 4); // compressed size header += decToHex(dataInfo.compressedSize, 4); // uncompressed size header += decToHex(dataInfo.uncompressedSize, 4); // file name length header += decToHex(encodedFileName.length, 2); // extra field length header += decToHex(extraFields.length, 2); var fileRecord = signature.LOCAL_FILE_HEADER + header + encodedFileName + extraFields; var dirRecord = signature.CENTRAL_FILE_HEADER + // version made by (00: DOS) decToHex(versionMadeBy, 2) + // file header (common to file and central directory) header + // file comment length decToHex(encodedComment.length, 2) + // disk number start "\x00\x00" + // internal file attributes TODO "\x00\x00" + // external file attributes decToHex(extFileAttr, 4) + // relative offset of local header decToHex(offset, 4) + // file name encodedFileName + // extra field extraFields + // file comment encodedComment; return { fileRecord: fileRecord, dirRecord: dirRecord }; }; /** * Generate the EOCD record. * @param {Number} entriesCount the number of entries in the zip file. * @param {Number} centralDirLength the length (in bytes) of the central dir. * @param {Number} localDirLength the length (in bytes) of the local dir. * @param {String} comment the zip file comment as a binary string. * @param {Function} encodeFileName the function to encode the comment. * @return {String} the EOCD record. */ var generateCentralDirectoryEnd = function (entriesCount, centralDirLength, localDirLength, comment, encodeFileName) { var dirEnd = ""; var encodedComment = utils.transformTo("string", encodeFileName(comment)); // end of central dir signature dirEnd = signature.CENTRAL_DIRECTORY_END + // number of this disk "\x00\x00" + // number of the disk with the start of the central directory "\x00\x00" + // total number of entries in the central directory on this disk decToHex(entriesCount, 2) + // total number of entries in the central directory decToHex(entriesCount, 2) + // size of the central directory 4 bytes decToHex(centralDirLength, 4) + // offset of start of central directory with respect to the starting disk number decToHex(localDirLength, 4) + // .ZIP file comment length decToHex(encodedComment.length, 2) + // .ZIP file comment encodedComment; return dirEnd; }; /** * Generate data descriptors for a file entry. * @param {Object} streamInfo the hash generated by a worker, containing information * on the file entry. * @return {String} the data descriptors. */ var generateDataDescriptors = function (streamInfo) { var descriptor = ""; descriptor = signature.DATA_DESCRIPTOR + // crc-32 4 bytes decToHex(streamInfo["crc32"], 4) + // compressed size 4 bytes decToHex(streamInfo["compressedSize"], 4) + // uncompressed size 4 bytes decToHex(streamInfo["uncompressedSize"], 4); return descriptor; }; /** * A worker to concatenate other workers to create a zip file. * @param {Boolean} streamFiles `true` to stream the content of the files, * `false` to accumulate it. * @param {String} comment the comment to use. * @param {String} platform the platform to use, "UNIX" or "DOS". * @param {Function} encodeFileName the function to encode file names and comments. */ function ZipFileWorker(streamFiles, comment, platform, encodeFileName) { GenericWorker.call(this, "ZipFileWorker"); // The number of bytes written so far. This doesn't count accumulated chunks. this.bytesWritten = 0; // The comment of the zip file this.zipComment = comment; // The platform "generating" the zip file. this.zipPlatform = platform; // the function to encode file names and comments. this.encodeFileName = encodeFileName; // Should we stream the content of the files ? this.streamFiles = streamFiles; // If `streamFiles` is false, we will need to accumulate the content of the // files to calculate sizes / crc32 (and write them *before* the content). // This boolean indicates if we are accumulating chunks (it will change a lot // during the lifetime of this worker). this.accumulate = false; // The buffer receiving chunks when accumulating content. this.contentBuffer = []; // The list of generated directory records. this.dirRecords = []; // The offset (in bytes) from the beginning of the zip file for the current source. this.currentSourceOffset = 0; // The total number of entries in this zip file. this.entriesCount = 0; // the name of the file currently being added, null when handling the end of the zip file. // Used for the emitted metadata. this.currentFile = null; this._sources = []; } utils.inherits(ZipFileWorker, GenericWorker); /** * @see GenericWorker.push */ ZipFileWorker.prototype.push = function (chunk) { var currentFilePercent = chunk.meta.percent || 0; var entriesCount = this.entriesCount; var remainingFiles = this._sources.length; if(this.accumulate) { this.contentBuffer.push(chunk); } else { this.bytesWritten += chunk.data.length; GenericWorker.prototype.push.call(this, { data : chunk.data, meta : { currentFile : this.currentFile, percent : entriesCount ? (currentFilePercent + 100 * (entriesCount - remainingFiles - 1)) / entriesCount : 100 } }); } }; /** * The worker started a new source (an other worker). * @param {Object} streamInfo the streamInfo object from the new source. */ ZipFileWorker.prototype.openedSource = function (streamInfo) { this.currentSourceOffset = this.bytesWritten; this.currentFile = streamInfo["file"].name; var streamedContent = this.streamFiles && !streamInfo["file"].dir; // don't stream folders (because they don't have any content) if(streamedContent) { var record = generateZipParts(streamInfo, streamedContent, false, this.currentSourceOffset, this.zipPlatform, this.encodeFileName); this.push({ data : record.fileRecord, meta : {percent:0} }); } else { // we need to wait for the whole file before pushing anything this.accumulate = true; } }; /** * The worker finished a source (an other worker). * @param {Object} streamInfo the streamInfo object from the finished source. */ ZipFileWorker.prototype.closedSource = function (streamInfo) { this.accumulate = false; var streamedContent = this.streamFiles && !streamInfo["file"].dir; var record = generateZipParts(streamInfo, streamedContent, true, this.currentSourceOffset, this.zipPlatform, this.encodeFileName); this.dirRecords.push(record.dirRecord); if(streamedContent) { // after the streamed file, we put data descriptors this.push({ data : generateDataDescriptors(streamInfo), meta : {percent:100} }); } else { // the content wasn't streamed, we need to push everything now // first the file record, then the content this.push({ data : record.fileRecord, meta : {percent:0} }); while(this.contentBuffer.length) { this.push(this.contentBuffer.shift()); } } this.currentFile = null; }; /** * @see GenericWorker.flush */ ZipFileWorker.prototype.flush = function () { var localDirLength = this.bytesWritten; for(var i = 0; i < this.dirRecords.length; i++) { this.push({ data : this.dirRecords[i], meta : {percent:100} }); } var centralDirLength = this.bytesWritten - localDirLength; var dirEnd = generateCentralDirectoryEnd(this.dirRecords.length, centralDirLength, localDirLength, this.zipComment, this.encodeFileName); this.push({ data : dirEnd, meta : {percent:100} }); }; /** * Prepare the next source to be read. */ ZipFileWorker.prototype.prepareNextSource = function () { this.previous = this._sources.shift(); this.openedSource(this.previous.streamInfo); if (this.isPaused) { this.previous.pause(); } else { this.previous.resume(); } }; /** * @see GenericWorker.registerPrevious */ ZipFileWorker.prototype.registerPrevious = function (previous) { this._sources.push(previous); var self = this; previous.on("data", function (chunk) { self.processChunk(chunk); }); previous.on("end", function () { self.closedSource(self.previous.streamInfo); if(self._sources.length) { self.prepareNextSource(); } else { self.end(); } }); previous.on("error", function (e) { self.error(e); }); return this; }; /** * @see GenericWorker.resume */ ZipFileWorker.prototype.resume = function () { if(!GenericWorker.prototype.resume.call(this)) { return false; } if (!this.previous && this._sources.length) { this.prepareNextSource(); return true; } if (!this.previous && !this._sources.length && !this.generatedError) { this.end(); return true; } }; /** * @see GenericWorker.error */ ZipFileWorker.prototype.error = function (e) { var sources = this._sources; if(!GenericWorker.prototype.error.call(this, e)) { return false; } for(var i = 0; i < sources.length; i++) { try { sources[i].error(e); } catch(e) { // the `error` exploded, nothing to do } } return true; }; /** * @see GenericWorker.lock */ ZipFileWorker.prototype.lock = function () { GenericWorker.prototype.lock.call(this); var sources = this._sources; for(var i = 0; i < sources.length; i++) { sources[i].lock(); } }; module.exports = ZipFileWorker; ================================================ FILE: lib/generate/index.js ================================================ "use strict"; var compressions = require("../compressions"); var ZipFileWorker = require("./ZipFileWorker"); /** * Find the compression to use. * @param {String} fileCompression the compression defined at the file level, if any. * @param {String} zipCompression the compression defined at the load() level. * @return {Object} the compression object to use. */ var getCompression = function (fileCompression, zipCompression) { var compressionName = fileCompression || zipCompression; var compression = compressions[compressionName]; if (!compression) { throw new Error(compressionName + " is not a valid compression method !"); } return compression; }; /** * Create a worker to generate a zip file. * @param {JSZip} zip the JSZip instance at the right root level. * @param {Object} options to generate the zip file. * @param {String} comment the comment to use. */ exports.generateWorker = function (zip, options, comment) { var zipFileWorker = new ZipFileWorker(options.streamFiles, comment, options.platform, options.encodeFileName); var entriesCount = 0; try { zip.forEach(function (relativePath, file) { entriesCount++; var compression = getCompression(file.options.compression, options.compression); var compressionOptions = file.options.compressionOptions || options.compressionOptions || {}; var dir = file.dir, date = file.date; file._compressWorker(compression, compressionOptions) .withStreamInfo("file", { name : relativePath, dir : dir, date : date, comment : file.comment || "", unixPermissions : file.unixPermissions, dosPermissions : file.dosPermissions }) .pipe(zipFileWorker); }); zipFileWorker.entriesCount = entriesCount; } catch (e) { zipFileWorker.error(e); } return zipFileWorker; }; ================================================ FILE: lib/index.js ================================================ "use strict"; /** * Representation a of zip file in js * @constructor */ function JSZip() { // if this constructor is used without `new`, it adds `new` before itself: if(!(this instanceof JSZip)) { return new JSZip(); } if(arguments.length) { throw new Error("The constructor with parameters has been removed in JSZip 3.0, please check the upgrade guide."); } // object containing the files : // { // "folder/" : {...}, // "folder/data.txt" : {...} // } // NOTE: we use a null prototype because we do not // want filenames like "toString" coming from a zip file // to overwrite methods and attributes in a normal Object. this.files = Object.create(null); this.comment = null; // Where we are in the hierarchy this.root = ""; this.clone = function() { var newObj = new JSZip(); for (var i in this) { if (typeof this[i] !== "function") { newObj[i] = this[i]; } } return newObj; }; } JSZip.prototype = require("./object"); JSZip.prototype.loadAsync = require("./load"); JSZip.support = require("./support"); JSZip.defaults = require("./defaults"); // TODO find a better way to handle this version, // a require('package.json').version doesn't work with webpack, see #327 JSZip.version = "3.10.1"; JSZip.loadAsync = function (content, options) { return new JSZip().loadAsync(content, options); }; JSZip.external = require("./external"); module.exports = JSZip; ================================================ FILE: lib/license_header.js ================================================ /*! JSZip v__VERSION__ - A JavaScript class for generating and reading zip files (c) 2009-2016 Stuart Knightley Dual licenced under the MIT license or GPLv3. See https://raw.github.com/Stuk/jszip/main/LICENSE.markdown. JSZip uses the library pako released under the MIT license : https://github.com/nodeca/pako/blob/main/LICENSE */ ================================================ FILE: lib/load.js ================================================ "use strict"; var utils = require("./utils"); var external = require("./external"); var utf8 = require("./utf8"); var ZipEntries = require("./zipEntries"); var Crc32Probe = require("./stream/Crc32Probe"); var nodejsUtils = require("./nodejsUtils"); /** * Check the CRC32 of an entry. * @param {ZipEntry} zipEntry the zip entry to check. * @return {Promise} the result. */ function checkEntryCRC32(zipEntry) { return new external.Promise(function (resolve, reject) { var worker = zipEntry.decompressed.getContentWorker().pipe(new Crc32Probe()); worker.on("error", function (e) { reject(e); }) .on("end", function () { if (worker.streamInfo.crc32 !== zipEntry.decompressed.crc32) { reject(new Error("Corrupted zip : CRC32 mismatch")); } else { resolve(); } }) .resume(); }); } module.exports = function (data, options) { var zip = this; options = utils.extend(options || {}, { base64: false, checkCRC32: false, optimizedBinaryString: false, createFolders: false, decodeFileName: utf8.utf8decode }); if (nodejsUtils.isNode && nodejsUtils.isStream(data)) { return external.Promise.reject(new Error("JSZip can't accept a stream when loading a zip file.")); } return utils.prepareContent("the loaded zip file", data, true, options.optimizedBinaryString, options.base64) .then(function (data) { var zipEntries = new ZipEntries(options); zipEntries.load(data); return zipEntries; }).then(function checkCRC32(zipEntries) { var promises = [external.Promise.resolve(zipEntries)]; var files = zipEntries.files; if (options.checkCRC32) { for (var i = 0; i < files.length; i++) { promises.push(checkEntryCRC32(files[i])); } } return external.Promise.all(promises); }).then(function addFiles(results) { var zipEntries = results.shift(); var files = zipEntries.files; for (var i = 0; i < files.length; i++) { var input = files[i]; var unsafeName = input.fileNameStr; var safeName = utils.resolve(input.fileNameStr); zip.file(safeName, input.decompressed, { binary: true, optimizedBinaryString: true, date: input.date, dir: input.dir, comment: input.fileCommentStr.length ? input.fileCommentStr : null, unixPermissions: input.unixPermissions, dosPermissions: input.dosPermissions, createFolders: options.createFolders }); if (!input.dir) { zip.file(safeName).unsafeOriginalName = unsafeName; } } if (zipEntries.zipComment.length) { zip.comment = zipEntries.zipComment; } return zip; }); }; ================================================ FILE: lib/nodejs/NodejsStreamInputAdapter.js ================================================ "use strict"; var utils = require("../utils"); var GenericWorker = require("../stream/GenericWorker"); /** * A worker that use a nodejs stream as source. * @constructor * @param {String} filename the name of the file entry for this stream. * @param {Readable} stream the nodejs stream. */ function NodejsStreamInputAdapter(filename, stream) { GenericWorker.call(this, "Nodejs stream input adapter for " + filename); this._upstreamEnded = false; this._bindStream(stream); } utils.inherits(NodejsStreamInputAdapter, GenericWorker); /** * Prepare the stream and bind the callbacks on it. * Do this ASAP on node 0.10 ! A lazy binding doesn't always work. * @param {Stream} stream the nodejs stream to use. */ NodejsStreamInputAdapter.prototype._bindStream = function (stream) { var self = this; this._stream = stream; stream.pause(); stream .on("data", function (chunk) { self.push({ data: chunk, meta : { percent : 0 } }); }) .on("error", function (e) { if(self.isPaused) { this.generatedError = e; } else { self.error(e); } }) .on("end", function () { if(self.isPaused) { self._upstreamEnded = true; } else { self.end(); } }); }; NodejsStreamInputAdapter.prototype.pause = function () { if(!GenericWorker.prototype.pause.call(this)) { return false; } this._stream.pause(); return true; }; NodejsStreamInputAdapter.prototype.resume = function () { if(!GenericWorker.prototype.resume.call(this)) { return false; } if(this._upstreamEnded) { this.end(); } else { this._stream.resume(); } return true; }; module.exports = NodejsStreamInputAdapter; ================================================ FILE: lib/nodejs/NodejsStreamOutputAdapter.js ================================================ "use strict"; var Readable = require("readable-stream").Readable; var utils = require("../utils"); utils.inherits(NodejsStreamOutputAdapter, Readable); /** * A nodejs stream using a worker as source. * @see the SourceWrapper in http://nodejs.org/api/stream.html * @constructor * @param {StreamHelper} helper the helper wrapping the worker * @param {Object} options the nodejs stream options * @param {Function} updateCb the update callback. */ function NodejsStreamOutputAdapter(helper, options, updateCb) { Readable.call(this, options); this._helper = helper; var self = this; helper.on("data", function (data, meta) { if (!self.push(data)) { self._helper.pause(); } if(updateCb) { updateCb(meta); } }) .on("error", function(e) { self.emit("error", e); }) .on("end", function () { self.push(null); }); } NodejsStreamOutputAdapter.prototype._read = function() { this._helper.resume(); }; module.exports = NodejsStreamOutputAdapter; ================================================ FILE: lib/nodejsUtils.js ================================================ "use strict"; module.exports = { /** * True if this is running in Nodejs, will be undefined in a browser. * In a browser, browserify won't include this file and the whole module * will be resolved an empty object. */ isNode : typeof Buffer !== "undefined", /** * Create a new nodejs Buffer from an existing content. * @param {Object} data the data to pass to the constructor. * @param {String} encoding the encoding to use. * @return {Buffer} a new Buffer. */ newBufferFrom: function(data, encoding) { if (Buffer.from && Buffer.from !== Uint8Array.from) { return Buffer.from(data, encoding); } else { if (typeof data === "number") { // Safeguard for old Node.js versions. On newer versions, // Buffer.from(number) / Buffer(number, encoding) already throw. throw new Error("The \"data\" argument must not be a number"); } return new Buffer(data, encoding); } }, /** * Create a new nodejs Buffer with the specified size. * @param {Integer} size the size of the buffer. * @return {Buffer} a new Buffer. */ allocBuffer: function (size) { if (Buffer.alloc) { return Buffer.alloc(size); } else { var buf = new Buffer(size); buf.fill(0); return buf; } }, /** * Find out if an object is a Buffer. * @param {Object} b the object to test. * @return {Boolean} true if the object is a Buffer, false otherwise. */ isBuffer : function(b){ return Buffer.isBuffer(b); }, isStream : function (obj) { return obj && typeof obj.on === "function" && typeof obj.pause === "function" && typeof obj.resume === "function"; } }; ================================================ FILE: lib/object.js ================================================ "use strict"; var utf8 = require("./utf8"); var utils = require("./utils"); var GenericWorker = require("./stream/GenericWorker"); var StreamHelper = require("./stream/StreamHelper"); var defaults = require("./defaults"); var CompressedObject = require("./compressedObject"); var ZipObject = require("./zipObject"); var generate = require("./generate"); var nodejsUtils = require("./nodejsUtils"); var NodejsStreamInputAdapter = require("./nodejs/NodejsStreamInputAdapter"); /** * Add a file in the current folder. * @private * @param {string} name the name of the file * @param {String|ArrayBuffer|Uint8Array|Buffer} data the data of the file * @param {Object} originalOptions the options of the file * @return {Object} the new file. */ var fileAdd = function(name, data, originalOptions) { // be sure sub folders exist var dataType = utils.getTypeOf(data), parent; /* * Correct options. */ var o = utils.extend(originalOptions || {}, defaults); o.date = o.date || new Date(); if (o.compression !== null) { o.compression = o.compression.toUpperCase(); } if (typeof o.unixPermissions === "string") { o.unixPermissions = parseInt(o.unixPermissions, 8); } // UNX_IFDIR 0040000 see zipinfo.c if (o.unixPermissions && (o.unixPermissions & 0x4000)) { o.dir = true; } // Bit 4 Directory if (o.dosPermissions && (o.dosPermissions & 0x0010)) { o.dir = true; } if (o.dir) { name = forceTrailingSlash(name); } if (o.createFolders && (parent = parentFolder(name))) { folderAdd.call(this, parent, true); } var isUnicodeString = dataType === "string" && o.binary === false && o.base64 === false; if (!originalOptions || typeof originalOptions.binary === "undefined") { o.binary = !isUnicodeString; } var isCompressedEmpty = (data instanceof CompressedObject) && data.uncompressedSize === 0; if (isCompressedEmpty || o.dir || !data || data.length === 0) { o.base64 = false; o.binary = true; data = ""; o.compression = "STORE"; dataType = "string"; } /* * Convert content to fit. */ var zipObjectContent = null; if (data instanceof CompressedObject || data instanceof GenericWorker) { zipObjectContent = data; } else if (nodejsUtils.isNode && nodejsUtils.isStream(data)) { zipObjectContent = new NodejsStreamInputAdapter(name, data); } else { zipObjectContent = utils.prepareContent(name, data, o.binary, o.optimizedBinaryString, o.base64); } var object = new ZipObject(name, zipObjectContent, o); this.files[name] = object; /* TODO: we can't throw an exception because we have async promises (we can have a promise of a Date() for example) but returning a promise is useless because file(name, data) returns the JSZip object for chaining. Should we break that to allow the user to catch the error ? return external.Promise.resolve(zipObjectContent) .then(function () { return object; }); */ }; /** * Find the parent folder of the path. * @private * @param {string} path the path to use * @return {string} the parent folder, or "" */ var parentFolder = function (path) { if (path.slice(-1) === "/") { path = path.substring(0, path.length - 1); } var lastSlash = path.lastIndexOf("/"); return (lastSlash > 0) ? path.substring(0, lastSlash) : ""; }; /** * Returns the path with a slash at the end. * @private * @param {String} path the path to check. * @return {String} the path with a trailing slash. */ var forceTrailingSlash = function(path) { // Check the name ends with a / if (path.slice(-1) !== "/") { path += "/"; // IE doesn't like substr(-1) } return path; }; /** * Add a (sub) folder in the current folder. * @private * @param {string} name the folder's name * @param {boolean=} [createFolders] If true, automatically create sub * folders. Defaults to false. * @return {Object} the new folder. */ var folderAdd = function(name, createFolders) { createFolders = (typeof createFolders !== "undefined") ? createFolders : defaults.createFolders; name = forceTrailingSlash(name); // Does this folder already exist? if (!this.files[name]) { fileAdd.call(this, name, null, { dir: true, createFolders: createFolders }); } return this.files[name]; }; /** * Cross-window, cross-Node-context regular expression detection * @param {Object} object Anything * @return {Boolean} true if the object is a regular expression, * false otherwise */ function isRegExp(object) { return Object.prototype.toString.call(object) === "[object RegExp]"; } // return the actual prototype of JSZip var out = { /** * @see loadAsync */ load: function() { throw new Error("This method has been removed in JSZip 3.0, please check the upgrade guide."); }, /** * Call a callback function for each entry at this folder level. * @param {Function} cb the callback function: * function (relativePath, file) {...} * It takes 2 arguments : the relative path and the file. */ forEach: function(cb) { var filename, relativePath, file; // ignore warning about unwanted properties because this.files is a null prototype object /* eslint-disable-next-line guard-for-in */ for (filename in this.files) { file = this.files[filename]; relativePath = filename.slice(this.root.length, filename.length); if (relativePath && filename.slice(0, this.root.length) === this.root) { // the file is in the current root cb(relativePath, file); // TODO reverse the parameters ? need to be clean AND consistent with the filter search fn... } } }, /** * Filter nested files/folders with the specified function. * @param {Function} search the predicate to use : * function (relativePath, file) {...} * It takes 2 arguments : the relative path and the file. * @return {Array} An array of matching elements. */ filter: function(search) { var result = []; this.forEach(function (relativePath, entry) { if (search(relativePath, entry)) { // the file matches the function result.push(entry); } }); return result; }, /** * Add a file to the zip file, or search a file. * @param {string|RegExp} name The name of the file to add (if data is defined), * the name of the file to find (if no data) or a regex to match files. * @param {String|ArrayBuffer|Uint8Array|Buffer} data The file data, either raw or base64 encoded * @param {Object} o File options * @return {JSZip|Object|Array} this JSZip object (when adding a file), * a file (when searching by string) or an array of files (when searching by regex). */ file: function(name, data, o) { if (arguments.length === 1) { if (isRegExp(name)) { var regexp = name; return this.filter(function(relativePath, file) { return !file.dir && regexp.test(relativePath); }); } else { // text var obj = this.files[this.root + name]; if (obj && !obj.dir) { return obj; } else { return null; } } } else { // more than one argument : we have data ! name = this.root + name; fileAdd.call(this, name, data, o); } return this; }, /** * Add a directory to the zip file, or search. * @param {String|RegExp} arg The name of the directory to add, or a regex to search folders. * @return {JSZip} an object with the new directory as the root, or an array containing matching folders. */ folder: function(arg) { if (!arg) { return this; } if (isRegExp(arg)) { return this.filter(function(relativePath, file) { return file.dir && arg.test(relativePath); }); } // else, name is a new folder var name = this.root + arg; var newFolder = folderAdd.call(this, name); // Allow chaining by returning a new object with this folder as the root var ret = this.clone(); ret.root = newFolder.name; return ret; }, /** * Delete a file, or a directory and all sub-files, from the zip * @param {string} name the name of the file to delete * @return {JSZip} this JSZip object */ remove: function(name) { name = this.root + name; var file = this.files[name]; if (!file) { // Look for any folders if (name.slice(-1) !== "/") { name += "/"; } file = this.files[name]; } if (file && !file.dir) { // file delete this.files[name]; } else { // maybe a folder, delete recursively var kids = this.filter(function(relativePath, file) { return file.name.slice(0, name.length) === name; }); for (var i = 0; i < kids.length; i++) { delete this.files[kids[i].name]; } } return this; }, /** * @deprecated This method has been removed in JSZip 3.0, please check the upgrade guide. */ generate: function() { throw new Error("This method has been removed in JSZip 3.0, please check the upgrade guide."); }, /** * Generate the complete zip file as an internal stream. * @param {Object} options the options to generate the zip file : * - compression, "STORE" by default. * - type, "base64" by default. Values are : string, base64, uint8array, arraybuffer, blob. * @return {StreamHelper} the streamed zip file. */ generateInternalStream: function(options) { var worker, opts = {}; try { opts = utils.extend(options || {}, { streamFiles: false, compression: "STORE", compressionOptions : null, type: "", platform: "DOS", comment: null, mimeType: "application/zip", encodeFileName: utf8.utf8encode }); opts.type = opts.type.toLowerCase(); opts.compression = opts.compression.toUpperCase(); // "binarystring" is preferred but the internals use "string". if(opts.type === "binarystring") { opts.type = "string"; } if (!opts.type) { throw new Error("No output type specified."); } utils.checkSupport(opts.type); // accept nodejs `process.platform` if( opts.platform === "darwin" || opts.platform === "freebsd" || opts.platform === "linux" || opts.platform === "sunos" ) { opts.platform = "UNIX"; } if (opts.platform === "win32") { opts.platform = "DOS"; } var comment = opts.comment || this.comment || ""; worker = generate.generateWorker(this, opts, comment); } catch (e) { worker = new GenericWorker("error"); worker.error(e); } return new StreamHelper(worker, opts.type || "string", opts.mimeType); }, /** * Generate the complete zip file asynchronously. * @see generateInternalStream */ generateAsync: function(options, onUpdate) { return this.generateInternalStream(options).accumulate(onUpdate); }, /** * Generate the complete zip file asynchronously. * @see generateInternalStream */ generateNodeStream: function(options, onUpdate) { options = options || {}; if (!options.type) { options.type = "nodebuffer"; } return this.generateInternalStream(options).toNodejsStream(onUpdate); } }; module.exports = out; ================================================ FILE: lib/readable-stream-browser.js ================================================ "use strict"; /* * This file is used by module bundlers (browserify/webpack/etc) when * including a stream implementation. We use "readable-stream" to get a * consistent behavior between nodejs versions but bundlers often have a shim * for "stream". Using this shim greatly improve the compatibility and greatly * reduce the final size of the bundle (only one stream implementation, not * two). */ module.exports = require("stream"); ================================================ FILE: lib/reader/ArrayReader.js ================================================ "use strict"; var DataReader = require("./DataReader"); var utils = require("../utils"); function ArrayReader(data) { DataReader.call(this, data); for(var i = 0; i < this.data.length; i++) { data[i] = data[i] & 0xFF; } } utils.inherits(ArrayReader, DataReader); /** * @see DataReader.byteAt */ ArrayReader.prototype.byteAt = function(i) { return this.data[this.zero + i]; }; /** * @see DataReader.lastIndexOfSignature */ ArrayReader.prototype.lastIndexOfSignature = function(sig) { var sig0 = sig.charCodeAt(0), sig1 = sig.charCodeAt(1), sig2 = sig.charCodeAt(2), sig3 = sig.charCodeAt(3); for (var i = this.length - 4; i >= 0; --i) { if (this.data[i] === sig0 && this.data[i + 1] === sig1 && this.data[i + 2] === sig2 && this.data[i + 3] === sig3) { return i - this.zero; } } return -1; }; /** * @see DataReader.readAndCheckSignature */ ArrayReader.prototype.readAndCheckSignature = function (sig) { var sig0 = sig.charCodeAt(0), sig1 = sig.charCodeAt(1), sig2 = sig.charCodeAt(2), sig3 = sig.charCodeAt(3), data = this.readData(4); return sig0 === data[0] && sig1 === data[1] && sig2 === data[2] && sig3 === data[3]; }; /** * @see DataReader.readData */ ArrayReader.prototype.readData = function(size) { this.checkOffset(size); if(size === 0) { return []; } var result = this.data.slice(this.zero + this.index, this.zero + this.index + size); this.index += size; return result; }; module.exports = ArrayReader; ================================================ FILE: lib/reader/DataReader.js ================================================ "use strict"; var utils = require("../utils"); function DataReader(data) { this.data = data; // type : see implementation this.length = data.length; this.index = 0; this.zero = 0; } DataReader.prototype = { /** * Check that the offset will not go too far. * @param {string} offset the additional offset to check. * @throws {Error} an Error if the offset is out of bounds. */ checkOffset: function(offset) { this.checkIndex(this.index + offset); }, /** * Check that the specified index will not be too far. * @param {string} newIndex the index to check. * @throws {Error} an Error if the index is out of bounds. */ checkIndex: function(newIndex) { if (this.length < this.zero + newIndex || newIndex < 0) { throw new Error("End of data reached (data length = " + this.length + ", asked index = " + (newIndex) + "). Corrupted zip ?"); } }, /** * Change the index. * @param {number} newIndex The new index. * @throws {Error} if the new index is out of the data. */ setIndex: function(newIndex) { this.checkIndex(newIndex); this.index = newIndex; }, /** * Skip the next n bytes. * @param {number} n the number of bytes to skip. * @throws {Error} if the new index is out of the data. */ skip: function(n) { this.setIndex(this.index + n); }, /** * Get the byte at the specified index. * @param {number} i the index to use. * @return {number} a byte. */ byteAt: function() { // see implementations }, /** * Get the next number with a given byte size. * @param {number} size the number of bytes to read. * @return {number} the corresponding number. */ readInt: function(size) { var result = 0, i; this.checkOffset(size); for (i = this.index + size - 1; i >= this.index; i--) { result = (result << 8) + this.byteAt(i); } this.index += size; return result; }, /** * Get the next string with a given byte size. * @param {number} size the number of bytes to read. * @return {string} the corresponding string. */ readString: function(size) { return utils.transformTo("string", this.readData(size)); }, /** * Get raw data without conversion, bytes. * @param {number} size the number of bytes to read. * @return {Object} the raw data, implementation specific. */ readData: function() { // see implementations }, /** * Find the last occurrence of a zip signature (4 bytes). * @param {string} sig the signature to find. * @return {number} the index of the last occurrence, -1 if not found. */ lastIndexOfSignature: function() { // see implementations }, /** * Read the signature (4 bytes) at the current position and compare it with sig. * @param {string} sig the expected signature * @return {boolean} true if the signature matches, false otherwise. */ readAndCheckSignature: function() { // see implementations }, /** * Get the next date. * @return {Date} the date. */ readDate: function() { var dostime = this.readInt(4); return new Date(Date.UTC( ((dostime >> 25) & 0x7f) + 1980, // year ((dostime >> 21) & 0x0f) - 1, // month (dostime >> 16) & 0x1f, // day (dostime >> 11) & 0x1f, // hour (dostime >> 5) & 0x3f, // minute (dostime & 0x1f) << 1)); // second } }; module.exports = DataReader; ================================================ FILE: lib/reader/NodeBufferReader.js ================================================ "use strict"; var Uint8ArrayReader = require("./Uint8ArrayReader"); var utils = require("../utils"); function NodeBufferReader(data) { Uint8ArrayReader.call(this, data); } utils.inherits(NodeBufferReader, Uint8ArrayReader); /** * @see DataReader.readData */ NodeBufferReader.prototype.readData = function(size) { this.checkOffset(size); var result = this.data.slice(this.zero + this.index, this.zero + this.index + size); this.index += size; return result; }; module.exports = NodeBufferReader; ================================================ FILE: lib/reader/StringReader.js ================================================ "use strict"; var DataReader = require("./DataReader"); var utils = require("../utils"); function StringReader(data) { DataReader.call(this, data); } utils.inherits(StringReader, DataReader); /** * @see DataReader.byteAt */ StringReader.prototype.byteAt = function(i) { return this.data.charCodeAt(this.zero + i); }; /** * @see DataReader.lastIndexOfSignature */ StringReader.prototype.lastIndexOfSignature = function(sig) { return this.data.lastIndexOf(sig) - this.zero; }; /** * @see DataReader.readAndCheckSignature */ StringReader.prototype.readAndCheckSignature = function (sig) { var data = this.readData(4); return sig === data; }; /** * @see DataReader.readData */ StringReader.prototype.readData = function(size) { this.checkOffset(size); // this will work because the constructor applied the "& 0xff" mask. var result = this.data.slice(this.zero + this.index, this.zero + this.index + size); this.index += size; return result; }; module.exports = StringReader; ================================================ FILE: lib/reader/Uint8ArrayReader.js ================================================ "use strict"; var ArrayReader = require("./ArrayReader"); var utils = require("../utils"); function Uint8ArrayReader(data) { ArrayReader.call(this, data); } utils.inherits(Uint8ArrayReader, ArrayReader); /** * @see DataReader.readData */ Uint8ArrayReader.prototype.readData = function(size) { this.checkOffset(size); if(size === 0) { // in IE10, when using subarray(idx, idx), we get the array [0x00] instead of []. return new Uint8Array(0); } var result = this.data.subarray(this.zero + this.index, this.zero + this.index + size); this.index += size; return result; }; module.exports = Uint8ArrayReader; ================================================ FILE: lib/reader/readerFor.js ================================================ "use strict"; var utils = require("../utils"); var support = require("../support"); var ArrayReader = require("./ArrayReader"); var StringReader = require("./StringReader"); var NodeBufferReader = require("./NodeBufferReader"); var Uint8ArrayReader = require("./Uint8ArrayReader"); /** * Create a reader adapted to the data. * @param {String|ArrayBuffer|Uint8Array|Buffer} data the data to read. * @return {DataReader} the data reader. */ module.exports = function (data) { var type = utils.getTypeOf(data); utils.checkSupport(type); if (type === "string" && !support.uint8array) { return new StringReader(data); } if (type === "nodebuffer") { return new NodeBufferReader(data); } if (support.uint8array) { return new Uint8ArrayReader(utils.transformTo("uint8array", data)); } return new ArrayReader(utils.transformTo("array", data)); }; ================================================ FILE: lib/signature.js ================================================ "use strict"; exports.LOCAL_FILE_HEADER = "PK\x03\x04"; exports.CENTRAL_FILE_HEADER = "PK\x01\x02"; exports.CENTRAL_DIRECTORY_END = "PK\x05\x06"; exports.ZIP64_CENTRAL_DIRECTORY_LOCATOR = "PK\x06\x07"; exports.ZIP64_CENTRAL_DIRECTORY_END = "PK\x06\x06"; exports.DATA_DESCRIPTOR = "PK\x07\x08"; ================================================ FILE: lib/stream/ConvertWorker.js ================================================ "use strict"; var GenericWorker = require("./GenericWorker"); var utils = require("../utils"); /** * A worker which convert chunks to a specified type. * @constructor * @param {String} destType the destination type. */ function ConvertWorker(destType) { GenericWorker.call(this, "ConvertWorker to " + destType); this.destType = destType; } utils.inherits(ConvertWorker, GenericWorker); /** * @see GenericWorker.processChunk */ ConvertWorker.prototype.processChunk = function (chunk) { this.push({ data : utils.transformTo(this.destType, chunk.data), meta : chunk.meta }); }; module.exports = ConvertWorker; ================================================ FILE: lib/stream/Crc32Probe.js ================================================ "use strict"; var GenericWorker = require("./GenericWorker"); var crc32 = require("../crc32"); var utils = require("../utils"); /** * A worker which calculate the crc32 of the data flowing through. * @constructor */ function Crc32Probe() { GenericWorker.call(this, "Crc32Probe"); this.withStreamInfo("crc32", 0); } utils.inherits(Crc32Probe, GenericWorker); /** * @see GenericWorker.processChunk */ Crc32Probe.prototype.processChunk = function (chunk) { this.streamInfo.crc32 = crc32(chunk.data, this.streamInfo.crc32 || 0); this.push(chunk); }; module.exports = Crc32Probe; ================================================ FILE: lib/stream/DataLengthProbe.js ================================================ "use strict"; var utils = require("../utils"); var GenericWorker = require("./GenericWorker"); /** * A worker which calculate the total length of the data flowing through. * @constructor * @param {String} propName the name used to expose the length */ function DataLengthProbe(propName) { GenericWorker.call(this, "DataLengthProbe for " + propName); this.propName = propName; this.withStreamInfo(propName, 0); } utils.inherits(DataLengthProbe, GenericWorker); /** * @see GenericWorker.processChunk */ DataLengthProbe.prototype.processChunk = function (chunk) { if(chunk) { var length = this.streamInfo[this.propName] || 0; this.streamInfo[this.propName] = length + chunk.data.length; } GenericWorker.prototype.processChunk.call(this, chunk); }; module.exports = DataLengthProbe; ================================================ FILE: lib/stream/DataWorker.js ================================================ "use strict"; var utils = require("../utils"); var GenericWorker = require("./GenericWorker"); // the size of the generated chunks // TODO expose this as a public variable var DEFAULT_BLOCK_SIZE = 16 * 1024; /** * A worker that reads a content and emits chunks. * @constructor * @param {Promise} dataP the promise of the data to split */ function DataWorker(dataP) { GenericWorker.call(this, "DataWorker"); var self = this; this.dataIsReady = false; this.index = 0; this.max = 0; this.data = null; this.type = ""; this._tickScheduled = false; dataP.then(function (data) { self.dataIsReady = true; self.data = data; self.max = data && data.length || 0; self.type = utils.getTypeOf(data); if(!self.isPaused) { self._tickAndRepeat(); } }, function (e) { self.error(e); }); } utils.inherits(DataWorker, GenericWorker); /** * @see GenericWorker.cleanUp */ DataWorker.prototype.cleanUp = function () { GenericWorker.prototype.cleanUp.call(this); this.data = null; }; /** * @see GenericWorker.resume */ DataWorker.prototype.resume = function () { if(!GenericWorker.prototype.resume.call(this)) { return false; } if (!this._tickScheduled && this.dataIsReady) { this._tickScheduled = true; utils.delay(this._tickAndRepeat, [], this); } return true; }; /** * Trigger a tick a schedule an other call to this function. */ DataWorker.prototype._tickAndRepeat = function() { this._tickScheduled = false; if(this.isPaused || this.isFinished) { return; } this._tick(); if(!this.isFinished) { utils.delay(this._tickAndRepeat, [], this); this._tickScheduled = true; } }; /** * Read and push a chunk. */ DataWorker.prototype._tick = function() { if(this.isPaused || this.isFinished) { return false; } var size = DEFAULT_BLOCK_SIZE; var data = null, nextIndex = Math.min(this.max, this.index + size); if (this.index >= this.max) { // EOF return this.end(); } else { switch(this.type) { case "string": data = this.data.substring(this.index, nextIndex); break; case "uint8array": data = this.data.subarray(this.index, nextIndex); break; case "array": case "nodebuffer": data = this.data.slice(this.index, nextIndex); break; } this.index = nextIndex; return this.push({ data : data, meta : { percent : this.max ? this.index / this.max * 100 : 0 } }); } }; module.exports = DataWorker; ================================================ FILE: lib/stream/GenericWorker.js ================================================ "use strict"; /** * A worker that does nothing but passing chunks to the next one. This is like * a nodejs stream but with some differences. On the good side : * - it works on IE 6-9 without any issue / polyfill * - it weights less than the full dependencies bundled with browserify * - it forwards errors (no need to declare an error handler EVERYWHERE) * * A chunk is an object with 2 attributes : `meta` and `data`. The former is an * object containing anything (`percent` for example), see each worker for more * details. The latter is the real data (String, Uint8Array, etc). * * @constructor * @param {String} name the name of the stream (mainly used for debugging purposes) */ function GenericWorker(name) { // the name of the worker this.name = name || "default"; // an object containing metadata about the workers chain this.streamInfo = {}; // an error which happened when the worker was paused this.generatedError = null; // an object containing metadata to be merged by this worker into the general metadata this.extraStreamInfo = {}; // true if the stream is paused (and should not do anything), false otherwise this.isPaused = true; // true if the stream is finished (and should not do anything), false otherwise this.isFinished = false; // true if the stream is locked to prevent further structure updates (pipe), false otherwise this.isLocked = false; // the event listeners this._listeners = { "data":[], "end":[], "error":[] }; // the previous worker, if any this.previous = null; } GenericWorker.prototype = { /** * Push a chunk to the next workers. * @param {Object} chunk the chunk to push */ push : function (chunk) { this.emit("data", chunk); }, /** * End the stream. * @return {Boolean} true if this call ended the worker, false otherwise. */ end : function () { if (this.isFinished) { return false; } this.flush(); try { this.emit("end"); this.cleanUp(); this.isFinished = true; } catch (e) { this.emit("error", e); } return true; }, /** * End the stream with an error. * @param {Error} e the error which caused the premature end. * @return {Boolean} true if this call ended the worker with an error, false otherwise. */ error : function (e) { if (this.isFinished) { return false; } if(this.isPaused) { this.generatedError = e; } else { this.isFinished = true; this.emit("error", e); // in the workers chain exploded in the middle of the chain, // the error event will go downward but we also need to notify // workers upward that there has been an error. if(this.previous) { this.previous.error(e); } this.cleanUp(); } return true; }, /** * Add a callback on an event. * @param {String} name the name of the event (data, end, error) * @param {Function} listener the function to call when the event is triggered * @return {GenericWorker} the current object for chainability */ on : function (name, listener) { this._listeners[name].push(listener); return this; }, /** * Clean any references when a worker is ending. */ cleanUp : function () { this.streamInfo = this.generatedError = this.extraStreamInfo = null; this._listeners = []; }, /** * Trigger an event. This will call registered callback with the provided arg. * @param {String} name the name of the event (data, end, error) * @param {Object} arg the argument to call the callback with. */ emit : function (name, arg) { if (this._listeners[name]) { for(var i = 0; i < this._listeners[name].length; i++) { this._listeners[name][i].call(this, arg); } } }, /** * Chain a worker with an other. * @param {Worker} next the worker receiving events from the current one. * @return {worker} the next worker for chainability */ pipe : function (next) { return next.registerPrevious(this); }, /** * Same as `pipe` in the other direction. * Using an API with `pipe(next)` is very easy. * Implementing the API with the point of view of the next one registering * a source is easier, see the ZipFileWorker. * @param {Worker} previous the previous worker, sending events to this one * @return {Worker} the current worker for chainability */ registerPrevious : function (previous) { if (this.isLocked) { throw new Error("The stream '" + this + "' has already been used."); } // sharing the streamInfo... this.streamInfo = previous.streamInfo; // ... and adding our own bits this.mergeStreamInfo(); this.previous = previous; var self = this; previous.on("data", function (chunk) { self.processChunk(chunk); }); previous.on("end", function () { self.end(); }); previous.on("error", function (e) { self.error(e); }); return this; }, /** * Pause the stream so it doesn't send events anymore. * @return {Boolean} true if this call paused the worker, false otherwise. */ pause : function () { if(this.isPaused || this.isFinished) { return false; } this.isPaused = true; if(this.previous) { this.previous.pause(); } return true; }, /** * Resume a paused stream. * @return {Boolean} true if this call resumed the worker, false otherwise. */ resume : function () { if(!this.isPaused || this.isFinished) { return false; } this.isPaused = false; // if true, the worker tried to resume but failed var withError = false; if(this.generatedError) { this.error(this.generatedError); withError = true; } if(this.previous) { this.previous.resume(); } return !withError; }, /** * Flush any remaining bytes as the stream is ending. */ flush : function () {}, /** * Process a chunk. This is usually the method overridden. * @param {Object} chunk the chunk to process. */ processChunk : function(chunk) { this.push(chunk); }, /** * Add a key/value to be added in the workers chain streamInfo once activated. * @param {String} key the key to use * @param {Object} value the associated value * @return {Worker} the current worker for chainability */ withStreamInfo : function (key, value) { this.extraStreamInfo[key] = value; this.mergeStreamInfo(); return this; }, /** * Merge this worker's streamInfo into the chain's streamInfo. */ mergeStreamInfo : function () { for(var key in this.extraStreamInfo) { if (!Object.prototype.hasOwnProperty.call(this.extraStreamInfo, key)) { continue; } this.streamInfo[key] = this.extraStreamInfo[key]; } }, /** * Lock the stream to prevent further updates on the workers chain. * After calling this method, all calls to pipe will fail. */ lock: function () { if (this.isLocked) { throw new Error("The stream '" + this + "' has already been used."); } this.isLocked = true; if (this.previous) { this.previous.lock(); } }, /** * * Pretty print the workers chain. */ toString : function () { var me = "Worker " + this.name; if (this.previous) { return this.previous + " -> " + me; } else { return me; } } }; module.exports = GenericWorker; ================================================ FILE: lib/stream/StreamHelper.js ================================================ "use strict"; var utils = require("../utils"); var ConvertWorker = require("./ConvertWorker"); var GenericWorker = require("./GenericWorker"); var base64 = require("../base64"); var support = require("../support"); var external = require("../external"); var NodejsStreamOutputAdapter = null; if (support.nodestream) { try { NodejsStreamOutputAdapter = require("../nodejs/NodejsStreamOutputAdapter"); } catch(e) { // ignore } } /** * Apply the final transformation of the data. If the user wants a Blob for * example, it's easier to work with an U8intArray and finally do the * ArrayBuffer/Blob conversion. * @param {String} type the name of the final type * @param {String|Uint8Array|Buffer} content the content to transform * @param {String} mimeType the mime type of the content, if applicable. * @return {String|Uint8Array|ArrayBuffer|Buffer|Blob} the content in the right format. */ function transformZipOutput(type, content, mimeType) { switch(type) { case "blob" : return utils.newBlob(utils.transformTo("arraybuffer", content), mimeType); case "base64" : return base64.encode(content); default : return utils.transformTo(type, content); } } /** * Concatenate an array of data of the given type. * @param {String} type the type of the data in the given array. * @param {Array} dataArray the array containing the data chunks to concatenate * @return {String|Uint8Array|Buffer} the concatenated data * @throws Error if the asked type is unsupported */ function concat (type, dataArray) { var i, index = 0, res = null, totalLength = 0; for(i = 0; i < dataArray.length; i++) { totalLength += dataArray[i].length; } switch(type) { case "string": return dataArray.join(""); case "array": return Array.prototype.concat.apply([], dataArray); case "uint8array": res = new Uint8Array(totalLength); for(i = 0; i < dataArray.length; i++) { res.set(dataArray[i], index); index += dataArray[i].length; } return res; case "nodebuffer": return Buffer.concat(dataArray); default: throw new Error("concat : unsupported type '" + type + "'"); } } /** * Listen a StreamHelper, accumulate its content and concatenate it into a * complete block. * @param {StreamHelper} helper the helper to use. * @param {Function} updateCallback a callback called on each update. Called * with one arg : * - the metadata linked to the update received. * @return Promise the promise for the accumulation. */ function accumulate(helper, updateCallback) { return new external.Promise(function (resolve, reject){ var dataArray = []; var chunkType = helper._internalType, resultType = helper._outputType, mimeType = helper._mimeType; helper .on("data", function (data, meta) { dataArray.push(data); if(updateCallback) { updateCallback(meta); } }) .on("error", function(err) { dataArray = []; reject(err); }) .on("end", function (){ try { var result = transformZipOutput(resultType, concat(chunkType, dataArray), mimeType); resolve(result); } catch (e) { reject(e); } dataArray = []; }) .resume(); }); } /** * An helper to easily use workers outside of JSZip. * @constructor * @param {Worker} worker the worker to wrap * @param {String} outputType the type of data expected by the use * @param {String} mimeType the mime type of the content, if applicable. */ function StreamHelper(worker, outputType, mimeType) { var internalType = outputType; switch(outputType) { case "blob": case "arraybuffer": internalType = "uint8array"; break; case "base64": internalType = "string"; break; } try { // the type used internally this._internalType = internalType; // the type used to output results this._outputType = outputType; // the mime type this._mimeType = mimeType; utils.checkSupport(internalType); this._worker = worker.pipe(new ConvertWorker(internalType)); // the last workers can be rewired without issues but we need to // prevent any updates on previous workers. worker.lock(); } catch(e) { this._worker = new GenericWorker("error"); this._worker.error(e); } } StreamHelper.prototype = { /** * Listen a StreamHelper, accumulate its content and concatenate it into a * complete block. * @param {Function} updateCb the update callback. * @return Promise the promise for the accumulation. */ accumulate : function (updateCb) { return accumulate(this, updateCb); }, /** * Add a listener on an event triggered on a stream. * @param {String} evt the name of the event * @param {Function} fn the listener * @return {StreamHelper} the current helper. */ on : function (evt, fn) { var self = this; if(evt === "data") { this._worker.on(evt, function (chunk) { fn.call(self, chunk.data, chunk.meta); }); } else { this._worker.on(evt, function () { utils.delay(fn, arguments, self); }); } return this; }, /** * Resume the flow of chunks. * @return {StreamHelper} the current helper. */ resume : function () { utils.delay(this._worker.resume, [], this._worker); return this; }, /** * Pause the flow of chunks. * @return {StreamHelper} the current helper. */ pause : function () { this._worker.pause(); return this; }, /** * Return a nodejs stream for this helper. * @param {Function} updateCb the update callback. * @return {NodejsStreamOutputAdapter} the nodejs stream. */ toNodejsStream : function (updateCb) { utils.checkSupport("nodestream"); if (this._outputType !== "nodebuffer") { // an object stream containing blob/arraybuffer/uint8array/string // is strange and I don't know if it would be useful. // I you find this comment and have a good usecase, please open a // bug report ! throw new Error(this._outputType + " is not supported by this method"); } return new NodejsStreamOutputAdapter(this, { objectMode : this._outputType !== "nodebuffer" }, updateCb); } }; module.exports = StreamHelper; ================================================ FILE: lib/support.js ================================================ "use strict"; exports.base64 = true; exports.array = true; exports.string = true; exports.arraybuffer = typeof ArrayBuffer !== "undefined" && typeof Uint8Array !== "undefined"; exports.nodebuffer = typeof Buffer !== "undefined"; // contains true if JSZip can read/generate Uint8Array, false otherwise. exports.uint8array = typeof Uint8Array !== "undefined"; if (typeof ArrayBuffer === "undefined") { exports.blob = false; } else { var buffer = new ArrayBuffer(0); try { exports.blob = new Blob([buffer], { type: "application/zip" }).size === 0; } catch (e) { try { var Builder = self.BlobBuilder || self.WebKitBlobBuilder || self.MozBlobBuilder || self.MSBlobBuilder; var builder = new Builder(); builder.append(buffer); exports.blob = builder.getBlob("application/zip").size === 0; } catch (e) { exports.blob = false; } } } try { exports.nodestream = !!require("readable-stream").Readable; } catch(e) { exports.nodestream = false; } ================================================ FILE: lib/utf8.js ================================================ "use strict"; var utils = require("./utils"); var support = require("./support"); var nodejsUtils = require("./nodejsUtils"); var GenericWorker = require("./stream/GenericWorker"); /** * The following functions come from pako, from pako/lib/utils/strings * released under the MIT license, see pako https://github.com/nodeca/pako/ */ // Table with utf8 lengths (calculated by first byte of sequence) // Note, that 5 & 6-byte values and some 4-byte values can not be represented in JS, // because max possible codepoint is 0x10ffff var _utf8len = new Array(256); for (var i=0; i<256; i++) { _utf8len[i] = (i >= 252 ? 6 : i >= 248 ? 5 : i >= 240 ? 4 : i >= 224 ? 3 : i >= 192 ? 2 : 1); } _utf8len[254]=_utf8len[254]=1; // Invalid sequence start // convert string to array (typed, when possible) var string2buf = function (str) { var buf, c, c2, m_pos, i, str_len = str.length, buf_len = 0; // count binary size for (m_pos = 0; m_pos < str_len; m_pos++) { c = str.charCodeAt(m_pos); if ((c & 0xfc00) === 0xd800 && (m_pos+1 < str_len)) { c2 = str.charCodeAt(m_pos+1); if ((c2 & 0xfc00) === 0xdc00) { c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00); m_pos++; } } buf_len += c < 0x80 ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4; } // allocate buffer if (support.uint8array) { buf = new Uint8Array(buf_len); } else { buf = new Array(buf_len); } // convert for (i=0, m_pos = 0; i < buf_len; m_pos++) { c = str.charCodeAt(m_pos); if ((c & 0xfc00) === 0xd800 && (m_pos+1 < str_len)) { c2 = str.charCodeAt(m_pos+1); if ((c2 & 0xfc00) === 0xdc00) { c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00); m_pos++; } } if (c < 0x80) { /* one byte */ buf[i++] = c; } else if (c < 0x800) { /* two bytes */ buf[i++] = 0xC0 | (c >>> 6); buf[i++] = 0x80 | (c & 0x3f); } else if (c < 0x10000) { /* three bytes */ buf[i++] = 0xE0 | (c >>> 12); buf[i++] = 0x80 | (c >>> 6 & 0x3f); buf[i++] = 0x80 | (c & 0x3f); } else { /* four bytes */ buf[i++] = 0xf0 | (c >>> 18); buf[i++] = 0x80 | (c >>> 12 & 0x3f); buf[i++] = 0x80 | (c >>> 6 & 0x3f); buf[i++] = 0x80 | (c & 0x3f); } } return buf; }; // Calculate max possible position in utf8 buffer, // that will not break sequence. If that's not possible // - (very small limits) return max size as is. // // buf[] - utf8 bytes array // max - length limit (mandatory); var utf8border = function(buf, max) { var pos; max = max || buf.length; if (max > buf.length) { max = buf.length; } // go back from last position, until start of sequence found pos = max-1; while (pos >= 0 && (buf[pos] & 0xC0) === 0x80) { pos--; } // Fuckup - very small and broken sequence, // return max, because we should return something anyway. if (pos < 0) { return max; } // If we came to start of buffer - that means vuffer is too small, // return max too. if (pos === 0) { return max; } return (pos + _utf8len[buf[pos]] > max) ? pos : max; }; // convert array to string var buf2string = function (buf) { var i, out, c, c_len; var len = buf.length; // Reserve max possible length (2 words per char) // NB: by unknown reasons, Array is significantly faster for // String.fromCharCode.apply than Uint16Array. var utf16buf = new Array(len*2); for (out=0, i=0; i 4) { utf16buf[out++] = 0xfffd; i += c_len-1; continue; } // apply mask on first byte c &= c_len === 2 ? 0x1f : c_len === 3 ? 0x0f : 0x07; // join the rest while (c_len > 1 && i < len) { c = (c << 6) | (buf[i++] & 0x3f); c_len--; } // terminated by end of string? if (c_len > 1) { utf16buf[out++] = 0xfffd; continue; } if (c < 0x10000) { utf16buf[out++] = c; } else { c -= 0x10000; utf16buf[out++] = 0xd800 | ((c >> 10) & 0x3ff); utf16buf[out++] = 0xdc00 | (c & 0x3ff); } } // shrinkBuf(utf16buf, out) if (utf16buf.length !== out) { if(utf16buf.subarray) { utf16buf = utf16buf.subarray(0, out); } else { utf16buf.length = out; } } // return String.fromCharCode.apply(null, utf16buf); return utils.applyFromCharCode(utf16buf); }; // That's all for the pako functions. /** * Transform a javascript string into an array (typed if possible) of bytes, * UTF-8 encoded. * @param {String} str the string to encode * @return {Array|Uint8Array|Buffer} the UTF-8 encoded string. */ exports.utf8encode = function utf8encode(str) { if (support.nodebuffer) { return nodejsUtils.newBufferFrom(str, "utf-8"); } return string2buf(str); }; /** * Transform a bytes array (or a representation) representing an UTF-8 encoded * string into a javascript string. * @param {Array|Uint8Array|Buffer} buf the data de decode * @return {String} the decoded string. */ exports.utf8decode = function utf8decode(buf) { if (support.nodebuffer) { return utils.transformTo("nodebuffer", buf).toString("utf-8"); } buf = utils.transformTo(support.uint8array ? "uint8array" : "array", buf); return buf2string(buf); }; /** * A worker to decode utf8 encoded binary chunks into string chunks. * @constructor */ function Utf8DecodeWorker() { GenericWorker.call(this, "utf-8 decode"); // the last bytes if a chunk didn't end with a complete codepoint. this.leftOver = null; } utils.inherits(Utf8DecodeWorker, GenericWorker); /** * @see GenericWorker.processChunk */ Utf8DecodeWorker.prototype.processChunk = function (chunk) { var data = utils.transformTo(support.uint8array ? "uint8array" : "array", chunk.data); // 1st step, re-use what's left of the previous chunk if (this.leftOver && this.leftOver.length) { if(support.uint8array) { var previousData = data; data = new Uint8Array(previousData.length + this.leftOver.length); data.set(this.leftOver, 0); data.set(previousData, this.leftOver.length); } else { data = this.leftOver.concat(data); } this.leftOver = null; } var nextBoundary = utf8border(data); var usableData = data; if (nextBoundary !== data.length) { if (support.uint8array) { usableData = data.subarray(0, nextBoundary); this.leftOver = data.subarray(nextBoundary, data.length); } else { usableData = data.slice(0, nextBoundary); this.leftOver = data.slice(nextBoundary, data.length); } } this.push({ data : exports.utf8decode(usableData), meta : chunk.meta }); }; /** * @see GenericWorker.flush */ Utf8DecodeWorker.prototype.flush = function () { if(this.leftOver && this.leftOver.length) { this.push({ data : exports.utf8decode(this.leftOver), meta : {} }); this.leftOver = null; } }; exports.Utf8DecodeWorker = Utf8DecodeWorker; /** * A worker to endcode string chunks into utf8 encoded binary chunks. * @constructor */ function Utf8EncodeWorker() { GenericWorker.call(this, "utf-8 encode"); } utils.inherits(Utf8EncodeWorker, GenericWorker); /** * @see GenericWorker.processChunk */ Utf8EncodeWorker.prototype.processChunk = function (chunk) { this.push({ data : exports.utf8encode(chunk.data), meta : chunk.meta }); }; exports.Utf8EncodeWorker = Utf8EncodeWorker; ================================================ FILE: lib/utils.js ================================================ "use strict"; var support = require("./support"); var base64 = require("./base64"); var nodejsUtils = require("./nodejsUtils"); var external = require("./external"); require("setimmediate"); /** * Convert a string that pass as a "binary string": it should represent a byte * array but may have > 255 char codes. Be sure to take only the first byte * and returns the byte array. * @param {String} str the string to transform. * @return {Array|Uint8Array} the string in a binary format. */ function string2binary(str) { var result = null; if (support.uint8array) { result = new Uint8Array(str.length); } else { result = new Array(str.length); } return stringToArrayLike(str, result); } /** * Create a new blob with the given content and the given type. * @param {String|ArrayBuffer} part the content to put in the blob. DO NOT use * an Uint8Array because the stock browser of android 4 won't accept it (it * will be silently converted to a string, "[object Uint8Array]"). * * Use only ONE part to build the blob to avoid a memory leak in IE11 / Edge: * when a large amount of Array is used to create the Blob, the amount of * memory consumed is nearly 100 times the original data amount. * * @param {String} type the mime type of the blob. * @return {Blob} the created blob. */ exports.newBlob = function(part, type) { exports.checkSupport("blob"); try { // Blob constructor return new Blob([part], { type: type }); } catch (e) { try { // deprecated, browser only, old way var Builder = self.BlobBuilder || self.WebKitBlobBuilder || self.MozBlobBuilder || self.MSBlobBuilder; var builder = new Builder(); builder.append(part); return builder.getBlob(type); } catch (e) { // well, fuck ?! throw new Error("Bug : can't construct the Blob."); } } }; /** * The identity function. * @param {Object} input the input. * @return {Object} the same input. */ function identity(input) { return input; } /** * Fill in an array with a string. * @param {String} str the string to use. * @param {Array|ArrayBuffer|Uint8Array|Buffer} array the array to fill in (will be mutated). * @return {Array|ArrayBuffer|Uint8Array|Buffer} the updated array. */ function stringToArrayLike(str, array) { for (var i = 0; i < str.length; ++i) { array[i] = str.charCodeAt(i) & 0xFF; } return array; } /** * An helper for the function arrayLikeToString. * This contains static information and functions that * can be optimized by the browser JIT compiler. */ var arrayToStringHelper = { /** * Transform an array of int into a string, chunk by chunk. * See the performances notes on arrayLikeToString. * @param {Array|ArrayBuffer|Uint8Array|Buffer} array the array to transform. * @param {String} type the type of the array. * @param {Integer} chunk the chunk size. * @return {String} the resulting string. * @throws Error if the chunk is too big for the stack. */ stringifyByChunk: function(array, type, chunk) { var result = [], k = 0, len = array.length; // shortcut if (len <= chunk) { return String.fromCharCode.apply(null, array); } while (k < len) { if (type === "array" || type === "nodebuffer") { result.push(String.fromCharCode.apply(null, array.slice(k, Math.min(k + chunk, len)))); } else { result.push(String.fromCharCode.apply(null, array.subarray(k, Math.min(k + chunk, len)))); } k += chunk; } return result.join(""); }, /** * Call String.fromCharCode on every item in the array. * This is the naive implementation, which generate A LOT of intermediate string. * This should be used when everything else fail. * @param {Array|ArrayBuffer|Uint8Array|Buffer} array the array to transform. * @return {String} the result. */ stringifyByChar: function(array){ var resultStr = ""; for(var i = 0; i < array.length; i++) { resultStr += String.fromCharCode(array[i]); } return resultStr; }, applyCanBeUsed : { /** * true if the browser accepts to use String.fromCharCode on Uint8Array */ uint8array : (function () { try { return support.uint8array && String.fromCharCode.apply(null, new Uint8Array(1)).length === 1; } catch (e) { return false; } })(), /** * true if the browser accepts to use String.fromCharCode on nodejs Buffer. */ nodebuffer : (function () { try { return support.nodebuffer && String.fromCharCode.apply(null, nodejsUtils.allocBuffer(1)).length === 1; } catch (e) { return false; } })() } }; /** * Transform an array-like object to a string. * @param {Array|ArrayBuffer|Uint8Array|Buffer} array the array to transform. * @return {String} the result. */ function arrayLikeToString(array) { // Performances notes : // -------------------- // String.fromCharCode.apply(null, array) is the fastest, see // see http://jsperf.com/converting-a-uint8array-to-a-string/2 // but the stack is limited (and we can get huge arrays !). // // result += String.fromCharCode(array[i]); generate too many strings ! // // This code is inspired by http://jsperf.com/arraybuffer-to-string-apply-performance/2 // TODO : we now have workers that split the work. Do we still need that ? var chunk = 65536, type = exports.getTypeOf(array), canUseApply = true; if (type === "uint8array") { canUseApply = arrayToStringHelper.applyCanBeUsed.uint8array; } else if (type === "nodebuffer") { canUseApply = arrayToStringHelper.applyCanBeUsed.nodebuffer; } if (canUseApply) { while (chunk > 1) { try { return arrayToStringHelper.stringifyByChunk(array, type, chunk); } catch (e) { chunk = Math.floor(chunk / 2); } } } // no apply or chunk error : slow and painful algorithm // default browser on android 4.* return arrayToStringHelper.stringifyByChar(array); } exports.applyFromCharCode = arrayLikeToString; /** * Copy the data from an array-like to an other array-like. * @param {Array|ArrayBuffer|Uint8Array|Buffer} arrayFrom the origin array. * @param {Array|ArrayBuffer|Uint8Array|Buffer} arrayTo the destination array which will be mutated. * @return {Array|ArrayBuffer|Uint8Array|Buffer} the updated destination array. */ function arrayLikeToArrayLike(arrayFrom, arrayTo) { for (var i = 0; i < arrayFrom.length; i++) { arrayTo[i] = arrayFrom[i]; } return arrayTo; } // a matrix containing functions to transform everything into everything. var transform = {}; // string to ? transform["string"] = { "string": identity, "array": function(input) { return stringToArrayLike(input, new Array(input.length)); }, "arraybuffer": function(input) { return transform["string"]["uint8array"](input).buffer; }, "uint8array": function(input) { return stringToArrayLike(input, new Uint8Array(input.length)); }, "nodebuffer": function(input) { return stringToArrayLike(input, nodejsUtils.allocBuffer(input.length)); } }; // array to ? transform["array"] = { "string": arrayLikeToString, "array": identity, "arraybuffer": function(input) { return (new Uint8Array(input)).buffer; }, "uint8array": function(input) { return new Uint8Array(input); }, "nodebuffer": function(input) { return nodejsUtils.newBufferFrom(input); } }; // arraybuffer to ? transform["arraybuffer"] = { "string": function(input) { return arrayLikeToString(new Uint8Array(input)); }, "array": function(input) { return arrayLikeToArrayLike(new Uint8Array(input), new Array(input.byteLength)); }, "arraybuffer": identity, "uint8array": function(input) { return new Uint8Array(input); }, "nodebuffer": function(input) { return nodejsUtils.newBufferFrom(new Uint8Array(input)); } }; // uint8array to ? transform["uint8array"] = { "string": arrayLikeToString, "array": function(input) { return arrayLikeToArrayLike(input, new Array(input.length)); }, "arraybuffer": function(input) { return input.buffer; }, "uint8array": identity, "nodebuffer": function(input) { return nodejsUtils.newBufferFrom(input); } }; // nodebuffer to ? transform["nodebuffer"] = { "string": arrayLikeToString, "array": function(input) { return arrayLikeToArrayLike(input, new Array(input.length)); }, "arraybuffer": function(input) { return transform["nodebuffer"]["uint8array"](input).buffer; }, "uint8array": function(input) { return arrayLikeToArrayLike(input, new Uint8Array(input.length)); }, "nodebuffer": identity }; /** * Transform an input into any type. * The supported output type are : string, array, uint8array, arraybuffer, nodebuffer. * If no output type is specified, the unmodified input will be returned. * @param {String} outputType the output type. * @param {String|Array|ArrayBuffer|Uint8Array|Buffer} input the input to convert. * @throws {Error} an Error if the browser doesn't support the requested output type. */ exports.transformTo = function(outputType, input) { if (!input) { // undefined, null, etc // an empty string won't harm. input = ""; } if (!outputType) { return input; } exports.checkSupport(outputType); var inputType = exports.getTypeOf(input); var result = transform[inputType][outputType](input); return result; }; /** * Resolve all relative path components, "." and "..", in a path. If these relative components * traverse above the root then the resulting path will only contain the final path component. * * All empty components, e.g. "//", are removed. * @param {string} path A path with / or \ separators * @returns {string} The path with all relative path components resolved. */ exports.resolve = function(path) { var parts = path.split("/"); var result = []; for (var index = 0; index < parts.length; index++) { var part = parts[index]; // Allow the first and last component to be empty for trailing slashes. if (part === "." || (part === "" && index !== 0 && index !== parts.length - 1)) { continue; } else if (part === "..") { result.pop(); } else { result.push(part); } } return result.join("/"); }; /** * Return the type of the input. * The type will be in a format valid for JSZip.utils.transformTo : string, array, uint8array, arraybuffer. * @param {Object} input the input to identify. * @return {String} the (lowercase) type of the input. */ exports.getTypeOf = function(input) { if (typeof input === "string") { return "string"; } if (Object.prototype.toString.call(input) === "[object Array]") { return "array"; } if (support.nodebuffer && nodejsUtils.isBuffer(input)) { return "nodebuffer"; } if (support.uint8array && input instanceof Uint8Array) { return "uint8array"; } if (support.arraybuffer && input instanceof ArrayBuffer) { return "arraybuffer"; } }; /** * Throw an exception if the type is not supported. * @param {String} type the type to check. * @throws {Error} an Error if the browser doesn't support the requested type. */ exports.checkSupport = function(type) { var supported = support[type.toLowerCase()]; if (!supported) { throw new Error(type + " is not supported by this platform"); } }; exports.MAX_VALUE_16BITS = 65535; exports.MAX_VALUE_32BITS = -1; // well, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" is parsed as -1 /** * Prettify a string read as binary. * @param {string} str the string to prettify. * @return {string} a pretty string. */ exports.pretty = function(str) { var res = "", code, i; for (i = 0; i < (str || "").length; i++) { code = str.charCodeAt(i); res += "\\x" + (code < 16 ? "0" : "") + code.toString(16).toUpperCase(); } return res; }; /** * Defer the call of a function. * @param {Function} callback the function to call asynchronously. * @param {Array} args the arguments to give to the callback. */ exports.delay = function(callback, args, self) { setImmediate(function () { callback.apply(self || null, args || []); }); }; /** * Extends a prototype with an other, without calling a constructor with * side effects. Inspired by nodejs' `utils.inherits` * @param {Function} ctor the constructor to augment * @param {Function} superCtor the parent constructor to use */ exports.inherits = function (ctor, superCtor) { var Obj = function() {}; Obj.prototype = superCtor.prototype; ctor.prototype = new Obj(); }; /** * Merge the objects passed as parameters into a new one. * @private * @param {...Object} var_args All objects to merge. * @return {Object} a new object with the data of the others. */ exports.extend = function() { var result = {}, i, attr; for (i = 0; i < arguments.length; i++) { // arguments is not enumerable in some browsers for (attr in arguments[i]) { if (Object.prototype.hasOwnProperty.call(arguments[i], attr) && typeof result[attr] === "undefined") { result[attr] = arguments[i][attr]; } } } return result; }; /** * Transform arbitrary content into a Promise. * @param {String} name a name for the content being processed. * @param {Object} inputData the content to process. * @param {Boolean} isBinary true if the content is not an unicode string * @param {Boolean} isOptimizedBinaryString true if the string content only has one byte per character. * @param {Boolean} isBase64 true if the string content is encoded with base64. * @return {Promise} a promise in a format usable by JSZip. */ exports.prepareContent = function(name, inputData, isBinary, isOptimizedBinaryString, isBase64) { // if inputData is already a promise, this flatten it. var promise = external.Promise.resolve(inputData).then(function(data) { var isBlob = support.blob && (data instanceof Blob || ["[object File]", "[object Blob]"].indexOf(Object.prototype.toString.call(data)) !== -1); if (isBlob && typeof FileReader !== "undefined") { return new external.Promise(function (resolve, reject) { var reader = new FileReader(); reader.onload = function(e) { resolve(e.target.result); }; reader.onerror = function(e) { reject(e.target.error); }; reader.readAsArrayBuffer(data); }); } else { return data; } }); return promise.then(function(data) { var dataType = exports.getTypeOf(data); if (!dataType) { return external.Promise.reject( new Error("Can't read the data of '" + name + "'. Is it " + "in a supported JavaScript type (String, Blob, ArrayBuffer, etc) ?") ); } // special case : it's way easier to work with Uint8Array than with ArrayBuffer if (dataType === "arraybuffer") { data = exports.transformTo("uint8array", data); } else if (dataType === "string") { if (isBase64) { data = base64.decode(data); } else if (isBinary) { // optimizedBinaryString === true means that the file has already been filtered with a 0xFF mask if (isOptimizedBinaryString !== true) { // this is a string, not in a base64 format. // Be sure that this is a correct "binary string" data = string2binary(data); } } } return data; }); }; ================================================ FILE: lib/zipEntries.js ================================================ "use strict"; var readerFor = require("./reader/readerFor"); var utils = require("./utils"); var sig = require("./signature"); var ZipEntry = require("./zipEntry"); var support = require("./support"); // class ZipEntries {{{ /** * All the entries in the zip file. * @constructor * @param {Object} loadOptions Options for loading the stream. */ function ZipEntries(loadOptions) { this.files = []; this.loadOptions = loadOptions; } ZipEntries.prototype = { /** * Check that the reader is on the specified signature. * @param {string} expectedSignature the expected signature. * @throws {Error} if it is an other signature. */ checkSignature: function(expectedSignature) { if (!this.reader.readAndCheckSignature(expectedSignature)) { this.reader.index -= 4; var signature = this.reader.readString(4); throw new Error("Corrupted zip or bug: unexpected signature " + "(" + utils.pretty(signature) + ", expected " + utils.pretty(expectedSignature) + ")"); } }, /** * Check if the given signature is at the given index. * @param {number} askedIndex the index to check. * @param {string} expectedSignature the signature to expect. * @return {boolean} true if the signature is here, false otherwise. */ isSignature: function(askedIndex, expectedSignature) { var currentIndex = this.reader.index; this.reader.setIndex(askedIndex); var signature = this.reader.readString(4); var result = signature === expectedSignature; this.reader.setIndex(currentIndex); return result; }, /** * Read the end of the central directory. */ readBlockEndOfCentral: function() { this.diskNumber = this.reader.readInt(2); this.diskWithCentralDirStart = this.reader.readInt(2); this.centralDirRecordsOnThisDisk = this.reader.readInt(2); this.centralDirRecords = this.reader.readInt(2); this.centralDirSize = this.reader.readInt(4); this.centralDirOffset = this.reader.readInt(4); this.zipCommentLength = this.reader.readInt(2); // warning : the encoding depends of the system locale // On a linux machine with LANG=en_US.utf8, this field is utf8 encoded. // On a windows machine, this field is encoded with the localized windows code page. var zipComment = this.reader.readData(this.zipCommentLength); var decodeParamType = support.uint8array ? "uint8array" : "array"; // To get consistent behavior with the generation part, we will assume that // this is utf8 encoded unless specified otherwise. var decodeContent = utils.transformTo(decodeParamType, zipComment); this.zipComment = this.loadOptions.decodeFileName(decodeContent); }, /** * Read the end of the Zip 64 central directory. * Not merged with the method readEndOfCentral : * The end of central can coexist with its Zip64 brother, * I don't want to read the wrong number of bytes ! */ readBlockZip64EndOfCentral: function() { this.zip64EndOfCentralSize = this.reader.readInt(8); this.reader.skip(4); // this.versionMadeBy = this.reader.readString(2); // this.versionNeeded = this.reader.readInt(2); this.diskNumber = this.reader.readInt(4); this.diskWithCentralDirStart = this.reader.readInt(4); this.centralDirRecordsOnThisDisk = this.reader.readInt(8); this.centralDirRecords = this.reader.readInt(8); this.centralDirSize = this.reader.readInt(8); this.centralDirOffset = this.reader.readInt(8); this.zip64ExtensibleData = {}; var extraDataSize = this.zip64EndOfCentralSize - 44, index = 0, extraFieldId, extraFieldLength, extraFieldValue; while (index < extraDataSize) { extraFieldId = this.reader.readInt(2); extraFieldLength = this.reader.readInt(4); extraFieldValue = this.reader.readData(extraFieldLength); this.zip64ExtensibleData[extraFieldId] = { id: extraFieldId, length: extraFieldLength, value: extraFieldValue }; } }, /** * Read the end of the Zip 64 central directory locator. */ readBlockZip64EndOfCentralLocator: function() { this.diskWithZip64CentralDirStart = this.reader.readInt(4); this.relativeOffsetEndOfZip64CentralDir = this.reader.readInt(8); this.disksCount = this.reader.readInt(4); if (this.disksCount > 1) { throw new Error("Multi-volumes zip are not supported"); } }, /** * Read the local files, based on the offset read in the central part. */ readLocalFiles: function() { var i, file; for (i = 0; i < this.files.length; i++) { file = this.files[i]; this.reader.setIndex(file.localHeaderOffset); this.checkSignature(sig.LOCAL_FILE_HEADER); file.readLocalPart(this.reader); file.handleUTF8(); file.processAttributes(); } }, /** * Read the central directory. */ readCentralDir: function() { var file; this.reader.setIndex(this.centralDirOffset); while (this.reader.readAndCheckSignature(sig.CENTRAL_FILE_HEADER)) { file = new ZipEntry({ zip64: this.zip64 }, this.loadOptions); file.readCentralPart(this.reader); this.files.push(file); } if (this.centralDirRecords !== this.files.length) { if (this.centralDirRecords !== 0 && this.files.length === 0) { // We expected some records but couldn't find ANY. // This is really suspicious, as if something went wrong. throw new Error("Corrupted zip or bug: expected " + this.centralDirRecords + " records in central dir, got " + this.files.length); } else { // We found some records but not all. // Something is wrong but we got something for the user: no error here. // console.warn("expected", this.centralDirRecords, "records in central dir, got", this.files.length); } } }, /** * Read the end of central directory. */ readEndOfCentral: function() { var offset = this.reader.lastIndexOfSignature(sig.CENTRAL_DIRECTORY_END); if (offset < 0) { // Check if the content is a truncated zip or complete garbage. // A "LOCAL_FILE_HEADER" is not required at the beginning (auto // extractible zip for example) but it can give a good hint. // If an ajax request was used without responseType, we will also // get unreadable data. var isGarbage = !this.isSignature(0, sig.LOCAL_FILE_HEADER); if (isGarbage) { throw new Error("Can't find end of central directory : is this a zip file ? " + "If it is, see https://stuk.github.io/jszip/documentation/howto/read_zip.html"); } else { throw new Error("Corrupted zip: can't find end of central directory"); } } this.reader.setIndex(offset); var endOfCentralDirOffset = offset; this.checkSignature(sig.CENTRAL_DIRECTORY_END); this.readBlockEndOfCentral(); /* extract from the zip spec : 4) If one of the fields in the end of central directory record is too small to hold required data, the field should be set to -1 (0xFFFF or 0xFFFFFFFF) and the ZIP64 format record should be created. 5) The end of central directory record and the Zip64 end of central directory locator record must reside on the same disk when splitting or spanning an archive. */ if (this.diskNumber === utils.MAX_VALUE_16BITS || this.diskWithCentralDirStart === utils.MAX_VALUE_16BITS || this.centralDirRecordsOnThisDisk === utils.MAX_VALUE_16BITS || this.centralDirRecords === utils.MAX_VALUE_16BITS || this.centralDirSize === utils.MAX_VALUE_32BITS || this.centralDirOffset === utils.MAX_VALUE_32BITS) { this.zip64 = true; /* Warning : the zip64 extension is supported, but ONLY if the 64bits integer read from the zip file can fit into a 32bits integer. This cannot be solved : JavaScript represents all numbers as 64-bit double precision IEEE 754 floating point numbers. So, we have 53bits for integers and bitwise operations treat everything as 32bits. see https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Bitwise_Operators and http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf section 8.5 */ // should look for a zip64 EOCD locator offset = this.reader.lastIndexOfSignature(sig.ZIP64_CENTRAL_DIRECTORY_LOCATOR); if (offset < 0) { throw new Error("Corrupted zip: can't find the ZIP64 end of central directory locator"); } this.reader.setIndex(offset); this.checkSignature(sig.ZIP64_CENTRAL_DIRECTORY_LOCATOR); this.readBlockZip64EndOfCentralLocator(); // now the zip64 EOCD record if (!this.isSignature(this.relativeOffsetEndOfZip64CentralDir, sig.ZIP64_CENTRAL_DIRECTORY_END)) { // console.warn("ZIP64 end of central directory not where expected."); this.relativeOffsetEndOfZip64CentralDir = this.reader.lastIndexOfSignature(sig.ZIP64_CENTRAL_DIRECTORY_END); if (this.relativeOffsetEndOfZip64CentralDir < 0) { throw new Error("Corrupted zip: can't find the ZIP64 end of central directory"); } } this.reader.setIndex(this.relativeOffsetEndOfZip64CentralDir); this.checkSignature(sig.ZIP64_CENTRAL_DIRECTORY_END); this.readBlockZip64EndOfCentral(); } var expectedEndOfCentralDirOffset = this.centralDirOffset + this.centralDirSize; if (this.zip64) { expectedEndOfCentralDirOffset += 20; // end of central dir 64 locator expectedEndOfCentralDirOffset += 12 /* should not include the leading 12 bytes */ + this.zip64EndOfCentralSize; } var extraBytes = endOfCentralDirOffset - expectedEndOfCentralDirOffset; if (extraBytes > 0) { // console.warn(extraBytes, "extra bytes at beginning or within zipfile"); if (this.isSignature(endOfCentralDirOffset, sig.CENTRAL_FILE_HEADER)) { // The offsets seem wrong, but we have something at the specified offset. // So… we keep it. } else { // the offset is wrong, update the "zero" of the reader // this happens if data has been prepended (crx files for example) this.reader.zero = extraBytes; } } else if (extraBytes < 0) { throw new Error("Corrupted zip: missing " + Math.abs(extraBytes) + " bytes."); } }, prepareReader: function(data) { this.reader = readerFor(data); }, /** * Read a zip file and create ZipEntries. * @param {String|ArrayBuffer|Uint8Array|Buffer} data the binary string representing a zip file. */ load: function(data) { this.prepareReader(data); this.readEndOfCentral(); this.readCentralDir(); this.readLocalFiles(); } }; // }}} end of ZipEntries module.exports = ZipEntries; ================================================ FILE: lib/zipEntry.js ================================================ "use strict"; var readerFor = require("./reader/readerFor"); var utils = require("./utils"); var CompressedObject = require("./compressedObject"); var crc32fn = require("./crc32"); var utf8 = require("./utf8"); var compressions = require("./compressions"); var support = require("./support"); var MADE_BY_DOS = 0x00; var MADE_BY_UNIX = 0x03; /** * Find a compression registered in JSZip. * @param {string} compressionMethod the method magic to find. * @return {Object|null} the JSZip compression object, null if none found. */ var findCompression = function(compressionMethod) { for (var method in compressions) { if (!Object.prototype.hasOwnProperty.call(compressions, method)) { continue; } if (compressions[method].magic === compressionMethod) { return compressions[method]; } } return null; }; // class ZipEntry {{{ /** * An entry in the zip file. * @constructor * @param {Object} options Options of the current file. * @param {Object} loadOptions Options for loading the stream. */ function ZipEntry(options, loadOptions) { this.options = options; this.loadOptions = loadOptions; } ZipEntry.prototype = { /** * say if the file is encrypted. * @return {boolean} true if the file is encrypted, false otherwise. */ isEncrypted: function() { // bit 1 is set return (this.bitFlag & 0x0001) === 0x0001; }, /** * say if the file has utf-8 filename/comment. * @return {boolean} true if the filename/comment is in utf-8, false otherwise. */ useUTF8: function() { // bit 11 is set return (this.bitFlag & 0x0800) === 0x0800; }, /** * Read the local part of a zip file and add the info in this object. * @param {DataReader} reader the reader to use. */ readLocalPart: function(reader) { var compression, localExtraFieldsLength; // we already know everything from the central dir ! // If the central dir data are false, we are doomed. // On the bright side, the local part is scary : zip64, data descriptors, both, etc. // The less data we get here, the more reliable this should be. // Let's skip the whole header and dash to the data ! reader.skip(22); // in some zip created on windows, the filename stored in the central dir contains \ instead of /. // Strangely, the filename here is OK. // I would love to treat these zip files as corrupted (see http://www.info-zip.org/FAQ.html#backslashes // or APPNOTE#4.4.17.1, "All slashes MUST be forward slashes '/'") but there are a lot of bad zip generators... // Search "unzip mismatching "local" filename continuing with "central" filename version" on // the internet. // // I think I see the logic here : the central directory is used to display // content and the local directory is used to extract the files. Mixing / and \ // may be used to display \ to windows users and use / when extracting the files. // Unfortunately, this lead also to some issues : http://seclists.org/fulldisclosure/2009/Sep/394 this.fileNameLength = reader.readInt(2); localExtraFieldsLength = reader.readInt(2); // can't be sure this will be the same as the central dir // the fileName is stored as binary data, the handleUTF8 method will take care of the encoding. this.fileName = reader.readData(this.fileNameLength); reader.skip(localExtraFieldsLength); if (this.compressedSize === -1 || this.uncompressedSize === -1) { throw new Error("Bug or corrupted zip : didn't get enough information from the central directory " + "(compressedSize === -1 || uncompressedSize === -1)"); } compression = findCompression(this.compressionMethod); if (compression === null) { // no compression found throw new Error("Corrupted zip : compression " + utils.pretty(this.compressionMethod) + " unknown (inner file : " + utils.transformTo("string", this.fileName) + ")"); } this.decompressed = new CompressedObject(this.compressedSize, this.uncompressedSize, this.crc32, compression, reader.readData(this.compressedSize)); }, /** * Read the central part of a zip file and add the info in this object. * @param {DataReader} reader the reader to use. */ readCentralPart: function(reader) { this.versionMadeBy = reader.readInt(2); reader.skip(2); // this.versionNeeded = reader.readInt(2); this.bitFlag = reader.readInt(2); this.compressionMethod = reader.readString(2); this.date = reader.readDate(); this.crc32 = reader.readInt(4); this.compressedSize = reader.readInt(4); this.uncompressedSize = reader.readInt(4); var fileNameLength = reader.readInt(2); this.extraFieldsLength = reader.readInt(2); this.fileCommentLength = reader.readInt(2); this.diskNumberStart = reader.readInt(2); this.internalFileAttributes = reader.readInt(2); this.externalFileAttributes = reader.readInt(4); this.localHeaderOffset = reader.readInt(4); if (this.isEncrypted()) { throw new Error("Encrypted zip are not supported"); } // will be read in the local part, see the comments there reader.skip(fileNameLength); this.readExtraFields(reader); this.parseZIP64ExtraField(reader); this.fileComment = reader.readData(this.fileCommentLength); }, /** * Parse the external file attributes and get the unix/dos permissions. */ processAttributes: function () { this.unixPermissions = null; this.dosPermissions = null; var madeBy = this.versionMadeBy >> 8; // Check if we have the DOS directory flag set. // We look for it in the DOS and UNIX permissions // but some unknown platform could set it as a compatibility flag. this.dir = this.externalFileAttributes & 0x0010 ? true : false; if(madeBy === MADE_BY_DOS) { // first 6 bits (0 to 5) this.dosPermissions = this.externalFileAttributes & 0x3F; } if(madeBy === MADE_BY_UNIX) { this.unixPermissions = (this.externalFileAttributes >> 16) & 0xFFFF; // the octal permissions are in (this.unixPermissions & 0x01FF).toString(8); } // fail safe : if the name ends with a / it probably means a folder if (!this.dir && this.fileNameStr.slice(-1) === "/") { this.dir = true; } }, /** * Parse the ZIP64 extra field and merge the info in the current ZipEntry. * @param {DataReader} reader the reader to use. */ parseZIP64ExtraField: function() { if (!this.extraFields[0x0001]) { return; } // should be something, preparing the extra reader var extraReader = readerFor(this.extraFields[0x0001].value); // I really hope that these 64bits integer can fit in 32 bits integer, because js // won't let us have more. if (this.uncompressedSize === utils.MAX_VALUE_32BITS) { this.uncompressedSize = extraReader.readInt(8); } if (this.compressedSize === utils.MAX_VALUE_32BITS) { this.compressedSize = extraReader.readInt(8); } if (this.localHeaderOffset === utils.MAX_VALUE_32BITS) { this.localHeaderOffset = extraReader.readInt(8); } if (this.diskNumberStart === utils.MAX_VALUE_32BITS) { this.diskNumberStart = extraReader.readInt(4); } }, /** * Read the central part of a zip file and add the info in this object. * @param {DataReader} reader the reader to use. */ readExtraFields: function(reader) { var end = reader.index + this.extraFieldsLength, extraFieldId, extraFieldLength, extraFieldValue; if (!this.extraFields) { this.extraFields = {}; } while (reader.index + 4 < end) { extraFieldId = reader.readInt(2); extraFieldLength = reader.readInt(2); extraFieldValue = reader.readData(extraFieldLength); this.extraFields[extraFieldId] = { id: extraFieldId, length: extraFieldLength, value: extraFieldValue }; } reader.setIndex(end); }, /** * Apply an UTF8 transformation if needed. */ handleUTF8: function() { var decodeParamType = support.uint8array ? "uint8array" : "array"; if (this.useUTF8()) { this.fileNameStr = utf8.utf8decode(this.fileName); this.fileCommentStr = utf8.utf8decode(this.fileComment); } else { var upath = this.findExtraFieldUnicodePath(); if (upath !== null) { this.fileNameStr = upath; } else { // ASCII text or unsupported code page var fileNameByteArray = utils.transformTo(decodeParamType, this.fileName); this.fileNameStr = this.loadOptions.decodeFileName(fileNameByteArray); } var ucomment = this.findExtraFieldUnicodeComment(); if (ucomment !== null) { this.fileCommentStr = ucomment; } else { // ASCII text or unsupported code page var commentByteArray = utils.transformTo(decodeParamType, this.fileComment); this.fileCommentStr = this.loadOptions.decodeFileName(commentByteArray); } } }, /** * Find the unicode path declared in the extra field, if any. * @return {String} the unicode path, null otherwise. */ findExtraFieldUnicodePath: function() { var upathField = this.extraFields[0x7075]; if (upathField) { var extraReader = readerFor(upathField.value); // wrong version if (extraReader.readInt(1) !== 1) { return null; } // the crc of the filename changed, this field is out of date. if (crc32fn(this.fileName) !== extraReader.readInt(4)) { return null; } return utf8.utf8decode(extraReader.readData(upathField.length - 5)); } return null; }, /** * Find the unicode comment declared in the extra field, if any. * @return {String} the unicode comment, null otherwise. */ findExtraFieldUnicodeComment: function() { var ucommentField = this.extraFields[0x6375]; if (ucommentField) { var extraReader = readerFor(ucommentField.value); // wrong version if (extraReader.readInt(1) !== 1) { return null; } // the crc of the comment changed, this field is out of date. if (crc32fn(this.fileComment) !== extraReader.readInt(4)) { return null; } return utf8.utf8decode(extraReader.readData(ucommentField.length - 5)); } return null; } }; module.exports = ZipEntry; ================================================ FILE: lib/zipObject.js ================================================ "use strict"; var StreamHelper = require("./stream/StreamHelper"); var DataWorker = require("./stream/DataWorker"); var utf8 = require("./utf8"); var CompressedObject = require("./compressedObject"); var GenericWorker = require("./stream/GenericWorker"); /** * A simple object representing a file in the zip file. * @constructor * @param {string} name the name of the file * @param {String|ArrayBuffer|Uint8Array|Buffer} data the data * @param {Object} options the options of the file */ var ZipObject = function(name, data, options) { this.name = name; this.dir = options.dir; this.date = options.date; this.comment = options.comment; this.unixPermissions = options.unixPermissions; this.dosPermissions = options.dosPermissions; this._data = data; this._dataBinary = options.binary; // keep only the compression this.options = { compression : options.compression, compressionOptions : options.compressionOptions }; }; ZipObject.prototype = { /** * Create an internal stream for the content of this object. * @param {String} type the type of each chunk. * @return StreamHelper the stream. */ internalStream: function (type) { var result = null, outputType = "string"; try { if (!type) { throw new Error("No output type specified."); } outputType = type.toLowerCase(); var askUnicodeString = outputType === "string" || outputType === "text"; if (outputType === "binarystring" || outputType === "text") { outputType = "string"; } result = this._decompressWorker(); var isUnicodeString = !this._dataBinary; if (isUnicodeString && !askUnicodeString) { result = result.pipe(new utf8.Utf8EncodeWorker()); } if (!isUnicodeString && askUnicodeString) { result = result.pipe(new utf8.Utf8DecodeWorker()); } } catch (e) { result = new GenericWorker("error"); result.error(e); } return new StreamHelper(result, outputType, ""); }, /** * Prepare the content in the asked type. * @param {String} type the type of the result. * @param {Function} onUpdate a function to call on each internal update. * @return Promise the promise of the result. */ async: function (type, onUpdate) { return this.internalStream(type).accumulate(onUpdate); }, /** * Prepare the content as a nodejs stream. * @param {String} type the type of each chunk. * @param {Function} onUpdate a function to call on each internal update. * @return Stream the stream. */ nodeStream: function (type, onUpdate) { return this.internalStream(type || "nodebuffer").toNodejsStream(onUpdate); }, /** * Return a worker for the compressed content. * @private * @param {Object} compression the compression object to use. * @param {Object} compressionOptions the options to use when compressing. * @return Worker the worker. */ _compressWorker: function (compression, compressionOptions) { if ( this._data instanceof CompressedObject && this._data.compression.magic === compression.magic ) { return this._data.getCompressedWorker(); } else { var result = this._decompressWorker(); if(!this._dataBinary) { result = result.pipe(new utf8.Utf8EncodeWorker()); } return CompressedObject.createWorkerFrom(result, compression, compressionOptions); } }, /** * Return a worker for the decompressed content. * @private * @return Worker the worker. */ _decompressWorker : function () { if (this._data instanceof CompressedObject) { return this._data.getContentWorker(); } else if (this._data instanceof GenericWorker) { return this._data; } else { return new DataWorker(this._data); } } }; var removedMethods = ["asText", "asBinary", "asNodeBuffer", "asUint8Array", "asArrayBuffer"]; var removedFn = function () { throw new Error("This method has been removed in JSZip 3.0, please check the upgrade guide."); }; for(var i = 0; i < removedMethods.length; i++) { ZipObject.prototype[removedMethods[i]] = removedFn; } module.exports = ZipObject; ================================================ FILE: package.json ================================================ { "name": "jszip", "version": "3.10.1", "author": "Stuart Knightley ", "description": "Create, read and edit .zip files with JavaScript http://stuartk.com/jszip", "scripts": { "test": "npm run test-node && npm run test-browser && tsc", "test-node": "qunit --require ./test/helpers/test-utils.js --require ./test/helpers/node-test-utils.js test/asserts/", "test-browser": "grunt build && node test/run.js --test", "benchmark": "npm run benchmark-node && npm run benchmark-browser", "benchmark-node": "node test/benchmark/node.js", "benchmark-browser": "node test/run.js --benchmark", "lint": "eslint ." }, "contributors": [ { "name": "Franz Buchinger" }, { "name": "António Afonso" }, { "name": "David Duponchel" }, { "name": "yiminghe" } ], "main": "./lib/index", "browser": { "./lib/index": "./dist/jszip.min.js", "readable-stream": "./lib/readable-stream-browser.js" }, "types": "./index.d.ts", "repository": { "type": "git", "url": "https://github.com/Stuk/jszip.git" }, "keywords": [ "zip", "deflate", "inflate" ], "devDependencies": { "benchmark": "^2.1.4", "browserify": "~13.0.0", "eslint": "^8.18.0", "grunt": "~0.4.1", "grunt-browserify": "~5.0.0", "grunt-cli": "~1.1.0", "grunt-contrib-uglify": "~4.0.1", "http-server": "^13.0.2", "jszip-utils": "~0.0.2", "package-json-versionify": "1.0.2", "playwright": "^1.51.0", "qunit": "~2.9.2", "tmp": "0.0.28", "typescript": "^4.6.3" }, "dependencies": { "lie": "~3.3.0", "pako": "~1.0.2", "readable-stream": "~2.3.6", "setimmediate": "^1.0.5" }, "license": "(MIT OR GPL-3.0-or-later)" } ================================================ FILE: sponsors.md ================================================ --- title: "Sponsors" layout: default section: main --- [JSZip](https://github.com/Stuk/jszip) was created in 2009 by [Stuart](https://github.com/Stuk). Since then it has received well over [600 million downloads](https://npm-stat.com/charts.html?package=jszip&from=2009-06-20&to=2022-06-20), is depended on by over 3000 packages on npm, and powers zipping and unzipping on sites large and small. This project only exists because of all the work dedicated to it by me and the other contributors. If you or your company has benefited from JSZip then please consider [sponsoring on Github](https://github.com/sponsors/Stuk). ## 💎 Diamond ## 🥇 Gold ## 🥈 Silver ## 🥉 Bronze ## Supporters ================================================ FILE: test/.eslintrc.js ================================================ "use strict"; module.exports = { globals: { JSZip: false, JSZipUtils: false, JSZipTestUtils: false, QUnit: false, }, }; ================================================ FILE: test/asserts/constructor.js ================================================ "use strict"; QUnit.module("constructor"); QUnit.test("JSZip exists", function(assert){ assert.ok(JSZip, "JSZip exists"); }); QUnit.test("new JSZip()", function(assert){ var zip = new JSZip(); assert.ok(zip instanceof JSZip, "Constructor works"); }); QUnit.test("JSZip()", function(assert){ var zip = JSZip(); assert.ok(zip instanceof JSZip, "Constructor adds `new` before itself where necessary"); }); ================================================ FILE: test/asserts/delete.js ================================================ "use strict"; QUnit.module("delete"); JSZipTestUtils.testZipFile("Delete file", "ref/text.zip", function(assert, expected) { var zip = new JSZip(); zip.file("Remove.txt", "This file should be deleted\n"); zip.file("Hello.txt", "Hello World\n"); zip.remove("Remove.txt"); var done = assert.async(); zip.generateAsync({type:"binarystring"}).then(function(actual) { assert.ok(JSZipTestUtils.similar(actual, expected, JSZipTestUtils.MAX_BYTES_DIFFERENCE_PER_ZIP_ENTRY) , "Generated ZIP matches reference ZIP"); done(); })["catch"](JSZipTestUtils.assertNoError); }); JSZipTestUtils.testZipFile("Delete file in folder", "ref/folder.zip", function(assert, expected) { var zip = new JSZip(); zip.folder("folder").file("Remove.txt", "This folder and file should be deleted\n"); zip.remove("folder/Remove.txt"); var done = assert.async(); zip.generateAsync({type:"binarystring"}).then(function(actual) { assert.ok(JSZipTestUtils.similar(actual, expected, JSZipTestUtils.MAX_BYTES_DIFFERENCE_PER_ZIP_ENTRY) , "Generated ZIP matches reference ZIP"); done(); })["catch"](JSZipTestUtils.assertNoError); }); JSZipTestUtils.testZipFile("Delete file in folder, with a relative path", "ref/folder.zip", function(assert, expected) { var zip = new JSZip(); var folder = zip.folder("folder"); folder.file("Remove.txt", "This folder and file should be deleted\n"); folder.remove("Remove.txt"); var done = assert.async(); zip.generateAsync({type:"binarystring"}).then(function(actual) { assert.ok(JSZipTestUtils.similar(actual, expected, JSZipTestUtils.MAX_BYTES_DIFFERENCE_PER_ZIP_ENTRY) , "Generated ZIP matches reference ZIP"); done(); })["catch"](JSZipTestUtils.assertNoError); }); JSZipTestUtils.testZipFile("Delete folder", "ref/text.zip", function(assert, expected) { var zip = new JSZip(); zip.folder("remove").file("Remove.txt", "This folder and file should be deleted\n"); zip.file("Hello.txt", "Hello World\n"); zip.remove("remove"); var done = assert.async(); zip.generateAsync({type:"binarystring"}).then(function(actual) { assert.ok(JSZipTestUtils.similar(actual, expected, JSZipTestUtils.MAX_BYTES_DIFFERENCE_PER_ZIP_ENTRY) , "Generated ZIP matches reference ZIP"); done(); })["catch"](JSZipTestUtils.assertNoError); }); JSZipTestUtils.testZipFile("Delete folder with a final /", "ref/text.zip", function(assert, expected) { var zip = new JSZip(); zip.folder("remove").file("Remove.txt", "This folder and file should be deleted\n"); zip.file("Hello.txt", "Hello World\n"); zip.remove("remove/"); var done = assert.async(); zip.generateAsync({type:"binarystring"}).then(function(actual) { assert.ok(JSZipTestUtils.similar(actual, expected, JSZipTestUtils.MAX_BYTES_DIFFERENCE_PER_ZIP_ENTRY) , "Generated ZIP matches reference ZIP"); done(); })["catch"](JSZipTestUtils.assertNoError); }); JSZipTestUtils.testZipFile("Delete unknown path", "ref/text.zip", function(assert, expected) { var zip = new JSZip(); zip.file("Hello.txt", "Hello World\n"); zip.remove("unknown_file"); zip.remove("unknown_folder/Hello.txt"); var done = assert.async(); zip.generateAsync({type:"binarystring"}).then(function(actual) { assert.ok(JSZipTestUtils.similar(actual, expected, JSZipTestUtils.MAX_BYTES_DIFFERENCE_PER_ZIP_ENTRY) , "Generated ZIP matches reference ZIP"); done(); })["catch"](JSZipTestUtils.assertNoError); }); JSZipTestUtils.testZipFile("Delete nested folders", "ref/text.zip", function(assert, expected) { var zip = new JSZip(); zip.folder("remove").file("Remove.txt", "This folder and file should be deleted\n"); zip.folder("remove/second").file("Sub.txt", "This should be removed"); zip.file("remove/second/another.txt", "Another file"); zip.file("Hello.txt", "Hello World\n"); zip.remove("remove"); var done = assert.async(); zip.generateAsync({type:"binarystring"}).then(function(actual) { assert.ok(JSZipTestUtils.similar(actual, expected, JSZipTestUtils.MAX_BYTES_DIFFERENCE_PER_ZIP_ENTRY) , "Generated ZIP matches reference ZIP"); done(); })["catch"](JSZipTestUtils.assertNoError); }); JSZipTestUtils.testZipFile("Delete nested folders from relative path", "ref/folder.zip", function(assert, expected) { var zip = new JSZip(); zip.folder("folder"); zip.folder("folder/1/2/3"); zip.folder("folder").remove("1"); var done = assert.async(); zip.generateAsync({type:"binarystring"}).then(function(actual) { assert.ok(JSZipTestUtils.similar(actual, expected, JSZipTestUtils.MAX_BYTES_DIFFERENCE_PER_ZIP_ENTRY) , "Generated ZIP matches reference ZIP"); JSZipTestUtils.checkGenerateStability(assert, actual); done(); })["catch"](JSZipTestUtils.assertNoError); }); ================================================ FILE: test/asserts/deprecated.js ================================================ "use strict"; QUnit.module("deprecated"); QUnit.test("Removed load method throws an exception", function(assert) { assert.throws( function() { new JSZip().load(""); }, /upgrade guide/, "load() throws an exception" ); }); QUnit.test("Removed constructor with data throws an exception", function(assert) { assert.throws( function() { // eslint-disable-next-line no-new new JSZip(""); }, /upgrade guide/, "new JSZip(data) throws an exception" ); }); QUnit.test("Removed asText method throws an exception", function(assert) { var file = JSZipTestUtils.createZipAll().file("Hello.txt"); assert.throws( function() { file.asText(); }, /upgrade guide/, "file.asText() throws an exception" ); }); QUnit.test("Removed generate method throws an exception", function(assert) { assert.throws( function() { new JSZip().generate({type:"string"}); }, /upgrade guide/, "generate() throws an exception" ); }); ================================================ FILE: test/asserts/external.js ================================================ "use strict"; QUnit.module("external"); /** * Creates a wrapper around an existing Promise implementation to count * calls and detect custom implementations. * @param {Promise} OriginalPromise the promise to wrap * @return {Promise} the wrapped promise */ function createPromiseProxy(OriginalPromise) { function MyShinyPromise (input) { if (input.then) { // thenable, we wrap it this._promise = input; } else { // executor this._promise = new OriginalPromise(input); } MyShinyPromise.calls++; } MyShinyPromise.calls = 0; MyShinyPromise.prototype = { then: function (onFulfilled, onRejected) { return new MyShinyPromise(this._promise.then(onFulfilled, onRejected)); }, "catch": function (onRejected) { return new MyShinyPromise(this._promise["catch"](onRejected)); }, isACustomImplementation: true }; MyShinyPromise.resolve = function (value) { return new MyShinyPromise(OriginalPromise.resolve(value)); }; MyShinyPromise.reject = function (value) { return new MyShinyPromise(OriginalPromise.reject(value)); }; MyShinyPromise.all = function (value) { return new MyShinyPromise(OriginalPromise.all(value)); }; return MyShinyPromise; } QUnit.test("JSZip.external.Promise", function (assert) { assert.ok(JSZip.external.Promise, "JSZip.external.Promise is defined"); assert.ok(JSZip.external.Promise.resolve, "JSZip.external.Promise looks like a Promise"); assert.ok(JSZip.external.Promise.reject, "JSZip.external.Promise looks like a Promise"); }); QUnit.test("load JSZip doesn't override the global Promise", function (assert) { if (typeof Promise !== "undefined"){ assert.equal(Promise, JSZipTestUtils.oldPromise, "the previous Promise didn't change"); assert.equal(Promise, JSZip.external.Promise, "JSZip.external.Promise reused the global Promise"); } else { assert.ok(JSZip.external.Promise, "JSZip.external.Promise is defined even if the global Promise doesn't exist"); } }); QUnit.test("external.Promise can be replaced in .async()", function (assert) { var done = assert.async(); var OriginalPromise = JSZip.external.Promise; var MyShinyPromise = createPromiseProxy(OriginalPromise); JSZip.external.Promise = MyShinyPromise; var promise = JSZipTestUtils.createZipAll().file("Hello.txt").async("string").then(function () { assert.ok(MyShinyPromise.calls > 0, "at least 1 call of the new Promise"); JSZip.external.Promise = OriginalPromise; done(); })["catch"](JSZipTestUtils.assertNoError); assert.ok(promise.isACustomImplementation, "the custom implementation is used"); }); QUnit.test("external.Promise can be replaced in .generateAsync()", function (assert) { var done = assert.async(); var OriginalPromise = JSZip.external.Promise; var MyShinyPromise = createPromiseProxy(OriginalPromise); JSZip.external.Promise = MyShinyPromise; var promise = JSZipTestUtils.createZipAll().generateAsync({type:"string"}).then(function () { assert.ok(MyShinyPromise.calls > 0, "at least 1 call of the new Promise"); JSZip.external.Promise = OriginalPromise; done(); })["catch"](JSZipTestUtils.assertNoError); assert.ok(promise.isACustomImplementation, "the custom implementation is used"); }); JSZipTestUtils.testZipFile("external.Promise can be replaced in .loadAsync()", "ref/all.zip", function (assert, all) { var done = assert.async(); var OriginalPromise = JSZip.external.Promise; var MyShinyPromise = createPromiseProxy(OriginalPromise); JSZip.external.Promise = MyShinyPromise; var promise = JSZip.loadAsync(all).then(function () { assert.ok(MyShinyPromise.calls > 0, "at least 1 call of the new Promise"); JSZip.external.Promise = OriginalPromise; done(); })["catch"](JSZipTestUtils.assertNoError); assert.ok(promise.isACustomImplementation, "the custom implementation is used"); }); ================================================ FILE: test/asserts/file.js ================================================ "use strict"; QUnit.module("file", function () { function str2blob (str) { var u8 = new Uint8Array(str.length); for(var i = 0; i < str.length; i++) { u8[i] = str.charCodeAt(i); } try { // don't use an Uint8Array, see the comment on utils.newBlob return new Blob([u8.buffer], {type:"text/plain"}); } catch (e) { var Builder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder; var builder = new Builder(); builder.append(u8.buffer); return builder.getBlob("text/plain"); } } QUnit.module("add"); JSZipTestUtils.testZipFile("Zip text file !", "ref/text.zip", function(assert, expected) { var done = assert.async(); var zip = new JSZip(); zip.file("Hello.txt", "Hello World\n"); JSZipTestUtils.checkBasicStreamBehavior(assert, zip.generateInternalStream({type:"binarystring"})); zip.generateAsync({type:"binarystring"}).then(function (actual) { assert.ok(JSZipTestUtils.similar(actual, expected, JSZipTestUtils.MAX_BYTES_DIFFERENCE_PER_ZIP_ENTRY) , "Generated ZIP matches reference ZIP"); JSZipTestUtils.checkGenerateStability(assert, actual); done(); })["catch"](JSZipTestUtils.assertNoError); }); JSZipTestUtils.testZipFile("Zip text, folder and image", "ref/all.zip", function(assert, expected) { var zip = new JSZip(); zip.file("Hello.txt", "Hello World\n"); zip.folder("images").file("smile.gif", "R0lGODdhBQAFAIACAAAAAP/eACwAAAAABQAFAAACCIwPkWerClIBADs=", {base64: true}); var done = assert.async(); zip.generateAsync({type:"binarystring"}).then(function(actual) { assert.ok(JSZipTestUtils.similar(actual, expected, 3 * JSZipTestUtils.MAX_BYTES_DIFFERENCE_PER_ZIP_ENTRY) , "Generated ZIP matches reference ZIP"); JSZipTestUtils.checkGenerateStability(assert, actual); done(); })["catch"](JSZipTestUtils.assertNoError); }); JSZipTestUtils.testZipFile("Add a file to overwrite", "ref/text.zip", function(assert, expected) { var zip = new JSZip(); zip.file("Hello.txt", "hello ?"); zip.file("Hello.txt", "Hello World\n"); var done = assert.async(); zip.generateAsync({type:"binarystring"}).then(function (actual) { assert.ok(JSZipTestUtils.similar(actual, expected, JSZipTestUtils.MAX_BYTES_DIFFERENCE_PER_ZIP_ENTRY) , "Generated ZIP matches reference ZIP"); JSZipTestUtils.checkGenerateStability(assert, actual); done(); })["catch"](JSZipTestUtils.assertNoError); }); JSZipTestUtils.testZipFile("Zip text file with date", "ref/text.zip", function(assert, expected) { var zip = new JSZip(); zip.file("Hello.txt", "Hello World\n", {date : new Date("July 17, 2009 14:36:57")}); var done = assert.async(); zip.generateAsync({type:"binarystring"}).then(function(actual) { /* Expected differing bytes: 2 version number 4 central dir version numbers 4 external file attributes 10 Total */ assert.ok(JSZipTestUtils.similar(actual, expected, 10) , "Generated ZIP matches reference ZIP"); JSZipTestUtils.checkGenerateStability(assert, actual); done(); })["catch"](JSZipTestUtils.assertNoError); }); JSZipTestUtils.testZipFile("Zip image file", "ref/image.zip", function(assert, expected) { var zip = new JSZip(); zip.file("smile.gif", "R0lGODdhBQAFAIACAAAAAP/eACwAAAAABQAFAAACCIwPkWerClIBADs=", {base64: true}); var done = assert.async(); zip.generateAsync({type:"binarystring"}).then(function(actual) { assert.ok(JSZipTestUtils.similar(actual, expected, JSZipTestUtils.MAX_BYTES_DIFFERENCE_PER_ZIP_ENTRY) , "Generated ZIP matches reference ZIP"); JSZipTestUtils.checkGenerateStability(assert, actual); done(); })["catch"](JSZipTestUtils.assertNoError); }); JSZipTestUtils.testZipFile("add file: from XHR (with bytes > 255)", "ref/text.zip", function(assert, textZip) { var zip = new JSZip(); zip.file("text.zip", textZip, {binary:true}); var done = assert.async(); zip.generateAsync({type:"binarystring"}).then(function(actual) { // high-order byte is discarded and won't mess up the result JSZipTestUtils.checkGenerateStability(assert, actual); done(); })["catch"](JSZipTestUtils.assertNoError); }); QUnit.test("add file: wrong string as base64", function(assert) { var zip = new JSZip(); zip.file("text.txt", "a random string", {base64:true}); var done = assert.async(); zip.generateAsync({type:"binarystring"}).then(function() { assert.ok(false, "generateAsync should fail"); done(); })["catch"](function (e) { assert.equal(e.message, "Invalid base64 input, bad content length.", "triggers the correct error"); done(); }); }); QUnit.test("add file: data url instead of base64", function(assert) { var zip = new JSZip(); zip.file("text.txt", "data:image/png;base64,YmFzZTY0", {base64:true}); var done = assert.async(); zip.generateAsync({type:"binarystring"}).then(function() { assert.ok(false, "generateAsync should fail"); done(); })["catch"](function (e) { assert.equal(e.message, "Invalid base64 input, it looks like a data url.", "triggers the correct error"); done(); }); }); function testFileDataGetters (assert, opts) { if (typeof opts.rawData === "undefined") { opts.rawData = opts.textData; } _actualTestFileDataGetters.testGetter(assert, opts, "string"); _actualTestFileDataGetters.testGetter(assert, opts, "text"); _actualTestFileDataGetters.testGetter(assert, opts, "base64"); _actualTestFileDataGetters.testGetter(assert, opts, "array"); _actualTestFileDataGetters.testGetter(assert, opts, "binarystring"); _actualTestFileDataGetters.testGetter(assert, opts, "arraybuffer"); _actualTestFileDataGetters.testGetter(assert, opts, "uint8array"); _actualTestFileDataGetters.testGetter(assert, opts, "nodebuffer"); _actualTestFileDataGetters.testGetter(assert, opts, "blob"); _actualTestFileDataGetters.testGetter(assert, opts, "unknown"); _actualTestFileDataGetters.testGetter(assert, opts, null); var done = assert.async(); opts.zip.generateAsync({type:"binarystring"}) .then(JSZip.loadAsync) .then(function(zip) { var reloaded = { name : "(reloaded) " + opts.name, zip : zip, textData : opts.textData, rawData : opts.rawData }; _actualTestFileDataGetters.testGetter(assert, reloaded, "string"); _actualTestFileDataGetters.testGetter(assert, reloaded, "text"); _actualTestFileDataGetters.testGetter(assert, reloaded, "base64"); _actualTestFileDataGetters.testGetter(assert, reloaded, "array"); _actualTestFileDataGetters.testGetter(assert, reloaded, "binarystring"); _actualTestFileDataGetters.testGetter(assert, reloaded, "arraybuffer"); _actualTestFileDataGetters.testGetter(assert, reloaded, "uint8array"); _actualTestFileDataGetters.testGetter(assert, reloaded, "nodebuffer"); _actualTestFileDataGetters.testGetter(assert, reloaded, "blob"); _actualTestFileDataGetters.testGetter(assert, reloaded, "unknown"); _actualTestFileDataGetters.testGetter(assert, reloaded, null); opts.zip.file("file.txt", "changing the content after the call won't change the result"); done(); })["catch"](JSZipTestUtils.assertNoError); opts.zip.file("file.txt", "changing the content after the call won't change the result"); } var _actualTestFileDataGetters = { testGetter : function (assert, opts, askedType) { var asyncTestName = "[test = " + opts.name + "] [method = async(" + askedType + ")] "; var stream = opts.zip.file("file.txt").internalStream(askedType); JSZipTestUtils.checkBasicStreamBehavior(assert, stream, asyncTestName); var done = assert.async(); opts.zip.file("file.txt").async(askedType).then(function(result) { _actualTestFileDataGetters["assert_" + askedType](opts, null, result, asyncTestName); done(); }, function (err) { _actualTestFileDataGetters["assert_" + askedType](opts, err, null, asyncTestName); done(); }); }, assert_string: function (opts, err, txt, testName) { QUnit.assert.equal(err, null, testName + "no error"); QUnit.assert.equal(txt, opts.textData, testName + "content ok"); }, assert_text: function () { this.assert_string.apply(this, arguments); }, assert_base64: function (opts, err, bin, testName) { QUnit.assert.equal(err, null, testName + "no error"); QUnit.assert.equal(bin, JSZipTestUtils.base64encode(opts.rawData), testName + "content ok"); }, assert_binarystring : function (opts, err, bin, testName) { QUnit.assert.equal(err, null, testName + "no error"); QUnit.assert.equal(bin, opts.rawData, testName + "content ok"); }, assert_array : function (opts, err, array, testName) { QUnit.assert.equal(err, null, testName + "no error"); QUnit.assert.ok(array instanceof Array, testName + "the result is a instance of Array"); var actual = JSZipTestUtils.toString(array); QUnit.assert.equal(actual, opts.rawData, testName + "content ok"); }, assert_arraybuffer : function (opts, err, buffer, testName) { if (JSZip.support.arraybuffer) { QUnit.assert.equal(err, null, testName + "no error"); QUnit.assert.ok(buffer instanceof ArrayBuffer, testName + "the result is a instance of ArrayBuffer"); var actual = JSZipTestUtils.toString(buffer); QUnit.assert.equal(actual, opts.rawData, testName + "content ok"); } else { QUnit.assert.equal(buffer, null, testName + "no data"); QUnit.assert.ok(err.message.match("not supported by this platform"), testName + "the error message is useful"); } }, assert_uint8array : function (opts, err, bufferView, testName) { if (JSZip.support.uint8array) { QUnit.assert.equal(err, null, testName + "no error"); QUnit.assert.ok(bufferView instanceof Uint8Array, testName+ "the result is a instance of Uint8Array"); var actual = JSZipTestUtils.toString(bufferView); QUnit.assert.equal(actual, opts.rawData, testName + "content ok"); } else { QUnit.assert.equal(bufferView, null, testName + "no data"); QUnit.assert.ok(err.message.match("not supported by this platform"), testName + "the error message is useful"); } }, assert_nodebuffer : function (opts, err, buffer, testName) { if (JSZip.support.nodebuffer) { QUnit.assert.equal(err, null, testName + "no error"); QUnit.assert.ok(buffer instanceof Buffer, testName + "the result is a instance of Buffer"); var actual = JSZipTestUtils.toString(buffer); QUnit.assert.equal(actual, opts.rawData, testName + "content ok"); } else { QUnit.assert.equal(buffer, null, testName + "no data"); QUnit.assert.ok(err.message.match("not supported by this platform"), testName + "the error message is useful"); } }, assert_blob : function (opts, err, blob, testName) { if (JSZip.support.blob) { QUnit.assert.equal(err, null, testName + "no error"); QUnit.assert.ok(blob instanceof Blob, testName + "the result is a instance of Blob"); QUnit.assert.equal(blob.type, "", testName + "the result has the right mime type"); QUnit.assert.equal(blob.size, opts.rawData.length, testName + "the result has the right length"); } else { QUnit.assert.equal(blob, null, testName + "no data"); QUnit.assert.ok(err.message.match("not supported by this platform"), testName + "the error message is useful"); } }, assert_unknown : function (opts, err, buffer, testName) { QUnit.assert.equal(buffer, null, testName + "no data"); QUnit.assert.ok(err.message.match("not supported by this platform"), testName + "the error message is useful"); }, assert_null : function (opts, err, buffer, testName) { QUnit.assert.equal(buffer, null, testName + "no data"); QUnit.assert.ok(err.message.match("No output type specified"), testName + "the error message is useful"); } }; QUnit.test("add file: file(name, undefined)", function (assert) { var zip = new JSZip(), undef; zip.file("file.txt", undef); testFileDataGetters(assert, {name : "undefined", zip : zip, textData : ""}); zip = new JSZip(); zip.file("file.txt", undef, {binary:true}); testFileDataGetters(assert, {name : "undefined as binary", zip : zip, textData : ""}); zip = new JSZip(); zip.file("file.txt", undef, {base64:true}); testFileDataGetters(assert, {name : "undefined as base64", zip : zip, textData : ""}); }); QUnit.test("add file: file(name, null)", function (assert) { var zip = new JSZip(); zip.file("file.txt", null); testFileDataGetters(assert, {name : "null", zip : zip, textData : ""}); zip = new JSZip(); zip.file("file.txt", null, {binary:true}); testFileDataGetters(assert, {name : "null as binary", zip : zip, textData : ""}); zip = new JSZip(); zip.file("file.txt", null, {base64:true}); testFileDataGetters(assert, {name : "null as base64", zip : zip, textData : ""}); }); QUnit.test("add file: file(name, stringAsText)", function (assert) { var zip = new JSZip(); zip.file("file.txt", "€15\n", {binary:false}); testFileDataGetters(assert, {name : "utf8", zip : zip, textData : "€15\n", rawData : "\xE2\x82\xAC15\n"}); zip = new JSZip(); zip.file("file.txt", "test\r\ntest\r\n", {binary:false}); testFileDataGetters(assert, {name : "\\r\\n", zip : zip, textData : "test\r\ntest\r\n"}); }); QUnit.test("add file: file(name, stringAsBinary)", function (assert) { var zip = new JSZip(); zip.file("file.txt", "\xE2\x82\xAC15\n", {binary:true}); testFileDataGetters(assert, {name : "utf8", zip : zip, textData : "€15\n", rawData : "\xE2\x82\xAC15\n"}); zip = new JSZip(); zip.file("file.txt", "test\r\ntest\r\n", {binary:true}); testFileDataGetters(assert, {name : "\\r\\n", zip : zip, textData : "test\r\ntest\r\n"}); }); QUnit.test("add file: file(name, array)", function (assert) { var zip = new JSZip(); function toArray(str) { var array = new Array(str.length); for (var i = 0; i < str.length; i++) { array[i] = str.charCodeAt(i); } return array; } zip.file("file.txt", toArray("\xE2\x82\xAC15\n"), {binary:true}); testFileDataGetters(assert, {name : "utf8", zip : zip, textData : "€15\n", rawData : "\xE2\x82\xAC15\n"}); zip = new JSZip(); zip.file("file.txt", toArray("test\r\ntest\r\n"), {binary:true}); testFileDataGetters(assert, {name : "\\r\\n", zip : zip, textData : "test\r\ntest\r\n"}); }); QUnit.test("add file: file(name, base64)", function (assert) { var zip = new JSZip(); zip.file("file.txt", "4oKsMTUK", {base64:true}); testFileDataGetters(assert, {name : "utf8", zip : zip, textData : "€15\n", rawData : "\xE2\x82\xAC15\n"}); zip = new JSZip(); zip.file("file.txt", "dGVzdA0KdGVzdA0K", {base64:true}); testFileDataGetters(assert, {name : "\\r\\n", zip : zip, textData : "test\r\ntest\r\n"}); }); QUnit.test("add file: file(name, unsupported)", function (assert) { var done = assert.async(); var zip = new JSZip(); zip.file("test.txt", new Date()); zip.file("test.txt") .async("string") // XXX zip.file(name, data) returns a ZipObject for chaining, // we need to try to get the value to get the error .then(function () { assert.ok(false, "An unsupported object was added, but no exception thrown"); done(); }, function (e) { assert.ok(e.message.match("Is it in a supported JavaScript type"), "the error message is useful"); done(); }); }); if (JSZip.support.uint8array) { QUnit.test("add file: file(name, Uint8Array)", function (assert) { var str2array = function (str) { var array = new Uint8Array(str.length); for(var i = 0; i < str.length; i++) { array[i] = str.charCodeAt(i); } return array; }; var zip = new JSZip(); zip.file("file.txt", str2array("\xE2\x82\xAC15\n")); testFileDataGetters(assert, {name : "utf8", zip : zip, textData : "€15\n", rawData : "\xE2\x82\xAC15\n"}); zip = new JSZip(); zip.file("file.txt", str2array("test\r\ntest\r\n")); testFileDataGetters(assert, {name : "\\r\\n", zip : zip, textData : "test\r\ntest\r\n"}); zip = new JSZip(); zip.file("file.txt", str2array("")); testFileDataGetters(assert, {name : "empty content", zip : zip, textData : ""}); }); } if (JSZip.support.arraybuffer) { QUnit.test("add file: file(name, ArrayBuffer)", function (assert) { var str2buffer = function (str) { var array = new Uint8Array(str.length); for(var i = 0; i < str.length; i++) { array[i] = str.charCodeAt(i); } return array.buffer; }; var zip = new JSZip(); zip.file("file.txt", str2buffer("\xE2\x82\xAC15\n")); testFileDataGetters(assert, {name : "utf8", zip : zip, textData : "€15\n", rawData : "\xE2\x82\xAC15\n"}); zip = new JSZip(); zip.file("file.txt", str2buffer("test\r\ntest\r\n")); testFileDataGetters(assert, {name : "\\r\\n", zip : zip, textData : "test\r\ntest\r\n"}); zip = new JSZip(); zip.file("file.txt", str2buffer("")); testFileDataGetters(assert, {name : "empty content", zip : zip, textData : ""}); }); } if (JSZip.support.blob) { QUnit.test("add file: file(name, Blob)", function (assert) { var zip = new JSZip(); zip.file("file.txt", str2blob("\xE2\x82\xAC15\n")); testFileDataGetters(assert, {name : "utf8", zip : zip, textData : "€15\n", rawData : "\xE2\x82\xAC15\n"}); zip = new JSZip(); zip.file("file.txt", str2blob("test\r\ntest\r\n")); testFileDataGetters(assert, {name : "\\r\\n", zip : zip, textData : "test\r\ntest\r\n"}); zip = new JSZip(); zip.file("file.txt", str2blob("")); testFileDataGetters(assert, {name : "empty content", zip : zip, textData : ""}); }); } if (typeof Promise !== "undefined") { QUnit.test("add file: file(name, native Promise)", function (assert) { var str2promise = function (str) { return new Promise(function(resolve) { setTimeout(function () { resolve(str); }, 10); }); }; var zip = new JSZip(); zip.file("file.txt", str2promise("\xE2\x82\xAC15\n")); testFileDataGetters(assert, {name : "utf8", zip : zip, textData : "€15\n", rawData : "\xE2\x82\xAC15\n"}); zip = new JSZip(); zip.file("file.txt", str2promise("test\r\ntest\r\n")); testFileDataGetters(assert, {name : "\\r\\n", zip : zip, textData : "test\r\ntest\r\n"}); zip = new JSZip(); zip.file("file.txt", str2promise("")); testFileDataGetters(assert, {name : "empty content", zip : zip, textData : ""}); }); } QUnit.test("add file: file(name, polyfill Promise[string] as binary)", function (assert) { var str2promise = function (str) { return new JSZip.external.Promise(function(resolve) { setTimeout(function () { resolve(str); }, 10); }); }; var zip = new JSZip(); zip.file("file.txt", str2promise("\xE2\x82\xAC15\n"), {binary: true}); testFileDataGetters(assert, {name : "utf8", zip : zip, textData : "€15\n", rawData : "\xE2\x82\xAC15\n"}); }); QUnit.test("add file: file(name, polyfill Promise[string] force text)", function (assert) { var str2promise = function (str) { return new JSZip.external.Promise(function(resolve) { setTimeout(function () { resolve(str); }, 10); }); }; var zip = new JSZip(); zip.file("file.txt", str2promise("€15\n"), {binary: false}); testFileDataGetters(assert, {name : "utf8", zip : zip, textData : "€15\n", rawData : "\xE2\x82\xAC15\n"}); }); /* * Fix #325 for this one * QUnit.test("add file: file(name, polyfill Promise[string] as text)", function (assert) { var str2promise = function (str) { return new JSZip.external.Promise(function(resolve, reject) { setTimeout(function () { resolve(str); }, 10); }); }; var zip = new JSZip(); zip.file("file.txt", str2promise("€15\n")); testFileDataGetters(assert, {name : "utf8", zip : zip, textData : "€15\n", rawData : "\xE2\x82\xAC15\n"}); zip = new JSZip(); zip.file("file.txt", str2promise("test\r\ntest\r\n")); testFileDataGetters(assert, {name : "\\r\\n", zip : zip, textData : "test\r\ntest\r\n"}); zip = new JSZip(); zip.file("file.txt", str2promise("")); testFileDataGetters(assert, {name : "empty content", zip : zip, textData : ""}); }); */ if (JSZip.support.blob) { QUnit.test("add file: file(name, polyfill Promise[Blob])", function (assert) { var str2promiseOfBlob = function (str) { return new JSZip.external.Promise(function(resolve) { setTimeout(function () { resolve(str2blob(str)); }, 10); }); }; var zip = new JSZip(); zip.file("file.txt", str2promiseOfBlob("\xE2\x82\xAC15\n")); testFileDataGetters(assert, {name : "utf8", zip : zip, textData : "€15\n", rawData : "\xE2\x82\xAC15\n"}); zip = new JSZip(); zip.file("file.txt", str2promiseOfBlob("test\r\ntest\r\n")); testFileDataGetters(assert, {name : "\\r\\n", zip : zip, textData : "test\r\ntest\r\n"}); zip = new JSZip(); zip.file("file.txt", str2promiseOfBlob("")); testFileDataGetters(assert, {name : "empty content", zip : zip, textData : ""}); }); } if (JSZip.support.nodebuffer) { QUnit.test("add file: file(name, Buffer)", function (assert) { var str2buffer = function (str) { var array = new Buffer(str.length); for(var i = 0; i < str.length; i++) { array[i] = str.charCodeAt(i); } return array; }; var zip = new JSZip(); zip.file("file.txt", str2buffer("\xE2\x82\xAC15\n")); testFileDataGetters(assert, {name : "utf8", zip : zip, textData : "€15\n", rawData : "\xE2\x82\xAC15\n"}); zip = new JSZip(); zip.file("file.txt", str2buffer("test\r\ntest\r\n")); testFileDataGetters(assert, {name : "\\r\\n", zip : zip, textData : "test\r\ntest\r\n"}); zip = new JSZip(); zip.file("file.txt", str2buffer("")); testFileDataGetters(assert, {name : "empty content", zip : zip, textData : ""}); }); } QUnit.module("about folders"); QUnit.test("Zip folder() shouldn't throw an exception", function (assert) { var zip = new JSZip(); try { zip.folder(); assert.ok(true, "no exception thrown"); } catch (e) { assert.ok(false, e.message||e); } }); JSZipTestUtils.testZipFile("Zip empty folder", "ref/folder.zip", function(assert, expected) { var zip = new JSZip(); zip.folder("folder"); var done = assert.async(); zip.generateAsync({type:"binarystring"}).then(function(actual) { assert.ok(JSZipTestUtils.similar(actual, expected, JSZipTestUtils.MAX_BYTES_DIFFERENCE_PER_ZIP_ENTRY) , "Generated ZIP matches reference ZIP"); JSZipTestUtils.checkGenerateStability(assert, actual); done(); })["catch"](JSZipTestUtils.assertNoError); }); QUnit.test("file() creates a folder with dir:true", function (assert) { var zip = new JSZip(); zip.file("folder", null, { dir : true }); assert.ok(zip.files["folder/"].dir, "the folder with options is marked as a folder"); }); QUnit.test("file() creates a folder with the right unix permissions", function (assert) { var zip = new JSZip(); zip.file("folder", null, { unixPermissions : parseInt("40500", 8) }); assert.ok(zip.files["folder/"].dir, "the folder with options is marked as a folder"); }); QUnit.test("file() creates a folder with the right dos permissions", function (assert) { var zip = new JSZip(); zip.file("folder", null, { dosPermissions : parseInt("010000", 2) }); assert.ok(zip.files["folder/"].dir, "the folder with options is marked as a folder"); }); QUnit.test("A folder stays a folder when created with file", function (assert) { var referenceDate = new Date("July 17, 2009 14:36:56"); var referenceComment = "my comment"; var zip = new JSZip(); zip.file("folder", null, { dir : true, date : referenceDate, comment : referenceComment, unixPermissions : parseInt("40500", 8) }); assert.ok(zip.files["folder/"].dir, "the folder with options is marked as a folder"); assert.equal(zip.files["folder/"].date.getTime(), referenceDate.getTime(), "the folder with options has the correct date"); assert.equal(zip.files["folder/"].comment, referenceComment, "the folder with options has the correct comment"); assert.equal(zip.files["folder/"].unixPermissions.toString(8), "40500", "the folder with options has the correct UNIX permissions"); var done = assert.async(); zip.generateAsync({type:"string", platform:"UNIX"}) .then(JSZip.loadAsync) .then(function (reloaded) { assert.ok(reloaded.files["folder/"].dir, "the folder with options is marked as a folder"); assert.ok(reloaded.files["folder/"].dir, "the folder with options is marked as a folder"); assert.equal(reloaded.files["folder/"].date.getTime(), referenceDate.getTime(), "the folder with options has the correct date"); assert.equal(reloaded.files["folder/"].comment, referenceComment, "the folder with options has the correct comment"); assert.equal(reloaded.files["folder/"].unixPermissions.toString(8), "40500", "the folder with options has the correct UNIX permissions"); done(); })["catch"](JSZipTestUtils.assertNoError); }); QUnit.test("file() adds a slash for directories", function (assert) { var zip = new JSZip(); zip.file("folder_without_slash", null, { dir : true }); zip.file("folder_with_slash/", null, { dir : true }); assert.ok(zip.files["folder_without_slash/"], "added a slash if not provided"); assert.ok(zip.files["folder_with_slash/"], "keep the existing slash"); }); QUnit.test("folder() doesn't overwrite existing entries", function (assert) { var referenceComment = "my comment"; var zip = new JSZip(); zip.file("folder", null, { dir : true, comment : referenceComment, unixPermissions : parseInt("40500", 8) }); // calling folder() doesn't override it zip.folder("folder"); assert.equal(zip.files["folder/"].comment, referenceComment, "the folder with options has the correct comment"); assert.equal(zip.files["folder/"].unixPermissions.toString(8), "40500", "the folder with options has the correct UNIX permissions"); }); QUnit.test("createFolders works on a file", function (assert) { var zip = new JSZip(); zip.file("false/0/1/2/file", "content", {createFolders:false, unixPermissions:"644"}); zip.file("true/0/1/2/file", "content", {createFolders:true, unixPermissions:"644"}); assert.ok(!zip.files["false/"], "the false/ folder doesn't exist"); assert.ok(zip.files["true/"], "the true/ folder exists"); assert.equal(zip.files["true/"].unixPermissions, null, "the options are not propagated"); }); QUnit.test("createFolders works on a folder", function (assert) { var zip = new JSZip(); zip.file("false/0/1/2/folder", null, {createFolders:false, unixPermissions:"777",dir:true}); zip.file("true/0/1/2/folder", null, {createFolders:true, unixPermissions:"777",dir:true}); assert.ok(!zip.files["false/"], "the false/ folder doesn't exist"); assert.ok(zip.files["true/"], "the true/ folder exists"); assert.equal(zip.files["true/"].unixPermissions, null, "the options are not propagated"); }); QUnit.test("folder follows the default createFolders settings", function (assert) { var zip = new JSZip(); zip.folder("true/0/1/2/folder"); assert.ok(zip.files["true/"], "the true/ folder exists"); }); QUnit.test("A folder stays a folder", function (assert) { var zip = new JSZip(); zip.folder("folder/"); assert.ok(zip.files["folder/"].dir, "the folder is marked as a folder"); var done = assert.async(); zip.generateAsync({type:"binarystring"}) .then(JSZip.loadAsync) .then(function (reloaded) { assert.ok(reloaded.files["folder/"].dir, "the folder is marked as a folder"); done(); })["catch"](JSZipTestUtils.assertNoError); }); QUnit.test("Folders are created by default", function (assert) { var zip = new JSZip(); zip.file("test/Readme", "Hello World!\n"); assert.ok(zip.files["test/Readme"], "the file exists"); assert.ok(zip.files["test/"], "the folder exists"); }); QUnit.test("Folders can be avoided with createFolders", function (assert) { var zip = new JSZip(); zip.file("test/Readme", "Hello World!\n", {createFolders: false}); assert.ok(zip.files["test/Readme"], "the file exists"); assert.ok(!zip.files["test/"], "the folder doesn't exist"); }); QUnit.module("find entries"); QUnit.test("Finding a file", function(assert) { var zip = new JSZip(); zip.file("Readme", "Hello World!\n"); zip.file("Readme.French", "Bonjour tout le monde!\n"); zip.file("Readme.Pirate", "Ahoy m'hearty!\n"); var done = assert.async(); zip.file("Readme.French").async("string").then(function (content) { assert.equal(content, "Bonjour tout le monde!\n", "Exact match found"); done(); })["catch"](JSZipTestUtils.assertNoError); assert.equal(zip.file("Readme.Deutsch"), null, "Match exactly nothing"); assert.equal(zip.file(/Readme\../).length, 2, "Match regex free text"); assert.equal(zip.file(/pirate/i).length, 1, "Match regex 1 result"); }); QUnit.test("Finding a file (text search) with a relative folder", function (assert) { var zip = new JSZip(); zip.folder("files/default").file("Readme", "Hello World!\n"); zip.folder("files/translation").file("Readme.French", "Bonjour tout le monde!\n"); zip.folder("files").folder("translation").file("Readme.Pirate", "Ahoy m'hearty!\n"); var done = assert.async(3); zip.file("files/translation/Readme.French").async("string").then(function (content) { assert.equal(content, "Bonjour tout le monde!\n", "finding file with the full path"); done(); })["catch"](JSZipTestUtils.assertNoError); zip.folder("files").file("translation/Readme.French").async("string").then(function (content) { assert.equal(content, "Bonjour tout le monde!\n", "finding file with a relative path"); done(); })["catch"](JSZipTestUtils.assertNoError); zip.folder("files/translation").file("Readme.French").async("string").then(function (content) { assert.equal(content, "Bonjour tout le monde!\n", "finding file with a relative path"); done(); })["catch"](JSZipTestUtils.assertNoError); }); QUnit.test("Finding files (regex) with a relative folder", function (assert) { var zip = new JSZip(); zip.folder("files/default").file("Readme", "Hello World!\n"); zip.folder("files/translation").file("Readme.French", "Bonjour tout le monde!\n"); zip.folder("files").folder("translation").file("Readme.Pirate", "Ahoy m'hearty!\n"); assert.equal(zip.file(/Readme/).length, 3, "match files in subfolders"); assert.equal(zip.folder("files/translation").file(/Readme/).length, 2, "regex match only in subfolders"); assert.equal(zip.folder("files").folder("translation").file(/Readme/).length, 2, "regex match only in subfolders"); assert.equal(zip.folder("files/translation").file(/pirate/i).length, 1, "regex match only in subfolders"); assert.equal(zip.folder("files/translation").file(/^readme/i).length, 2, "regex match only with the relative path"); assert.equal(zip.folder("files/default").file(/pirate/i).length, 0, "regex match only in subfolders"); }); QUnit.test("Finding folders", function (assert) { var zip = new JSZip(); zip.folder("root/").folder("sub1/"); zip.folder("root/sub2/subsub1"); assert.equal(zip.folder(/sub2\/$/).length, 1, "unique result"); assert.equal(zip.folder(/sub1/).length, 2, "multiple results"); assert.equal(zip.folder(/root/).length, 4, "match on whole path"); }); QUnit.test("Finding folders with relative path", function (assert) { var zip = new JSZip(); zip.folder("root/").folder("sub1/"); zip.folder("root/sub2/subsub1"); var root = zip.folder("root/sub2"); assert.equal(root.folder(/sub2\/$/).length, 0, "current folder is not matched"); assert.equal(root.folder(/sub1/).length, 1, "sub folder is matched"); assert.equal(root.folder(/^subsub1/).length, 1, "relative folder path is used"); assert.equal(root.folder(/root/).length, 0, "parent folder is not matched"); }); function zipObjectsAssertions(assert, zipObject) { var date = new Date("July 17, 2009 14:36:57"); assert.equal(zipObject.name, "Hello.txt", "ZipObject#name is here"); assert.equal(zipObject.comment, "my comment", "ZipObject#comment is here"); // the zip date has a 2s resolution var delta = Math.abs(zipObject.date.getTime() - date.getTime()); assert.ok(delta < 2000/* ms */, date, "ZipObject#date is here"); } QUnit.test("ZipObject attributes", function (assert) { var date = new Date("July 17, 2009 14:36:57"); var zip = new JSZip(); zip.file("Hello.txt", "Hello World\n", {comment:"my comment", date:date}); zipObjectsAssertions(assert, zip.file("Hello.txt")); zipObjectsAssertions(assert, zip.files["Hello.txt"]); var done = assert.async(); zip.generateAsync({type:"binarystring"}) .then(JSZip.loadAsync) .then(function(reloaded) { zipObjectsAssertions(assert, reloaded.file("Hello.txt")); zipObjectsAssertions(assert, reloaded.files["Hello.txt"]); done(); })["catch"](JSZipTestUtils.assertNoError); }); QUnit.test("generate uses updated ZipObject date attribute", function (assert) { var date = new Date("July 17, 2009 14:36:57"); var zip = new JSZip(); zip.file("Hello.txt", "Hello World\n", {comment:"my comment"}); // date = now zip.files["Hello.txt"].date = date; var done = assert.async(); zip.generateAsync({type:"binarystring"}) .then(JSZip.loadAsync) .then(function(reloaded) { zipObjectsAssertions(assert, reloaded.file("Hello.txt")); zipObjectsAssertions(assert, reloaded.files["Hello.txt"]); done(); })["catch"](JSZipTestUtils.assertNoError); }); }); ================================================ FILE: test/asserts/filter.js ================================================ "use strict"; QUnit.module("filter"); QUnit.test("Filtering a zip", function(assert) { var zip = new JSZip(); zip.file("1.txt", "1\n"); zip.file("2.txt", "2\n"); zip.file("3.log", "3\n"); var result = zip.filter(function(relativeFilename) { return relativeFilename.indexOf(".txt") !== -1; }); assert.equal(result.length, 2, "filter has filtered"); assert.ok(result[0].name.indexOf(".txt") !== -1, "filter has filtered the good file"); assert.ok(result[1].name.indexOf(".txt") !== -1, "filter has filtered the good file"); }); QUnit.test("Filtering a zip from a relative path", function(assert) { var zip = new JSZip(); zip.file("foo/1.txt", "1\n"); zip.file("foo/2.txt", "2\n"); zip.file("foo/3.log", "3\n"); zip.file("1.txt", "1\n"); zip.file("2.txt", "2\n"); zip.file("3.log", "3\n"); var count = 0; var result = zip.folder("foo").filter(function(relativeFilename) { count++; return relativeFilename.indexOf("3") !== -1; }); assert.equal(count, 3, "the callback has been called the right number of times"); assert.equal(result.length, 1, "filter has filtered"); assert.equal(result[0].name, "foo/3.log", "filter has filtered the good file"); }); QUnit.test("Filtering a zip : the full path is still accessible", function(assert) { var zip = new JSZip(); zip.file("foo/1.txt", "1\n"); zip.file("foo/2.txt", "2\n"); zip.file("foo/3.log", "3\n"); zip.file("1.txt", "1\n"); zip.file("2.txt", "2\n"); zip.file("3.log", "3\n"); var result = zip.folder("foo").filter(function(relativeFilename, file) { return file.name.indexOf("3") !== -1; }); assert.equal(result.length, 1, "the filter only match files/folders in the current folder"); assert.equal(result[0].name, "foo/3.log", "filter has filtered the good file"); }); ================================================ FILE: test/asserts/foreach.js ================================================ "use strict"; QUnit.module("forEach"); QUnit.test("forEach works on /", function (assert) { var zip = JSZipTestUtils.createZipAll(); var count = 0; var calls = []; assert.equal(zip.root, ""); zip.forEach(function (path, elt) { assert.equal(path, elt.name, "the full path is given on / for " + elt.name); count++; calls.push(path); }); assert.equal(count, 3, "the callback has been called the right number of times"); assert.deepEqual(calls, ["Hello.txt", "images/", "images/smile.gif"], "all paths have been called"); }); QUnit.test("forEach works on a sub folder", function (assert) { var zip = new JSZip(); var sub = zip.folder("subfolder"); sub.file("Hello.txt", "Hello World\n"); sub.folder("images").file("smile.gif", "R0lGODdhBQAFAIACAAAAAP/eACwAAAAABQAFAAACCIwPkWerClIBADs=", {base64: true}); var count = 0; var calls = []; assert.ok(zip.file("subfolder/Hello.txt")); assert.equal(sub.root, "subfolder/"); sub.forEach(function (path, elt) { assert.equal(path, elt.name.substr("subfolder/".length), "the full path is given on subfolder/ for " + path); count++; calls.push(path); }); assert.equal(count, 3, "the callback has been called the right number of times"); assert.deepEqual(calls, ["Hello.txt", "images/", "images/smile.gif"], "all paths have been called"); }); ================================================ FILE: test/asserts/generate.js ================================================ "use strict"; QUnit.module("generate"); function testGenerateFor(testCases, fn) { while(testCases.length) { var testCase = testCases.shift(); fn(testCase.name, testCase.file, testCase.streamFiles); } } function testGenerate(assert, options) { var done = assert.async(); var triggeredCallback = false; new JSZip.external.Promise(function(resolve) { resolve(options.prepare()); }) .then(function (zip) { JSZipTestUtils.checkBasicStreamBehavior(assert, zip.generateInternalStream(options.options)); return zip; }) .then(function(zip) { var promise = zip.generateAsync(options.options); zip.file("Hello.txt", "updating the zip file after the call won't change the result"); return promise; }) .then(function(result) { triggeredCallback = true; options.assertions(null, result); if (!options.skipReloadTest) { JSZipTestUtils.checkGenerateStability(assert, result, options.options); } done(); }, function (err) { triggeredCallback = true; options.assertions(err, null); done(); }); assert.ok(!triggeredCallback, "the async callback is async"); } testGenerateFor([{ name : "no stream", file : "ref/all.zip", streamFiles : false }, { name : "with stream", // zip -fd -0 -X -r all-stream.zip Hello.txt images/ file : "ref/all-stream.zip", streamFiles : true }], function(testName, file, streamFiles) { JSZipTestUtils.testZipFile("generate : type:string. " + testName, file, function(assert, expected) { testGenerate(assert, { prepare : JSZipTestUtils.createZipAll, options : {type:"binarystring",streamFiles:streamFiles}, assertions : function (err, result) { assert.equal(err, null, "no error"); assert.ok(JSZipTestUtils.similar(result, expected, 3 * JSZipTestUtils.MAX_BYTES_DIFFERENCE_PER_ZIP_ENTRY) , "generated ZIP matches reference ZIP"); } }); }); JSZipTestUtils.testZipFile("generate : type:base64. " + testName, file, function(assert) { testGenerate(assert, { prepare : function () { // fix date to get a predictable output var zip = new JSZip(); zip.file("Hello.txt", "Hello World\n", {date: new Date(1234567891011)}); zip.file("images", null, {dir:true, date: new Date(1234876591011)}); zip.file("images/smile.gif", "R0lGODdhBQAFAIACAAAAAP/eACwAAAAABQAFAAACCIwPkWerClIBADs=", {base64: true, date: new Date(1234123491011)}); return zip; }, skipReloadTest : true, options : {type:"base64",streamFiles:streamFiles}, assertions : function (err, result) { assert.equal(err, null, "no error"); assert.equal(result, JSZipTestUtils.base64encode("all.zip.base64,stream=" + streamFiles), "generated ZIP matches reference ZIP"); } }); }); JSZipTestUtils.testZipFile("generate : type:uint8array. " + testName, file, function(assert, expected) { testGenerate(assert, { prepare : JSZipTestUtils.createZipAll, options : {type:"uint8array",streamFiles:streamFiles}, assertions : function (err, result) { if (JSZip.support.uint8array) { assert.equal(err, null, "no error"); assert.ok(result instanceof Uint8Array, "the result is a instance of Uint8Array"); // var actual = JSZipTestUtils.toString(result); assert.ok(JSZipTestUtils.similar(result, expected, 3 * JSZipTestUtils.MAX_BYTES_DIFFERENCE_PER_ZIP_ENTRY) , "generated ZIP matches reference ZIP"); } else { assert.equal(result, null, "no data"); assert.ok(err.message.match("not supported by this platform"), "the error message is useful"); } } }); }); JSZipTestUtils.testZipFile("generate : type:arraybuffer. " + testName, file, function(assert, expected) { testGenerate(assert, { prepare : JSZipTestUtils.createZipAll, options : {type:"arraybuffer",streamFiles:streamFiles}, assertions : function (err, result) { if (JSZip.support.arraybuffer) { assert.equal(err, null, "no error"); assert.ok(result instanceof ArrayBuffer, "the result is a instance of ArrayBuffer"); assert.ok(JSZipTestUtils.similar(result, expected, 3 * JSZipTestUtils.MAX_BYTES_DIFFERENCE_PER_ZIP_ENTRY) , "generated ZIP matches reference ZIP"); } else { assert.equal(result, null, "no data"); assert.ok(err.message.match("not supported by this platform"), "the error message is useful"); } } }); }); JSZipTestUtils.testZipFile("generate : type:nodebuffer. " + testName, file, function(assert, expected) { testGenerate(assert, { prepare : JSZipTestUtils.createZipAll, options : {type:"nodebuffer",streamFiles:streamFiles}, assertions : function (err, result) { if (JSZip.support.nodebuffer) { assert.equal(err, null, "no error"); assert.ok(result instanceof Buffer, "the result is a instance of ArrayBuffer"); var actual = JSZipTestUtils.toString(result); assert.ok(JSZipTestUtils.similar(actual, expected, 3 * JSZipTestUtils.MAX_BYTES_DIFFERENCE_PER_ZIP_ENTRY) , "generated ZIP matches reference ZIP"); } else { assert.equal(result, null, "no data"); assert.ok(err.message.match("not supported by this platform"), "the error message is useful"); } } }); }); JSZipTestUtils.testZipFile("generate : type:blob. " + testName, file, function(assert, expected) { testGenerate(assert, { prepare : JSZipTestUtils.createZipAll, options : {type:"blob",streamFiles:streamFiles}, skipReloadTest : true, assertions : function (err, result) { if (JSZip.support.blob) { assert.equal(err, null, "no error"); assert.ok(result instanceof Blob, "the result is a instance of Blob"); assert.equal(result.type, "application/zip", "the result has the right mime type"); assert.equal(result.size, expected.length, "the result has the right length"); } else { assert.equal(result, null, "no data"); assert.ok(err.message.match("not supported by this platform"), "the error message is useful"); } } }); }); JSZipTestUtils.testZipFile("generate : type:blob mimeType:application/ods. " + testName, file, function(assert, expected) { testGenerate(assert, { prepare : JSZipTestUtils.createZipAll, options : {type:"blob",mimeType: "application/ods",streamFiles:streamFiles}, skipReloadTest : true, assertions : function (err, result) { if (JSZip.support.blob) { assert.equal(err, null, "no error"); assert.ok(result instanceof Blob, "the result is a instance of Blob"); assert.equal(result.type, "application/ods", "the result has the right mime type"); assert.equal(result.size, expected.length, "the result has the right length"); } else { assert.equal(result, null, "no data"); assert.ok(err.message.match("not supported by this platform"), "the error message is useful"); } } }); }); }); testGenerateFor([{ name : "no stream", // zip -0 -X store.zip Hello.txt file : "ref/store.zip", streamFiles : false }, { name : "with stream", // zip -0 -X -fd store-stream.zip Hello.txt file : "ref/store-stream.zip", streamFiles : true }], function(testName, file, streamFiles) { JSZipTestUtils.testZipFile("STORE doesn't compress, " + testName, file, function(assert, expected) { testGenerate(assert, { prepare : function () { var zip = new JSZip(); zip.file("Hello.txt", "This a looong file : we need to see the difference between the different compression methods.\n"); return zip; }, options : {type:"binarystring", compression:"STORE",streamFiles:streamFiles}, assertions : function (err, result) { assert.equal(err, null, "no error"); assert.ok(JSZipTestUtils.similar(result, expected, JSZipTestUtils.MAX_BYTES_DIFFERENCE_PER_ZIP_ENTRY) , "generated ZIP matches reference ZIP"); } }); }); }); testGenerateFor([{ name : "no stream", // zip -6 -X deflate.zip Hello.txt file : "ref/deflate.zip", streamFiles : false }, { name : "with stream", // zip -6 -X -fd deflate-stream.zip Hello.txt file : "ref/deflate-stream.zip", streamFiles : true }], function(testName, file, streamFiles) { JSZipTestUtils.testZipFile("DEFLATE compress, " + testName, file, function(assert, expected) { testGenerate(assert, { prepare : function () { var zip = new JSZip(); zip.file("Hello.txt", "This a looong file : we need to see the difference between the different compression methods.\n"); return zip; }, options : {type:"binarystring", compression:"DEFLATE",streamFiles:streamFiles}, assertions : function (err, result) { assert.equal(err, null, "no error"); assert.ok(JSZipTestUtils.similar(result, expected, JSZipTestUtils.MAX_BYTES_DIFFERENCE_PER_ZIP_ENTRY) , "generated ZIP matches reference ZIP"); } }); }); }); JSZipTestUtils.testZipFile("STORE is the default method", "ref/text.zip", function(assert, expected) { var zip = new JSZip(); zip.file("Hello.txt", "Hello World\n"); var done = assert.async(); zip.generateAsync({type:"binarystring", compression:"STORE"}).then(function(content) { // no difference with the "Zip text file" test. assert.ok(JSZipTestUtils.similar(content, expected, JSZipTestUtils.MAX_BYTES_DIFFERENCE_PER_ZIP_ENTRY) , "Generated ZIP matches reference ZIP"); done(); })["catch"](JSZipTestUtils.assertNoError); }); function testLazyDecompression(assert, from, to) { var done = assert.async(); JSZipTestUtils.createZipAll().generateAsync({type:"binarystring", compression:from}).then(function(actual) { done(); testGenerate(assert, { prepare : function () { // the zip object will contain compressed objects return JSZip.loadAsync(actual); }, skipReloadTest : true, options : {type:"binarystring", compression:to}, assertions : function (err) { assert.equal(err, null, from + " -> " + to + " : no error"); } }); })["catch"](JSZipTestUtils.assertNoError); } QUnit.test("Lazy decompression works", function(assert) { testLazyDecompression(assert, "STORE", "STORE"); testLazyDecompression(assert, "DEFLATE", "STORE"); testLazyDecompression(assert, "STORE", "DEFLATE"); testLazyDecompression(assert, "DEFLATE", "DEFLATE"); }); // zip -0 -X empty.zip plop && zip -d empty.zip plop JSZipTestUtils.testZipFile("empty zip", "ref/empty.zip", function(assert, expected) { testGenerate(assert, { prepare : function () { var zip = new JSZip(); return zip; }, options : {type:"binarystring"}, assertions : function (err, result) { assert.equal(err, null, "no error"); assert.ok(JSZipTestUtils.similar(result, expected, 0 * JSZipTestUtils.MAX_BYTES_DIFFERENCE_PER_ZIP_ENTRY) , "generated ZIP matches reference ZIP"); } }); }); QUnit.test("unknown compression throws an exception", function (assert) { testGenerate(assert, { prepare : JSZipTestUtils.createZipAll, options : {type:"string",compression:"MAYBE"}, assertions : function (err, result) { assert.equal(result, null, "no data"); assert.ok(err.message.match("not a valid compression"), "the error message is useful"); } }); }); QUnit.test("missing type throws an exception", function (assert) { testGenerate(assert, { prepare : JSZipTestUtils.createZipAll, options : {}, assertions : function (err, result) { assert.equal(result, null, "no data"); assert.ok(err.message.match("No output type specified."), "the error message is useful"); } }); }); QUnit.test("generateAsync uses the current folder level", function (assert) { var done = assert.async(); var zip = new JSZip(); zip.file("file1", "a"); zip.folder("root1"); zip.folder("root2").file("leaf1", "a"); zip.folder("root2") .generateAsync({type:"string"}) .then(JSZip.loadAsync) .then(function(zip) { assert.ok(!zip.file("file1"), "root files are not present"); assert.ok(!zip.file("root1"), "root folders are not present"); assert.ok(!zip.file("root2"), "root folders are not present"); assert.ok(zip.file("leaf1"), "leaves are present"); done(); })["catch"](JSZipTestUtils.assertNoError); }); QUnit.test("generateAsync keep the explicit / folder", function (assert) { var done = assert.async(); var zip = new JSZip(); zip.file("/file1", "a"); zip.file("/root1/file2", "b"); zip.generateAsync({type:"string"}) .then(JSZip.loadAsync) .then(function(zip) { assert.ok(zip.file("/file1"), "root files are present"); assert.ok(zip.file("/root1/file2"), "root folders are present"); done(); })["catch"](JSZipTestUtils.assertNoError); }); JSZipTestUtils.testZipFile("generate with promises as files", "ref/all.zip", function (assert, expected) { var done = assert.async(); var zip = new JSZip(); zip.file("Hello.txt", new JSZip.external.Promise(function (resolve) { setTimeout(function () { resolve("Hello World\n"); }, 50); })); zip.folder("images").file("smile.gif", new JSZip.external.Promise(function (resolve) { setTimeout(function () { resolve("R0lGODdhBQAFAIACAAAAAP/eACwAAAAABQAFAAACCIwPkWerClIBADs="); }, 100); }), {base64: true}); zip.generateAsync({type:"string"}) .then(function (result) { assert.ok(JSZipTestUtils.similar(result, expected, 3 * JSZipTestUtils.MAX_BYTES_DIFFERENCE_PER_ZIP_ENTRY) , "generated ZIP matches reference ZIP"); done(); })["catch"](JSZipTestUtils.assertNoError); }); ================================================ FILE: test/asserts/load.js ================================================ "use strict"; QUnit.module("load", function () { JSZipTestUtils.testZipFile("load(string) works", "ref/all.zip", function(assert, file) { var done = assert.async(); assert.ok(typeof file === "string"); JSZip.loadAsync(file) .then(function (zip) { return zip.file("Hello.txt").async("string"); }) .then(function(result) { assert.equal(result, "Hello World\n", "the zip was correctly read."); done(); })["catch"](JSZipTestUtils.assertNoError); }); JSZipTestUtils.testZipFile("Load files which shadow Object prototype methods", "ref/pollution.zip", function(assert, file) { var done = assert.async(); assert.ok(typeof file === "string"); JSZip.loadAsync(file) .then(function (zip) { assert.notEqual(Object.getPrototypeOf(zip.files), zip.files.__proto__); return zip.file("__proto__").async("string"); }) .then(function(result) { assert.equal(result, "hello\n", "the zip was correctly read."); done(); })["catch"](JSZipTestUtils.assertNoError); }); JSZipTestUtils.testZipFile("load(string) handles bytes > 255", "ref/all.zip", function(assert, file) { var done = assert.async(); // the method used to load zip with ajax will remove the extra bits. // adding extra bits :) var updatedFile = ""; for (var i = 0; i < file.length; i++) { updatedFile += String.fromCharCode((file.charCodeAt(i) & 0xff) + 0x4200); } JSZip.loadAsync(updatedFile) .then(function (zip) { return zip.file("Hello.txt").async("string"); }) .then(function (content) { assert.equal(content, "Hello World\n", "the zip was correctly read."); done(); })["catch"](JSZipTestUtils.assertNoError); }); JSZipTestUtils.testZipFile("load(Array) works", "ref/deflate.zip", function(assert, file) { var done = assert.async(); var updatedFile = new Array(file.length); for( var i = 0; i < file.length; ++i ) { updatedFile[i] = file.charCodeAt(i); } JSZip.loadAsync(updatedFile) .then(function (zip) { return zip.file("Hello.txt").async("string"); }) .then(function (content) { assert.equal(content, "This a looong file : we need to see the difference between the different compression methods.\n", "the zip was correctly read."); done(); })["catch"](JSZipTestUtils.assertNoError); }); JSZipTestUtils.testZipFile("load(array) handles bytes > 255", "ref/deflate.zip", function(assert, file) { var done = assert.async(); var updatedFile = new Array(file.length); for( var i = 0; i < file.length; ++i ) { updatedFile[i] = file.charCodeAt(i) + 0x4200; } JSZip.loadAsync(updatedFile) .then(function (zip) { return zip.file("Hello.txt").async("string"); }) .then(function (content) { assert.equal(content, "This a looong file : we need to see the difference between the different compression methods.\n", "the zip was correctly read."); done(); })["catch"](JSZipTestUtils.assertNoError); }); if (JSZip.support.arraybuffer) { JSZipTestUtils.testZipFile("load(ArrayBuffer) works", "ref/all.zip", function(assert, fileAsString) { var done = assert.async(3); var file = new ArrayBuffer(fileAsString.length); var bufferView = new Uint8Array(file); for( var i = 0; i < fileAsString.length; ++i ) { bufferView[i] = fileAsString.charCodeAt(i); } assert.ok(file instanceof ArrayBuffer); // when reading an arraybuffer, the CompressedObject mechanism will keep it and subarray() a Uint8Array. // if we request a file in the same format, we might get the same Uint8Array or its ArrayBuffer (the original zip file). JSZip.loadAsync(file) .then(function (zip) { return zip.file("Hello.txt").async("arraybuffer"); }).then(function (content){ assert.equal(content.byteLength, 12, "don't get the original buffer"); done(); })["catch"](JSZipTestUtils.assertNoError); JSZip.loadAsync(file) .then(function (zip) { return zip.file("Hello.txt").async("uint8array"); }).then(function (content){ assert.equal(content.buffer.byteLength, 12, "don't get a view of the original buffer"); done(); }); JSZip.loadAsync(file) .then(function (zip) { return zip.file("Hello.txt").async("string"); }).then(function (content){ assert.equal(content, "Hello World\n", "the zip was correctly read."); done(); }); }); } if (JSZip.support.nodebuffer) { JSZipTestUtils.testZipFile("load(Buffer) works", "ref/all.zip", function(assert, fileAsString) { var done = assert.async(); var file = new Buffer(fileAsString.length); for( var i = 0; i < fileAsString.length; ++i ) { file[i] = fileAsString.charCodeAt(i); } JSZip.loadAsync(file) .then(function (zip) { return zip.file("Hello.txt").async("string"); }).then(function (content){ assert.equal(content, "Hello World\n", "the zip was correctly read."); done(); }); }); } if (JSZip.support.uint8array) { JSZipTestUtils.testZipFile("load(Uint8Array) works", "ref/all.zip", function(assert, fileAsString) { var done = assert.async(3); var file = new Uint8Array(fileAsString.length); for( var i = 0; i < fileAsString.length; ++i ) { file[i] = fileAsString.charCodeAt(i); } assert.ok(file instanceof Uint8Array); // when reading an arraybuffer, the CompressedObject mechanism will keep it and subarray() a Uint8Array. // if we request a file in the same format, we might get the same Uint8Array or its ArrayBuffer (the original zip file). JSZip.loadAsync(file) .then(function (zip) { return zip.file("Hello.txt").async("arraybuffer"); }).then(function (content){ assert.equal(content.byteLength, 12, "don't get the original buffer"); done(); })["catch"](JSZipTestUtils.assertNoError); JSZip.loadAsync(file) .then(function (zip) { return zip.file("Hello.txt").async("uint8array"); }).then(function (content){ assert.equal(content.buffer.byteLength, 12, "don't get a view of the original buffer"); done(); })["catch"](JSZipTestUtils.assertNoError); JSZip.loadAsync(file) .then(function (zip) { return zip.file("Hello.txt").async("string"); }).then(function (content){ assert.equal(content, "Hello World\n", "the zip was correctly read."); done(); })["catch"](JSZipTestUtils.assertNoError); }); } // zip -6 -X deflate.zip Hello.txt JSZipTestUtils.testZipFile("zip with DEFLATE", "ref/deflate.zip", function(assert, file) { var done = assert.async(); JSZip.loadAsync(file) .then(function (zip) { return zip.file("Hello.txt").async("string"); }).then(function (content){ assert.equal(content, "This a looong file : we need to see the difference between the different compression methods.\n", "the zip was correctly read."); done(); })["catch"](JSZipTestUtils.assertNoError); }); // zip -0 -X -z -c archive_comment.zip Hello.txt JSZipTestUtils.testZipFile("read zip with comment", "ref/archive_comment.zip", function(assert, file) { var done = assert.async(); JSZip.loadAsync(file) .then(function (zip) { assert.equal(zip.comment, "file comment", "the archive comment was correctly read."); assert.equal(zip.file("Hello.txt").comment, "entry comment", "the entry comment was correctly read."); return zip.file("Hello.txt").async("string"); }).then(function (content){ assert.equal(content, "Hello World\n", "the zip was correctly read."); done(); })["catch"](JSZipTestUtils.assertNoError); }); JSZipTestUtils.testZipFile("generate zip with comment", "ref/archive_comment.zip", function(assert, file) { var zip = new JSZip(); zip.file("Hello.txt", "Hello World\n", {comment:"entry comment"}); var done = assert.async(); zip.generateAsync({type:"binarystring", comment:"file comment"}).then(function(generated) { assert.ok(JSZipTestUtils.similar(generated, file, JSZipTestUtils.MAX_BYTES_DIFFERENCE_PER_ZIP_ENTRY) , "Generated ZIP matches reference ZIP"); JSZipTestUtils.checkGenerateStability(assert, generated); done(); })["catch"](JSZipTestUtils.assertNoError); }); // zip -0 extra_attributes.zip Hello.txt JSZipTestUtils.testZipFile("zip with extra attributes", "ref/extra_attributes.zip", function(assert, file) { var done = assert.async(); JSZip.loadAsync(file) .then(function (zip) { return zip.file("Hello.txt").async("string"); }).then(function (content){ assert.equal(content, "Hello World\n", "the zip was correctly read."); done(); })["catch"](JSZipTestUtils.assertNoError); }); // use -fz to force use of Zip64 format // zip -fz -0 zip64.zip Hello.txt JSZipTestUtils.testZipFile("zip 64", "ref/zip64.zip", function(assert, file) { var done = assert.async(); JSZip.loadAsync(file) .then(function (zip) { return zip.file("Hello.txt").async("string"); }).then(function (content){ assert.equal(content, "Hello World\n", "the zip was correctly read."); done(); })["catch"](JSZipTestUtils.assertNoError); }); // use -fd to force data descriptors as if streaming // zip -fd -0 data_descriptor.zip Hello.txt JSZipTestUtils.testZipFile("zip with data descriptor", "ref/data_descriptor.zip", function(assert, file) { var done = assert.async(); JSZip.loadAsync(file) .then(function (zip) { return zip.file("Hello.txt").async("string"); }).then(function (content){ assert.equal(content, "Hello World\n", "the zip was correctly read."); done(); })["catch"](JSZipTestUtils.assertNoError); }); // combo of zip64 and data descriptors : // zip -fz -fd -0 data_descriptor_zip64.zip Hello.txt // this generate a corrupted zip file :( // TODO : find how to get the two features // zip -0 -X zip_within_zip.zip Hello.txt && zip -0 -X nested.zip Hello.txt zip_within_zip.zip JSZipTestUtils.testZipFile("nested zip", "ref/nested.zip", function(assert, file) { var done = assert.async(2); JSZip.loadAsync(file) .then(function (zip) { return zip.file("zip_within_zip.zip").async("binarystring"); }) .then(JSZip.loadAsync) .then(function (innerZip) { return innerZip.file("Hello.txt").async("string"); }).then(function (content) { assert.equal(content, "Hello World\n", "the inner zip was correctly read."); done(); })["catch"](JSZipTestUtils.assertNoError); JSZip.loadAsync(file) .then(function (zip) { return zip.file("Hello.txt").async("string"); }).then(function (content){ assert.equal(content, "Hello World\n", "the zip was correctly read."); done(); })["catch"](JSZipTestUtils.assertNoError); }); // zip -fd -0 nested_data_descriptor.zip data_descriptor.zip JSZipTestUtils.testZipFile("nested zip with data descriptors", "ref/nested_data_descriptor.zip", function(assert, file) { var done = assert.async(); JSZip.loadAsync(file) .then(function (zip) { return zip.file("data_descriptor.zip").async("binarystring"); }) .then(JSZip.loadAsync) .then(function (zip) { return zip.file("Hello.txt").async("string"); }).then(function (content) { assert.equal(content, "Hello World\n", "the inner zip was correctly read."); done(); })["catch"](JSZipTestUtils.assertNoError); }); // zip -fz -0 nested_zip64.zip zip64.zip JSZipTestUtils.testZipFile("nested zip 64", "ref/nested_zip64.zip", function(assert, file) { var done = assert.async(); JSZip.loadAsync(file) .then(function (zip) { return zip.file("zip64.zip").async("binarystring"); }) .then(JSZip.loadAsync) .then(function (zip) { return zip.file("Hello.txt").async("string"); }).then(function (content) { assert.equal(content, "Hello World\n", "the inner zip was correctly read."); done(); })["catch"](JSZipTestUtils.assertNoError); }); // nested zip 64 with data descriptors // zip -fz -fd -0 nested_data_descriptor_zip64.zip data_descriptor_zip64.zip // this generate a corrupted zip file :( // TODO : find how to get the two features // zip -X -0 utf8_in_name.zip €15.txt JSZipTestUtils.testZipFile("Zip text file with UTF-8 characters in filename", "ref/utf8_in_name.zip", function(assert, file) { var done = assert.async(2); JSZip.loadAsync(file) .then(function (zip){ assert.ok(zip.file("€15.txt") !== null, "the utf8 file is here."); return zip.file("€15.txt").async("string"); }) .then(function (content) { assert.equal(content, "€15\n", "the utf8 content was correctly read (with file().async)."); done(); })["catch"](JSZipTestUtils.assertNoError); JSZip.loadAsync(file) .then(function (zip){ return zip.files["€15.txt"].async("string"); }) .then(function (content) { assert.equal(content, "€15\n", "the utf8 content was correctly read (with files[].async)."); done(); })["catch"](JSZipTestUtils.assertNoError); }); // Created with winrar // winrar will replace the euro symbol with a '_' but set the correct unicode path in an extra field. JSZipTestUtils.testZipFile("Zip text file with UTF-8 characters in filename and windows compatibility", "ref/winrar_utf8_in_name.zip", function(assert, file) { var done = assert.async(2); JSZip.loadAsync(file) .then(function (zip){ assert.ok(zip.file("€15.txt") !== null, "the utf8 file is here."); return zip.file("€15.txt").async("string"); }) .then(function (content) { assert.equal(content, "€15\n", "the utf8 content was correctly read (with file().async)."); done(); })["catch"](JSZipTestUtils.assertNoError); JSZip.loadAsync(file) .then(function (zip){ return zip.files["€15.txt"].async("string"); }) .then(function (content) { assert.equal(content, "€15\n", "the utf8 content was correctly read (with files[].async)."); done(); })["catch"](JSZipTestUtils.assertNoError); }); // zip backslash.zip -0 -X Hel\\lo.txt JSZipTestUtils.testZipFile("Zip text file with backslash in filename", "ref/backslash.zip", function(assert, file) { var done = assert.async(); JSZip.loadAsync(file) .then(function (zip){ return zip.file("Hel\\lo.txt").async("string"); }) .then(function (content) { assert.equal(content, "Hello World\n", "the utf8 content was correctly read."); done(); })["catch"](JSZipTestUtils.assertNoError); }); // use izarc to generate a zip file on windows JSZipTestUtils.testZipFile("Zip text file from windows with \\ in central dir", "ref/slashes_and_izarc.zip", function(assert, file) { var done = assert.async(); JSZip.loadAsync(file) .then(function (zip){ return zip.folder("test").file("Hello.txt").async("string"); }) .then(function (content) { assert.equal(content, "Hello world\r\n", "the content was correctly read."); done(); })["catch"](JSZipTestUtils.assertNoError); }); // cat Hello.txt all.zip > all_prepended_bytes.zip JSZipTestUtils.testZipFile("zip file with prepended bytes", "ref/all_prepended_bytes.zip", function(assert, file) { var done = assert.async(); JSZip.loadAsync(file) .then(function success(zip) { return zip.file("Hello.txt").async("string"); }).then(function (content) { assert.equal(content, "Hello World\n", "the zip was correctly read."); done(); })["catch"](JSZipTestUtils.assertNoError); }); // cat all.zip Hello.txt > all_appended_bytes.zip JSZipTestUtils.testZipFile("zip file with appended bytes", "ref/all_appended_bytes.zip", function(assert, file) { var done = assert.async(); JSZip.loadAsync(file) .then(function success(zip) { return zip.file("Hello.txt").async("string"); }).then(function (content) { assert.equal(content, "Hello World\n", "the zip was correctly read."); done(); })["catch"](JSZipTestUtils.assertNoError); }); // cat Hello.txt zip64.zip > zip64_prepended_bytes.zip JSZipTestUtils.testZipFile("zip64 file with extra bytes", "ref/zip64_prepended_bytes.zip", function(assert, file) { var done = assert.async(); JSZip.loadAsync(file) .then(function success(zip) { return zip.file("Hello.txt").async("string"); }).then(function (content) { assert.equal(content, "Hello World\n", "the zip was correctly read."); done(); })["catch"](JSZipTestUtils.assertNoError); }); // cat zip64.zip Hello.txt > zip64_appended_bytes.zip JSZipTestUtils.testZipFile("zip64 file with extra bytes", "ref/zip64_appended_bytes.zip", function(assert, file) { var done = assert.async(); JSZip.loadAsync(file) .then(function success(zip) { return zip.file("Hello.txt").async("string"); }).then(function (content) { assert.equal(content, "Hello World\n", "the zip was correctly read."); done(); })["catch"](JSZipTestUtils.assertNoError); }); JSZipTestUtils.testZipFile("load(promise) works", "ref/all.zip", function(assert, fileAsString) { var done = assert.async(); JSZip.loadAsync(JSZip.external.Promise.resolve(fileAsString)) .then(function (zip) { return zip.file("Hello.txt").async("string"); }).then(function (content){ assert.equal(content, "Hello World\n", "the zip was correctly read."); done(); })["catch"](JSZipTestUtils.assertNoError); }); if (JSZip.support.blob) { JSZipTestUtils.testZipFile("load(blob) works", "ref/all.zip", function(assert, fileAsString) { var u8 = new Uint8Array(fileAsString.length); for( var i = 0; i < fileAsString.length; ++i ) { u8[i] = fileAsString.charCodeAt(i); } var file = null; try { // don't use an Uint8Array, see the comment on utils.newBlob file = new Blob([u8.buffer], {type:"application/zip"}); } catch (e) { var Builder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder; var builder = new Builder(); builder.append(u8.buffer); file = builder.getBlob("application/zip"); } var done = assert.async(); JSZip.loadAsync(file) .then(function (zip) { return zip.file("Hello.txt").async("string"); }).then(function (content){ assert.equal(content, "Hello World\n", "the zip was correctly read."); done(); })["catch"](JSZipTestUtils.assertNoError); }); } JSZipTestUtils.testZipFile("valid crc32", "ref/all.zip", function(assert, file) { var done = assert.async(); JSZip.loadAsync(file, {checkCRC32:true}) .then(function success() { assert.ok(true, "no exception were thrown"); done(); }, function failure(e) { assert.ok(false, "An exception were thrown: " + e.message); done(); }); }); JSZipTestUtils.testZipFile("loading in a sub folder", "ref/all.zip", function(assert, file) { var done = assert.async(); var zip = new JSZip(); zip.folder("sub").loadAsync(file) .then(function success(zip) { assert.ok(zip.file("Hello.txt"), "the zip was correctly read."); assert.equal(zip.file("Hello.txt").name, "sub/Hello.txt", "the zip was read in a sub folder"); assert.equal(zip.root, "sub/", "the promise contains the correct folder level"); done(); }, function failure(e) { assert.ok(false, "An exception were thrown: " + e.message); done(); }); }); JSZipTestUtils.testZipFile("loading overwrite files", "ref/all.zip", function(assert, file) { var done = assert.async(); var zip = new JSZip(); zip.file("Hello.txt", "bonjour à tous"); zip.file("Bye.txt", "au revoir"); zip.loadAsync(file) .then(function success(zip) { return JSZip.external.Promise.all([ zip.file("Hello.txt").async("text"), zip.file("Bye.txt").async("text") ]); }).then(function (result) { var hello = result[0]; var bye = result[1]; assert.equal(hello, "Hello World\n", "conflicting content was overwritten."); assert.equal(bye, "au revoir", "other content was kept."); done(); }, function failure(e) { assert.ok(false, "An exception were thrown: " + e.message); done(); }); }); QUnit.module("not supported features"); // zip -0 -X -e encrypted.zip Hello.txt JSZipTestUtils.testZipFile("basic encryption", "ref/encrypted.zip", function(assert, file) { var done = assert.async(); JSZip.loadAsync(file) .then(function success() { assert.ok(false, "Encryption is not supported, but no exception were thrown"); done(); }, function failure(e) { assert.equal(e.message, "Encrypted zip are not supported", "the error message is useful"); done(); }); }); QUnit.module("corrupted zip"); JSZipTestUtils.testZipFile("bad compression method", "ref/invalid/compression.zip", function(assert, file) { var done = assert.async(); JSZip.loadAsync(file) .then(function success() { assert.ok(false, "no exception were thrown"); done(); }, function failure(e) { assert.ok(e.message.match("Corrupted zip"), "the error message is useful"); done(); }); }); // dd if=all.zip of=all_missing_bytes.zip bs=32 skip=1 JSZipTestUtils.testZipFile("zip file with missing bytes", "ref/all_missing_bytes.zip", function(assert, file) { var done = assert.async(); JSZip.loadAsync(file) .then(function success() { assert.ok(false, "no exception were thrown"); done(); }, function failure(e) { assert.ok(e.message.match("Corrupted zip"), "the error message is useful"); done(); }); }); // dd if=zip64.zip of=zip64_missing_bytes.zip bs=32 skip=1 JSZipTestUtils.testZipFile("zip64 file with missing bytes", "ref/zip64_missing_bytes.zip", function(assert, file) { var done = assert.async(); JSZip.loadAsync(file) .then(function success() { assert.ok(false, "no exception were thrown"); done(); }, function failure(e) { assert.ok(e.message.match("Corrupted zip"), "the error message is useful"); done(); }); }); JSZipTestUtils.testZipFile("zip file with extra field is Non-standard", "ref/extra_filed_non_standard.zip", function(assert, file) { var done = assert.async(); JSZip.loadAsync(file) .then(function success() { assert.ok(true, "no exception were thrown"); done(); }, function failure(e) { assert.ok(false, "An exception were thrown: " + e.message); done(); }); }); QUnit.test("not a zip file", function(assert) { var done = assert.async(); JSZip.loadAsync("this is not a zip file") .then(function success() { assert.ok(false, "no exception were thrown"); done(); }, function failure(e) { assert.ok(e.message.match("stuk.github.io/jszip/documentation"), "the error message is useful"); done(); }); }); QUnit.test("truncated zip file", function(assert) { var done = assert.async(); JSZip.loadAsync("PK\x03\x04\x0A\x00\x00\x00") .then(function success() { done(); assert.ok(false, "no exception were thrown"); }, function failure(e) { assert.ok(e.message.match("Corrupted zip"), "the error message is useful"); done(); }); }); JSZipTestUtils.testZipFile("invalid crc32 but no check", "ref/invalid/crc32.zip", function(assert, file) { var done = assert.async(); JSZip.loadAsync(file, {checkCRC32:false}) .then(function success() { assert.ok(true, "no exception were thrown"); done(); }, function failure() { assert.ok(false, "An exception were thrown but the check should have been disabled."); done(); }); }); JSZipTestUtils.testZipFile("invalid crc32", "ref/invalid/crc32.zip", function(assert, file) { var done = assert.async(); JSZip.loadAsync(file, {checkCRC32:true}) .then(function success() { assert.ok(false, "no exception were thrown"); done(); }, function failure(e) { assert.ok(e.message.match("Corrupted zip"), "the error message is useful"); done(); }); }); JSZipTestUtils.testZipFile("bad offset", "ref/invalid/bad_offset.zip", function(assert, file) { var done = assert.async(); JSZip.loadAsync(file, {checkCRC32:false}) .then(function success() { assert.ok(false, "no exception were thrown"); done(); }, function failure(e) { assert.ok(e.message.match("Corrupted zip"), "the error message is useful"); done(); }); }); JSZipTestUtils.testZipFile("bad decompressed size, read a file", "ref/invalid/bad_decompressed_size.zip", function(assert, file) { var done = assert.async(); JSZip.loadAsync(file) .then(function (zip) { return zip.file("Hello.txt").async("string"); }) .then(function success() { assert.ok(false, "successful result in an error test"); done(); }, function failure(e) { assert.ok(e.message.match("size mismatch"), "async call : the error message is useful"); done(); }); }); JSZipTestUtils.testZipFile("bad decompressed size, generate a zip", "ref/invalid/bad_decompressed_size.zip", function(assert, file) { var done = assert.async(); JSZip.loadAsync(file) .then(function (zip) { // add other files to be sure to trigger the right code path zip.file("zz", "zz"); return zip.generateAsync({ type:"string", compression:"DEFLATE" // a different compression to force a read }); }) .then(function success() { assert.ok(false, "successful result in an error test"); done(); }, function failure(e) { assert.ok(e.message.match("size mismatch"), "async call : the error message is useful"); done(); }); }); QUnit.module("complex files"); if (typeof window === "undefined" || QUnit.urlParams.complexfiles) { // http://www.feedbooks.com/book/8/the-metamorphosis JSZipTestUtils.testZipFile("Franz Kafka - The Metamorphosis.epub", "ref/complex_files/Franz Kafka - The Metamorphosis.epub", function(assert, file) { var done = assert.async(2); JSZip.loadAsync(file) .then(function(zip) { assert.equal(zip.filter(function(){return true;}).length, 26, "the zip contains the good number of elements."); return zip.file("mimetype").async("string"); }) .then(function (content) { assert.equal(content, "application/epub+zip\r\n", "the zip was correctly read."); done(); })["catch"](JSZipTestUtils.assertNoError); JSZip.loadAsync(file) .then(function(zip) { return zip.file("OPS/main0.xml").async("string"); }) .then(function (content) { // the .ncx file tells us that the first chapter is in the main0.xml file. assert.ok(content.indexOf("One morning, as Gregor Samsa was waking up from anxious dreams") !== -1, "the zip was correctly read."); done(); })["catch"](JSZipTestUtils.assertNoError); }); // a showcase in http://msdn.microsoft.com/en-us/windows/hardware/gg463429 JSZipTestUtils.testZipFile("Outlook2007_Calendar.xps, createFolders: false", "ref/complex_files/Outlook2007_Calendar.xps", function(assert, file) { var done = assert.async(); JSZip.loadAsync(file, {createFolders: false}) .then(function(zip) { // the zip file contains 15 entries. assert.equal(zip.filter(function(){return true;}).length, 15, "the zip contains the good number of elements."); return zip.file("[Content_Types].xml").async("string"); }) .then(function (content) { assert.ok(content.indexOf("application/vnd.ms-package.xps-fixeddocument+xml") !== -1, "the zip was correctly read."); done(); })["catch"](JSZipTestUtils.assertNoError); }); // Same test as above, but with createFolders option set to true JSZipTestUtils.testZipFile("Outlook2007_Calendar.xps, createFolders: true", "ref/complex_files/Outlook2007_Calendar.xps", function(assert, file) { var done = assert.async(); JSZip.loadAsync(file, {createFolders: true}) .then(function(zip) { // the zip file contains 15 entries, but we get 23 when creating all the sub-folders. assert.equal(zip.filter(function(){return true;}).length, 23, "the zip contains the good number of elements."); return zip.file("[Content_Types].xml").async("string"); }) .then(function (content) { assert.ok(content.indexOf("application/vnd.ms-package.xps-fixeddocument+xml") !== -1, "the zip was correctly read."); done(); })["catch"](JSZipTestUtils.assertNoError); }); // an example file in http://cheeso.members.winisp.net/srcview.aspx?dir=js-unzip // the data come from http://www.antarctica.ac.uk/met/READER/upper_air/ JSZipTestUtils.testZipFile("AntarcticaTemps.xlsx, createFolders: false", "ref/complex_files/AntarcticaTemps.xlsx", function(assert, file) { var done = assert.async(); JSZip.loadAsync(file, {createFolders: false}) .then(function(zip) { // the zip file contains 17 entries. assert.equal(zip.filter(function(){return true;}).length, 17, "the zip contains the good number of elements."); return zip.file("[Content_Types].xml").async("string"); }).then(function (content) { assert.ok(content.indexOf("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml") !== -1, "the zip was correctly read."); done(); })["catch"](JSZipTestUtils.assertNoError); }); // Same test as above, but with createFolders option set to true JSZipTestUtils.testZipFile("AntarcticaTemps.xlsx, createFolders: true", "ref/complex_files/AntarcticaTemps.xlsx", function(assert, file) { var done = assert.async(); JSZip.loadAsync(file, {createFolders: true}) .then(function(zip) { // the zip file contains 16 entries, but we get 27 when creating all the sub-folders. assert.equal(zip.filter(function(){return true;}).length, 27, "the zip contains the good number of elements."); return zip.file("[Content_Types].xml").async("string"); }).then(function (content) { assert.ok(content.indexOf("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml") !== -1, "the zip was correctly read."); done(); })["catch"](JSZipTestUtils.assertNoError); }); // same as two up, but in the Open Document format JSZipTestUtils.testZipFile("AntarcticaTemps.ods, createFolders: false", "ref/complex_files/AntarcticaTemps.ods", function (assert, file) { var done = assert.async(); JSZip.loadAsync(file, {createFolders: false}) .then(function(zip) { // the zip file contains 20 entries. assert.equal(zip.filter(function () {return true;}).length, 20, "the zip contains the good number of elements."); return zip.file("META-INF/manifest.xml").async("string"); }) .then(function (content) { assert.ok(content.indexOf("application/vnd.oasis.opendocument.spreadsheet") !== -1, "the zip was correctly read."); done(); })["catch"](JSZipTestUtils.assertNoError); }); // same as above, but in the Open Document format JSZipTestUtils.testZipFile("AntarcticaTemps.ods, createFolders: true", "ref/complex_files/AntarcticaTemps.ods", function (assert, file) { var done = assert.async(); JSZip.loadAsync(file, {createFolders: true}) .then(function(zip) { // the zip file contains 19 entries, but we get 27 when creating all the sub-folders. assert.equal(zip.filter(function () {return true;}).length, 27, "the zip contains the good number of elements."); return zip.file("META-INF/manifest.xml").async("string"); }) .then(function (content) { assert.ok(content.indexOf("application/vnd.oasis.opendocument.spreadsheet") !== -1, "the zip was correctly read."); done(); })["catch"](JSZipTestUtils.assertNoError); }); } }); ================================================ FILE: test/asserts/permissions.js ================================================ "use strict"; QUnit.module("permissions", function () { // touch file_{666,640,400,755} // mkdir dir_{777,755,500} // for mode in 777 755 500 666 640 400; do // chmod $mode *_$mode // done // then : // zip -r linux_zip.zip . // 7z a -r linux_7z.zip . // ... function assertUnixPermissions(assert, file){ function doAsserts(zip, fileName, dir, octal) { var mode = parseInt(octal, 8); assert.equal(zip.files[fileName].dosPermissions, null, fileName + ", no DOS permissions"); assert.equal(zip.files[fileName].dir, dir, fileName + " dir flag"); assert.equal(zip.files[fileName].unixPermissions, mode, fileName + " mode " + octal); } var done = assert.async(); JSZip.loadAsync(file, {createFolders:false}) .then(function(zip) { doAsserts(zip, "dir_777/", true, "40777"); doAsserts(zip, "dir_755/", true, "40755"); doAsserts(zip, "dir_500/", true, "40500"); doAsserts(zip, "file_666", false, "100666"); doAsserts(zip, "file_640", false, "100640"); doAsserts(zip, "file_400", false, "100400"); doAsserts(zip, "file_755", false, "100755"); done(); })["catch"](JSZipTestUtils.assertNoError); } function assertDosPermissions(assert, file){ function doAsserts(zip, fileName, dir, binary) { var mode = parseInt(binary, 2); assert.equal(zip.files[fileName].unixPermissions, null, fileName + ", no UNIX permissions"); assert.equal(zip.files[fileName].dir, dir, fileName + " dir flag"); assert.equal(zip.files[fileName].dosPermissions, mode, fileName + " mode " + mode); } var done = assert.async(); JSZip.loadAsync(file, {createFolders:false}) .then(function(zip) { if (zip.files["dir/"]) { doAsserts(zip, "dir/", true, "010000"); } if (zip.files["dir_hidden/"]) { doAsserts(zip, "dir_hidden/", true, "010010"); } doAsserts(zip, "file", false, "100000"); doAsserts(zip, "file_ro", false, "100001"); doAsserts(zip, "file_hidden", false, "100010"); doAsserts(zip, "file_ro_hidden", false, "100011"); done(); })["catch"](JSZipTestUtils.assertNoError); } function reloadAndAssertUnixPermissions(assert, file){ var done = assert.async(); JSZip.loadAsync(file, {createFolders:false}) .then(function (zip) { return zip.generateAsync({type:"string", platform:"UNIX"}); }) .then(function (content) { assertUnixPermissions(assert, content); done(); })["catch"](JSZipTestUtils.assertNoError); } function reloadAndAssertDosPermissions(assert, file){ var done = assert.async(); JSZip.loadAsync(file, {createFolders:false}) .then(function (zip) { return zip.generateAsync({type:"string", platform:"DOS"}); }) .then(function (content) { assertDosPermissions(assert, content); done(); })["catch"](JSZipTestUtils.assertNoError); } JSZipTestUtils.testZipFile("permissions on linux : file created by zip", "ref/permissions/linux_zip.zip", assertUnixPermissions); JSZipTestUtils.testZipFile("permissions on linux : file created by zip, reloaded", "ref/permissions/linux_zip.zip", reloadAndAssertUnixPermissions); JSZipTestUtils.testZipFile("permissions on linux : file created by 7z", "ref/permissions/linux_7z.zip", assertUnixPermissions); JSZipTestUtils.testZipFile("permissions on linux : file created by 7z, reloaded", "ref/permissions/linux_7z.zip", reloadAndAssertUnixPermissions); JSZipTestUtils.testZipFile("permissions on linux : file created by file-roller on ubuntu", "ref/permissions/linux_file_roller-ubuntu.zip", assertUnixPermissions); JSZipTestUtils.testZipFile("permissions on linux : file created by file-roller on ubuntu, reloaded", "ref/permissions/linux_file_roller-ubuntu.zip", reloadAndAssertUnixPermissions); JSZipTestUtils.testZipFile("permissions on linux : file created by file-roller on xubuntu", "ref/permissions/linux_file_roller-xubuntu.zip", assertUnixPermissions); JSZipTestUtils.testZipFile("permissions on linux : file created by file-roller on xubuntu, reloaded", "ref/permissions/linux_file_roller-xubuntu.zip", reloadAndAssertUnixPermissions); JSZipTestUtils.testZipFile("permissions on linux : file created by ark", "ref/permissions/linux_ark.zip", assertUnixPermissions); JSZipTestUtils.testZipFile("permissions on linux : file created by ark, reloaded", "ref/permissions/linux_ark.zip", reloadAndAssertUnixPermissions); JSZipTestUtils.testZipFile("permissions on mac : file created by finder", "ref/permissions/mac_finder.zip", assertUnixPermissions); JSZipTestUtils.testZipFile("permissions on mac : file created by finder, reloaded", "ref/permissions/mac_finder.zip", reloadAndAssertUnixPermissions); JSZipTestUtils.testZipFile("permissions on windows : file created by the compressed folders feature", "ref/permissions/windows_compressed_folders.zip", assertDosPermissions); JSZipTestUtils.testZipFile("permissions on windows : file created by the compressed folders feature, reloaded", "ref/permissions/windows_compressed_folders.zip", reloadAndAssertDosPermissions); JSZipTestUtils.testZipFile("permissions on windows : file created by 7z", "ref/permissions/windows_7z.zip", assertDosPermissions); JSZipTestUtils.testZipFile("permissions on windows : file created by 7z, reloaded", "ref/permissions/windows_7z.zip", reloadAndAssertDosPermissions); JSZipTestUtils.testZipFile("permissions on windows : file created by izarc", "ref/permissions/windows_izarc.zip", assertDosPermissions); JSZipTestUtils.testZipFile("permissions on windows : file created by izarc, reloaded", "ref/permissions/windows_izarc.zip", reloadAndAssertDosPermissions); JSZipTestUtils.testZipFile("permissions on windows : file created by winrar", "ref/permissions/windows_winrar.zip", assertDosPermissions); JSZipTestUtils.testZipFile("permissions on windows : file created by winrar, reloaded", "ref/permissions/windows_winrar.zip", reloadAndAssertDosPermissions); }); ================================================ FILE: test/asserts/stream.js ================================================ "use strict"; QUnit.module("stream", function () { QUnit.module("internal"); QUnit.test("A stream is pausable", function (assert) { // let's get a stream that generates a lot of chunks (~40) var zip = new JSZip(); var txt = "a text"; for(var i = 0; i < 10; i++) { zip.file(i + ".txt", txt); } var allowChunks = true; var chunkCount = 0; var done = assert.async(); var helper = zip.generateInternalStream({streamFiles:true, type:"binarystring"}); helper .on("data", function () { chunkCount++; assert.equal(allowChunks, true, "be sure to get chunks only when allowed"); /* * We stop at ~ half of chunks. * A setTimeout aside this stream is not reliable and can be * triggered *after* the completion of the stream. */ if (chunkCount === 20) { allowChunks = false; helper.pause(); setTimeout(function () { allowChunks = true; helper.resume(); }, 50); } }) .on("error", function (e) { done(); assert.ok(false, e.message); }) .on("end", function () { done(); }); helper.resume(); }); QUnit.module("nodejs"); if (JSZip.support.nodestream) { var fs = require("fs"); } function generateStreamTest(name, ref, createFunction, generateOptions) { JSZipTestUtils.testZipFile(name,ref, function(assert, expected) { var done = assert.async(); var tempFile = require("tmp").tmpNameSync({postfix:".zip"}); var zip = createFunction(); zip.generateNodeStream(generateOptions) .pipe(fs.createWriteStream(tempFile)) .on("close", function () { fs.readFile(tempFile, function (e, data) { var actual = JSZipTestUtils.toString(data); assert.ok(JSZipTestUtils.similar(actual, expected, 3 * JSZipTestUtils.MAX_BYTES_DIFFERENCE_PER_ZIP_ENTRY) , "generated ZIP matches reference ZIP"); fs.unlink(tempFile, function (err) { if (err) { assert.ok(false, err); } done(); }); }); }) .on("error", function (e) { assert.ok(false, e.message); fs.unlink(tempFile, function (err) { if (err) { assert.ok(false, err); } done(); }); }); }); } function zipObjectStreamTest(name, createFunction) { QUnit.test(name, function(assert) { var tempFile = require("tmp").tmpNameSync({postfix:".txt"}); var done = assert.async(); createFunction().pipe(fs.createWriteStream(tempFile)) .on("close", function () { fs.readFile(tempFile, function (e, data) { var actual = JSZipTestUtils.toString(data); assert.equal(actual, "Hello World\n", "the generated content is ok"); fs.unlink(tempFile, function (err) { if (err) { assert.ok(false, err); } done(); }); }); }) .on("error", function (e) { assert.ok(false, e.message); fs.unlink(tempFile, function (err) { if (err) { assert.ok(false, err); } done(); }); }); }); } if (JSZip.support.nodestream) { generateStreamTest( "generateNodeStream(type:nodebuffer / !streamFiles) generates a working stream", "ref/all.zip", JSZipTestUtils.createZipAll, {type:"nodebuffer",streamFiles:false} ); generateStreamTest( "generateNodeStream(type: / !streamFiles) generates a working stream", "ref/all.zip", JSZipTestUtils.createZipAll, {streamFiles:false} ); generateStreamTest( "generateNodeStream() generates a working stream", "ref/all.zip", JSZipTestUtils.createZipAll ); generateStreamTest( "generateNodeStream(type:nodebuffer / streamFiles) generates a working stream", "ref/all-stream.zip", JSZipTestUtils.createZipAll, {type:"nodebuffer",streamFiles:true} ); generateStreamTest( "generateNodeStream(type: / streamFiles) generates a working stream", "ref/all-stream.zip", JSZipTestUtils.createZipAll, {streamFiles:true} ); generateStreamTest( "generateNodeStream(type:nodebuffer / !streamFiles) generates a working stream from other streams", "ref/all.zip", function () { var helloStream = JSZipTestUtils.createZipAll().file("Hello.txt").nodeStream(); var imgStream = JSZipTestUtils.createZipAll().file("images/smile.gif").nodeStream(); var zip = new JSZip(); zip.file("Hello.txt", helloStream); zip.folder("images").file("smile.gif", imgStream); return zip; }, {type:"nodebuffer",streamFiles:false} ); generateStreamTest( "generateNodeStream(type:nodebuffer / streamFiles) generates a working stream from other streams", "ref/all-stream.zip", function () { var helloStream = JSZipTestUtils.createZipAll().file("Hello.txt").nodeStream(); var imgStream = JSZipTestUtils.createZipAll().file("images/smile.gif").nodeStream(); var zip = new JSZip(); zip.file("Hello.txt", helloStream); zip.folder("images").file("smile.gif", imgStream); return zip; }, {type:"nodebuffer",streamFiles:true} ); zipObjectStreamTest("ZipObject#nodeStream generates a working stream[nodebuffer]", function() { var zip = JSZipTestUtils.createZipAll(); return zip.file("Hello.txt").nodeStream("nodebuffer"); }); zipObjectStreamTest("ZipObject#nodeStream generates a working stream[default]", function() { var zip = JSZipTestUtils.createZipAll(); return zip.file("Hello.txt").nodeStream(); }); QUnit.test("a ZipObject containing a stream can be read with async", function(assert) { var done = assert.async(); var stream = JSZipTestUtils.createZipAll().file("Hello.txt").nodeStream(); var zip = new JSZip(); zip.file("Hello.txt", stream); zip.file("Hello.txt").async("text").then(function(actual) { assert.equal(actual, "Hello World\n", "the stream has been read correctly"); done(); })["catch"](JSZipTestUtils.assertNoError); }); QUnit.test("a ZipObject containing a stream can't be read with async 2 times", function(assert) { var done = assert.async(); var stream = JSZipTestUtils.createZipAll().file("Hello.txt").nodeStream(); var zip = new JSZip(); zip.file("Hello.txt", stream); // first time, consume the node stream zip.file("Hello.txt").async("text"); // second time, it shouldn't work zip.file("Hello.txt").async("text") .then(function () { assert.ok(false, "calling 2 times a stream should generate an error"); done(); }, function ko(e) { assert.ok(e.message.match("has already been used"), "the error message is useful"); done(); }); }); QUnit.test("a ZipObject containing a stream can't be read with nodeStream 2 times", function(assert) { var done = assert.async(); var stream = JSZipTestUtils.createZipAll().file("Hello.txt").nodeStream(); var zip = new JSZip(); zip.file("Hello.txt", stream); // first time, consume the node stream zip.file("Hello.txt").nodeStream().resume(); // second time, it shouldn't work zip.file("Hello.txt").nodeStream() .on("error", function (e) { assert.ok(e.message.match("has already been used"), "the error message is useful"); done(); }) .on ("end", function () { assert.ok(false, "calling 2 times a stream should generate an error"); done(); }) .resume(); }); QUnit.test("generateAsync with a stream can't be read 2 times", function(assert) { var done = assert.async(); var stream = JSZipTestUtils.createZipAll().file("Hello.txt").nodeStream(); var zip = new JSZip(); zip.file("Hello.txt", stream); // first time, consume the node stream zip.generateAsync({type:"string"}); // second time, it shouldn't work zip.generateAsync({type:"string"}) .then(function () { assert.ok(false, "calling 2 times a stream should generate an error"); done(); }, function ko(e) { assert.ok(e.message.match("has already been used"), "the error message is useful"); done(); }); }); QUnit.test("generateNodeStream with a stream can't be read 2 times", function(assert) { var done = assert.async(); var stream = JSZipTestUtils.createZipAll().file("Hello.txt").nodeStream(); var zip = new JSZip(); zip.file("Hello.txt", stream); // first time, consume the node stream zip.generateNodeStream().resume(); // second time, it shouldn't work zip.generateNodeStream() .on("error", function (e) { assert.ok(e.message.match("has already been used"), "the error message is useful"); done(); }) .on ("end", function () { assert.ok(false, "calling 2 times a stream should generate an error"); done(); }) .resume(); }); QUnit.test("loadAsync ends with an error when called with a stream", function(assert) { var done = assert.async(); var stream = JSZipTestUtils.createZipAll().generateNodeStream({"type":"nodebuffer"}); JSZip.loadAsync(stream).then(function () { assert.ok(false, "loading a zip file from a stream is impossible"); done(); }, function (e) { assert.ok(e.message.match("can't accept a stream when loading"), "the error message is useful"); done(); }); }); } else { QUnit.test("generateNodeStream generates an error", function(assert) { try { var zip = new JSZip(); zip.generateNodeStream({type:"nodebuffer",streamFiles:true}); assert.ok(false, "generateNodeStream should generate an error"); } catch(err) { assert.ok(err.message.match("not supported by this platform"), "the error message is useful"); } }); QUnit.test("ZipObject#nodeStream generates an error", function(assert) { try { var zip = JSZipTestUtils.createZipAll(); zip.file("Hello.txt").nodeStream("nodebuffer"); assert.ok(false, "nodeStream should generate an error"); } catch(err) { assert.ok(err.message.match("not supported by this platform"), "the error message is useful"); } }); } }); ================================================ FILE: test/asserts/unicode.js ================================================ "use strict"; QUnit.module("unicode"); // zip -X -0 utf8.zip amount.txt JSZipTestUtils.testZipFile("Zip text file with UTF-8 characters", "ref/utf8.zip", function(assert, expected) { var zip = new JSZip(); zip.file("amount.txt", "€15\n"); var done = assert.async(); zip.generateAsync({type:"binarystring"}).then(function (actual) { assert.ok(JSZipTestUtils.similar(actual, expected, JSZipTestUtils.MAX_BYTES_DIFFERENCE_PER_ZIP_ENTRY) , "Generated ZIP matches reference ZIP"); JSZipTestUtils.checkGenerateStability(assert, actual); done(); })["catch"](JSZipTestUtils.assertNoError); }); QUnit.test("Text file with long unicode string", function(assert) { var expected = "€"; for(var i = 0; i < 13; i++) { expected = expected + expected; } var zip = new JSZip(); zip.file("amount.txt", expected); var done = assert.async(); zip.generateAsync({type:"binarystring"}) .then(JSZip.loadAsync) .then(function (zip) { var file = zip.file("amount.txt"); return file.async("string"); }).then(function (fileContent){ assert.equal(fileContent, expected, "Generated ZIP can be parsed"); done(); })["catch"](JSZipTestUtils.assertNoError); }); // zip -X -0 utf8_in_name.zip €15.txt JSZipTestUtils.testZipFile("Zip text file with UTF-8 characters in filename", "ref/utf8_in_name.zip", function(assert) { var zip = new JSZip(); zip.file("€15.txt", "€15\n"); var done = assert.async(); zip.generateAsync({type:"binarystring"}).then(function (actual) { // zip doesn't generate a strange file like us (utf8 flag AND unicode path extra field) // if one of the files has more data than the other, the bytes are no more aligned and the // error count goes through the roof. The parsing is checked on a other test so I'll // comment this one for now. // assert.ok(JSZipTestUtils.similar(actual, expected, JSZipTestUtils.MAX_BYTES_DIFFERENCE_PER_ZIP_ENTRY) , "Generated ZIP matches reference ZIP"); JSZipTestUtils.checkGenerateStability(assert, actual); done(); })["catch"](JSZipTestUtils.assertNoError); }); JSZipTestUtils.testZipFile("Zip text file with non unicode characters in filename: loadAsync without decodeFileName", "ref/local_encoding_in_name.zip", function(assert, content) { var done = assert.async(); JSZip.loadAsync(content).then(function (zipUnicode) { assert.ok(!zipUnicode.files["Новая папка/"], "default : the folder is not found"); assert.ok(!zipUnicode.files["Новая папка/Новый текстовый документ.txt"], "default : the file is not found"); done(); })["catch"](JSZipTestUtils.assertNoError); }); JSZipTestUtils.testZipFile("Zip text file with non unicode characters in filename: loadAsync with decodeFileName", "ref/local_encoding_in_name.zip", function(assert, content) { var conversions = { "bytes 8d ae a2 a0 ef 20 af a0 af aa a0 2f" : "Новая папка/", "bytes 8d ae a2 a0 ef 20 af a0 af aa a0 2f 8d ae a2 eb a9 20 e2 a5 aa e1 e2 ae a2 eb a9 20 a4 ae aa e3 ac a5 ad e2 2e 74 78 74" : "Новая папка/Новый текстовый документ.txt" }; var done = assert.async(); JSZip.loadAsync(content, { decodeFileName: function (bytes) { // here, a real iconv implementation var key = "bytes"; for(var i = 0; i < bytes.length; i++) { key += " " + bytes[i].toString(16); } return conversions[key] || ""; } }).then(function (zipCP866) { assert.ok(zipCP866.files["Новая папка/"], "with decodeFileName : the folder has been correctly read"); assert.ok(zipCP866.files["Новая папка/Новый текстовый документ.txt"], "with decodeFileName : the file has been correctly read"); done(); })["catch"](JSZipTestUtils.assertNoError); }); JSZipTestUtils.testZipFile("Zip text file with non unicode characters in filename: generateAsync with encodeassert, fileName", "ref/local_encoding_in_name.zip", function(assert, content) { var conversions = { "": [], "Новая папка/": [0x8d, 0xae, 0xa2, 0xa0, 0xef, 0x20, 0xaf, 0xa0, 0xaf, 0xaa, 0xa0, 0x2f], "Новая папка/Новый текстовый документ.txt": [0x8d, 0xae, 0xa2, 0xa0, 0xef, 0x20, 0xaf, 0xa0, 0xaf, 0xaa, 0xa0, 0x2f, 0x8d, 0xae, 0xa2, 0xeb, 0xa9, 0x20, 0xe2, 0xa5, 0xaa, 0xe1, 0xe2, 0xae, 0xa2, 0xeb, 0xa9, 0x20, 0xa4, 0xae, 0xaa, 0xe3, 0xac, 0xa5, 0xad, 0xe2, 0x2e, 0x74, 0x78, 0x74] }; function decodeCP866(bytes) { for(var text in conversions) { if (conversions[text].length === bytes.length) { return text; } } } function encodeCP866(string) { return conversions[string]; } var done = assert.async(); JSZip.loadAsync(content, { decodeFileName: decodeCP866 }).then(function (zipCP866) { return zipCP866.generateAsync({ type: "string", encodeFileName: encodeCP866 }); }).then(function (regeneratedContent) { return JSZip.loadAsync(regeneratedContent, { decodeFileName: decodeCP866 }); }).then(function (zipCP866) { // the example zip doesn't contain the unicode path extra field, we can't // compare them. assert.ok(zipCP866.files["Новая папка/"], "with decodeFileName : the folder has been correctly read"); assert.ok(zipCP866.files["Новая папка/Новый текстовый документ.txt"], "with decodeFileName : the file has been correctly read"); done(); })["catch"](JSZipTestUtils.assertNoError); }); // zip --entry-comments --archive-comment -X -0 pile_of_poo.zip Iñtërnâtiônàlizætiøn☃$'\360\237\222\251'.txt JSZipTestUtils.testZipFile("Zip text file and UTF-8, Pile Of Poo test", "ref/pile_of_poo.zip", function(assert, expected) { var zip = new JSZip(); // this is the string "Iñtërnâtiônàlizætiøn☃💩", // see http://mathiasbynens.be/notes/javascript-unicode // but escaped, to avoid troubles // thanks http://mothereff.in/js-escapes#1I%C3%B1t%C3%ABrn%C3%A2ti%C3%B4n%C3%A0liz%C3%A6ti%C3%B8n%E2%98%83%F0%9F%92%A9 var text = "I\xF1t\xEBrn\xE2ti\xF4n\xE0liz\xE6ti\xF8n\u2603\uD83D\uDCA9"; zip.file(text + ".txt", text, {comment : text}); var done = assert.async(3); zip.generateAsync({type:"binarystring", comment : text}).then(function(actual) { JSZipTestUtils.checkGenerateStability(assert, actual); JSZip.loadAsync(expected) .then(function (zip) { var file = zip.file(text + ".txt"); assert.ok(file, "JSZip finds the unicode file name on the external file"); assert.equal(file.comment, text, "JSZip can decode the external file comment"); return file.async("string"); }) .then(function (content) { assert.equal(content, text, "JSZip can decode the external file"); done(); })["catch"](JSZipTestUtils.assertNoError); JSZip.loadAsync(actual) .then(function (zip) { var file = zip.file(text + ".txt"); assert.ok(file, "JSZip finds the unicode file name on its own file"); assert.equal(file.comment, text, "JSZip can decode its own file comment"); return file.async("string"); }) .then(function (content) { assert.equal(content, text, "JSZip can decode its own file"); done(); })["catch"](JSZipTestUtils.assertNoError); done(); })["catch"](JSZipTestUtils.assertNoError); }); ================================================ FILE: test/asserts/utils.js ================================================ "use strict"; // These tests only run in Node var utils = require("../../lib/utils"); QUnit.module("utils"); QUnit.test("Paths are resolved correctly", function (assert) { // Backslashes can be part of filenames assert.strictEqual(utils.resolve("root\\a\\b"), "root\\a\\b"); assert.strictEqual(utils.resolve("root/a/b"), "root/a/b"); assert.strictEqual(utils.resolve("root/a/.."), "root"); assert.strictEqual(utils.resolve("root/a/../b"), "root/b"); assert.strictEqual(utils.resolve("root/a/./b"), "root/a/b"); assert.strictEqual(utils.resolve("root/../../../"), ""); assert.strictEqual(utils.resolve("////"), "/"); assert.strictEqual(utils.resolve("/a/b/c"), "/a/b/c"); assert.strictEqual(utils.resolve("a/b/c/"), "a/b/c/"); assert.strictEqual(utils.resolve("../../../../../a"), "a"); assert.strictEqual(utils.resolve("../app.js"), "app.js"); }); ================================================ FILE: test/asserts/version.js ================================================ "use strict"; QUnit.module("version"); QUnit.test("JSZip.version is correct", function(assert){ assert.ok(JSZip.version, "JSZip.version exists"); assert.ok(JSZip.version.match(/^\d+\.\d+\.\d+/), "JSZip.version looks like a correct version"); }); ================================================ FILE: test/benchmark/.eslintrc.js ================================================ "use strict"; module.exports = { globals: { // Added by index.html and node.js Benchmark: false, }, }; ================================================ FILE: test/benchmark/benchmark.js ================================================ "use strict"; (function (root, factory) { if (typeof module === "object" && module.exports) { module.exports = factory(); } else { root.benchmark = factory(); } }(typeof self !== "undefined" ? self : this, function () { return function (type) { return new Promise(resolve => { const suite = new Benchmark.Suite(); suite .add(`${type} generateAsync`, { defer: true, async fn(deferred) { const zip = new JSZip(); for (let i = 0; i < 50; i++) { zip.file("file_" + i, "R0lGODdhBQAFAIACAAAAAP/eACwAAAAABQAFAAACCIwPkWerClIBADs=", { base64: true, date: new Date(1234123491011) }); } await zip.generateAsync({ type }); deferred.resolve(); } }) .on("cycle", event => { // Output benchmark result by converting benchmark result to string console.log(String(event.target)); }) .on("complete", () => { console.log("Benchmark complete"); resolve(); }) .run({ "async": true }); }); }; })); ================================================ FILE: test/benchmark/index.html ================================================ JSZip Benchmark ================================================ FILE: test/benchmark/node.js ================================================ "use strict"; globalThis.Benchmark = require("benchmark"); globalThis.JSZip = require("../../lib/index"); const benchmark = require("./benchmark"); benchmark("nodebuffer"); ================================================ FILE: test/helpers/browser-test-utils.js ================================================ "use strict"; JSZipTestUtils.loadZipFile = function (name, callback) { JSZipUtils.getBinaryContent(name + "?_=" + ( new Date() ).getTime(), callback); }; ================================================ FILE: test/helpers/node-test-utils.js ================================================ "use strict"; var fs = require("fs"); var path = require("path"); global.JSZip = require("../../lib/index"); global.JSZipTestUtils.loadZipFile = function(name, callback) { fs.readFile(path.join("test", name), "binary", callback); }; process.on("uncaughtException", function(err) { console.log("uncaughtException: " + err, err.stack); process.exit(1); }); process.on("unhandledRejection", function(err) { console.log("unhandledRejection: " + err, err.stack); process.exit(1); }); ================================================ FILE: test/helpers/test-utils.js ================================================ "use strict"; (function (global) { // Expose assert object globally global.assert = QUnit.assert; var JSZipTestUtils = {}; JSZipTestUtils.similar = function similar(actual, expected, mistakes) { if(JSZip.support.arraybuffer) { if(actual instanceof ArrayBuffer) { actual = new Uint8Array(actual); } if(expected instanceof ArrayBuffer) { expected = new Uint8Array(expected); } } var actualIsString = typeof actual === "string"; var expectedIsString = typeof expected === "string"; if (actual.length !== expected.length) { mistakes -= Math.abs((actual.length||0) - (expected.length||0)); } for (var i = 0; i < Math.min(actual.length, expected.length); i++) { // actual is the generated zip, expected is what we got from the xhr. var actualByte = actualIsString ? actual.charCodeAt(i) : actual[i]; // expected can be a string with char codes > 255, be sure to take only one byte. var expectedByte = (expectedIsString ? expected.charCodeAt(i) : expected[i]) & 0xFF; if (actualByte !== expectedByte) { mistakes--; } } if (mistakes < 0) { return false; } else { return true; } }; /* Expected differing bytes: 2 version number 4 date/time 4 central dir version numbers 4 central dir date/time 4 external file attributes 18 Total */ JSZipTestUtils.MAX_BYTES_DIFFERENCE_PER_ZIP_ENTRY = 18; JSZipTestUtils.checkGenerateStability = function checkGenerateStability(assert, bytesStream, options) { var done = assert.async(); options = options || {type:"binarystring"}; // TODO checkcrc32 return new JSZip().loadAsync(bytesStream).then(function (zip) { return zip.generateAsync(options); }).then(function (content) { assert.ok(JSZipTestUtils.similar(bytesStream, content, 0), "generate stability : stable"); done(); })["catch"](JSZipTestUtils.assertNoError); }; JSZipTestUtils.checkBasicStreamBehavior = function checkBasicStreamBehavior(assert, stream, testName) { var done = assert.async(); if (!testName) { testName = ""; } var triggeredStream = false; stream .on("data", function (data, metadata) { // triggering a lot of passing checks makes the result unreadable if (!data) { assert.ok(data, testName + "basic check stream, data event handler, data is defined"); } if(!metadata) { assert.ok(metadata, testName + "basic check stream, data event handler, metadata is defined"); } triggeredStream = true; }) .on("error", function (e) { assert.ok(e, testName + "basic check stream, error event handler, error is defined"); triggeredStream = true; done(); }) .on("end", function () { triggeredStream = true; done(); }) .resume() ; assert.ok(!triggeredStream, testName + "basic check stream, the stream callback is async"); }; JSZipTestUtils.toString = function toString(obj) { if(typeof obj === "string" || !obj) { return obj; } if(obj instanceof ArrayBuffer) { obj = new Uint8Array(obj); } var res = ""; for(var i = 0; i < obj.length; i++) { res += String.fromCharCode(obj[i]); } return res; }; // cache for files var refZips = {}; JSZipTestUtils.fetchFile = function fetchFile(index, url, callback) { if(refZips[url]) { setTimeout(function () { callback(index, null, refZips[url]); }, 0); } else { JSZipTestUtils.loadZipFile(url, function (err, res) { var file = JSZipTestUtils.toString(res); refZips[url] = file; callback(index, err, file); }); } }; JSZipTestUtils.assertNoError = function assertNoError(err) { if (typeof console !== "undefined" && console.error) { console.error(err.stack); } QUnit.assert.ok(false, "unexpected error : " + err + ", " + err.stack); }; JSZipTestUtils.testZipFile = function testZipFile(testName, zipName, testFunction) { var simpleForm = !(zipName instanceof Array); var filesToFetch = []; if(simpleForm) { filesToFetch = [zipName]; } else { filesToFetch = zipName; } QUnit.test(testName, function (assert) { var done = assert.async(); var results = new Array(filesToFetch.length); var count = 0; var fetchError = null; function handleResult(index, err, file) { fetchError = fetchError || err; results[index] = file; count++; if (count === filesToFetch.length) { done(); if(fetchError) { assert.ok(false, fetchError); return; } if(simpleForm) { testFunction.call(null, assert, results[0]); } else { testFunction.call(null, assert, results); } } } for (var i = 0; i < filesToFetch.length; i++) { JSZipTestUtils.fetchFile(i, filesToFetch[i], handleResult); } }); }; JSZipTestUtils.createZipAll = function createZipAll() { var zip = new JSZip(); zip.file("Hello.txt", "Hello World\n"); zip.folder("images").file("smile.gif", "R0lGODdhBQAFAIACAAAAAP/eACwAAAAABQAFAAACCIwPkWerClIBADs=", {base64: true}); return zip; }; var base64Dict = { "": "", "\xE2\x82\xAC15\n": "4oKsMTUK", "test\r\ntest\r\n": "dGVzdA0KdGVzdA0K", "all.zip.base64,stream=false": "UEsDBAoAAAAAAO+7TTrj5ZWwDAAAAAwAAAAJAAAASGVsbG8udHh0SGVsbG8gV29ybGQKUEsDBAoAAAAAAA9qUToAAAAAAAAAAAAAAAAHAAAAaW1hZ2VzL1BLAwQKAAAAAACZoEg6PD/riikAAAApAAAAEAAAAGltYWdlcy9zbWlsZS5naWZHSUY4N2EFAAUAgAIAAAAA/94ALAAAAAAFAAUAAAIIjA+RZ6sKUgEAO1BLAQIUAAoAAAAAAO+7TTrj5ZWwDAAAAAwAAAAJAAAAAAAAAAAAAAAAAAAAAABIZWxsby50eHRQSwECFAAKAAAAAAAPalE6AAAAAAAAAAAAAAAABwAAAAAAAAAAABAAAAAzAAAAaW1hZ2VzL1BLAQIUAAoAAAAAAJmgSDo8P+uKKQAAACkAAAAQAAAAAAAAAAAAAAAAAFgAAABpbWFnZXMvc21pbGUuZ2lmUEsFBgAAAAADAAMAqgAAAK8AAAAAAA==", "all.zip.base64,stream=true": "UEsDBAoACAAAAO+7TToAAAAAAAAAAAAAAAAJAAAASGVsbG8udHh0SGVsbG8gV29ybGQKUEsHCOPllbAMAAAADAAAAFBLAwQKAAAAAAAPalE6AAAAAAAAAAAAAAAABwAAAGltYWdlcy9QSwMECgAIAAAAmaBIOgAAAAAAAAAAAAAAABAAAABpbWFnZXMvc21pbGUuZ2lmR0lGODdhBQAFAIACAAAAAP/eACwAAAAABQAFAAACCIwPkWerClIBADtQSwcIPD/riikAAAApAAAAUEsBAhQACgAIAAAA77tNOuPllbAMAAAADAAAAAkAAAAAAAAAAAAAAAAAAAAAAEhlbGxvLnR4dFBLAQIUAAoAAAAAAA9qUToAAAAAAAAAAAAAAAAHAAAAAAAAAAAAEAAAAEMAAABpbWFnZXMvUEsBAhQACgAIAAAAmaBIOjw/64opAAAAKQAAABAAAAAAAAAAAAAAAAAAaAAAAGltYWdlcy9zbWlsZS5naWZQSwUGAAAAAAMAAwCqAAAAzwAAAAAA" }; JSZipTestUtils.base64encode = function(input) { if (!(input in base64Dict)){ throw new Error("unknown key '" + input + "' in the base64 dictionary"); } return base64Dict[input]; }; if (global.JSZip) { throw new Error("JSZip is already defined, we can't capture the global state *before* its load"); } JSZipTestUtils.oldPromise = global.Promise; global.JSZipTestUtils = JSZipTestUtils; })(typeof window !== "undefined" && window || global); ================================================ FILE: test/index.html ================================================ JSZip Testing
    ================================================ FILE: test/run.js ================================================ "use strict"; const path = require("path"); const playwright = require("playwright"); const createServer = require("http-server").createServer; /** @typedef {{ name: string, message: string, module: string, result: boolean, expected: unknown, actual: unknown, source: string }} Failure */ /** * @typedef {{ passed: number, failed: number, total: number, runtime: number, tests: Failure[] }} Results */ /** * @param {string} browserType * @returns {Promise<[string, Results]>} */ async function runBrowser(browserType, waitFor, file) { console.log("Starting", browserType); const browser = await playwright[browserType].launch(); const context = await browser.newContext(); const page = await context.newPage(); await page.goto(`http://127.0.0.1:8080/test/${file}`); const result = await waitFor(page); console.log("Closing", browserType); await browser.close(); return [browserType, result]; } async function runBrowsers(waitFor, file) { const browsersTypes = ["chromium", "firefox", "webkit"]; const server = createServer({root: path.join(__dirname, "..")}); await new Promise(resolve => server.listen(8080, "127.0.0.1", resolve)); console.log("Server started"); try { const results = await Promise.all(browsersTypes.map(b => runBrowser(b, waitFor, file))); return results; } finally { server.close(); } } async function waitForTests(page) { let result; do { result = await page.evaluate(() => { return window.global_test_results; }); } while (!result); return result; } async function runTests() { const results = await runBrowsers(waitForTests, "index.html?hidepassed"); let failures = false; for (const result of results) { console.log(...result); failures = failures || result[1].failed > 0; } if (failures) { console.log("Tests failed"); process.exit(1); } else { console.log("Tests passed!"); } } async function waitForBenchmark(page) { return new Promise(resolve => { const logs = []; page.on("console", async message => { if (message.text() === "Benchmark complete") { resolve(logs); } else { logs.push(message.text()); } }); }); } async function runBenchmark() { const results = await runBrowsers(waitForBenchmark, "benchmark/index.html"); for (const [browser, logs] of results) { for (const log of logs) { console.log(browser, log); } } } switch (process.argv[2]) { case "--test": runTests(); break; case "--benchmark": runBenchmark(); break; default: throw new Error(`Unknown argument: ${process.argv[2]}`); } ================================================ FILE: tsconfig.json ================================================ { "compilerOptions": { /* Visit https://aka.ms/tsconfig.json to read more about this file */ /* Projects */ // "incremental": true, /* Enable incremental compilation */ // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */ // "tsBuildInfoFile": "./", /* Specify the folder for .tsbuildinfo incremental compilation files. */ // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects */ // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */ // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ /* Language and Environment */ "target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ // "jsx": "preserve", /* Specify what JSX code is generated. */ // "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */ // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */ // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h' */ // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */ // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using `jsx: react-jsx*`.` */ // "reactNamespace": "", /* Specify the object invoked for `createElement`. This only applies when targeting `react` JSX emit. */ // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */ // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ /* Modules */ "module": "commonjs", /* Specify what module code is generated. */ // "rootDir": "./", /* Specify the root folder within your source files. */ // "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */ // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ // "typeRoots": [], /* Specify multiple folders that act like `./node_modules/@types`. */ // "types": [], /* Specify type package names to be included without being referenced in a source file. */ // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ // "resolveJsonModule": true, /* Enable importing .json files */ // "noResolve": true, /* Disallow `import`s, `require`s or ``s from expanding the number of files TypeScript should add to a project. */ /* JavaScript Support */ // "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */ // "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */ // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from `node_modules`. Only applicable with `allowJs`. */ /* Emit */ // "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ // "declarationMap": true, /* Create sourcemaps for d.ts files. */ // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */ // "sourceMap": true, /* Create source map files for emitted JavaScript files. */ // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */ // "outDir": "./", /* Specify an output folder for all emitted files. */ // "removeComments": true, /* Disable emitting comments. */ "noEmit": true, /* Disable emitting files from a compilation. */ // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */ // "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */ // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */ // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */ // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */ // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */ // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */ // "newLine": "crlf", /* Set the newline character for emitting files. */ // "stripInternal": true, /* Disable emitting declarations that have `@internal` in their JSDoc comments. */ // "noEmitHelpers": true, /* Disable generating custom helper functions like `__extends` in compiled output. */ // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */ "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */ // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */ "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */ /* Type Checking */ "strict": true, /* Enable all strict type-checking options. */ // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied `any` type.. */ // "strictNullChecks": true, /* When type checking, take into account `null` and `undefined`. */ // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */ // "strictBindCallApply": true, /* Check that the arguments for `bind`, `call`, and `apply` methods match the original function. */ // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */ // "noImplicitThis": true, /* Enable error reporting when `this` is given the type `any`. */ // "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */ // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */ // "noUnusedLocals": true, /* Enable error reporting when a local variables aren't read. */ // "noUnusedParameters": true, /* Raise an error when a function parameter isn't read */ // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */ // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */ // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */ // "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */ // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */ // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type */ // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */ // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */ /* Completeness */ // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ // "skipLibCheck": true /* Skip type checking all .d.ts files. */ } } ================================================ FILE: vendor/FileSaver.js ================================================ /*! FileSaver.js * A saveAs() FileSaver implementation. * 2014-01-24 * * By Eli Grey, http://eligrey.com * License: X11/MIT * See LICENSE.md */ /*global self */ /*jslint bitwise: true, indent: 4, laxbreak: true, laxcomma: true, smarttabs: true, plusplus: true */ /*! @source http://purl.eligrey.com/github/FileSaver.js/blob/main/FileSaver.js */ var saveAs = saveAs // IE 10+ (native saveAs) || (typeof navigator !== "undefined" && navigator.msSaveOrOpenBlob && navigator.msSaveOrOpenBlob.bind(navigator)) // Everyone else || (function(view) { "use strict"; // IE <10 is explicitly unsupported if (typeof navigator !== "undefined" && /MSIE [1-9]\./.test(navigator.userAgent)) { return; } var doc = view.document // only get URL when necessary in case BlobBuilder.js hasn't overridden it yet , get_URL = function() { return view.URL || view.webkitURL || view; } , URL = view.URL || view.webkitURL || view , save_link = doc.createElementNS("http://www.w3.org/1999/xhtml", "a") , can_use_save_link = !view.externalHost && "download" in save_link , click = function(node) { var event = doc.createEvent("MouseEvents"); event.initMouseEvent( "click", true, false, view, 0, 0, 0, 0, 0 , false, false, false, false, 0, null ); node.dispatchEvent(event); } , webkit_req_fs = view.webkitRequestFileSystem , req_fs = view.requestFileSystem || webkit_req_fs || view.mozRequestFileSystem , throw_outside = function(ex) { (view.setImmediate || view.setTimeout)(function() { throw ex; }, 0); } , force_saveable_type = "application/octet-stream" , fs_min_size = 0 , deletion_queue = [] , process_deletion_queue = function() { var i = deletion_queue.length; while (i--) { var file = deletion_queue[i]; if (typeof file === "string") { // file is an object URL URL.revokeObjectURL(file); } else { // file is a File file.remove(); } } deletion_queue.length = 0; // clear queue } , dispatch = function(filesaver, event_types, event) { event_types = [].concat(event_types); var i = event_types.length; while (i--) { var listener = filesaver["on" + event_types[i]]; if (typeof listener === "function") { try { listener.call(filesaver, event || filesaver); } catch (ex) { throw_outside(ex); } } } } , FileSaver = function(blob, name) { // First try a.download, then web filesystem, then object URLs var filesaver = this , type = blob.type , blob_changed = false , object_url , target_view , get_object_url = function() { var object_url = get_URL().createObjectURL(blob); deletion_queue.push(object_url); return object_url; } , dispatch_all = function() { dispatch(filesaver, "writestart progress write writeend".split(" ")); } // on any filesys errors revert to saving with object URLs , fs_error = function() { // don't create more object URLs than needed if (blob_changed || !object_url) { object_url = get_object_url(blob); } if (target_view) { target_view.location.href = object_url; } else { window.open(object_url, "_blank"); } filesaver.readyState = filesaver.DONE; dispatch_all(); } , abortable = function(func) { return function() { if (filesaver.readyState !== filesaver.DONE) { return func.apply(this, arguments); } }; } , create_if_not_found = {create: true, exclusive: false} , slice ; filesaver.readyState = filesaver.INIT; if (!name) { name = "download"; } if (can_use_save_link) { object_url = get_object_url(blob); // FF for Android has a nasty garbage collection mechanism // that turns all objects that are not pure javascript into 'deadObject' // this means `doc` and `save_link` are unusable and need to be recreated // `view` is usable though: doc = view.document; save_link = doc.createElementNS("http://www.w3.org/1999/xhtml", "a"); save_link.href = object_url; save_link.download = name; var event = doc.createEvent("MouseEvents"); event.initMouseEvent( "click", true, false, view, 0, 0, 0, 0, 0 , false, false, false, false, 0, null ); save_link.dispatchEvent(event); filesaver.readyState = filesaver.DONE; dispatch_all(); return; } // Object and web filesystem URLs have a problem saving in Google Chrome when // viewed in a tab, so I force save with application/octet-stream // http://code.google.com/p/chromium/issues/detail?id=91158 if (view.chrome && type && type !== force_saveable_type) { slice = blob.slice || blob.webkitSlice; blob = slice.call(blob, 0, blob.size, force_saveable_type); blob_changed = true; } // Since I can't be sure that the guessed media type will trigger a download // in WebKit, I append .download to the filename. // https://bugs.webkit.org/show_bug.cgi?id=65440 if (webkit_req_fs && name !== "download") { name += ".download"; } if (type === force_saveable_type || webkit_req_fs) { target_view = view; } if (!req_fs) { fs_error(); return; } fs_min_size += blob.size; req_fs(view.TEMPORARY, fs_min_size, abortable(function(fs) { fs.root.getDirectory("saved", create_if_not_found, abortable(function(dir) { var save = function() { dir.getFile(name, create_if_not_found, abortable(function(file) { file.createWriter(abortable(function(writer) { writer.onwriteend = function(event) { target_view.location.href = file.toURL(); deletion_queue.push(file); filesaver.readyState = filesaver.DONE; dispatch(filesaver, "writeend", event); }; writer.onerror = function() { var error = writer.error; if (error.code !== error.ABORT_ERR) { fs_error(); } }; "writestart progress write abort".split(" ").forEach(function(event) { writer["on" + event] = filesaver["on" + event]; }); writer.write(blob); filesaver.abort = function() { writer.abort(); filesaver.readyState = filesaver.DONE; }; filesaver.readyState = filesaver.WRITING; }), fs_error); }), fs_error); }; dir.getFile(name, {create: false}, abortable(function(file) { // delete file if it already exists file.remove(); save(); }), abortable(function(ex) { if (ex.code === ex.NOT_FOUND_ERR) { save(); } else { fs_error(); } })); }), fs_error); }), fs_error); } , FS_proto = FileSaver.prototype , saveAs = function(blob, name) { return new FileSaver(blob, name); } ; FS_proto.abort = function() { var filesaver = this; filesaver.readyState = filesaver.DONE; dispatch(filesaver, "abort"); }; FS_proto.readyState = FS_proto.INIT = 0; FS_proto.WRITING = 1; FS_proto.DONE = 2; FS_proto.error = FS_proto.onwritestart = FS_proto.onprogress = FS_proto.onwrite = FS_proto.onabort = FS_proto.onerror = FS_proto.onwriteend = null; view.addEventListener("unload", process_deletion_queue, false); saveAs.unload = function() { process_deletion_queue(); view.removeEventListener("unload", process_deletion_queue, false); }; return saveAs; }( typeof self !== "undefined" && self || typeof window !== "undefined" && window || this.content )); // `self` is undefined in Firefox for Android content script context // while `this` is nsIContentFrameMessageManager // with an attribute `content` that corresponds to the window if (typeof module !== "undefined") module.exports = saveAs;