tar - Deno:处理 tar 存档会导致校验和错误(标准库)
问题描述
我想tar.ts
在Standard Library的帮助下处理一个 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
,这似乎是 std 代码中的错误
推荐阅读
- linux - SELinux 策略中定义的 __default__ 用户到 unconfined_u 的映射在哪里?
- python - AttributeError:'NoneType'对象在python中的discord.py中没有属性'strip'错误
- dart - About not being able to do pub install in Dart
- ruby-on-rails - I don't know how to set the access_token
- freemarker - know last iteration of list FTL
- c# - 经过身份验证的用户在 web api owin 中间件中丢失
- r - Combine column values in an R dataframe all at once
- javascript - GTM - Creating a list of products in GTM - Wordpress
- computer-vision - Scratch Detection with Limited Samples
- javascript - Zooming by mouse of high chart using angular in chrome is creating empty boxes