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

Use yazl instead of JSZip #42

Merged
merged 1 commit into from
Mar 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ var path = require('path');
var gutil = require('gulp-util');
var through = require('through2');
var chalk = require('chalk');
var JSZip = require('jszip');
var Yazl = require('yazl');
var concatStream = require('concat-stream');

module.exports = function (filename, opts) {
if (!filename) {
Expand All @@ -14,7 +15,7 @@ module.exports = function (filename, opts) {
opts.compress = typeof opts.compress === 'boolean' ? opts.compress : true;

var firstFile;
var zip = new JSZip();
var zip = new Yazl.ZipFile();

return through.obj(function (file, enc, cb) {
if (file.isStream()) {
Expand All @@ -29,10 +30,10 @@ module.exports = function (filename, opts) {
// because Windows...
var pathname = file.relative.replace(/\\/g, '/');

zip.file(pathname, file.contents, {
date: file.stat ? file.stat.mtime : new Date(),
createFolders: true,
dir: file.stat && file.stat.isDirectory && file.stat.isDirectory()
zip.addBuffer(file.contents, pathname, {
compress: opts.compress,
mtime: file.stat ? file.stat.mtime : new Date(),
mode: file.stat ? file.stat.mode : null
});

cb();
Expand All @@ -42,17 +43,17 @@ module.exports = function (filename, opts) {
return;
}

this.push(new gutil.File({
cwd: firstFile.cwd,
base: firstFile.base,
path: path.join(firstFile.base, filename),
contents: zip.generate({
type: 'nodebuffer',
compression: opts.compress ? 'DEFLATE' : 'STORE',
comment: opts.comment
})
}));

cb();
zip.end(function () {
zip.outputStream.pipe(concatStream(function (data) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't have to wait until the end callback to pipe the output stream. in fact, you can skip the zip.end function entirely - it's just to notify you when it knows the final size of the zip file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do think it's needed. I get timeout issues otherwise when running tests.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh sorry, you do need to call it. but you don't need to wait for the callback.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Data becomes available in this stream soon after calling one of addFile(), addReadStream(), or
addBuffer(). Clients can call pipe() on this stream at any time, such as immediately after getting
a new ZipFile instance, or long after calling end().

from https://github.com/thejoshwolfe/yazl#outputstream

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, makes no difference in speed or whatsoever though since we're only handling one buffer at a time.

this.push(new gutil.File({
cwd: firstFile.cwd,
base: firstFile.base,
path: path.join(firstFile.base, filename),
contents: data
}));

cb();
}.bind(this)));
}.bind(this));
});
};
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@
],
"dependencies": {
"chalk": "^1.0.0",
"concat-stream": "^1.4.7",
"gulp-util": "^3.0.0",
"jszip": "^2.4.0",
"through2": "^0.6.1"
"through2": "^0.6.1",
"yazl": "^2.1.0"
},
"devDependencies": {
"decompress-unzip": "*",
"gulp": "*",
"mocha": "*",
"gulp": "*"
"vinyl-assign": "*"
}
}
6 changes: 0 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ Type: `string`
Type: `boolean`
Default: `true`

##### comment

Type: `string`

Text information embedded in the zip file.


## License

Expand Down
31 changes: 22 additions & 9 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,43 @@
'use strict';
var fs = require('fs');
var path = require('path');
var assert = require('assert');
var gutil = require('gulp-util');
var unzip = require('decompress-unzip');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thejoshwolfe has https://github.com/thejoshwolfe/yauzl as well, just sayin' :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andrewrk, look what library I'm using in decompress-unzip :).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hahahaha

var vinylAssign = require('vinyl-assign');
var zip = require('./');

it('should zip files', function (cb) {
var stream = zip('test.zip');
var i = 0;
var unzipper = unzip();
var stats = fs.statSync(__dirname + '/fixture/fixture.txt');
var files = [];

unzipper.on('data', files.push.bind(files));
unzipper.on('end', function () {
assert.equal(files[0].path, 'fixture.txt');
assert.equal(files[1].path, 'fixture2.txt');
assert.equal(files[0].contents.toString(), 'hello world');
assert.equal(files[1].contents.toString(), 'hello world 2');
assert.equal(files[0].stat.mode, stats.mode);
assert.equal(files[1].stat.mode, stats.mode);
cb();
});

stream.on('data', function (file) {
i++;
assert.equal(path.normalize(file.path), path.join(__dirname, 'fixture/test.zip'));
assert.equal(file.relative, 'test.zip');
assert(file.contents.length > 0);
});

stream.on('end', function () {
assert(i === 1);
cb();
});

stream.write(new gutil.File({
cwd: __dirname,
base: __dirname + '/fixture',
path: __dirname + '/fixture/fixture.txt',
contents: new Buffer('hello world'),
stat: {
mtime: new Date()
mode: stats.mode,
mtime: stats.mtime
}
}));

Expand All @@ -36,9 +47,11 @@ it('should zip files', function (cb) {
path: __dirname + '/fixture/fixture2.txt',
contents: new Buffer('hello world 2'),
stat: {
mtime: new Date()
mode: stats.mode,
mtime: stats.mtime
}
}));

stream.pipe(vinylAssign({extract:true})).pipe(unzipper);
stream.end();
});