|
8 | 8 |
|
9 | 9 | ## Usage
|
10 | 10 |
|
| 11 | +### Reading Archives |
| 12 | + |
11 | 13 | ```ts
|
12 | 14 | export class Entries {
|
13 | 15 | [Symbol.iterator](): Iterator<Entry, void, void>
|
@@ -35,6 +37,43 @@ export class Archive {
|
35 | 37 | }
|
36 | 38 | ```
|
37 | 39 |
|
| 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 | + |
38 | 77 | ## Extract Single File
|
39 | 78 |
|
40 | 79 | 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