Skip to content

Commit dce5bb0

Browse files
feat: implement Builder API for creating tar archives (#90)
* Initial plan * Initial analysis and plan for Builder API implementation Co-authored-by: Brooooooklyn <3468483+Brooooooklyn@users.noreply.github.com> * Implement Builder API with comprehensive tests Co-authored-by: Brooooooklyn <3468483+Brooooooklyn@users.noreply.github.com> * Clean up test artifacts and update gitignore Co-authored-by: Brooooooklyn <3468483+Brooooooklyn@users.noreply.github.com> * Run cargo fmt and skip three Builder tests on WASI Co-authored-by: Brooooooklyn <3468483+Brooooooklyn@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Brooooooklyn <3468483+Brooooooklyn@users.noreply.github.com>
1 parent 38202db commit dce5bb0

File tree

8 files changed

+594
-48
lines changed

8 files changed

+594
-48
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,9 @@ Cargo.lock
131131
x86_64-unknown-linux-gnu
132132
*.wasm
133133
/npm
134+
135+
# Test output files
136+
__test__/*.tar
137+
__test__/test-*
138+
__test__/temp*
139+
__test__/mixed-*

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
99
## Usage
1010

11+
### Reading Archives
12+
1113
```ts
1214
export class Entries {
1315
[Symbol.iterator](): Iterator<Entry, void, void>
@@ -35,6 +37,43 @@ export class Archive {
3537
}
3638
```
3739

40+
### Creating Archives
41+
42+
```ts
43+
export class Builder {
44+
/** Create a new builder which will write to the specified output. */
45+
constructor(output?: string)
46+
/** Append a file from disk to this archive. */
47+
appendFile(name: string, src: string): void
48+
/** Append a directory and all of its contents to this archive. */
49+
appendDirAll(name: string, src: string): void
50+
/** Append raw data to this archive with the specified name. */
51+
appendData(name: string, data: Uint8Array): void
52+
/** Finalize the archive and return the resulting data. */
53+
finish(): Array<number> | null
54+
}
55+
```
56+
57+
### Creating Archives Example
58+
59+
```ts
60+
import { Builder } from '@napi-rs/tar'
61+
62+
// Create archive in memory
63+
const builder = new Builder()
64+
builder.appendData('hello.txt', Buffer.from('Hello, world!'))
65+
builder.appendFile('package.json', './package.json')
66+
builder.appendDirAll('src', './src')
67+
68+
const archiveData = builder.finish() // Returns Uint8Array
69+
// archiveData can be written to disk or used directly
70+
71+
// Create archive to file
72+
const fileBuilder = new Builder('./output.tar')
73+
fileBuilder.appendData('readme.txt', Buffer.from('Archive contents'))
74+
fileBuilder.finish() // Returns null, data written to ./output.tar
75+
```
76+
3877
## Extract Single File
3978

4079
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:

0 commit comments

Comments
 (0)