Skip to content

Implement Builder API for creating tar archives #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,9 @@ Cargo.lock
x86_64-unknown-linux-gnu
*.wasm
/npm

# Test output files
__test__/*.tar
__test__/test-*
__test__/temp*
__test__/mixed-*
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

## Usage

### Reading Archives

```ts
export class Entries {
[Symbol.iterator](): Iterator<Entry, void, void>
Expand Down Expand Up @@ -35,6 +37,43 @@ export class Archive {
}
```

### Creating Archives

```ts
export class Builder {
/** Create a new builder which will write to the specified output. */
constructor(output?: string)
/** Append a file from disk to this archive. */
appendFile(name: string, src: string): void
/** Append a directory and all of its contents to this archive. */
appendDirAll(name: string, src: string): void
/** Append raw data to this archive with the specified name. */
appendData(name: string, data: Uint8Array): void
/** Finalize the archive and return the resulting data. */
finish(): Array<number> | null
}
```

### Creating Archives Example

```ts
import { Builder } from '@napi-rs/tar'

// Create archive in memory
const builder = new Builder()
builder.appendData('hello.txt', Buffer.from('Hello, world!'))
builder.appendFile('package.json', './package.json')
builder.appendDirAll('src', './src')

const archiveData = builder.finish() // Returns Uint8Array
// archiveData can be written to disk or used directly

// Create archive to file
const fileBuilder = new Builder('./output.tar')
fileBuilder.appendData('readme.txt', Buffer.from('Archive contents'))
fileBuilder.finish() // Returns null, data written to ./output.tar
```

## Extract Single File

You can extract a specific file from a tar archive without extracting the entire archive. This is useful for inspecting Docker OCI images or extracting specific configuration files:
Expand Down
Loading
Loading