Skip to content

Commit

Permalink
Rename Untar.deflate to Untar.extract; add some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
arcatdmz committed May 11, 2019
1 parent 0ec99bd commit 94e109b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
10 changes: 6 additions & 4 deletions archive/tar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ struct posix_header { // byte offset
};
*/

const structure: Array<{ field: string; length: number }> = [
const utarStructure: Array<{ field: string; length: number }> = [
{
field: "fileName",
length: 100
Expand Down Expand Up @@ -189,7 +189,7 @@ function formatHeader(data: TarData): Uint8Array {
const encoder = new TextEncoder(),
buffer = clean(512);
let offset = 0;
structure.forEach(function(value): void {
utarStructure.forEach(function(value): void {
const entry = encoder.encode(data[value.field] || "");
buffer.set(entry, offset);
offset += value.length; // space it out with nulls
Expand All @@ -204,7 +204,7 @@ function formatHeader(data: TarData): Uint8Array {
function parseHeader(buffer: Uint8Array): { [key: string]: Uint8Array } {
const data: { [key: string]: Uint8Array } = {};
let offset = 0;
structure.forEach(function(value): void {
utarStructure.forEach(function(value): void {
const arr = buffer.subarray(offset, offset + value.length);
data[value.field] = arr;
offset += value.length;
Expand Down Expand Up @@ -291,6 +291,7 @@ export class Tar {
async append(fileName: string, opts: TarOptions): Promise<void> {
opts = opts || {};

// set meta data
const info = opts.filePath && (await Deno.stat(opts.filePath));

let mode =
Expand Down Expand Up @@ -378,7 +379,7 @@ export class Untar {
this.block = new Uint8Array(recordSize);
}

async deflate(writer: Deno.Writer): Promise<UntarOptions> {
async extract(writer: Deno.Writer): Promise<UntarOptions> {
await this.reader.readFull(this.block);
const header = parseHeader(this.block);

Expand All @@ -392,6 +393,7 @@ export class Untar {
checksum += header[key].reduce((p, c): number => p + c, 0);
});
checksum += encoder.encode(" ").reduce((p, c): number => p + c, 0);

if (parseInt(decoder.decode(header.checksum), 8) !== checksum) {
throw new Error("checksum error");
}
Expand Down
2 changes: 1 addition & 1 deletion archive/tar_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ test(async function deflateTarArchive(): Promise<void> {
// read data from a tar archive
const untar = new Untar(tar.getReader());
const buf = new Deno.Buffer();
const result = await untar.deflate(buf);
const result = await untar.extract(buf);
const untarText = new TextDecoder("utf-8").decode(buf.bytes());

// tests
Expand Down

0 comments on commit 94e109b

Please sign in to comment.