A lightweight library for extracting archive files (zip, 7z, rar) in the browser and Node.js, powered by libarchive compiled to WebAssembly.
- Cross-platform - Works in both Browser and Node.js environments
- Multiple formats - Supports zip, 7z, rar and more archive formats
- Web Worker support - Offload extraction to a worker thread for better performance
- TypeScript - Full TypeScript support with type definitions included
- Lightweight - Minimal dependencies, powered by WebAssembly
# npm
npm install filing
# yarn
yarn add filing
# pnpm
pnpm add filingUsing Web Worker prevents blocking the main thread during extraction:
import { FilingBrowserWorker } from 'filing'
const filing = new FilingBrowserWorker({
wasmUrl: 'https://unpkg.com/filing/dist/esm/wasm/archive.wasm'
})
// Extract from File or Blob
const fileInput = document.getElementById('file')
fileInput.addEventListener('change', async (e) => {
const [file] = e.target.files
const files = await filing.extract(file)
files.forEach((fileData) => {
console.log(fileData.pathname) // File path in archive
console.log(fileData.file) // Extracted File object
})
})For simple use cases or when Web Worker is not available:
import { FilingBrowser } from 'filing'
const filing = new FilingBrowser({
wasmUrl: 'https://unpkg.com/filing/dist/esm/wasm/archive.wasm'
})
const files = await filing.extract(file)
console.log('Extracted files:', files)import { FilingNodeJS } from 'filing'
import fs from 'fs'
const filing = new FilingNodeJS()
// Extract from Buffer
const buffer = fs.readFileSync('./archive.zip')
const files = await filing.extract(buffer)
files.forEach((fileData) => {
console.log(fileData.pathname) // File path in archive
console.log(fileData.buffer) // Extracted Buffer
})
// Or extract from file path directly
const files = await filing.extract('./archive.7z')You can also use Filing directly in the browser via CDN:
<script src="https://unpkg.com/filing/dist/umd/filing.min.js"></script>
<script>
const { FilingBrowser } = window.filing
const filing = new FilingBrowser({
wasmUrl: 'https://unpkg.com/filing/dist/esm/wasm/archive.wasm'
})
// Use filing.extract() as shown above
</script>| Option | Type | Required | Description |
|---|---|---|---|
wasmUrl |
string |
Yes* | URL to the archive.wasm file |
wasmBinary |
ArrayBuffer |
Yes* | Pre-loaded WASM binary |
onInitialized |
(instance) => void |
No | Callback when WASM is loaded |
onInitialFailed |
(error) => void |
No | Callback when loading fails |
*Either wasmUrl or wasmBinary is required.
FilingNodeJS automatically loads the WASM file, no options required.
Extracts files from an archive.
Parameters:
| Parameter | Type | Description |
|---|---|---|
file |
File | Blob (Browser) / Buffer | string (Node.js) |
Archive file to extract |
options.locale |
string |
Locale for filename encoding (optional) |
Returns: Promise<BrowserFileData[]> or Promise<NodeJSFileData[]>
// Browser file data
interface BrowserFileData {
filename: string // File name
pathname: string // Full path in archive
data: Int8Array // Raw file data
type: string // MIME type
file: File // Extracted File object
children?: BrowserFileData[]
}
// Node.js file data
interface NodeJSFileData {
filename: string // File name
pathname: string // Full path in archive
data: Int8Array // Raw file data
type: string // MIME type
buffer: Buffer // Extracted Buffer
children?: NodeJSFileData[]
}Filing uses libarchive under the hood, supporting a wide range of archive formats:
- ZIP - .zip
- 7-Zip - .7z
- RAR - .rar
- TAR - .tar, .tar.gz, .tar.bz2, .tar.xz
- And more...
Check out the examples directory for complete working examples:
- Browser Example - Using Web Worker in browser
- Browser CDN Example - Using CDN in browser
- Node.js Example - Using in Node.js
MIT © lianghang