├── .gitignore ├── .npmignore ├── .prettierrc.json5 ├── LICENSE ├── README.md ├── build-all.js ├── index.d.ts ├── package.json ├── pnpm-lock.yaml ├── src ├── default-inflate.browser.js ├── default-inflate.node.js └── index.js ├── test ├── test.html ├── test.js ├── testfile.xlsx ├── testfile.zip ├── testfile2.zip └── testfile64.zip ├── tsconfig.json └── with-pako-demo ├── index.html ├── index.js ├── lib.js ├── package-lock.json └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules/ 3 | *.min.mjs 4 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | with-pako-demo/ 2 | src/ 3 | *.zip 4 | # we only need mjs 5 | *.js 6 | test.* 7 | tsconfig.json 8 | *firstpass.js -------------------------------------------------------------------------------- /.prettierrc.json5: -------------------------------------------------------------------------------- 1 | { 2 | "trailingComma": "all", 3 | "printWidth": 100, 4 | "singleQuote": true, 5 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # but-unzip 2 | 3 | small unzip library. 4 | ~704 bytes for Node, 5 | and ~951^ bytes for browsers, _before_ gzip. 6 | 7 | ^92.5%+ of browsers support [the decompression API](https://caniuse.com/mdn-api_decompressionstream), which in 2025, is probably all your users. 8 | If you _really_ care about the last 7.5%, you can dynamically import `pako`, adding ~20k: see below. 9 | 10 | ## Usage 11 | 12 | Install via your favorite package manager and import `but-unzip`. 13 | Has zero dependencies. 14 | 15 | ```bash 16 | $ npm install but-unzip 17 | 18 | # for old browsers you need 19 | $ npm install pako 20 | ``` 21 | 22 | This library returns zip entries synchronously, but only returns an entry's uncompressed bytes after calling `.read()`, which'll give `Uint8Array` _or_ `Promise`. 23 | 24 | ### Naïve use 25 | 26 | If there's a built-in function to inflate compressed files (like in Node or most browsers), you can use the code like: 27 | 28 | ```js 29 | import { iter } from 'but-unzip'; 30 | import * as fs from 'fs'; 31 | 32 | const bytes = fs.readFileSync('somezip.zip'); 33 | 34 | for (const entry of iter(bytes)) { 35 | console.info(entry.name, entry.comment); 36 | const bytes = await entry.read(); 37 | // do something with bytes 38 | } 39 | ``` 40 | 41 | ### Provide inflate function 42 | 43 | If you're worried about maximum compatibility: 44 | 45 | ```js 46 | import { unzip, inflateRaw as platformInflateRaw } from 'but-unzip'; 47 | import { inflateRaw as pakoInflateRaw } from 'pako/lib/inflate.js'; 48 | 49 | async function decompressUint8Array(zipBytes) { 50 | const allEntries = unzip(zipBytes, platformInflateRaw || pakoInflateRaw); 51 | // do something with entries 52 | } 53 | ``` 54 | 55 | ### Dynamically import inflate 56 | 57 | You _could_ use dynamic `import()` instead to include `pako`, but the intersection of users who: 58 | 59 | - don't have the compression API 60 | - don't support ESM imports 61 | 62 | …are extremely high (e.g., IE11 and weird old browsers). 63 | 64 | ```js 65 | import { inflateRaw as platformInflateRaw } from 'but-unzip'; 66 | const inflateRaw = platformInflateRaw || (await import('pako/lib/inflate.js').inflateRaw); 67 | ``` 68 | 69 | ## Limitations 70 | 71 | * This library doesn't support ZIP64, but probably should. 72 | But your browser (and Node) will probably not be happy to work with 4gb+ files, especially as this is not a streaming library (it just gives everything at once). 73 | 74 | * Like literally every zip library that exists, this only supports compression types 0 (store) and 8 (deflate). 75 | 76 | ## Notes 77 | 78 | * In my testing with `esbuild`, Pako's ESM bundling can be a bit broken, so importing "pako/lib/inflate.js" adds ~20k. 79 | Importing `pako` wholesale, even if you only use `inflateRaw`, adds ~45k. 80 | 81 | * If you're handling user data and it could be really big, use a `Worker`. 82 | But also, the compression API is `async` and doesn't block your main thread. 83 | YMMV! 84 | -------------------------------------------------------------------------------- /build-all.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | import * as childProcess from 'child_process'; 4 | import * as fs from 'fs'; 5 | 6 | for (const platform of ['node', 'browser']) { 7 | // compile twice lol 8 | childProcess.execSync(`esbuild --format=esm --minify --bundle --tree-shaking=true --platform=${platform} src/index.js > index.${platform}-firstpass.min.mjs`); 9 | childProcess.execSync(`esbuild --format=esm --minify --platform=${platform} index.${platform}-firstpass.min.mjs > index.${platform}.min.mjs`); 10 | 11 | const stat = fs.statSync(`index.${platform}.min.mjs`); 12 | console.info(platform, '=>', stat.size); 13 | } 14 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Iterate over all items found in the zip file. 3 | * 4 | * @param raw The raw bytes of the zip file 5 | * @param inflate Optional inflate helper; needed in browsers without {@link DecompressionStream} 6 | */ 7 | export function iter( 8 | raw: Uint8Array, 9 | inflate?: (raw: Uint8Array) => Promise | Uint8Array, 10 | ): Generator; 11 | 12 | /** 13 | * Returns all items found in the zip file at once. 14 | * 15 | * @param raw The raw bytes of the zip file 16 | * @param inflate Optional inflate helper; needed in browsers without {@link DecompressionStream} 17 | */ 18 | export function unzip( 19 | raw: Uint8Array, 20 | inflate?: (raw: Uint8Array) => Promise | Uint8Array, 21 | ): Array; 22 | 23 | /** 24 | * Each {@link ZipItem} can have its decompressed bytes read by calling its `read()` method, 25 | * which'll give either `Uint8Array` _or_ `Promise` (as it's possible to be sync). 26 | */ 27 | export type ZipItem = { 28 | filename: string; 29 | comment: string; 30 | read: () => Promise | Uint8Array; 31 | }; 32 | 33 | /** 34 | * The platform-default inflate helper. This is provided for Node, and browsers with support for 35 | * {@link DecompressionStream}. 36 | * 37 | * If this is `undefined`, you'll need to provide a helper (e.g., from the Pako library) to `unzip` 38 | * and `iter`. 39 | */ 40 | export const inflateRaw: ((raw: Uint8Array) => Promise | Uint8Array) | undefined; 41 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "devDependencies": { 3 | "@types/node": "^18.15.12", 4 | "esbuild": "^0.25.4" 5 | }, 6 | "exports": { 7 | ".": { 8 | "import": { 9 | "types": "./index.d.ts", 10 | "node": "./index.node.min.mjs", 11 | "browser": "./index.browser.min.mjs" 12 | } 13 | } 14 | }, 15 | "imports": { 16 | "#default-inflate": { 17 | "node": "./src/default-inflate.node.js", 18 | "browser": "./src/default-inflate.browser.js" 19 | } 20 | }, 21 | "type": "module", 22 | "name": "but-unzip", 23 | "version": "0.1.6", 24 | "description": "tiny (<1k) unzip for node/browser", 25 | "scripts": { 26 | "test": "node --test", 27 | "build": "node build-all.js", 28 | "prepublishOnly": "npm run build" 29 | }, 30 | "keywords": [ 31 | "unzip" 32 | ], 33 | "types": "index.d.ts", 34 | "author": "Sam Thorogood ", 35 | "license": "Apache-2.0", 36 | "bugs": { 37 | "url": "https://github.com/samthor/but-unzip/issues" 38 | }, 39 | "homepage": "https://github.com/samthor/but-unzip#readme" 40 | } 41 | -------------------------------------------------------------------------------- /pnpm-lock.yaml: -------------------------------------------------------------------------------- 1 | lockfileVersion: '9.0' 2 | 3 | settings: 4 | autoInstallPeers: true 5 | excludeLinksFromLockfile: false 6 | 7 | importers: 8 | 9 | .: 10 | devDependencies: 11 | '@types/node': 12 | specifier: ^18.15.12 13 | version: 18.15.12 14 | esbuild: 15 | specifier: ^0.25.4 16 | version: 0.25.4 17 | 18 | packages: 19 | 20 | '@esbuild/aix-ppc64@0.25.4': 21 | resolution: {integrity: sha512-1VCICWypeQKhVbE9oW/sJaAmjLxhVqacdkvPLEjwlttjfwENRSClS8EjBz0KzRyFSCPDIkuXW34Je/vk7zdB7Q==} 22 | engines: {node: '>=18'} 23 | cpu: [ppc64] 24 | os: [aix] 25 | 26 | '@esbuild/android-arm64@0.25.4': 27 | resolution: {integrity: sha512-bBy69pgfhMGtCnwpC/x5QhfxAz/cBgQ9enbtwjf6V9lnPI/hMyT9iWpR1arm0l3kttTr4L0KSLpKmLp/ilKS9A==} 28 | engines: {node: '>=18'} 29 | cpu: [arm64] 30 | os: [android] 31 | 32 | '@esbuild/android-arm@0.25.4': 33 | resolution: {integrity: sha512-QNdQEps7DfFwE3hXiU4BZeOV68HHzYwGd0Nthhd3uCkkEKK7/R6MTgM0P7H7FAs5pU/DIWsviMmEGxEoxIZ+ZQ==} 34 | engines: {node: '>=18'} 35 | cpu: [arm] 36 | os: [android] 37 | 38 | '@esbuild/android-x64@0.25.4': 39 | resolution: {integrity: sha512-TVhdVtQIFuVpIIR282btcGC2oGQoSfZfmBdTip2anCaVYcqWlZXGcdcKIUklfX2wj0JklNYgz39OBqh2cqXvcQ==} 40 | engines: {node: '>=18'} 41 | cpu: [x64] 42 | os: [android] 43 | 44 | '@esbuild/darwin-arm64@0.25.4': 45 | resolution: {integrity: sha512-Y1giCfM4nlHDWEfSckMzeWNdQS31BQGs9/rouw6Ub91tkK79aIMTH3q9xHvzH8d0wDru5Ci0kWB8b3up/nl16g==} 46 | engines: {node: '>=18'} 47 | cpu: [arm64] 48 | os: [darwin] 49 | 50 | '@esbuild/darwin-x64@0.25.4': 51 | resolution: {integrity: sha512-CJsry8ZGM5VFVeyUYB3cdKpd/H69PYez4eJh1W/t38vzutdjEjtP7hB6eLKBoOdxcAlCtEYHzQ/PJ/oU9I4u0A==} 52 | engines: {node: '>=18'} 53 | cpu: [x64] 54 | os: [darwin] 55 | 56 | '@esbuild/freebsd-arm64@0.25.4': 57 | resolution: {integrity: sha512-yYq+39NlTRzU2XmoPW4l5Ifpl9fqSk0nAJYM/V/WUGPEFfek1epLHJIkTQM6bBs1swApjO5nWgvr843g6TjxuQ==} 58 | engines: {node: '>=18'} 59 | cpu: [arm64] 60 | os: [freebsd] 61 | 62 | '@esbuild/freebsd-x64@0.25.4': 63 | resolution: {integrity: sha512-0FgvOJ6UUMflsHSPLzdfDnnBBVoCDtBTVyn/MrWloUNvq/5SFmh13l3dvgRPkDihRxb77Y17MbqbCAa2strMQQ==} 64 | engines: {node: '>=18'} 65 | cpu: [x64] 66 | os: [freebsd] 67 | 68 | '@esbuild/linux-arm64@0.25.4': 69 | resolution: {integrity: sha512-+89UsQTfXdmjIvZS6nUnOOLoXnkUTB9hR5QAeLrQdzOSWZvNSAXAtcRDHWtqAUtAmv7ZM1WPOOeSxDzzzMogiQ==} 70 | engines: {node: '>=18'} 71 | cpu: [arm64] 72 | os: [linux] 73 | 74 | '@esbuild/linux-arm@0.25.4': 75 | resolution: {integrity: sha512-kro4c0P85GMfFYqW4TWOpvmF8rFShbWGnrLqlzp4X1TNWjRY3JMYUfDCtOxPKOIY8B0WC8HN51hGP4I4hz4AaQ==} 76 | engines: {node: '>=18'} 77 | cpu: [arm] 78 | os: [linux] 79 | 80 | '@esbuild/linux-ia32@0.25.4': 81 | resolution: {integrity: sha512-yTEjoapy8UP3rv8dB0ip3AfMpRbyhSN3+hY8mo/i4QXFeDxmiYbEKp3ZRjBKcOP862Ua4b1PDfwlvbuwY7hIGQ==} 82 | engines: {node: '>=18'} 83 | cpu: [ia32] 84 | os: [linux] 85 | 86 | '@esbuild/linux-loong64@0.25.4': 87 | resolution: {integrity: sha512-NeqqYkrcGzFwi6CGRGNMOjWGGSYOpqwCjS9fvaUlX5s3zwOtn1qwg1s2iE2svBe4Q/YOG1q6875lcAoQK/F4VA==} 88 | engines: {node: '>=18'} 89 | cpu: [loong64] 90 | os: [linux] 91 | 92 | '@esbuild/linux-mips64el@0.25.4': 93 | resolution: {integrity: sha512-IcvTlF9dtLrfL/M8WgNI/qJYBENP3ekgsHbYUIzEzq5XJzzVEV/fXY9WFPfEEXmu3ck2qJP8LG/p3Q8f7Zc2Xg==} 94 | engines: {node: '>=18'} 95 | cpu: [mips64el] 96 | os: [linux] 97 | 98 | '@esbuild/linux-ppc64@0.25.4': 99 | resolution: {integrity: sha512-HOy0aLTJTVtoTeGZh4HSXaO6M95qu4k5lJcH4gxv56iaycfz1S8GO/5Jh6X4Y1YiI0h7cRyLi+HixMR+88swag==} 100 | engines: {node: '>=18'} 101 | cpu: [ppc64] 102 | os: [linux] 103 | 104 | '@esbuild/linux-riscv64@0.25.4': 105 | resolution: {integrity: sha512-i8JUDAufpz9jOzo4yIShCTcXzS07vEgWzyX3NH2G7LEFVgrLEhjwL3ajFE4fZI3I4ZgiM7JH3GQ7ReObROvSUA==} 106 | engines: {node: '>=18'} 107 | cpu: [riscv64] 108 | os: [linux] 109 | 110 | '@esbuild/linux-s390x@0.25.4': 111 | resolution: {integrity: sha512-jFnu+6UbLlzIjPQpWCNh5QtrcNfMLjgIavnwPQAfoGx4q17ocOU9MsQ2QVvFxwQoWpZT8DvTLooTvmOQXkO51g==} 112 | engines: {node: '>=18'} 113 | cpu: [s390x] 114 | os: [linux] 115 | 116 | '@esbuild/linux-x64@0.25.4': 117 | resolution: {integrity: sha512-6e0cvXwzOnVWJHq+mskP8DNSrKBr1bULBvnFLpc1KY+d+irZSgZ02TGse5FsafKS5jg2e4pbvK6TPXaF/A6+CA==} 118 | engines: {node: '>=18'} 119 | cpu: [x64] 120 | os: [linux] 121 | 122 | '@esbuild/netbsd-arm64@0.25.4': 123 | resolution: {integrity: sha512-vUnkBYxZW4hL/ie91hSqaSNjulOnYXE1VSLusnvHg2u3jewJBz3YzB9+oCw8DABeVqZGg94t9tyZFoHma8gWZQ==} 124 | engines: {node: '>=18'} 125 | cpu: [arm64] 126 | os: [netbsd] 127 | 128 | '@esbuild/netbsd-x64@0.25.4': 129 | resolution: {integrity: sha512-XAg8pIQn5CzhOB8odIcAm42QsOfa98SBeKUdo4xa8OvX8LbMZqEtgeWE9P/Wxt7MlG2QqvjGths+nq48TrUiKw==} 130 | engines: {node: '>=18'} 131 | cpu: [x64] 132 | os: [netbsd] 133 | 134 | '@esbuild/openbsd-arm64@0.25.4': 135 | resolution: {integrity: sha512-Ct2WcFEANlFDtp1nVAXSNBPDxyU+j7+tId//iHXU2f/lN5AmO4zLyhDcpR5Cz1r08mVxzt3Jpyt4PmXQ1O6+7A==} 136 | engines: {node: '>=18'} 137 | cpu: [arm64] 138 | os: [openbsd] 139 | 140 | '@esbuild/openbsd-x64@0.25.4': 141 | resolution: {integrity: sha512-xAGGhyOQ9Otm1Xu8NT1ifGLnA6M3sJxZ6ixylb+vIUVzvvd6GOALpwQrYrtlPouMqd/vSbgehz6HaVk4+7Afhw==} 142 | engines: {node: '>=18'} 143 | cpu: [x64] 144 | os: [openbsd] 145 | 146 | '@esbuild/sunos-x64@0.25.4': 147 | resolution: {integrity: sha512-Mw+tzy4pp6wZEK0+Lwr76pWLjrtjmJyUB23tHKqEDP74R3q95luY/bXqXZeYl4NYlvwOqoRKlInQialgCKy67Q==} 148 | engines: {node: '>=18'} 149 | cpu: [x64] 150 | os: [sunos] 151 | 152 | '@esbuild/win32-arm64@0.25.4': 153 | resolution: {integrity: sha512-AVUP428VQTSddguz9dO9ngb+E5aScyg7nOeJDrF1HPYu555gmza3bDGMPhmVXL8svDSoqPCsCPjb265yG/kLKQ==} 154 | engines: {node: '>=18'} 155 | cpu: [arm64] 156 | os: [win32] 157 | 158 | '@esbuild/win32-ia32@0.25.4': 159 | resolution: {integrity: sha512-i1sW+1i+oWvQzSgfRcxxG2k4I9n3O9NRqy8U+uugaT2Dy7kLO9Y7wI72haOahxceMX8hZAzgGou1FhndRldxRg==} 160 | engines: {node: '>=18'} 161 | cpu: [ia32] 162 | os: [win32] 163 | 164 | '@esbuild/win32-x64@0.25.4': 165 | resolution: {integrity: sha512-nOT2vZNw6hJ+z43oP1SPea/G/6AbN6X+bGNhNuq8NtRHy4wsMhw765IKLNmnjek7GvjWBYQ8Q5VBoYTFg9y1UQ==} 166 | engines: {node: '>=18'} 167 | cpu: [x64] 168 | os: [win32] 169 | 170 | '@types/node@18.15.12': 171 | resolution: {integrity: sha512-Wha1UwsB3CYdqUm2PPzh/1gujGCNtWVUYF0mB00fJFoR4gTyWTDPjSm+zBF787Ahw8vSGgBja90MkgFwvB86Dg==} 172 | 173 | esbuild@0.25.4: 174 | resolution: {integrity: sha512-8pgjLUcUjcgDg+2Q4NYXnPbo/vncAY4UmyaCm0jZevERqCHZIaWwdJHkf8XQtu4AxSKCdvrUbT0XUr1IdZzI8Q==} 175 | engines: {node: '>=18'} 176 | hasBin: true 177 | 178 | snapshots: 179 | 180 | '@esbuild/aix-ppc64@0.25.4': 181 | optional: true 182 | 183 | '@esbuild/android-arm64@0.25.4': 184 | optional: true 185 | 186 | '@esbuild/android-arm@0.25.4': 187 | optional: true 188 | 189 | '@esbuild/android-x64@0.25.4': 190 | optional: true 191 | 192 | '@esbuild/darwin-arm64@0.25.4': 193 | optional: true 194 | 195 | '@esbuild/darwin-x64@0.25.4': 196 | optional: true 197 | 198 | '@esbuild/freebsd-arm64@0.25.4': 199 | optional: true 200 | 201 | '@esbuild/freebsd-x64@0.25.4': 202 | optional: true 203 | 204 | '@esbuild/linux-arm64@0.25.4': 205 | optional: true 206 | 207 | '@esbuild/linux-arm@0.25.4': 208 | optional: true 209 | 210 | '@esbuild/linux-ia32@0.25.4': 211 | optional: true 212 | 213 | '@esbuild/linux-loong64@0.25.4': 214 | optional: true 215 | 216 | '@esbuild/linux-mips64el@0.25.4': 217 | optional: true 218 | 219 | '@esbuild/linux-ppc64@0.25.4': 220 | optional: true 221 | 222 | '@esbuild/linux-riscv64@0.25.4': 223 | optional: true 224 | 225 | '@esbuild/linux-s390x@0.25.4': 226 | optional: true 227 | 228 | '@esbuild/linux-x64@0.25.4': 229 | optional: true 230 | 231 | '@esbuild/netbsd-arm64@0.25.4': 232 | optional: true 233 | 234 | '@esbuild/netbsd-x64@0.25.4': 235 | optional: true 236 | 237 | '@esbuild/openbsd-arm64@0.25.4': 238 | optional: true 239 | 240 | '@esbuild/openbsd-x64@0.25.4': 241 | optional: true 242 | 243 | '@esbuild/sunos-x64@0.25.4': 244 | optional: true 245 | 246 | '@esbuild/win32-arm64@0.25.4': 247 | optional: true 248 | 249 | '@esbuild/win32-ia32@0.25.4': 250 | optional: true 251 | 252 | '@esbuild/win32-x64@0.25.4': 253 | optional: true 254 | 255 | '@types/node@18.15.12': {} 256 | 257 | esbuild@0.25.4: 258 | optionalDependencies: 259 | '@esbuild/aix-ppc64': 0.25.4 260 | '@esbuild/android-arm': 0.25.4 261 | '@esbuild/android-arm64': 0.25.4 262 | '@esbuild/android-x64': 0.25.4 263 | '@esbuild/darwin-arm64': 0.25.4 264 | '@esbuild/darwin-x64': 0.25.4 265 | '@esbuild/freebsd-arm64': 0.25.4 266 | '@esbuild/freebsd-x64': 0.25.4 267 | '@esbuild/linux-arm': 0.25.4 268 | '@esbuild/linux-arm64': 0.25.4 269 | '@esbuild/linux-ia32': 0.25.4 270 | '@esbuild/linux-loong64': 0.25.4 271 | '@esbuild/linux-mips64el': 0.25.4 272 | '@esbuild/linux-ppc64': 0.25.4 273 | '@esbuild/linux-riscv64': 0.25.4 274 | '@esbuild/linux-s390x': 0.25.4 275 | '@esbuild/linux-x64': 0.25.4 276 | '@esbuild/netbsd-arm64': 0.25.4 277 | '@esbuild/netbsd-x64': 0.25.4 278 | '@esbuild/openbsd-arm64': 0.25.4 279 | '@esbuild/openbsd-x64': 0.25.4 280 | '@esbuild/sunos-x64': 0.25.4 281 | '@esbuild/win32-arm64': 0.25.4 282 | '@esbuild/win32-ia32': 0.25.4 283 | '@esbuild/win32-x64': 0.25.4 284 | -------------------------------------------------------------------------------- /src/default-inflate.browser.js: -------------------------------------------------------------------------------- 1 | export { inflateRaw }; 2 | 3 | let inflateRaw = undefined; 4 | 5 | const build = () => new DecompressionStream('deflate-raw'); 6 | 7 | try { 8 | build(); 9 | 10 | // https://zlib.net/manual.html: 11 | // A raw deflate stream is one with no zlib or gzip header or trailer. This routine would 12 | // normally be used in a utility that reads zip or gzip files and writes out uncompressed files. 13 | // The utility would decode the header and process the trailer on its own, hence this routine 14 | // expects only the raw deflate stream to decompress. This is different from the default 15 | // behavior of inflate(), which expects a zlib header and trailer around the deflate stream. 16 | 17 | inflateRaw = async (bytes) => { 18 | /** @type {{ readable: ReadableStream, writable: WritableStream }} */ 19 | const stream = build(); 20 | 21 | const w = stream.writable.getWriter(); 22 | const r = stream.readable.getReader(); 23 | 24 | /** @type {Uint8Array} */ 25 | let out; 26 | 27 | /** @type {Uint8Array[]} */ 28 | const agg = []; 29 | let size = 0; 30 | let i = 0; 31 | 32 | /** @type {ReadableStreamReadResult} */ 33 | let s; 34 | 35 | w.write(bytes); 36 | w.close(); 37 | 38 | while (!(s = await r.read()).done) { 39 | // use out as tmp var 40 | out = s.value; 41 | agg.push(out); 42 | size += out.length; 43 | } 44 | 45 | // we only got one chunk, return it 46 | if (!(agg.length - 1)) { 47 | return agg[0]; 48 | } 49 | 50 | // have to merge chunks 51 | out = new Uint8Array(size); 52 | agg.map((a) => ( 53 | out.set(a, i), 54 | i += a.length 55 | )); 56 | return out; 57 | }; 58 | } catch {} 59 | 60 | -------------------------------------------------------------------------------- /src/default-inflate.node.js: -------------------------------------------------------------------------------- 1 | import { inflateRaw } from 'zlib'; 2 | import { promisify } from 'util'; 3 | 4 | const p = promisify(inflateRaw); 5 | export { p as inflateRaw }; 6 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | 2 | import { inflateRaw } from '#default-inflate'; 3 | export { inflateRaw }; 4 | 5 | const LOCAL_FILE_HEADER_TYPE = 0x04034b50; 6 | const CENTRAL_FILE_HEADER_TYPE = 0x02014b50; 7 | const END_CENTRAL_TYPE = 0x06054b50; 8 | 9 | 10 | const d = new TextDecoder(); 11 | 12 | 13 | /** @type {(code: number) => never} */ 14 | const throwCode = (code) => { throw new Error('but-unzip~' + code); }; 15 | 16 | 17 | // TODO: this should try utf-8, but if fails, fall back to "dos" encoding 18 | /** @type {(raw: Uint8Array) => string} */ 19 | const decodeString = (raw) => d.decode(raw); 20 | 21 | /** 22 | * @type {(...args: Parameters) => { filename: string, comment: string, read: () => Promise|Uint8Array }[]} 23 | */ 24 | export const unzip = (...args) => [...iter(...args)]; 25 | 26 | /** 27 | * @param {Uint8Array} raw 28 | * @param {(raw: Uint8Array) => Promise|Uint8Array} inf 29 | * @return {Generator<{ filename: string, comment: string, read: () => Promise|Uint8Array }>} 30 | */ 31 | export function* iter(raw, inf = inflateRaw) { 32 | // TODO: could go forward as we generally don't expect a comment. Might be faster? 33 | let at = raw.length - 20; 34 | const bounds = Math.max(at - 65516, 2); // sub 2**256 - 20 (max comment length) 35 | 36 | while ( 37 | ((at = raw.lastIndexOf(0x50, at - 1)) !== -1) && 38 | !(raw[at + 1] === 0x4b && raw[at + 2] === 0x05 && raw[at + 3] === 0x06) && 39 | at > bounds 40 | ) { } 41 | 42 | if (at === -1) { 43 | throwCode(2); // bad zip format 44 | } 45 | 46 | /** @type {(startBy: number, endBy: number) => Uint8Array} */ 47 | const subarrayMove = (startBy, endBy) => raw.subarray(at += startBy, at += endBy); 48 | 49 | const dataView = new DataView(raw.buffer, raw.byteOffset); // we don't need byteLength, could be a longer buffer :shrug: 50 | 51 | /** @type {(off: number) => number} */ 52 | const u16 = (off) => dataView.getUint16(off + at, true); 53 | 54 | /** @type {(off: number) => number} */ 55 | const u32 = (off) => dataView.getUint32(off + at, true); 56 | 57 | 58 | // read end central directory 59 | let fileCount = u16(10); 60 | if (fileCount !== u16(8)) { 61 | throwCode(3); // no multi-disk support 62 | } 63 | const centralDirectoryStart = u32(16); 64 | at = centralDirectoryStart; 65 | 66 | // read central directory 67 | while (fileCount--) { 68 | const compressionMethod = u16(10); 69 | // const filenameLength = u16(28); 70 | const extraFieldsLength = u16(30); 71 | const commentLength = u16(32); 72 | const compressedSize = u32(20); 73 | 74 | // find local entry location 75 | const localEntryAt = u32(42); 76 | 77 | // read buffers, move at to after entry, and store where we were 78 | const filename = decodeString(subarrayMove(46, u16(28))); 79 | // we skip extraFields here 80 | const comment = decodeString(subarrayMove(extraFieldsLength, commentLength)); 81 | const nextCentralDirectoryEntry = at; 82 | 83 | /** @type {Uint8Array} */ 84 | let bytes; 85 | 86 | // >> start reading entry 87 | at = localEntryAt; 88 | 89 | // this is the local entry (filename + extra fields) length, which we skip 90 | bytes = subarrayMove(30 + u16(26) + u16(28), compressedSize); 91 | 92 | yield { 93 | filename, 94 | comment, 95 | read: () => { 96 | return (compressionMethod & 8) ? inf(bytes) : 97 | (compressionMethod ? throwCode(1) : bytes); 98 | // if (!compressionMethod) { 99 | // return bytes; 100 | // // do nothing 101 | // } else if (compressionMethod&8) { 102 | // return inf(bytes); 103 | // } 104 | // return throwCode(1); // only suppors DEFLATE 105 | }, 106 | }; 107 | 108 | at = nextCentralDirectoryEntry; 109 | // << finish reading entry 110 | } 111 | } 112 | 113 | -------------------------------------------------------------------------------- /test/test.html: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | import { unzip } from '../src/index.js'; 2 | import * as fs from 'node:fs'; 3 | import test from 'node:test'; 4 | import * as assert from 'node:assert'; 5 | 6 | test('unzip', () => { 7 | const b = fs.readFileSync('./test/testfile2.zip'); 8 | const out = unzip(b); 9 | 10 | assert.strictEqual(out.length, 2); 11 | assert.strictEqual(out[0]?.filename, 'package-lock.json'); 12 | assert.strictEqual(out[1]?.filename, 'package.json'); 13 | }); 14 | 15 | test('unzip bytes', async () => { 16 | const b = fs.readFileSync('./test/testfile.zip'); 17 | const out = unzip(b); 18 | 19 | assert.strictEqual(out.length, 1); 20 | 21 | const file = await out[0]?.read(); 22 | assert.strictEqual(new TextDecoder().decode(file), 'Hello!\n'); 23 | }); 24 | 25 | test('unzip zip64', async () => { 26 | const b = fs.readFileSync('./test/testfile64.zip'); 27 | const out = unzip(b); 28 | 29 | assert.strictEqual(out.length, 1); 30 | 31 | const file = await out[0]?.read(); 32 | assert.strictEqual(new TextDecoder().decode(file), 'This is a long string\n'); 33 | }); 34 | 35 | test('ignores bad file', () => { 36 | const sizes = [0, 2, 100, 256 ** 2, 256 ** 3]; 37 | for (const size of sizes) { 38 | const whatever = new Uint8Array(size); 39 | 40 | assert.throws(() => { 41 | unzip(whatever, () => new Uint8Array()); 42 | }); 43 | } 44 | }); 45 | 46 | test('opens xlsx file', async () => { 47 | const b = fs.readFileSync('./test/testfile.xlsx'); 48 | const out = unzip(b); 49 | 50 | assert.strictEqual(out.length, 11); 51 | 52 | const sheet2 = out.find((x) => x.filename === 'xl/worksheets/sheet2.xml'); 53 | assert.ok(sheet2); 54 | 55 | const bytes = await sheet2?.read(); 56 | const s = new TextDecoder().decode(bytes); 57 | assert.ok(s.startsWith(' -------------------------------------------------------------------------------- /with-pako-demo/index.js: -------------------------------------------------------------------------------- 1 | import { iter, inflateRaw } from './lib.js'; 2 | 3 | const bytes = await fetch('../testfile2.zip').then((r) => r.blob()); 4 | 5 | const buf = await bytes.arrayBuffer(); 6 | const uint8 = new Uint8Array(buf); 7 | 8 | for (const i of iter(uint8, inflateRaw)) { 9 | console.info(i); 10 | const b = await i.read(); 11 | console.info('bytes', b); 12 | 13 | const s = new TextDecoder().decode(b); 14 | console.info('s', s); 15 | } 16 | -------------------------------------------------------------------------------- /with-pako-demo/lib.js: -------------------------------------------------------------------------------- 1 | import { iter } from '../index.browser.min.mjs'; 2 | import { inflateRaw } from 'pako/lib/inflate.js'; 3 | 4 | export { iter, inflateRaw }; -------------------------------------------------------------------------------- /with-pako-demo/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "with-pako-demo", 3 | "lockfileVersion": 2, 4 | "requires": true, 5 | "packages": { 6 | "": { 7 | "dependencies": { 8 | "pako": "^2.0.4" 9 | } 10 | }, 11 | "node_modules/pako": { 12 | "version": "2.0.4", 13 | "resolved": "https://registry.npmjs.org/pako/-/pako-2.0.4.tgz", 14 | "integrity": "sha512-v8tweI900AUkZN6heMU/4Uy4cXRc2AYNRggVmTR+dEncawDJgCdLMximOVA2p4qO57WMynangsfGRb5WD6L1Bg==" 15 | } 16 | }, 17 | "dependencies": { 18 | "pako": { 19 | "version": "2.0.4", 20 | "resolved": "https://registry.npmjs.org/pako/-/pako-2.0.4.tgz", 21 | "integrity": "sha512-v8tweI900AUkZN6heMU/4Uy4cXRc2AYNRggVmTR+dEncawDJgCdLMximOVA2p4qO57WMynangsfGRb5WD6L1Bg==" 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /with-pako-demo/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "pako": "^2.0.4" 4 | } 5 | } 6 | --------------------------------------------------------------------------------