Deno:处理 tar 存档导致校验和错误(标准库)
Deno: Processing tar archive results in checksum error (Standard Library)
我想在 Standard Library 的 tar.ts
的帮助下处理 tar 存档。
可以通过以下代码将存档成功写入test.tar
:
import { Tar, Untar } from "https://deno.land/std/archive/tar.ts";
// create tar archive
const tar = new Tar();
const content = new TextEncoder().encode("hello tar world!");
await tar.append("output.txt", {
reader: new Deno.Buffer(content),
contentSize: content.byteLength,
});
await Deno.writeFile("./test.tar", tar.out);
但是,读取 tar 会触发错误:
error: Uncaught Error: checksum error
throw new Error("checksum error");
--------^
at Untar.extract (https://deno.land/std/archive/tar.ts:432:13)
at async file:///C:/Users/bela/Desktop/script/test.ts:23:16
代码:
// read from tar archive
const untar = new Untar(await Deno.open("./test.tar"));
const buf = new Deno.Buffer();
const result = await untar.extract(buf); // <-- this line triggers error
const untarText = new TextDecoder("utf-8").decode(buf.bytes());
我哪里漏了一步?
您必须使用 tar.getReader()
才能获得正确的 tar
内容。
const tar = new Tar();
const content = new TextEncoder().encode("hello tar world!");
await tar.append("output.txt", {
reader: new Deno.Buffer(content),
contentSize: content.byteLength,
});
const writer = await Deno.open("./test.tar", { write: true, create: true });
await Deno.copy(tar.getReader(), writer);
const untar = new Untar(await Deno.open("./test.tar", { read: true }));
const buf = new Deno.Buffer();
const result = await untar.extract(buf); // <-- this line triggers error
const untarText = new TextDecoder("utf-8").decode(buf.bytes());
console.log(untarText);
tar.out
目前是一个零填充 Uint8Array
,这似乎是标准代码中的错误
我想在 Standard Library 的 tar.ts
的帮助下处理 tar 存档。
可以通过以下代码将存档成功写入test.tar
:
import { Tar, Untar } from "https://deno.land/std/archive/tar.ts";
// create tar archive
const tar = new Tar();
const content = new TextEncoder().encode("hello tar world!");
await tar.append("output.txt", {
reader: new Deno.Buffer(content),
contentSize: content.byteLength,
});
await Deno.writeFile("./test.tar", tar.out);
但是,读取 tar 会触发错误:
error: Uncaught Error: checksum error
throw new Error("checksum error");
--------^
at Untar.extract (https://deno.land/std/archive/tar.ts:432:13)
at async file:///C:/Users/bela/Desktop/script/test.ts:23:16
代码:
// read from tar archive
const untar = new Untar(await Deno.open("./test.tar"));
const buf = new Deno.Buffer();
const result = await untar.extract(buf); // <-- this line triggers error
const untarText = new TextDecoder("utf-8").decode(buf.bytes());
我哪里漏了一步?
您必须使用 tar.getReader()
才能获得正确的 tar
内容。
const tar = new Tar();
const content = new TextEncoder().encode("hello tar world!");
await tar.append("output.txt", {
reader: new Deno.Buffer(content),
contentSize: content.byteLength,
});
const writer = await Deno.open("./test.tar", { write: true, create: true });
await Deno.copy(tar.getReader(), writer);
const untar = new Untar(await Deno.open("./test.tar", { read: true }));
const buf = new Deno.Buffer();
const result = await untar.extract(buf); // <-- this line triggers error
const untarText = new TextDecoder("utf-8").decode(buf.bytes());
console.log(untarText);
tar.out
目前是一个零填充 Uint8Array
,这似乎是标准代码中的错误