Skip to content

Commit

Permalink
refactor: use Deno API for gzip file handling
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangfuxing committed Nov 5, 2024
1 parent 7c08d6e commit 9e85873
Showing 1 changed file with 52 additions and 22 deletions.
74 changes: 52 additions & 22 deletions gzip/gzip_file.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,61 @@
// import { readAll, writeAll } from "../deps.ts";
/** very fast */
import { gunzip, gzip } from "../zlib/mod.ts";
// import { gunzip, gzip } from "../zlib/mod.ts";
/** slow */
// import { gzip, gunzip } from "./gzip.ts";
import { readAll, writeAll } from "../deps.ts";

/**
* Compress a file
* @param src Source file path
* @param dest Destination file path
*/
export async function gzipFile(src: string, dest: string): Promise<void> {
const reader = await Deno.open(src, {
read: true,
});
const writer = await Deno.open(dest, {
write: true,
create: true,
truncate: true,
});
await writeAll(writer, gzip(await readAll(reader), undefined));
writer.close();
reader.close();
// v0.0.1 - v0.4.9
// const reader = await Deno.open(src, {
// read: true,
// });
// const writer = await Deno.open(dest, {
// write: true,
// create: true,
// truncate: true,
// });
// await writeAll(writer, gzip(await readAll(reader), undefined));
// writer.close();
// reader.close();

// >= v0.5.0
const input = await Deno.open(src);
const output = await Deno.create(dest);

await input.readable
.pipeThrough(new DecompressionStream("gzip"))
.pipeTo(output.writable);
}

/**
* Decompress a file
* @param src Source file path
* @param dest Destination file path
*/
export async function gunzipFile(src: string, dest: string): Promise<void> {
const reader = await Deno.open(src, {
read: true,
});
const writer = await Deno.open(dest, {
write: true,
create: true,
truncate: true,
});
await writeAll(writer, gunzip(await readAll(reader)));
// v0.0.1 - v0.4.9
// const reader = await Deno.open(src, {
// read: true,
// });
// const writer = await Deno.open(dest, {
// write: true,
// create: true,
// truncate: true,
// });
// await writeAll(writer, gunzip(await readAll(reader)));

// >= v0.5.0
const input = await Deno.open(src);
const output = await Deno.create(dest);

await input.readable
.pipeThrough(new CompressionStream("gzip"))
.pipeTo(output.writable);
}

await gzipFile("./a.txt","an.txt.gz")

0 comments on commit 9e85873

Please sign in to comment.