Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can gzip-js be directly used in browser? #15

Open
extend1994 opened this issue Sep 19, 2017 · 7 comments
Open

Can gzip-js be directly used in browser? #15

extend1994 opened this issue Sep 19, 2017 · 7 comments

Comments

@extend1994
Copy link

extend1994 commented Sep 19, 2017

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

@Ruffio
Copy link

Ruffio commented Sep 19, 2017

@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...

@extend1994
Copy link
Author

extend1994 commented Sep 19, 2017

@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.

@PeterDaveHello
Copy link

Maybe this library needs browserify first?

@treii28
Copy link

treii28 commented Oct 9, 2017

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.

@PeterDaveHello
Copy link

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

@extend1994
Copy link
Author

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

@PeterDaveHello
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants