Compress and Uncompress For Deno

Compress and Uncompress For Deno

compress and uncompress for Deno If you read and write files, need the following permissions

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

Deno v1.2.1+ required.(Please wait, the current deno version v1.2.1)

if you use deno <= v1.2.1, you can use [email protected]. But there is a bug: the added folders have a wrong type "file".

The reason can be seen here:

https://github.com/denoland/deno/pull/6905

defination
// deno > v1.2.1
import { tar } from "https://deno.land/x/[email protected]/mod.ts";
// deno <= v1.2.1
// import { tar } from "https://deno.land/x/[email protected]/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/[email protected]/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");
// uncompress
await tar.uncompress("./test.tar","./dest");

deflate

This is a pure TypeScript implementation of deflate.

import { 
  deflate, 
  inflate, 
  /** Compress data using deflate, and do not append a zlib header. */
  deflateRaw, 
  inflateRaw
 } from "https://deno.land/x/[email protected]/mod.ts";
const str = "hello world!";
const bytes = new TextEncoder().encode(str);
// with zlib header
const compressed = deflate(bytes);
const decompressed = inflate(compressed);
// no zlib header
const compressed = deflateRaw(bytes);
const decompressed = inflateRaw(compressed);

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

stream mode used to read and write large files

import { GzipStream } from "https://deno.land/x/[email protected]/mod.ts";
const gzip = new GzipStream();
gzip.on("progress", (progress: string) => {
  console.log(progress); // 0.00% => 100.00%
});
await gzip.compress("./big.mkv", "./big.mkv.gz");
await gzip.uncompress("./big.mkv.gz", "./big.mkv");

no stream mode loading all data into memory, so can't get progress event

import { gzipFile, gunzipFile } from "https://deno.land/x/[email protected]/mod.ts";
await gzipFile("./deno.txt", "./deno.txt.gz"); // stream
await gunzipFile("./deno.txt.gz", "./deno.txt");

gzip/gunzip string or bytes

This is a pure TypeScript implementation. Almost as fast as Rust implementation.

import { gzip, gunzip } from "https://deno.land/x/[email protected]/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/[email protected]/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>;
exmaple
import { tgz } from "https://deno.land/x/[email protected]/mod.ts";
// compress folder
await tgz.compress("./test","./test.tar.gz");
// compress folder, exclude src directory
await tgz.compress("./test","./test.tar.gz", { excludeSrc: true });
// compress file
await tgz.compress("./test.txt","./test.tar.gz");
// uncompress 
await tgz.uncompress("./test.tar.gz","./dest");

zip

defination
import { zip } from "https://deno.land/x/[email protected]/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

Download Details:

Author: eno-library

GitHub: https://github.com/deno-library/compress

deno nodejs javascript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Top 5 Reasons Javascript Developers Prefer Deno over Nodejs

The maker of NodeJS, Ryan Dahl, has released a new runtime that aims to solve many of the shortcomings of Node. Your initial reaction might be “Oh great, another Javascript framework? Just what I needed…”. Don’t worry, I had the same reaction. After understanding the advantages, I too saw why Deno is exactly what a back-end Javascript developer needs in 2020. Let’s take a look at the top 5 reasons javascript developers are having a much smoother and more modern experience using Deno vs. Node.

Deno - How to Compile & Run Javascript Code in Deno Runtime

Learn How to Compile & Run Javascript Code in Deno Runtime.

What is Deno? Deno vs. NodeJS

What is Deno? Deno vs Node. Deno is the upgraded version of NodeJS. Explore features of Deno. Will it take over Node.js or not? Deno is based on Google's Chromium V8 JavaScript engine and written in programming language Rust as opposed to C++ for Node

Deno ¿el verdadero Nodejs?

Deno ¿el verdadero Nodejs? - El 13 de mayo de 2020 se liberó la versión 1.0 de Deno, un nuevo entorno de ejecución para correr javascript y typescript, basado en el motor V8 de Google y ...

Introduction to Deno - The Next Level of NodeJS Runtime

Introduction to Deno - The Next Level of NodeJS Runtime. Deno is a secure runtime for JavaScript and TypeScript. It is developed by same person, Ryan Dahl who developed NodeJS runtime as well…