Socket
Socket
Sign inDemoInstall

zip-stream

Package Overview
Dependencies
Maintainers
1
Versions
50
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

zip-stream - npm Package Compare versions

Comparing version 0.1.0-alpha to 0.1.0

lib/headers.js

271

lib/zip-stream.js
/**
* node-zip-stream
*
* Copyright (c) 2012-2013 Chris Talkington, contributors.
* Copyright (c) 2014 Chris Talkington, contributors.
* Licensed under the MIT license.
* https://github.com/ctalkington/node-zip-stream/blob/master/LICENSE-MIT
*/
*/
var inherits = require('util').inherits;
var Transform = require('stream').Transform || require('readable-stream').Transform;
var PassThrough = require('stream').PassThrough || require('readable-stream').PassThrough;
var ChecksumStream = require('./util/ChecksumStream');
var DeflateRawChecksum = require('./util/DeflateRawChecksum');
var headers = require('./headers');
var util = require('./util');
var ZipStream = module.exports = function(options) {
options = this.options = util.defaults(options, {
comment: '',
forceUTC: false
});
if (typeof options.zlib !== 'object') {
options.zlib = {};
}
if (typeof options.level === 'number' && options.level >= 0) {
options.zlib.level = options.level;
delete options.level;
} else if (typeof options.zlib.level !== 'number') {
options.zlib.level = 1;
}
Transform.call(this, options);
this.offset = 0;
this.files = [];
this._finalize = false;
this._finalized = false;
this._processing = false;
};
inherits(ZipStream, Transform);
ZipStream.prototype._afterAppend = function(file) {
this.files.push(file);
this._processing = false;
if (this._finalize) {
this.finalize();
}
};
ZipStream.prototype._appendBuffer = function(source, data, callback) {
var self = this;
var file = data;
file.offset = self.offset;
self.write(headers.encode('file', file));
function onend() {
self.write(headers.encode('fileDescriptor', file));
self._afterAppend(file);
callback(null);
}
if (file.store) {
file.uncompressedSize = source.length;
file.compressedSize = file.uncompressedSize;
file.crc32 = util.crc32(source).digest();
self.write(source);
onend();
} else {
var processStream = self._newProcessStream(file.store);
processStream.on('error', callback);
processStream.on('end', function() {
file.crc32 = processStream.digest;
file.uncompressedSize = processStream.rawSize;
file.compressedSize = processStream.compressedSize || processStream.rawSize;
onend();
});
processStream.pipe(self, { end: false });
processStream.end(source);
}
};
ZipStream.prototype._appendStream = function(source, data, callback) {
var self = this;
var file = data;
file.offset = self.offset;
self.write(headers.encode('file', file));
function onend() {
self.write(headers.encode('fileDescriptor', file));
self._afterAppend(file);
callback(null);
}
var processStream = self._newProcessStream(file.store);
processStream.on('error', callback);
processStream.on('end', function() {
file.crc32 = processStream.digest;
file.uncompressedSize = processStream.rawSize;
file.compressedSize = processStream.compressedSize || processStream.rawSize;
onend();
});
processStream.pipe(self, { end: false });
source.pipe(processStream);
};
ZipStream.prototype._emitErrorCallback = function(err, data) {
if (err) {
this.emit('error', err);
}
};
ZipStream.prototype._newProcessStream = function(store) {
var process;
if (store) {
process = new ChecksumStream();
} else {
process = new DeflateRawChecksum(this.options.zlib);
}
return process;
};
ZipStream.prototype._normalizeFileData = function(data) {
data = util.defaults(data, {
type: 'file',
name: null,
date: null,
store: false,
comment: ''
});
data.name = util.sanitizeFilePath(data.name);
if (typeof data.lastModifiedDate !== 'number') {
data.lastModifiedDate = util.dosDateTime(data.date, this.options.forceUTC);
}
if (this.options.zlib && this.options.zlib.level === 0) {
data.store = true;
}
data.flags = (1 << 3);
data.compressionMethod = data.store ? 0 : 8;
data.uncompressedSize = 0;
data.compressedSize = 0;
return data;
};
ZipStream.prototype._normalizeSource = function(source) {
if (typeof source === 'string') {
return new Buffer(source);
} else if (util.isStream(source) && !source._readableState) {
var normalized = new PassThrough();
source.pipe(normalized);
return normalized;
}
return source;
};
ZipStream.prototype._transform = function(chunk, encoding, callback) {
callback(null, chunk);
};
ZipStream.prototype._writeCentralDirectory = function() {
var files = this.files;
var comment = this.options.comment;
var cdoffset = this.offset;
var cdsize = 0;
var centralDirectoryBuffer;
for (var i = 0; i < files.length; i++) {
var file = files[i];
centralDirectoryBuffer = headers.encode('centralDirectory', file);
this.write(centralDirectoryBuffer);
cdsize += centralDirectoryBuffer.length;
}
var centralDirectoryFooterData = {
directoryRecordsDisk: files.length,
directoryRecords: files.length,
centralDirectorySize: cdsize,
centralDirectoryOffset: cdoffset,
comment: comment
};
this.write(headers.encode('centralFooter', centralDirectoryFooterData));
};
ZipStream.prototype.entry = function(source, data, callback) {
if (typeof callback !== 'function') {
callback = this._emitErrorCallback.bind(this);
}
if (this._processing) {
callback(new Error('already processing an entry'));
return;
}
if (this._finalize || this._finalized) {
callback(new Error('entry after finalize()'));
return;
}
data = this._normalizeFileData(data);
if (data.type !== 'file') {
callback(new Error('only "file" entries are currently supported'));
return;
}
if (typeof data.name !== 'string' || data.name.length === 0) {
callback(new Error('filename must be a non-empty string value'));
return;
}
this._processing = true;
source = this._normalizeSource(source);
if (Buffer.isBuffer(source)) {
this._appendBuffer(source, data, callback);
} else if (util.isStream(source)) {
this._appendStream(source, data, callback);
} else {
this._processing = false;
callback(new Error('input source must be valid Stream or Buffer instance'));
return;
}
};
ZipStream.prototype.finalize = function() {
if (this._processing) {
this._finalize = true;
return;
}
this._writeCentralDirectory();
this._finalized = true;
this.end();
};
ZipStream.prototype.write = function(chunk, cb) {
if (chunk) {
this.offset += chunk.length;
}
return Transform.prototype.write.call(this, chunk, cb);
};

