项目作者: nika-begiashvili

项目描述 :
Archive library for browsers
高级语言: JavaScript
项目地址: git://github.com/nika-begiashvili/libarchivejs.git
创建时间: 2018-06-14T09:21:07Z
项目社区:https://github.com/nika-begiashvili/libarchivejs

开源协议:MIT License

下载


Libarchivejs



npm version


license

Overview

Libarchivejs is a archive tool for browser and nodejs which can extract and create various types of compression, it’s a port of libarchive to WebAssembly and javascript wrapper to make it easier to use. Since it runs on WebAssembly performance should be near native. Supported formats: ZIP, 7-Zip, RAR v4, RAR v5, TAR .etc, Supported compression: GZIP, DEFLATE, BZIP2, LZMA .etc

Version 2.0 highlights!

  • Create archives
  • Use it in NodeJS

How to use

Install with npm i libarchive.js and use it as a ES module.

The library consists of two parts: ES module and webworker bundle, ES module part is your interface to talk to library, use it like any other module. The webworker bundle lives in the libarchive.js/dist folder so you need to make sure that it is available in your public folder since it will not get bundled if you’re using bundler (it’s all bundled up already) and specify correct path to Archive.init() method

if libarchive.js file is in the same directory as bundle file than you don’t need to call Archive.init() at all

  1. import {Archive} from 'libarchive.js/main.js';
  2. Archive.init({
  3. workerUrl: 'libarchive.js/dist/worker-bundle.js'
  4. });
  5. document.getElementById('file').addEventListener('change', async (e) => {
  6. const file = e.currentTarget.files[0];
  7. const archive = await Archive.open(file);
  8. let obj = await archive.extractFiles();
  9. console.log(obj);
  10. });
  11. // outputs
  12. {
  13. ".gitignore": {File},
  14. "addon": {
  15. "addon.py": {File},
  16. "addon.xml": {File}
  17. },
  18. "README.md": {File}
  19. }

More options

To get file listing without actually decompressing archive, use one of these methods

  1. await archive.getFilesObject();
  2. // outputs
  3. {
  4. ".gitignore": {CompressedFile},
  5. "addon": {
  6. "addon.py": {CompressedFile},
  7. "addon.xml": {CompressedFile}
  8. },
  9. "README.md": {CompressedFile}
  10. }
  11. await archive.getFilesArray();
  12. // outputs
  13. [
  14. {file: {CompressedFile}, path: ""},
  15. {file: {CompressedFile}, path: "addon/"},
  16. {file: {CompressedFile}, path: "addon/"},
  17. {file: {CompressedFile}, path: ""}
  18. ]

If these methods get called after archive.extractFiles(); they will contain actual files as well.

Decompression might take a while for larger files. To track each file as it gets extracted, archive.extractFiles accepts callback

  1. archive.extractFiles((entry) => { // { file: {File}, path: {String} }
  2. console.log(entry);
  3. });

Extract single file from archive

To extract a single file from the archive you can use the extract() method on the returned CompressedFile.

  1. const filesObj = await archive.getFilesObject();
  2. const file = await filesObj['.gitignore'].extract();

Check for encrypted data

  1. const archive = await Archive.open(file);
  2. await archive.hasEncryptedData();
  3. // true - yes
  4. // false - no
  5. // null - can not be determined

Extract encrypted archive

  1. const archive = await Archive.open(file);
  2. await archive.usePassword("password");
  3. let obj = await archive.extractFiles();

Create new archive

Note: pathname is optional in browser but required in NodeJS

  1. const archiveFile = await Archive.write({
  2. files: [
  3. { file: file, pathname: 'folder/file.zip' }
  4. ],
  5. outputFileName: "test.tar.gz",
  6. compression: ArchiveCompression.GZIP,
  7. format: ArchiveFormat.USTAR,
  8. passphrase: null,
  9. });

Use it in NodeJS

  1. import { Archive, ArchiveCompression, ArchiveFormat } from "libarchivejs/dist/libarchive-node.mjs";
  2. let buffer = fs.readFileSync("test/files/archives/README.md");
  3. let blob = new Blob([buffer]);
  4. const archiveFile = await Archive.write({
  5. files: [{
  6. file: blob,
  7. pathname: "README.md",
  8. }],
  9. outputFileName: "test.tar.gz",
  10. compression: ArchiveCompression.GZIP,
  11. format: ArchiveFormat.USTAR,
  12. passphrase: null,
  13. });

How it works

Libarchivejs is a port of the popular libarchive C library to WASM. Since WASM runs in the current thread, the library uses WebWorkers for heavy lifting. The ES Module (Archive class) is just a client for WebWorker. It’s tiny and doesn’t take up much space.

Only when you actually open archive file will the web worker be spawned and WASM module will be downloaded. Each Archive.open call corresponds to each WebWorker.

After calling an extractFiles worker, it will be terminated to free up memory. The client will still work with cached data.