-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use Deno API for gzip file handling
- Loading branch information
zhangfuxing
committed
Nov 5, 2024
1 parent
7c08d6e
commit 9e85873
Showing
1 changed file
with
52 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |