Git Product home page Git Product logo

zlibt's Introduction

zlibt2

  • zlib js full convert to typescript code for typescript, javscript(pure website), nodejs
  • zlib.js is ZLIB(RFC1950), DEFLATE(RFC1951), GZIP(RFC1952) and PKZIP implementation in JavaScript.

original zlibjs code

install

npm install zlibt2

support features:

  • Raw Deflate

  • Raw Inflate

  • Deflate

  • Inflate

  • GZIP

  • GUNZIP

  • PKZIP(zip)

  • PKUNZIP(unzip)

    • file attribute get operator support
  • UMD support(pure web support, tested on chrome with pure html and javascript)

    • ./node_modules/zlibt2/zlibt.umd.js
  • Node js support

  • Typescript support(tested on angular 4 and chrome)

  • No dependency(pure typescript)

  • partial module support

    • zip/unzip(pkzip) only module provide(/zlibt2/zip)
    • raw deflate/inflate only module provide(/zlibt2/raw)
    • deflate/inflate only module provide(/zlibt2/flate)
    • gzip/gunzip only module provide(/zlibt2/gzip)
    • support umd files for website
      • zip.umd.js, raw.umd.js, flate.umd.js, gzip.umd.js
  • if you do not need full supporting zlib, you can choose only one module. it help you make more small code when you use bundling solution.

    **full zlib bundled**
    - import zlibt2 module, webpack bundle full zlib2 module
    ```ts
    import { RawDeflate, RawInflate, Deflate, Inflate, Gzip, Gunzip, Zip, Unzip } from 'zlibt2';
    ```
    
    **only raw deflate/inflate bundled**
    ```ts
    import { RawDeflate, RawInflate } from 'zlibt2/raw';
    ```
    

    dependency:

  • there is no dependencies

build

  • Production mode build

    • all mode buid: bundle module and umd bundle module
      npm run build:prod:
      
    • all and patial mode buid: bundle module and umd bundle module, this build option build each modules too. output are 1 all module bundled package and each module bundle package
      npm run build:prod:all
      
    • partial module build:
      build:[module name]:prod
      /*module name can be 'gzip','zip','raw','flate'*/
      
  • Development mode build(developing mode with file watch)

      ```
      build:dev
      ```
    
  • package.json configration to deply

    • to deploy with package.json, you should change content in /npms/zlibt/package.json
    • /package.json file is not package config for deploy to npm package site

deploy script

  • to deply source, you have to run pre-deploy script, this script aggregate source code that is in npm packages

        npm run predeploy:prod
    
  • this script run './deploy/zlibt.js'

  • Production mode build

    • production build support, just change dev->prod

