A port of libarchive to WebAssembly, with a simple JS wrapper.
It is highly recommended that you offload reading and writing to a Worker. For the browser or deno, that'd be through Worker
and for node that would be through the worker_threads
module. There isn't an official worker wrapper yet, but using a library like Comlink can get you up and running with one in minutes.
# if you use yarn:
yarn add @magnetardev/archive
# if you use npm:
npm install @magnetardev/archive
Aren't there a few of these ports? Well, yes. In fact, this uses the exact same Dockerfile from libarchivejs. However, libarchivejs does not support Node and forces you to use a Worker. I also haven't found one that supports creating archives. Finally, the APIs most of the ports offer are not as simple as they can be, which is a shame. So, I made this.
import * as archive from '@magnetardev/archive';
const buffer = ...; // Obtain a Uint8Array version of your archive.
const reader = await archive.createReader(buffer);
for (const entry of reader) {
console.log(entry.path, entry.extract());
}
reader.close();
import * as archive from "@magnetardev/archive";
// Create a writer.
const writer = await archive.createWriter();
// Add a file
const contents = new TextEncoder().encode("hello, world!");
writer.add("hello.txt", contents);
// Finalize the archive and get a Uint8Array version.
const zip = writer.close();
console.log(zip);
Building is kind of a mess, but is quite easy with Docker. If you choose to do so without Docker, you need to compile OpenSSL, LZMA, and libarchive with emcc
. This short guide covers the Docker build process:
- Run
yarn build:wasm
. This can take quite a while the first time, but consecutive builds will be much faster. This builds just the WebAssembly port and the Emscripten wrapper. - Run
yarn build
. This builds the JS wrapper. - Done.