Giter Site home page Giter Site logo

compress's Introduction

compress

Utilities to compress and uncompress for Deno!

  • tar
  • deflate
  • gzip
  • tgz
  • zip

Usage

If you want to read and write files, you need the following permissions:

--allow-read --allow-write

tar

For tar (un)compression, Deno v1.2.2+ is required. The reason can be seen here:

denoland/deno#6905

Definition

import { tar } from "https://deno.land/x/[email protected]/mod.ts";
// or only import tar
// import { tar } from "https://deno.land/x/[email protected]/tar/mod.ts";
export interface compressInterface {
  excludeSrc?: boolean;      // does not contain the src directory
  debug?: boolean;           // list the files and folders
}
export interface uncompressInterface {
  debug?: boolean;           // list the files and folders
}
tar.compress(src, dest, options?: compressInterface): Promise<void>;
tar.uncompress(src, dest, options?: uncompressInterface): Promise<void>;

Example

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 library contains a pure TypeScript implementation of deflate, and you can use deflate on its own:

import {
  deflate,
  /** Compress data using deflate, and do not append a zlib header. */
  deflateRaw,
  inflate,
  inflateRaw,
} from "https://deno.land/x/[email protected]/mod.ts";
// or only import deflate, inflate, deflateRaw, inflateRaw
// import { deflate, inflate, deflateRaw, inflateRaw } from "https://deno.land/x/[email protected]/zlib/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

Definition

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;
}

Example

Let's compress and uncompress a file. (gzip only supports compressing and decompressing a single file.)

stream mode
Useful for reading and writing large files.

import { GzipStream } from "https://deno.land/x/[email protected]/mod.ts";
// or only import GzipStream
// import { GzipStream } from "https://deno.land/x/[email protected]/gzip/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
(This is loading all data into memory, so we can't get a progress event.)

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

gzip a string or a byte array

This is a pure TypeScript implementation, almost as fast as a Rust implementation.

import { gunzip, gzip } from "https://deno.land/x/[email protected]/mod.ts";
// or only import gzip, gunzip
// import { gzip, gunzip } from "https://deno.land/x/[email protected]/zlib/mod.ts";
// gzip
const bytes = new TextEncoder().encode("hello");
const compressed = gzip(bytes);
// gunzip
const decompressed = gunzip(compressed);

tgz

Definition

import { tgz } from "https://deno.land/x/[email protected]/mod.ts";
// or only import tgz
// import { tgz } from "https://deno.land/x/[email protected]/tgz/mod.ts";
export interface compressInterface {
  excludeSrc?: boolean;      // does not contain the src directory
  debug?: boolean;           // list the files and folders
}
export interface uncompressInterface {
  debug?: boolean;           // list the files and folders
}
tgz.compress(src, dest, options?: compressInterface): Promise<void>;
tgz.uncompress(src, dest, options?: uncompressInterface): Promise<void>;

Example

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

Not yet implemented

Definition

import { zip } from "https://deno.land/x/[email protected]/mod.ts";
export interface compressInterface {
  excludeSrc?: boolean;      // does not contain the src directory
  debug?: boolean;           // list the files and folders
}
export interface uncompressInterface {
  debug?: boolean;           // list the files and folders
}
zip.compress(src, dest, options?: compressInterface): Promise<void>;
zip.uncompress(src, dest, options?: uncompressInterface): Promise<void>;

test

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

compress's People

Contributors

anagorsky avatar bidek56 avatar fonsp avatar fuxingzhang avatar jakobhellermann avatar jsejcksn avatar mmmartt avatar squarepear avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

compress's Issues

NotFound: No such file or directory

The tar.uncompress() function does not work because it attempts to write to a file that doesn't exist. I'll create a quick pull request once I find and fix the issue!

Check file:///Users/username/Downloads/test/mod.ts
error: Uncaught (in promise) NotFound: No such file or directory (os error 2)
    throw new ErrorClass(res.err.message);
          ^
    at processResponse (deno:core/core.js:213:11)
    at Object.jsonOpAsync (deno:core/core.js:231:12)
    at async Object.open (deno:runtime/js/30_files.js:45:17)
    at async Module.uncompress (https://deno.land/x/[email protected]/tar/mod.ts:13:18)

Large number of files problem duping compression

compress v0.3.9
deno v1.13.2

Thank you for your effort, very useful utility.

When I try to tar.compress or tgz.compress on a directory including several subdirs and 23289 files I got the following error:
Uncaught RangeError: Maximum call stack size exceeded
at Tar.getReader (https://deno.land/[email protected]/archive/tar.ts:450:12)
at Module.compress (https://deno.land/x/[email protected]/tar/mod.ts:79:23)

I did not change any V8 options. Probably recursion is not suitable in this case.

Add a verbose flag to the compressInterface

For debug purposes I'd like to list the files as they are progressed in compression and decompression by setting a verbose flag. It would be helpful if this lib would provide such.

Uncaught (in promise) NotFound: No such file or directory (os error 2) on tgz.compress

I have a Laravel project folder called MY-PROJECT that contains several files with deep folder structure:

+ MY-PROJECT
| + app
| |- ...
| + bootstrap
| |- ...
| + database
| | + factories
| | |- UserFactory.php
| | + migrations
| | |- ...
| |- .gitignore
| + public
| |- ...
| + resources
| |- ...
| + routes
| |- ...
| + scripts
| |- ...
| + storage
| |- ...
| + tests
| |- ...
|- .editorconfig
|- .env
|- .env.example
|- .gitignore
|- .rnd
|- artisan
|- composer.json
|- composer.lock
|- my.cnf
|- my.cnf.example
|- phpunit.xml
|- README.md
|- server.php
|- webpack.mix.js

I try to compress it like this:

await tgz.compress('/Users/user/MY-PROJECT', '/Users/user/MY-PROJECT.tar.gz', {
	excludeSrc: true // doesn't matter if true or false
})

and it fails:

error: Uncaught (in promise) NotFound: No such file or directory (os error 2), stat '/Users/user/MY-PROJECT/database/my.cnf.example'
        const stat = await Deno.stat(filePath);
                     ^
    at async Object.stat (deno:runtime/js/30_fs.js:228:17)
    at async appendFolder (https://deno.land/x/[email protected]/tar/mod.ts:41:22)
    at async Module.compress (https://deno.land/x/[email protected]/tar/mod.ts:70:7)
    at async Module.compress (https://deno.land/x/[email protected]/tgz/mod.ts:27:3)

The recursion seems not to work right as of now.

$% deno --version
deno 1.19.3 (release, x86_64-apple-darwin)
v8 9.9.115.8
typescript 4.5.2

compress v0.4.1

Is there a way to pipe into it?

I'm wondering if there is a way for me to send it pipe into it. By sending it individual strings and it compresses it all up. Instead of having to write it to a file then use the package to compress that file.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.