27

Are there any open source DEFLATE encoder implementations for JavaScript? I need it to generate a binary format on the client-side that requires DEFLATE.

Eli Grey
  • 35,104
  • 14
  • 75
  • 93

2 Answers2

25

I believe Pako (https://github.com/nodeca/pako) is now the fastest javascript implementation of deflate and other zlib methods (inflate / gzip / ungzip). There are benchmarks on the github page. It also supports chunking if you need to work with big blobs.

Disclaimer: I am the author of this code.

caesay
  • 16,932
  • 15
  • 95
  • 160
Vitaly
  • 3,340
  • 26
  • 21
  • it is for node.js ... – Flash Thunder Jan 18 '15 at 04:44
  • 1
    `bower install pako`. See readme. It's for the browser. – Vitaly Jan 18 '15 at 08:37
  • I saw readme... did you see the code? as files in dist are being tried to work on browser, they use zlib in lib folder that is node.js without even trying to change it... – Flash Thunder Jan 18 '15 at 08:47
  • 11
    I'm author of this code and i know how it works. This code is written for browser, and works in many browser projects, independent on your opinion. – Vitaly Jan 18 '15 at 13:08
  • 2
    Yes, thank you. I did notice that (after answering my post on github). Just didn't know that there was `require` implementation in `javascript`, sorry for mess. – Flash Thunder Jan 18 '15 at 13:17
24

I found a DEFLATE encoder and decoder implementation at http://github.com/dankogai/js-deflate and they both work perfectly.

Eli Grey
  • 35,104
  • 14
  • 75
  • 93
  • 3
    Note that js-deflate expects strings with 1 byte per character, but JS uses UCS-2 encoding internally. So you should either apply base64 encoding first, or convert to raw UTF-8 (see http://ecmanaut.blogspot.com/2006/07/encoding-decoding-utf8-in-javascript.html for conversion methods) – user123444555621 Nov 28 '11 at 12:12
  • 4
    An example of js-deflate in action: http://jsfiddle.net/mwolfetech/X2NL6/ – mwolfetech Feb 08 '12 at 22:10
  • 3
    If you set compression level to 3 (down from default 6), it duplicates the last character! Also, it will sometimes produce an invalid output. – Ivan Sikiric Dec 10 '12 at 15:09
  • 2
    previous jsfiddle doesnt work due to wrong content type. Heres a fixed example: http://jsfiddle.net/X2NL6/263/ – Yaroslav Yakovlev Jan 14 '14 at 13:35
  • I can't get this library's output to be accepted by any other library. worthless. – thejoshwolfe Apr 09 '16 at 04:38
  • The fiddle example there Yaroslav now gives the length of the 64encoded raw. – Tatarize Dec 13 '16 at 19:58