MCPcopy Index your code
hub / github.com/Touffy/client-zip

github.com/Touffy/client-zip @v2.5.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v2.5.0 ↗ · + Follow
39 symbols 106 edges 16 files 0 documented · 0% 1 cross-repo links
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Test Size Dependencies Types

What is client-zip ?

client-zip concatenates multiple files (e.g. from multiple HTTP requests) into a single ZIP, in the browser, so you can let your users download all the files in one click. It does not compress the files or unzip existing archives.

client-zip is lightweight (6.4 kB minified, 2.6 kB gzipped), dependency-free, and 40 times faster than the old JSZip.

Quick Start

npm i client-zip

(or just load the module from a CDN such as UNPKG or jsDelivr)

For direct usage with a ServiceWorker's importScripts, a worker.js file is also available alongside the module.

import { downloadZip } from "https://cdn.jsdelivr.net/npm/client-zip/index.js"

async function downloadTestZip() {
  // define what we want in the ZIP
  const code = await fetch("https://raw.githubusercontent.com/Touffy/client-zip/master/src/index.ts")
  const intro = { name: "intro.txt", lastModified: new Date(), input: "Hello. This is the client-zip library." }

  // get the ZIP stream in a Blob
  const blob = await downloadZip([intro, code]).blob()

  // make and click a temporary link to download the Blob
  const link = document.createElement("a")
  link.href = URL.createObjectURL(blob)
  link.download = "test.zip"
  link.click()
  link.remove()

  // in real life, don't forget to revoke your Blob URLs if you use them
}

Compatibility

client-zip works in all modern browsers (and Deno) out of the box. If you bundle it with your app and try to transpile it down to lower than ES2020, it will break because it needs BigInts. Version 1.x may be painfully transpiled down to as low as ES2015.

The default release of version 2 targets ES2020 and is a bare ES module + an IIFE version suitable for a ServiceWorker's importScript. Version 1 releases were built for ES2018.

When necessary, client-zip version 2 will generate Zip64 archives. It will always specify "ZIP version 4.5 required to unzip", even when that's not really true. The resulting files are not readable by every ZIP reader out there.

Usage

The module exports three functions:

function downloadZip(files: ForAwaitable<InputTypes>, options?: Options): Response

function makeZip(files: ForAwaitable<InputTypes>, options?: Options): ReadableStream

function predictLength(metadata: Iterable<MetadataTypes>): bigint

downloadZip is obviously the main function and the only one exposed by the worker script. You give it an (async or not) iterable a.k.a ForAwaitable list of inputs. Each input (InputTypes) can be: * a Response * a File * or an object with the properties: - name: the file name ; optional if your input is a File or a Response because they have relevant metadata - lastModified: last modification date of the file (defaults to new Date() unless the input is a File or Response with a valid "Last-Modified" header) - input: something that contains your data; it can be a File, a Blob, a Response, some kind of ArrayView or a raw ArrayBuffer, a ReadableStream<Uint8Array> (yes, only Uint8Arrays, but most APIs give you just that type anyway), an AsyncIterable<ArrayBuffer | ArrayView | string>, … or just a string. - mode: override the POSIX file mode (by default, it will be 0o664). Should be between 0 and 0o777 — disrespect that constraint at your own risk.

The options argument currently supports three properties, length, metadata (see Content-Length prediction) and buffersAreUTF8 (see Filename encoding).

The function returns a Response immediately. You don't need to wait for the whole ZIP to be ready. It's up to you if you want to pipe the Response somewhere (e.g. if you are using client-zip inside a ServiceWorker) or let the browser buffer it all in a Blob.

