Package detail

zlibjs

imaya1.5mMIT0.3.1

zlib, gzip and zip implementation in JavaScript

readme

zlib.js

Build Status

Japanese version

zlib.js is ZLIB(RFC1950), DEFLATE(RFC1951), GZIP(RFC1952) and PKZIP implementation in JavaScript.

Usage

Use one in "bin" directory.

  • zlib_and_gzip.min.js: ZLIB + GZIP
    • (Raw)
      • rawdeflate.js: Raw Deflate
      • rawinflate.js: Raw Inflate
    • zlib.min.js: ZLIB Inflate + Deflate
      • inflate.min.js: ZLIB Inflate
      • deflate.min.js: ZLIB Deflate
      • inflate_stream.min.js: ZLIB Inflate (stream mode)
    • (GZIP)
      • gzip.min.js: GZIP
      • gunzip.min.js: GUNZIP
    • (PKZIP)
      • zip.min.js ZIP
      • unzip.min.js UNZIP
  • node-zlib.js: (ZLIB + GZIP for node.js)

Compression

Raw Deflate

// plain = Array.<number> or Uint8Array
var deflate = new Zlib.RawDeflate(plain);
var compressed = deflate.compress();

Raw Deflate Option

See ZLIB Option.

ZLIB

// plain = Array.<number> or Uint8Array
var deflate = new Zlib.Deflate(plain);
var compressed = deflate.compress();
ZLIB Option

Second argument of Zlib.Deflate constructor

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

Zlib.Deflate.CompressionType is enumerable, Choose one in NONE (Store), FIXED (Fixed Huffman Coding), DYNAMIC (Dynamic Huffman Coding). Default value is DYNAMIC.

lazy is Lazy Matching length. This parameter is deprecated.

GZIP

GZIP implementation is incomplete. However, there is no problem in usual use.

// plain = Array.<number> or Uint8Array
var gzip = new Zlib.Gzip(plain);
var compressed = gzip.compress();
GZIP Option
{
    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
}

PKZIP

var zip = new Zlib.Zip();
// plainData1
zip.addFile(plainData1, {
    filename: stringToByteArray('foo.txt')
});
zip.addFile(plainData2, {
    filename: stringToByteArray('bar.txt')
});
zip.addFile(plainData3, {
    filename: stringToByteArray('baz.txt')
});
var compressed = zip.compress();

function stringToByteArray(str) {
    var array = new (window.Uint8Array !== void 0 ? Uint8Array : Array)(str.length);
    var i;
    var il;

    for (i = 0, il = str.length; i < il; ++i) {
        array[i] = str.charCodeAt(i) & 0xff;
    }

    return array;
}
PKZIP Option

filename, comment, extraField are must use Uint8Array if enabled Typed Array.

{
    filename: (Array.<number>|Uint8Array), // filename
    comment: (Array.<number>|Uint8Array), //comment
    extraField: (Array.<number>|Uint8Array), // extra field
    compress: boolean, // compress when called "addFile" method.
    compressionMethod: Zlib.Zip.CompressionMethod, // STORE or DEFLATE
    os: Zlib.Zip.OperatingSystem, // MSDOS or UNIX or MACINTOSH
    deflateOption: Object // see: ZLIB Option
}

Decompression

Raw Deflate

// compressed = Array.<number> or Uint8Array
var inflate = new Zlib.RawInflate(compressed);
var plain = inflate.decompress();

Raw Deflate Option

See ZLIB Option.

ZLIB

// compressed = Array.<number> or Uint8Array
var inflate = new Zlib.Inflate(compressed);
var plain = inflate.decompress();
ZLIB Option

Second argument of Zlib.Inflate constructor

{
    '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)
}

Zlib.Inflate.BufferType is enumerable. Choose one ADAPTIVE(default) and BLOCK.

  • ADAPTIVE: buffer expansion based on compression ratio in filled buffer.
  • BLOCK: buffer expansion based on BufferSize.

GZIP

// compressed = Array.<number> or Uint8Array
var gunzip = new Zlib.Gunzip(compressed);
var plain = gunzip.decompress();

PKZIP

// compressed = Array.<number> or Uint8Array
var unzip = new Zlib.Unzip(compressed);
var filenames = unzip.getFilenames();
var plain = unzip.decompress(filenames[0]);

Node.js

see unit tests. https://github.com/imaya/zlib.js/blob/master/test/node-test.js

Debug

If you want to know the code before compile, SourceMaps and PrettyPrint can be used.

