Deno: Processing tar archive results in checksum error (Standard Library)

寵の児 提交于 2020-06-28 05:01:34

问题


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

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);

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  

The code:

// 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());

Where did I miss a step?


回答1:


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 is currently a zero-filled Uint8Array which appears to be a bug in the std code



来源:https://stackoverflow.com/questions/62262954/deno-processing-tar-archive-results-in-checksum-error-standard-library

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!