Deno:处理 tar 存档会导致校验和错误(标准库) [英] Deno: Processing tar archive results in checksum error (Standard Library)

查看:47
本文介绍了Deno:处理 tar 存档会导致校验和错误(标准库)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 标准库.

I would like to process a tar archive with help of tar.ts from Standard Library.

可以通过以下代码将存档成功写入test.tar:

The archive can be written successfully to test.tar by following code:

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 会触发错误:

However, reading the tar triggers an error:

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 内容.

You have to use tar.getReader() to get the correct tar content.

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 这似乎是标准代码中的一个错误

tar.out is currently a zero-filled Uint8Array which appears to be a bug in the std code

这篇关于Deno:处理 tar 存档会导致校验和错误(标准库)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