Source Map

If you want to use the Source Map, use dev version.

For example, you want to use Inflate with Source Map.

- inflate.min.js // release version
- inflate.dev.min.js // development version <- use this

Pretty Print

zlib.pretty.js is not renamed symbol.

How to build

Build using Grunt and Closure Compiler.

Requirement

  • Grunt
  • Python

Build

Use "grunt" command.

$ grunt [target]

Build target

target generate file implementation
deps deps.js (dependency: deps.js)
deflate deflate.min.js ZLIB Deflate
inflate inflate.min.js ZLIB Inflate
inflate_stream inflate_stream.min.js ZLIB Inflate (stream)
zlib zlib.min.js ZLIB Deflate + Inflate
gzip gzip.min.js GZIP Compression
gunzip gunzip.min.js GZIP Decompression
zlib_and_gzip zlib_and_gzip.min.js ZLIB + GZIP
node node-zlib.js ZLIB + GZIP for node.js
zip zip.min.js PKZIP Compression
unzip unzip.min.js PKZIP Decompression
all * default target

Test

Unit tests are using Karma and mocha.

$ npm test

Browser only

$ npm run test-karma

Node.js only

$ npm run test-mocha

Issue

Preset dictionary is not implemented.

License

Copyright © 2012 imaya. Licensed under the MIT License.

changelog

Change Log

0.3.1: 2017/07/07

https://github.com/imaya/zlib.js/compare/0.3.0...0.3.1

etc

  • Distribution files reduction in npm package
  • code cleanup

0.3.0: 2017/06/01

https://github.com/imaya/zlib.js/compare/0.2.0...0.3.0

decompression

  • fix bug in decode HLIT and HDIST (aefd58bc482258faaf70644717ac54b7f6d4e599)
  • check invalid code length from malicious compressed data (4971111c1ad49e3c353ed38d51850e1ff5c206b9)

compression

  • fix data exists after payload (08a5b2eb89e1d31178d272857fbdf55358bdcec4)

etc

  • Task runner: Migrate to Grunt from Ant
  • Testing framework: Migate to Karma and mocha from BusterJS

0.1.9, 0.2.0: 2014/02/21

https://github.com/imaya/zlib.js/compare/0.1.8...0.2.0

decompression

  • fix RLE bug HDIST between HLIT

etc

  • support buster.js 0.7.4
  • code cleanup

0.1.8: 2014/02/16

https://github.com/imaya/zlib.js/compare/0.1.7...0.1.8

not changed. (for npm republish)


0.1.7: 2013/07/12

https://github.com/imaya/zlib.js/compare/0.1.6...0.1.7

compression

  • support different password each file in PKZIP

decompression

  • fix PKZIP signedness
  • support different password each file in PKZIP
  • workaround iOS 6.x safari bug in stream version (thanks to Kazuho Oku)

0.1.6: 2013/05/10

https://github.com/imaya/zlib.js/compare/0.1.5...0.1.6

compression

  • export raw deflate

decompression

  • export raw inflate
  • fix inflate stream exporting

etc

  • export crc-32
  • add pretty print build
  • optimize compile settings
  • add english document
  • support source maps
  • support Travis CI
  • refactor unit test
  • update closure compiler (custom version)

0.1.5: 2013/02/10

https://github.com/imaya/zlib.js/compare/0.1.4...0.1.5

compression

  • fix PKZIP CRC-32 bug

etc

  • update PKZIP unit test

0.1.4: 2013/02/10

https://github.com/imaya/zlib.js/compare/0.1.3...0.1.4

compression

  • add PKZIP compression (basic support)

decompression

  • add PKZIP decompression (basic support)

etc

  • refactor build environment (use export js file)
  • remove license comment in source code

0.1.3: 2012/12/21

https://github.com/imaya/zlib.js/compare/0.1.2...0.1.3

compression

  • fix rare case bug

0.1.2: 2012/12/17

https://github.com/imaya/zlib.js/compare/0.1.1...0.1.2

compression

  • fix adler-32 bug (byte order)
  • refactor raw deflate code

decompression

  • refactor inflate stream code

etc

  • update closure compiler (custom version)
  • update inflate unit test (enable adler-32 verification)

0.1.1: 2012/11/15

https://github.com/imaya/zlib.js/compare/0.1.0...0.1.1

compression

  • fix huffman coding (add reverse package merge algorithm)

etc

  • fix gunzip unit test

0.1.0: 2012/09/24

  • first release