Unless your list of inputs is quite small, you should prefer generators (when zipping Files or other resources that are already available) and async generators (when zipping Responses so you can fetch them lazily, or other resources that are generated last-minute so you don't need to store them longer than necessary) to provide the inputs to downloadZip.

makeZip is just like downloadZip except it returns the underlying ReadableStream directly, for use cases that do not involve actually downloading to the client filesystem.

Content-Length prediction

Because of client-zip's streaming design, it can't look ahead at all the files to determine how big the complete archive will be. The returned Response will therefore not have a "Content-Length" header, and that can be problematic.

Starting with version 1.5, if you are able to gather all the relevant metadata (file sizes and names) before calling downloadZip, you can get it to predict the exact size of the archive and include it as a "Content-Length" header. The metadata must be a synchronous iterable, where each item (MetadataTypes) can be :

  • a Response, either from the actual request you will use as input, or a HEAD request (either way, the response body will not be consumed at this point)
  • a File
  • or an object with the properties:
  • name: the file name ; optional if your input is a File or a Response because they have relevant metadata
  • size: the byte length of the file ; also optional if you provide a File or a Response with a Content-Length header
  • input: same as what you'd pass as the actual input, except this is optional here, and passing a Stream is completely useless

If you already have Files (e.g. in a form input), it's alright to pass them as metadata too. However, if you would normally fetch each file from a server, or generate them dynamically, please try using a dedicated metadata endpoint or function, and transforming its response into an array of {name, size} objects, rather than doing all the requests or computations in advance just to get a Content-Length.

An object with a name but no input and no size (not even zero) will be interpreted as an empty folder and renamed accordingly. To properly specify empty files without an input, set the size explicitly to zero (0 or 0n).

This iterable of metadata can be passed as the metadata property of downloadZip's options, or, if you want to display the predicted size without actually creating the Zip file, to the predictLength function (not exposed in the worker script). Naturally, the metadata and actual data must match, and be provided in the same order! Otherwise, there could be inaccuracies in Zip64 lengths.

In the case of predictLength, you can even save the return value and pass it later to downloadZip as the length option, instead of repeating the metadata.

Filename encoding

(tl;dr: set buffersAreUTF8: true in the options argument)

In ZIP archives, the language encoding flag indicates that a filename is encoded in UTF-8. Some ZIP archive programs (e.g. build-in ZIP archive viewer in Windows) might not decode UTF-8 filenames correctly if this flag is off.

client-zip always encodes string filenames (including filenames extracted from URLs) as UTF-8 and sets this flag for the related entries. However, downloadZip's options include a buffersAreUTF8 setting, affecting filenames that you supply as an ArrayBuffer (or ArrayView).

By default (when buffersAreUTF8 is not set or undefined), each ArrayBuffer filename will be tested, and flagged only if it is valid UTF-8. It is a safe default, but a little inefficient because UTF-8 is the only thing you can get in most contexts anyway. So you may tell client-zip to skip the test by setting buffersAreUTF8: true ; ArrayBuffers will always be flagged as UTF-8 without checking.

If you happen to get your filenames from a dusty API reading from an antique filesystem with non-ASCII filenames encoded in some retro 8-bit encoding and you want to keep them that way in the ZIP archive, you may set buffersAreUTF8: false ; ArrayBuffer filenames will never be flagged as UTF-8. Please beware that the stored filenames will extract correctly only with a ZIP program using the same system encoding as the source.

Benchmarks

updated in may 2023

updated again in may 2023 (experiment 3)

I started this project because I wasn't impressed with what — at the time — appeared to be the only other ZIP library for browsers, JSZip. I later found other libraries, which I've included in the new benchmarks, and JSZip has improved dramatically (version 3.6 was 40 times slower vs. currently only 40% slower).

I requested Blob outputs from each lib, without compression. I measured the time until the blob was ready, on my M1 Pro. Sounds fair?

Experiment 1 consists of 4 files (total 539 MB) manually added to a file input from my local filesystem, so there is no latency and the ZIP format structural overhead is insignificant.

Experiment 2 is a set of 6214 small TGA files (total 119 MB). I tried to load them with a file input as before, but my browsers kept throwing errors while processing the large array of Files. So I had to switch to a different method, where the files are served over HTTP locally by nginx and fetched lazily. Unfortunately, that causes some atrocious latency across the board.

Experiment 3 is the same set of 6214 TGA files combined with very small PNG files for a total of 12 044 files (total 130 MB). This time, the files are fetched by a DownloadStream to minimize latency.

client-zip@2.4.3 fflate@0.7.4 zip.js@2.7.14 conflux@4.0.3 JSZip@3.10.1
experiment 1 Safari 1.647 (σ=21) s 1.792 (σ=15) s 1.912 (σ=80) s 1.820 (σ=16) s 2.122 (σ=60) s
baseline: 1.653 s Chrome 2.480 (σ=41) s 1.601 (σ=4) s 4.251 (σ=53) s 4.268 (σ=44) s 3.921 (σ=15) s
experiment 2 Safari 2.173 (σ=11) s 2.157 (σ=23) s 3.158 (σ=17) s 1.794 (σ=13) s 2.631 (σ=27) s
baseline: 0.615 s Chrome 3.567 (σ=77) s 3.506 (σ=9) s 5.689 (σ=17) s 3.174 (σ=22) s 4.602 (σ=50) s
experiment 3 Safari 1.768 (σ=12) s 1.691 (σ=19) s 3.149 (σ=45) s 1.511 (σ=38) s 2.703 (σ=79) s
baseline: 0.892 s Chrome 4.604 (σ=79) s 3.972 (σ=85) s 7.507 (σ=261) s 3.812 (σ=80) s 6.297 (σ=35) s

The experiments were run 10 times (not counting a first run to let the JavaScript engine "warm up" and ensure the browser caches everything) for each lib and each dataset, with the dev tools closed (this is important, opening the dev tools has a noticeable impact on CPU and severe impact on HTTP latency). The numbers in the table are the mean time of the ten runs, with the standard deviation in parentheses.

For the baseline, I timed the zip -0 process in my UNIX shell. As advertised, fflate run just as fast — in Chrome, anyway, and when there is no overhead for HTTP (experiment 1). In the same test, client-zip beats everyone else in Safari.

Conflux does particularly well in the second and third experiments thanks to its internal use of ReadableStreams, which seem to run faster than async generators.

Zip.js workers were disabled because I didn't want to bother fixing the error I got from the library. Using workers on this task could only help by sacrificing lots of memory, anyway. But I suppose Zip.js really needs those workers to offset its disgraceful single-threaded performance.

It's interesting that Chrome performs so much worse than Safari with client-zip and conflux, the two libraries that rely on WHATWG Streams and (in my case) async iterables, whereas it shows better (and extremely consistent) runtimes with fflate, which uses synchronous code with callbacks, in experiment 1. Zip.js and JSZip used to be faster in Chrome than Safari, but clearly things have changed. Experiments 2 and 3 are really taxing for Chrome.

In a different experiment using Deno to avoid storing very large output files, memory usage for any amount of data remained constant or close enough. My tests maxed out at 36.1 MB of RAM while processing nearly 6 GB.

Now, comparing bundle size is clearly unfair because the others do a bunch of things that my library doesn't. Here you go anyway (sizes are shown in decimal kilobytes):

client-zip@2.5.0 fflate@0.7.4 zip.js@2.7.14 conflux@4.0.3 JSZip@3.10.1
minified 6.4 kB 29.8 kB 163.2 kB 198.8 kB 94.9 kB
minified + gzipped 2.6 kB 11 kB 58 kB 56.6 kB 27.6 kB

The datasets I used in the new tests are not public domain, but nothing sensitive either ; I can send them if you ask.

Known Issues

Core symbols most depended-on inside this repo

flagNameUTF8
called by 12
src/zip.ts
normalizeMetadata
called by 10
src/metadata.ts
makeUint8Array
called by 9
src/utils.ts
crc32
called by 7
src/crc32.ts
makeBuffer
called by 7
src/utils.ts
clampInt32
called by 7
src/utils.ts
encodeString
called by 6
src/utils.ts
centralHeader
called by 5
src/zip.ts

Shape

Function 39

Languages

TypeScript100%

Modules by API surface

src/zip.ts9 symbols
src/index.ts8 symbols
src/utils.ts5 symbols
src/input.ts5 symbols
src/metadata.ts4 symbols
test/integration.test.ts3 symbols
test/zip.test.ts1 symbols
test/metadata.test.ts1 symbols
src/polyfills.ts1 symbols
src/datetime.ts1 symbols
src/crc32.ts1 symbols

Used by 1 indexed graphs manifest dependencies, hub-wide

For agents

$ claude mcp add client-zip \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact