beatgammit / gzip-js

GZIP implemented in pure JavaScript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can gzip-js be directly used in browser?

extend1994 opened this issue Β· comments

commented

Hi!
I saw you described

The main goal of this project is to bring GZIP compression to the browser.

in Readme.md.
But I can not find a way to use it in browser.
Could you please tell me the way how to use it?
Thank you πŸ˜„

For the reference cdnjs/cdnjs#11818

@extend1994 have you even read the readme? https://github.com/beatgammit/gzip-js At the buttom in the usage section, you can read how to use it...

commented

@Ruffio
I have read it.
I tried to create a html file, include lib/gzip.js using <script src="lib/gzip.js"> and write

var gzip = require('gzip-js'),
	options = {
		level: 3,
		name: 'hello-world.txt',
		timestamp: parseInt(Date.now() / 1000, 10)
	};

// out will be a JavaScript Array of bytes
var out = gzip.zip('Hello world', options);

in <script> blocks. But errors appeared.
Did I use it in a wrong way?
I am not familiar with this.

Maybe this library needs browserify first?

commented

Install browserify (globally if you want the binary):
> npm install -g browserify

install gzip-js locally:
> npm install gzip-js

create a file with something simple like gzipjs_node.js:
var gzipjs = require('gzip-js');

or if you prefer something object like:

var gzipjs = function() {

        this.gzip = require('gzip-js');
        this.defaultLevel = 9;
        this.getTimestamp = function() { return parseInt(Date.now() / 1000, 10); };
        this.gzipString = function(content, filename) {
            var options = {
                level: this.defaultLevel,
                name: filename,
                timestamp: this.getTimestamp()
            };
            return this.gzip.zip(content, options);
        };
        this.zipStringToFile(content, filename) {
            var sZip = this.gzipString(content, filename);
            var sFile = new File([new Blob([sZip])], filename + '.gz');
            return sFile;
        };
    };

run browserify on it and you can use the result in your browser.
> browserify gzipjs_node.js -o gzipjs.js

You can create your own instances with var myZipper = new gzipjs or something similar and reference either the .gzip property directly or write helper functions like I did above.

Thanks @treii28 ! So looks like we do need browserify!

commented

@treii28 Thanks a lot!
@PeterDaveHello Are we going to do browserify and add the lib?

Maybe not, and I don't think we should discuss here ...