2020-06-10 17:10:47 -04:00
|
|
|
# Usage
|
|
|
|
|
|
|
|
## Tar
|
|
|
|
|
|
|
|
```ts
|
2020-10-04 08:18:36 -04:00
|
|
|
import { Tar } from "https://deno.land/std@$STD_VERSION/archive/tar.ts";
|
2020-06-10 17:10:47 -04:00
|
|
|
|
|
|
|
const tar = new Tar();
|
|
|
|
const content = new TextEncoder().encode("Deno.land");
|
|
|
|
await tar.append("deno.txt", {
|
|
|
|
reader: new Deno.Buffer(content),
|
|
|
|
contentSize: content.byteLength,
|
|
|
|
});
|
|
|
|
|
2020-10-03 16:19:11 -04:00
|
|
|
// Or specifying a filePath.
|
2020-06-10 17:10:47 -04:00
|
|
|
await tar.append("land.txt", {
|
|
|
|
filePath: "./land.txt",
|
|
|
|
});
|
|
|
|
|
2020-10-03 16:19:11 -04:00
|
|
|
// use tar.getReader() to read the contents.
|
2020-06-10 17:10:47 -04:00
|
|
|
|
|
|
|
const writer = await Deno.open("./out.tar", { write: true, create: true });
|
|
|
|
await Deno.copy(tar.getReader(), writer);
|
|
|
|
writer.close();
|
|
|
|
```
|
|
|
|
|
|
|
|
## Untar
|
|
|
|
|
|
|
|
```ts
|
2020-10-04 08:18:36 -04:00
|
|
|
import { Untar } from "https://deno.land/std@$STD_VERSION/archive/tar.ts";
|
|
|
|
import { ensureFile } from "https://deno.land/std@$STD_VERSION/fs/ensure_file.ts";
|
|
|
|
import { ensureDir } from "https://deno.land/std@$STD_VERSION/fs/ensure_dir.ts";
|
2020-06-10 17:10:47 -04:00
|
|
|
|
|
|
|
const reader = await Deno.open("./out.tar", { read: true });
|
|
|
|
const untar = new Untar(reader);
|
|
|
|
|
|
|
|
for await (const entry of untar) {
|
|
|
|
console.log(entry); // metadata
|
|
|
|
/*
|
|
|
|
fileName: "archive/deno.txt",
|
|
|
|
fileMode: 33204,
|
|
|
|
mtime: 1591657305,
|
|
|
|
uid: 0,
|
|
|
|
gid: 0,
|
|
|
|
size: 24400,
|
|
|
|
type: 'file'
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (entry.type === "directory") {
|
|
|
|
await ensureDir(entry.fileName);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
await ensureFile(entry.fileName);
|
|
|
|
const file = await Deno.open(entry.fileName, { write: true });
|
2020-10-03 16:19:11 -04:00
|
|
|
// <entry> is a reader.
|
2020-06-10 17:10:47 -04:00
|
|
|
await Deno.copy(entry, file);
|
|
|
|
}
|
|
|
|
reader.close();
|
|
|
|
```
|