ZLib APIS

  • Zlib TypeScript

    rawdeflate, rawinflate

    import { RawDeflate, RawInflate } from "zlibt2";
    
    //compress
    const datas = [1, 2, 3, 4, 5, 6];
    const rawDeflate = new RawDeflate(datas);
    const rawCompress = rawDeflate.compress();
    
    //decompress
    const rawInflate = new RawInflate(rawCompress);
    const rawPlain = rawInflate.decompress();

    deflate, inflate

    import { Deflate, Inflate } from "zlibt2";
    
    //compress
    const datas = [1, 2, 3, 4, 5, 6];
    const deflate = new Deflate(datas);
    const compress = deflate.compress();
    
    //decompress
    const inflate = new Inflate(compress);
    const plain = inflate.decompress();

    gzip, gunzip

    import { RawDeflate, RawInflate } from "zlibt2";
    
    //compress
    const datas = [1, 2, 3, 4, 5, 6];
    const gzip = new Gzip(datas);
    const compressed = gzip.compress();
    
    //decompress
    const gunzip = new Gunzip(compressed);
    const resultGZipArray = Array.from(gunzip.decompress());

    pkzip, pkunzip(zip, unzip)

    import { RawDeflate, RawInflate } from "zlibt2";
    
    //compress
    const datas = [1, 2, 3, 4, 5, 6];
    const zip = new Zip();
    zip.addFile(datas, {
        filename: this.stringToByteArray("foo.txt"),
    });
    const zipcompressed = zip.compress();
    const unzip = new Unzip(zipcompressed);
    const filenames = unzip.getFilenames();
    const externalFA = unzip.getFileHeaderAttribute(
        filenames[0],
        "externalFileAttributes"
    );
    
    //get file mode from zip file
    const filemode = (externalFA >>> 16) & 0xffff;
    const resultUnzipArray = Array.from(unzip.decompress("foo.txt", null));

    **float or number array buffer(Uint8Array) case

    • case of number byte array make some error, because of huffman code
    • so if you want right values, please use compressionType 0 option
    • ISSUE 15
    {
        const floats = new Float32Array(datas.length);
        for (let i = 0; i < datas.length; i++) {
            floats[i] = datas[i];
        }
        const floatsAsByte = new Uint8Array(floats.buffer);
        console.log("number array to Uint8Array: ", floatsAsByte);
        // compress
        const deflate = new Deflate(floatsAsByte, { compressionType: 0 });
        const compress = deflate.compress();
        //decompress
        const inflate = new Inflate(compress);
        const plain = inflate.decompress();
    
        const decombuffer = new Uint8Array(plain);
        const floats_plain = new Float32Array(decombuffer.buffer);
        console.log(
            "float, uint8 array to decomress to number array: ",
            Array.from(floats_plain)
        );
    }
  • Zlib JavaScript

    rawdeflate, rawinflate

    const Zlib = require("zlibt2");
    
    const rawDeflate = new Zlib.RawDeflate(datas);
    const rawCompress = rawDeflate.compress();
    
    const rawInflate = new Zlib.RawInflate(rawCompress);
    const rawPlain = rawInflate.decompress();
    var resultRawArray = Array.from(rawPlain);

    deflate, inflate

    const Zlib = require("zlibt2");
    
    const deflate = new Zlib.Deflate(datas);
    const compress = deflate.compress();
    
    const inflate = new Zlib.Inflate(compress);
    const plain = inflate.decompress();
    var resultArray = Array.from(plain);

    gzip, gunzip

    const Zlib = require("zlibt2");
    
    var gzip = new Zlib.Gzip(datas);
    var compressed = gzip.compress();
    
    var gunzip = new Zlib.Gunzip(compressed);
    var resultGZipArray = Array.from(gunzip.decompress());

    pkzip, pkunzip(zip, unzip)

    const Zlib = require("zlibt2");
    
    function stringToByteArray(str) {
        var array = new Uint8Array(str.length);
        var i;
        var il;
    
        for (i = 0, il = str.length; i < il; ++i) {
            array[i] = str.charCodeAt(i) & 0xff;
        }
    
        return array;
    }
    var zip = new Zlib.Zip();
    zip.addFile(datas, {
        filename: stringToByteArray("foo.txt"),
    });
    var zipcompressed = zip.compress();
    var unzip = new Zlib.Unzip(zipcompressed);
    const filenames = unzip.getFilenames();
    const externalFA = unzip.getFileHeaderAttribute(
        "foo.txt",
        "externalFileAttributes"
    );
    const filemode = (externalFA >>> 16) & 0xffff;
    var resultUnzipArray = Array.from(unzip.decompress("foo.txt"));
  • Zlib Web

    • support all object RawDeflate, RawInflate, Zip, Unzip, Gzip, Gunzip

    example zlib code

    <!DOCTYPE html>
    <html>
        <head>
            <script src="zlibt.umd.js" type="application/javascript"></script>
        </head>
        <body>
            <h1>zlib Test Page</h1>
            <p>My first paragraph.</p>
    
            <script>
                const datas = [1, 2, 3, 4, 5, 6];
                //zlib test
                console.log(`raw input data: ${datas}`);
                const rawDeflate = new RawDeflate(datas);
                const rawCompress = rawDeflate.compress();
    
                const rawInflate = new RawInflate(rawCompress);
                const rawPlain = rawInflate.decompress();
                var resultRawArray = Array.from(rawPlain);
                console.log(`raw uncompress result: ${resultRawArray}`);
            </script>
        </body>
    </html>
  • Zlib Compress Options

    {
        compressionType: Zlib.Deflate.CompressionType, // compression type
        lazy: number // lazy matching parameter
    }
  • Zlib Decompress Options

    {
        'index': number, // start position in input buffer
        'bufferSize': number, // initial output buffer size
        'bufferType': Zlib.Inflate.BufferType, // buffer expantion type
        'resize': boolean, // resize buffer(ArrayBuffer) when end of decompression (default: false)
        'verify': boolean  // verify decompression result (default: false)
    }
  • GZip Compress Options

    {
        deflateOptions: Object, // see: deflate option (ZLIB Option)
        flags: {
            fname: boolean, // use filename?
            comment: boolean, // use comment?
            fhcrc: boolean // use file checksum?
        },
        filename: string, // filename
        comment: string // comment
    }

unzip get file header attrubute function

  • support attribute name

    1. 'needVersion'
    2. 'flags'
    3. 'compression'
    4. 'time'
    5. 'date'
    6. 'crc32'
    7. 'compressedSize'
    8. 'plainSize'
    9. 'internalFileAttributes'
    10. 'externalFileAttributes'
    • how to get file permission number like '0o777'
    const unzip = new Zlib.Unzip(compressed, null);
    const filenames = unzip.getFilenames();
    const externalFA = unzip.getFileHeaderAttribute(
        filenames[0],
        "externalFileAttributes"
    );
    const filemode = (externalFA >>> 16) & 0xffff;

zlibt's People

Contributors

madfishdt avatar dependabot[bot] avatar osamaabusitta avatar

Stargazers

fy avatar  avatar Brandon Bennett avatar hongjie avatar Eduardo Izquierdo Rojas avatar  avatar 汤姆大叔 avatar  avatar Salvatore avatar Jiko avatar Colin Diesh avatar  avatar

Watchers

James Cloos avatar  avatar Eduardo Izquierdo Rojas avatar

zlibt's Issues

Doesn't uncompress correctly

I'm not using this library anymore, but I thought you should know it is unable to recreate a Uint8Array.

Here is my modified example from your code and the outputs below which don't match. Also, a bunch of string code is printed out to the console if I try any use anything else (like Gzip or Gunzip).

import { Deflate, Inflate } from 'zlibt2';

// create float data
const datas = [1, 2, 3, 4, 5, 6];
const floats = new Float32Array(datas.length);
for (let i = 0; i < datas.length; i++) {
  floats[i] = datas[i];
}

// represent float as byte
const floatsAsByte = new Uint8Array(floats.buffer);

// compress
const deflate = new Deflate(floatsAsByte, {});
const compress = deflate.compress();

//decompress
const inflate = new Inflate(compress);
const plain = inflate.decompress();

// compare results
console.log(floatsAsByte);
console.log(plain);

And the output:

Uint8Array(24) [
  0, 0, 128, 63, 0, 0,   0, 64,
  0, 0,  64, 64, 0, 0, 128, 64,
  0, 0, 160, 64, 0, 0, 192, 64
]
Uint8Array(11) [
   0, 0, 128, 63, 0,
   0, 0,  64,  0, 0,
  64
]

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.