4

package.json
{
"name": "zip-stream",
"version": "0.1.0-alpha",
"version": "0.1.0",
"description": "a streaming zip generator.",

@@ -32,3 +32,3 @@ "homepage": "https://github.com/ctalkington/node-zip-stream",

"readable-stream": "~1.1.9",
"iconv-lite" : "~0.2.11"
"lodash.defaults": "~2.4.1"
},

@@ -35,0 +35,0 @@ "devDependencies": {

@@ -1,6 +0,6 @@

# zip-stream v0.1.0-alpha [![Build Status](https://secure.travis-ci.org/ctalkington/node-zip-stream.png?branch=master)](http://travis-ci.org/ctalkington/node-zip-stream)
# zip-stream v0.1.0 [![Build Status](https://secure.travis-ci.org/ctalkington/node-zip-stream.png?branch=master)](http://travis-ci.org/ctalkington/node-zip-stream)
zip-stream is a streaming zip generator. It was built to be a successor to [node-zipstream](https://github.com/wellawaretech/node-zipstream). Dependencies are kept to a minimum through the use of many of node's built-in modules including the use of zlib module for compression.
zip-stream is a streaming zip generator. It was built to be a successor to [zipstream](https://npmjs.org/package/zipstream). Dependencies are kept to a minimum through the use of many of node's built-in modules including the use of zlib module for compression.
## Install
### Install

@@ -13,5 +13,79 @@ ```bash

### Usage
This module is meant to be wrapped internally by other modules and therefore lacks any queue management. This means you have to wait until the previous entry has been fully consumed to add another. Nested callbacks should be used to add multiple entries. There are modules like [async](https://npmjs.org/package/async) that ease the so called "callback hell".
If you want a module that handles entry queueing and much more, you should check out [archiver](https://npmjs.org/package/archiver) which uses this module internally.
```js
var packer = require('zip-stream');
var archive = new packer(); // OR new packer(options)
archive.on('error', function(err) {
throw err;
});
// pipe archive where you want it (ie fs, http, etc)
// listen to the destination's end, close, or finish event
archive.entry('string contents', { name: 'string.txt' }, function(err) {
if (err) throw err;
archive.finalize();
});
```
### Instance API
#### entry(input, data, callback(err))
Appends an input source (text string, buffer, or stream) to the instance. When the instance has received, processed, and emitted the input, the callback is fired.
#### finalize()
Finalizes the instance. You should listen to the destination stream's `end`/`close`/`finish` event to know when all output has been safely consumed.
### Instance Options
#### comment `string`
Sets the zip comment.
#### forceUTC `boolean`
If true, forces the file date and time to UTC. Helps with testing across timezones.
#### zlib `object`
Passed to node's [zlib](http://nodejs.org/api/zlib.html#zlib_options) module to control compression. Options may vary by node version.
### Entry Data
#### name `string` `required`
Sets the entry name including internal path.
#### type `string`
Sets the entry type. Defaults to `file`. (allowed types to be expanded in future)
#### date `string|Date`
Sets the entry date. This can be any valid date string or instance. Defaults to current time in locale.
#### store `boolean`
If true, entry contents will be stored without compression.
#### comment `string`
Sets the entry comment.
#### mode `number`
Sets the entry permissions. (experimental)
## Things of Interest
- [Changelog](https://github.com/ctalkington/node-zip-stream/blob/master/CHANGELOG)
- [Changelog](https://github.com/ctalkington/node-zip-stream/releases)
- [Contributing](https://github.com/ctalkington/node-zip-stream/blob/master/CONTRIBUTING.md)

@@ -22,2 +96,2 @@ - [MIT License](https://github.com/ctalkington/node-zip-stream/blob/master/LICENSE-MIT)

Concept inspired by Antoine van Wel's [node-zipstream](https://github.com/wellawaretech/node-zipstream).
Concept inspired by Antoine van Wel's [zipstream](https://npmjs.org/package/zipstream) module, which is no longer being updated.

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc