https://github.com/magnetardev/archive
A port of libarchive to WebAssembly, with a simple JS wrapper.
https://github.com/magnetardev/archive
7zip compression decompression javascript libarchive typescript webassembly zip
Last synced: about 1 year ago
JSON representation
A port of libarchive to WebAssembly, with a simple JS wrapper.
- Host: GitHub
- URL: https://github.com/magnetardev/archive
- Owner: magnetardev
- Created: 2021-06-19T22:27:57.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2022-08-16T04:53:16.000Z (over 3 years ago)
- Last Synced: 2024-10-24T18:33:05.353Z (over 1 year ago)
- Topics: 7zip, compression, decompression, javascript, libarchive, typescript, webassembly, zip
- Language: TypeScript
- Homepage:
- Size: 1.19 MB
- Stars: 7
- Watchers: 3
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Archive
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](https://github.com/GoogleChromeLabs/comlink) can get you up and running with one in minutes.
### Install
```shell
# if you use yarn:
yarn add @magnetardev/archive
# if you use npm:
npm install @magnetardev/archive
```
### Why?
Aren't there a few of these ports? Well, yes. In fact, this uses the exact same Dockerfile from [libarchivejs](https://github.com/nika-begiashvili/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.
## Examples
### Reading an archive
```js
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();
```
### Creating an archive
```js
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
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:
1. 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.
2. Run `yarn build`. This builds the JS wrapper.
3. Done.