Simple modern, small, zero dependency zip library for browser and node based JavaScript
[](https://travis-ci.org/greggman/zipup)
[[Live Tests](https://greggman.github.io/zipup/test/)]
* Less than 3k gzipped.
# How to use
```js
import { Zip } from '@greggman/zipup';
const zip = new Zip();
zip.addFile('foo.txt', someString);
zip.addFile('bar.png', someArrayBuffer);
zip.addFile('folder/moo.mp4', someArrayBufferView);
zip.addFile('folder/stuff.bin', someBlob);
const blob = await zip.finalize(comment?: string);
```
## Why?
Because most of the other libraries are old and crufty. The browser itself now
supports compression and so does node.js so why not just use those. Example:
JSZip is 97k minified, 28k gzipped. zipup is 8k minified, 3k (gzipped). Yes,
that's not a completely fair comparison. zipup has the functionality I need. I
don't need the other stuff. Nor do I need 13 dependencies.
Note: the library uses [`CompressionStream`](https://caniuse.com/?search=compressionstream).
Use a [polyfill](https://github.com/101arrowz/compression-streams-polyfill) if you're supporting
old stuff.
# API
```js
class Zip {
constructor(options: { platform?: Platform });
async addFile(pathOrInfo: string | EntryInfo, string | ArrayBuffer | ArrayBufferView | Blob): Promise;
addFolder(pathOrInfo: string | EntryInfo): ZipFolder;
async finalize(comment?: string): Promise;
};
class EntryInfo {
name: string, // name of entry
comment?: string, // the comment for this entry
lastModDate?: Date, // a Date
}
class ZipFolder {
async addFile(pathOrInfo: string | EntryInfo, string | ArrayBuffer | ArrayBufferView | Blob): Promise;
addFolder(pathOrInfo: string | EntryInfo): ZipFolder;
}
type Platform = 'windows' | 'linux' | 'macos' | 'unix';
```
As you can see above you can pass either just a path, or more data
```js
zip.addFile('hello.txt', someString);
zip.addFile({
name: 'readme.txt',
comment: 'the readme file',
lastModData: new Date(), // or any date
}, content)
```
`addFile` is asynchronous. You do not have to wait as `finalize` will automatically wait
but `addFile` does return a promise if you want to throttle (🤷♂️).
You can add a file to a folder either by including it in the name
```js
zip.addFile('folder/file.bin', ...);
```
or by creating a folder
```js
const folder = zip.addFolder('folder');
folder.addFile('file.bin');
```
# Notes:
## Live Browser Tests
[https://greggman.github.io/zipup/test/](https://greggman.github.io/zipup/test/)
# License
MIT