Skip to content

Commit

Permalink
Open methods - only stream up to length (#310)
Browse files Browse the repository at this point in the history
* Open methods - only stream up to length

* bump version
  • Loading branch information
ZJONSSON authored May 4, 2024
1 parent 6a165e5 commit 637df97
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 10 deletions.
8 changes: 7 additions & 1 deletion lib/Open/directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,13 @@ module.exports = function centralDirectory(source, options) {
vars.comment = comment;
vars.type = (vars.uncompressedSize === 0 && /[\/\\]$/.test(vars.path)) ? 'Directory' : 'File';
vars.stream = function(_password) {
return unzip(source, vars.offsetToLocalFileHeader,_password, vars);
var totalSize = 30
+ 10 // add an extra buffer
+ (vars.extraFieldLength || 0)
+ (vars.fileNameLength || 0)
+ vars.compressedSize;

return unzip(source, vars.offsetToLocalFileHeader,_password, vars, totalSize);
};
vars.buffer = function(_password) {
return BufferStream(vars.stream(_password));
Expand Down
14 changes: 9 additions & 5 deletions lib/Open/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ module.exports = {
var source = {
stream: function(offset, length) {
var stream = Stream.PassThrough();
stream.end(buffer.slice(offset, length));
var end = length ? offset + length : undefined;
stream.end(buffer.slice(offset, end));
return stream;
},
size: function() {
Expand All @@ -19,8 +20,9 @@ module.exports = {
},
file: function(filename, options) {
var source = {
stream: function(offset,length) {
return fs.createReadStream(filename,{start: offset, end: length && offset+length});
stream: function(start,length) {
var end = length ? start + length : undefined;
return fs.createReadStream(filename,{start, end});
},
size: function() {
return new Promise(function(resolve,reject) {
Expand All @@ -46,8 +48,9 @@ module.exports = {
var source = {
stream : function(offset,length) {
var options = Object.create(params);
var end = length ? offset + length : '';
options.headers = Object.create(params.headers);
options.headers.range = 'bytes='+offset+'-' + (length ? length : '');
options.headers.range = 'bytes='+offset+'-' + end;
return request(options);
},
size: function() {
Expand Down Expand Up @@ -83,7 +86,8 @@ module.exports = {
var d = {};
for (var key in params)
d[key] = params[key];
d.Range = 'bytes='+offset+'-' + (length ? length : '');
var end = length ? offset + length : '';
d.Range = 'bytes='+offset+'-' + end;
return client.getObject(d).createReadStream();
}
};
Expand Down
4 changes: 2 additions & 2 deletions lib/Open/unzip.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ var parseExtraField = require('../parseExtraField');
var parseDateTime = require('../parseDateTime');
var parseBuffer = require('../parseBuffer');

module.exports = function unzip(source,offset,_password, directoryVars) {
module.exports = function unzip(source, offset, _password, directoryVars, length) {
var file = PullStream(),
entry = Stream.PassThrough();

var req = source.stream(offset);
var req = source.stream(offset, length);
req.pipe(file).on('error', function(e) {
entry.emit('error', e);
});
Expand Down
2 changes: 1 addition & 1 deletion lib/PullStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ PullStream.prototype.stream = function(eof,includeEof) {
packet = self.buffer.slice(0,eof);
self.buffer = self.buffer.slice(eof);
eof -= packet.length;
done = !eof;
done = done || !eof;
} else {
var match = self.buffer.indexOf(eof);
if (match !== -1) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "unzipper",
"version": "0.11.4",
"version": "0.11.5",
"description": "Unzip cross-platform streaming API ",
"author": "Evan Oxfeld <[email protected]>",
"contributors": [
Expand Down

0 comments on commit 637df97

Please sign in to comment.