Top Related Projects
Quick Overview
zlib.js is a JavaScript implementation of the zlib compression library. It allows for compression and decompression of data directly in the browser or in Node.js environments, providing a pure JavaScript alternative to native zlib implementations.
Pros
- Pure JavaScript implementation, enabling cross-platform compatibility
- Supports both browser and Node.js environments
- Offers various compression methods, including DEFLATE, INFLATE, and GZIP
- Provides asynchronous processing options for improved performance
Cons
- May have slower performance compared to native implementations
- Limited maintenance and updates in recent years
- Lacks some advanced features found in more recent compression libraries
- Documentation could be more comprehensive
Code Examples
- Compressing a string:
const zlib = require('zlib.js');
const input = 'Hello, world!';
const compressed = zlib.deflate(input);
console.log(compressed);
- Decompressing data:
const zlib = require('zlib.js');
const compressed = /* compressed data */;
const decompressed = zlib.inflate(compressed);
console.log(decompressed.toString());
- Using GZIP compression:
const zlib = require('zlib.js');
const input = 'This is a longer string to compress';
const gzipped = zlib.gzip(input);
console.log(gzipped);
Getting Started
To use zlib.js in your project, follow these steps:
-
Install the library using npm:
npm install zlib.js
-
Import the library in your JavaScript file:
const zlib = require('zlib.js');
-
Use the compression and decompression methods as needed:
const compressed = zlib.deflate('Your data here'); const decompressed = zlib.inflate(compressed);
Note: For browser usage, include the zlib.js file in your HTML and use the Zlib
global object instead of requiring the module.
Competitor Comparisons
A massively spiffy yet delicately unobtrusive compression library.
Pros of zlib
- Written in C, offering better performance for native applications
- More comprehensive and feature-rich, supporting a wider range of compression algorithms
- Widely used and battle-tested in many production environments
Cons of zlib
- Requires compilation and is not directly usable in web browsers
- Larger codebase and potentially more complex to integrate into projects
- May have a steeper learning curve for developers unfamiliar with C
Code Comparison
zlib (C):
z_stream strm;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
deflateInit(&strm, level);
zlib.js (JavaScript):
var deflate = new Zlib.Deflate(data, {
level: level,
gzip: true
});
var compressed = deflate.compress();
Additional Notes
zlib.js is specifically designed for use in web browsers and Node.js environments, providing a pure JavaScript implementation of zlib compression algorithms. This makes it easier to integrate into web-based projects but may sacrifice some performance compared to the native C implementation of zlib.
zlib, on the other hand, is the original and widely-used compression library that serves as the foundation for many compression tools and libraries across various platforms. It offers more fine-grained control and potentially better performance for native applications.
high speed zlib port to javascript, works in browser & node.js
Pros of pako
- More actively maintained with recent updates
- Better performance, especially for larger files
- Supports both Node.js and browser environments
Cons of pako
- Slightly larger file size
- Less comprehensive documentation
Code Comparison
zlib.js:
var compressed = zlib.deflate(input);
var decompressed = zlib.inflate(compressed);
pako:
var compressed = pako.deflate(input);
var decompressed = pako.inflate(compressed);
Both libraries offer similar APIs for basic compression and decompression operations. However, pako provides additional methods and options for more advanced use cases.
Performance
pako generally outperforms zlib.js, especially when dealing with larger files. This makes it a better choice for applications that require high-speed compression and decompression.
Compatibility
While zlib.js is primarily designed for browser use, pako supports both browser and Node.js environments, making it more versatile for different types of projects.
Community and Support
pako has a larger user base and more active development, which translates to better long-term support and more frequent updates. zlib.js, on the other hand, has not seen significant updates in recent years.
File Size
zlib.js has a slightly smaller file size, which may be beneficial for projects where every kilobyte counts. However, the difference is minimal and unlikely to impact most applications significantly.
High performance (de)compression in an 8kB package
Pros of fflate
- Significantly faster compression and decompression speeds
- Smaller bundle size, making it more suitable for browser-based applications
- Supports more compression formats, including Gzip and Zlib
Cons of fflate
- Less mature project with potentially fewer real-world use cases
- May have less comprehensive documentation compared to zlib.js
Code Comparison
fflate:
import { gzip, ungzip } from 'fflate';
const compressed = gzip(new TextEncoder().encode('Hello, World!'));
const decompressed = ungzip(compressed);
console.log(new TextDecoder().decode(decompressed));
zlib.js:
const compressed = zlib.deflate(unescape(encodeURIComponent('Hello, World!')));
const decompressed = zlib.inflate(compressed);
console.log(decodeURIComponent(escape(decompressed)));
Summary
fflate offers improved performance and a smaller footprint compared to zlib.js, making it an attractive option for modern web applications. However, zlib.js has been around longer and may have more extensive documentation. The code comparison shows that fflate uses more modern APIs, while zlib.js relies on older encoding methods. Developers should consider their specific needs and target environments when choosing between these libraries.
LZ-based compression algorithm for JavaScript
Pros of lz-string
- Smaller library size, making it more lightweight for web applications
- Faster compression and decompression speeds for small to medium-sized data
- Simpler API, easier to use for basic compression tasks
Cons of lz-string
- Less compression efficiency for larger data sets compared to zlib.js
- Limited compression algorithm options (LZ-based only)
- Less widespread adoption and community support
Code Comparison
lz-string:
var compressed = LZString.compress("Hello, World!");
var decompressed = LZString.decompress(compressed);
zlib.js:
var compressed = zlib.deflate("Hello, World!");
var decompressed = zlib.inflate(compressed);
Summary
lz-string is a lightweight and fast compression library, ideal for small to medium-sized data in web applications. It offers a simpler API and faster processing times for smaller data sets. However, it may not be as efficient for larger data and has fewer compression options compared to zlib.js.
zlib.js, on the other hand, provides better compression ratios for larger data sets and offers more advanced compression algorithms. It has wider adoption and community support but comes with a larger library size and slightly more complex API.
Choose lz-string for quick, simple compression tasks in web applications, and zlib.js for more robust compression needs or when dealing with larger data sets.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
zlib.js
zlib.js 㯠ZLIB(RFC1950), DEFLATE(RFC1951), GZIP(RFC1952), PKZIP ã® JavaScript å®è£ ã§ãã
使ãæ¹
zlib.js ã¯å¿ è¦ãªæ©è½ãã¨ã«åå²ããã¦ãã¾ãã bin ãã£ã¬ã¯ããªããå¿ è¦ãªãã®ãå©ç¨ãã¦ãã ããã
- zlib_and_gzip.min.js: ZLIB + GZIP
- (Raw)
- rawdeflate.js: Raw Deflate
- raw.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
- (Raw)
- node-zlib.js: (ZLIB + GZIP for node.js)
å§ç¸® (Compress)
Raw Deflate
// plain = Array.<number> or Uint8Array
var deflate = new Zlib.RawDeflate(plain);
var compressed = deflate.compress();
Raw Deflate Option
ZLIB Option ãåç §ãã¦ãã ããã
ZLIB
// plain = Array.<number> or Uint8Array
var deflate = new Zlib.Deflate(plain);
var compressed = deflate.compress();
ZLIB Option
Zlib.Deflate
ã®ç¬¬äºå¼æ°ã«ãªãã¸ã§ã¯ãã渡ãäºã§å§ç¸®ãªãã·ã§ã³ãæå®ããäºãåºæ¥ã¾ãã
{
compressionType: Zlib.Deflate.CompressionType, // å§ç¸®ã¿ã¤ã
lazy: number // lazy matching ã®é¾å¤
}
Zlib.Deflate.CompressionType
ã¯
NONE
(ç¡å§ç¸®), FIXED
(åºå®ãããã³ç¬¦å·), DYNAMIC
(åçãããã³ç¬¦å·) ããé¸æããäºãã§ãã¾ãã
default 㯠DYNAMIC
ã§ãã
lazy
㯠Lazy Matching ã®é¾å¤ãæå®ãã¾ãã
Lazy Matching ã¨ã¯ãLZSS ã®ãããé·ãé¾å¤ããä½ãã£ãå ´åã次㮠Byte ãã LZSS ã®æé·ä¸è´ã試ã¿ããããé·ã®é·ãæ¹ãé¸æããææ³ã§ãã
GZIP
GZIP ã®å®è£ ã¯ç¾å¨ä¸å®å ¨ã§ããããã ã®å§ç¸®ã³ã³ããã¨ãã¦ä½¿ç¨ããå ´åã«ã¯ç¹ã«åé¡ã¯ããã¾ããã zlib.js ãç¨ãã¦ä½æããã GZIP ã® OS ã¯ãèªåçã« UNKNOWN ã«è¨å®ããã¾ãã
// plain = Array.<number> or Uint8Array
var gzip = new Zlib.Gzip(plain);
var compressed = gzip.compress();
GZIP Option
{
deflateOptions: Object, // deflate option (ZLIB Option åç
§)
flags: {
fname: boolean, // ãã¡ã¤ã«åã使ç¨ããã
comment: boolean, // ã³ã¡ã³ãã使ç¨ããã
fhcrc: boolean // FHCRC ã使ç¨ããã
},
filename: string, // flags.fname ã true ã®ã¨ãã«æ¸ãè¾¼ããã¡ã¤ã«å
comment: string // flags.comment ã true ã®ã¨ãã«æ¸ãè¾¼ãã³ã¡ã³ã
}
PKZIP
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 㯠Typed Array ã使ç¨å¯è½ãªå ´åã¯å¿ ã Uint8Array ã使ç¨ãã¦ãã ããã
{
filename: (Array.<number>|Uint8Array), // ãã¡ã¤ã«å
comment: (Array.<number>|Uint8Array), // ã³ã¡ã³ã
extraField: (Array.<number>|Uint8Array), // ãã®ä»ã®é å
compress: boolean, // addFile ã¡ã½ãããå¼ãã ã¨ãã«å§ç¸®ããã (é常㯠compress ã¡ã½ããã®å¼ã³åºãæã«å§ç¸®)
compressionMethod: Zlib.Zip.CompressionMethod, // STORE or DEFLATE
os: Zlib.Zip.OperatingSystem, // MSDOS or UNIX or MACINTOSH
deflateOption: Object // see: ZLIB Option
}
伸張 (Decompress)
å§ç¸®ããããã¼ã¿ã®ä¼¸å¼µã¯ãåºæ¬çã«åã³ã³ã¹ãã©ã¯ã¿ã«å§ç¸®ããããã¼ã¿ã渡ãã
ããã® decompress
ã¡ã½ãããå¼ã¶äºã§ä¼¸å¼µå¦çãéå§ããäºãåºæ¥ã¾ãã
Raw Deflate
// compressed = Array.<number> or Uint8Array
var inflate = new Zlib.RawInflate(compressed);
var plain = inflate.decompress();
Raw Deflate Option
ZLIB Option ãåç §ãã¦ãã ããã
ZLIB
// compressed = Array.<number> or Uint8Array
var inflate = new Zlib.Inflate(compressed);
var plain = inflate.decompress();
ZLIB Option
Zlib.Inflate
ã®ç¬¬äºå¼æ°ã«ãªãã¸ã§ã¯ãã渡ãäºã§ä¼¸å¼µãªãã·ã§ã³ãæå®ããäºãã§ãã¾ãã
{
'index': number, // å
¥åãããã¡ã®éå§ä½ç½®
'bufferSize': number, // åºåãããã¡ã®åæãµã¤ãº
'bufferType': Zlib.Inflate.BufferType, // ãããã¡ã®ç®¡çæ¹æ³
'resize': boolean, // åºåãããã¡ã®ãªãµã¤ãº
'verify': boolean // 伸張çµæã®æ¤è¨¼ãè¡ãã
}
Zlib.Inflate.BufferType
㯠ADAPTIVE
(default) ã BLOCK
ãé¸æããäºãã§ãã¾ãã
ADAPTIVE
ã¯ãããã¡ã伸張å¾ã®ãµã¤ãºãäºæ¸¬ãã¦ä¸æ°ã«æ¡å¼µãã¾ããããã¼ã¿ã«ãã£ã¦ã¯ä½åã«ã¡ã¢ãªã使ç¨ããããäºãããã¾ããBLOCK
ã§ã¯BufferSize
ãã¤æ¡å¼µãã¦ããã¾ãããåä½ã¯ãã¾ãéãããã¾ããã
resize
ãªãã·ã§ã³ã¯ Typed Array å©ç¨å¯è½æ
decompress
ã¡ã½ããã§è¿å´ããå¤ã® ArrayBuffer
ã Uint8Array
ã®é·ãã¾ã§ç¸®å°ããã¾ãã
default 㯠false
ã§ãã
verify
ãªãã·ã§ã³ã¯ Adler-32 Checksum ã®æ¤è¨¼ãè¡ãããæå®ãã¾ãã
default 㯠false
ã§ãã
GZIP
// compressed = Array.<number> or Uint8Array
var gunzip = new Zlib.Gunzip(compressed);
var plain = gunzip.decompress();
Gunzip ã®ãªãã·ã§ã³ã¯ç¾å¨ããã¾ããã
PKZIP
PKZIP ã®æ§ç¯ã¨åæ§ã«è¤æ°ãã¡ã¤ã«ãæ±ããããä»ã®ãã®ã¨ã¯å°ã使ãæ¹ãç°ãªãã¾ãã
// compressed = Array.<number> or Uint8Array
var unzip = new Zlib.Unzip(compressed);
var filenames = unzip.getFilenames();
var plain = unzip.decompress(filenames[0]);
Unzip ã®ãªãã·ã§ã³ã¯ç¾å¨ããã¾ããã
Node.js
Node.js ã§ä½¿ç¨ããå ´åã¯ã¦ããããã¹ããåç §ãã¦ãã ããã https://github.com/imaya/zlib.js/blob/master/test/node-test.js
Debug
zlib.js ã§ã¯ JavaScript ãã¡ã¤ã«ã minify ãããå½¢ã§æä¾ãã¦ãã¾ãããéçºä¸ããããã°æã« minify ããåã®ç¶æ ãç¥ãããäºãããã¾ãã ãããã£ãæã®ããã« SourceMaps ãã¡ã¤ã«ã Pretty Print ããããã¡ã¤ã«ãæä¾ãã¦ãã¾ãã
Source Map
Source Map ã使ãããå ´åã¯ãã¡ã¤ã«åã« dev
ã®ã¤ãããã¼ã¸ã§ã³ã使ãã¾ãã
ä¾ãã° Source Map ãæå¹ã«ãã Inflate ã使ãããå ´åã¯ä»¥ä¸ã«ãªãã¾ãã
- inflate.min.js // ãªãªã¼ã¹ãã¼ã¸ã§ã³
- inflate.dev.min.js // éçºãã¼ã¸ã§ã³ï¼ããã使ãï¼
Pretty Print
SourceMaps ã¨ã¯ç°ãªãã¾ãããminify ã®å¤æ°åã®ç縮ã®ã¿é¿ããããã°è¯ãã¨ããå ´åã«ã¯ã Closure Compiler ã§èªã¿ããããããã¡ã¤ã«ãå©ç¨ãããã¨ãå¯è½ã§ãã
zlib.pretty.js
ã¨ãããã¡ã¤ã«åã§å
¨ã¦ã®å®è£
ãã¯ãã£ã¦ãã¾ãã®ã§ãminify ããããã®ããã®ãã¡ã¤ã«ã«ç½®ãæããã ãã§ä½¿ç¨ã§ãã¾ãã
How to build
ãã«ã㯠Grunt 㨠Closure Compiler ã使ç¨ãã¦è¡ãã¾ãã
å¿ è¦ãªç°å¢
- Grunt
- Python
ãã«ã
Grunt ã使ã£ã¦ãã«ããè¡ãã¾ãã
$ grunt [target]
ãã«ãã¿ã¼ã²ãã
target | ãã¡ã¤ã«å | å«ã¾ããå®è£ |
---|---|---|
deps | deps.js | ä¾åé¢ä¿ã®è§£æ±º |
deflate | deflate.min.js | ZLIB Deflate |
inflate | inflate.min.js | ZLIB Inflate |
inflate_stream | inlfate_stream.min.js | ZLIB Inlate (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 |
ãã¹ã
ãã©ã¦ã¶ã§ã¯ Karma, Node.js ã§ã¯ mocha ã使ã£ã¦ãã¹ããè¡ãã¾ãã
$ npm test
ãã©ã¦ã¶ã®ã¿ã®ãã¹ã
$ npm run test-karma
Node.js ã®ã¿ã®ãã¹ã
$ npm run test-mocha
Issue
ç¾å¨ããªã»ããè¾æ¸ãç¨ããå§ç¸®å½¢å¼ã«ã¯å¯¾å¿ãã¦ãã¾ããã ããªã»ããè¾æ¸ã¯é常ã®å§ç¸®ã§ã¯å©ç¨ãããªããããå½±é¿ã¯å°ãªãã¨æãã¾ãã
ã©ã¤ã»ã³ã¹
Copyright © 2012 imaya. Licensed under the MIT License.
Top Related Projects
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot