Skip to main content
Deno 2 is finally here 🎉️
Learn more

compress

compress and uncompress for Deno

  • tar
  • deflate
  • gzip
  • tgz
  • zip

Useage

If you read and write files, need the following permissions

–allow-read –allow-write

tar

defination

import { tar } from "https://deno.land/x/compress@v0.1.2/mod.ts";
export interface compressInterface {
  excludeSrc?: boolean; // exclude src directory, default: include src directory
}
tar.compress(src, dest, options?: compressInterface): Promise<void>;
tar.uncompress(src, dest): Promise<void>;

exmaple

import { tar } from "https://deno.land/x/compress@v0.1.2/mod.ts";
// compress folder
await tar.compress("./test","./test.tar");
// compress folder, exclude src directory
await tar.compress("./test","./test.tar", { excludeSrc: true });
// compress file
await tar.compress("./test.txt","./test.tar");
await tar.uncompress("./test.tar","./test");

deflate

This is a pure TypeScript implementation of deflate.

import { deflate, inflate } from "https://deno.land/x/compress@v0.1.2/mod.ts";
const str = "hello world!";
const bytes = new TextEncoder().encode(str);
const compressed = deflate(bytes);
const decompressed = inflate(compressed);
assert(str === new TextDecoder().decode(decompressed));

gzip

defination

interface GzipOptions {
  level: number;
  timestamp?: number;
  name?: string;
}
gzip(bytes: Uint8Array, options?:GzipOptions): Uint8Array;
gunzip(bytes: Uint8Array): Uint8Array;
gzipFile(src: string, dest: string): Promise<void>;
gunzipFile(src: string, dest: string): Promise<void>;
class GzipStream {
  compress(src: string, dest: string): Promise<void>;
  uncompress(src: string, dest: string): Promise<void>;
  on(event: "progress", listener: (percent: string) => void): this;
}

exmaple
compress and uncompress file, only supports compressing and decompressing a single file

no stream, loading all data into memory

// need --allow-net for use https://github.com/hazae41/denoflate
// not export in mod.ts and not in deps.ts for it requires permission --allow-net  
// so you must import from this file
import { gzipFile, gunzipFile } from "https://deno.land/x/compress@v0.1.2/gzip/gzip_file.ts";
await gzipFile("./deno.txt", "./deno.txt.gz"); // stream
await gunzipFile("./deno.txt.gz", "./deno.txt");

stream, only gzip.compress is available

import { GzipStream } from "https://deno.land/x/compress@v0.1.2/mod.ts";
const gzip = new GzipStream();
gzip.on("progress", (progress: string) => {
  console.log(progress); // 0.00% => 100.00%
});
await gzip.compress("./big.exe", "./big.exe.gz");
// Having problems with streaming decompression, don't use.
await gzip.uncompress("./deno.txt.gz", "./deno.txt");

gzip or gunzip
This is a pure TypeScript implementation.

If you want to run fast, you may need https://github.com/hazae41/denoflate

import { gzip, gunzip } from "https://deno.land/x/compress@v0.1.2/mod.ts";
// gzip
const bytes = new TextEncoder().encode("hello");
const compressed = gzip(bytes);
// gunzip
const decompressed = gunzip(compressed);

tgz

defination

import { tgz } from "https://deno.land/x/compress@v0.1.2/mod.ts";
interface compressInterface {
  excludeSrc?: boolean; // exclude src directory, default: include src directory
}
tgz.compress(src, dest, options?: compressInterface): Promise<void>;
tgz.uncompress(src, dest): Promise<void>;

zip

defination

import { zip } from "https://deno.land/x/compress@v0.1.2/mod.ts";
interface compressInterface {
  excludeSrc?: boolean; // exclude src directory, default: include src directory
}
zip.compress(src, dest, options?: compressInterface): Promise<void>;
zip.uncompress(src, dest): Promise<void>;

test

deno test --allow-read --allow-write