Skip to content

Commit

Permalink
Retry stream if not big enough
Browse files Browse the repository at this point in the history
  • Loading branch information
ZJONSSON committed May 11, 2024
1 parent bd1baac commit 51692b4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/Open/unzip.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ var parseExtraField = require('../parseExtraField');
var parseDateTime = require('../parseDateTime');
var parseBuffer = require('../parseBuffer');

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

var req = source.stream(offset, length);
req.pipe(file).on('error', function(e) {
Expand All @@ -35,6 +35,17 @@ module.exports = function unzip(source, offset, _password, directoryVars, length
['extraFieldLength', 2],
]);

var localSize = 30
+ 100 // add extra padding
+ (vars.extraFieldLength || 0)
+ (vars.fileNameLength || 0)
+ vars.compressedSize;

if (localSize > length) {
entry.emit('streamRetry', localSize);
return unzip(source, offset, _password, directoryVars, localSize, entry);
}

vars.lastModifiedDateTime = parseDateTime(vars.lastModifiedDate, vars.lastModifiedTime);

const fileName = await file.pull(vars.fileNameLength);
Expand Down
15 changes: 15 additions & 0 deletions test/office-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var path = require('path');
var unzip = require('../');
var il = require('iconv-lite');
var Promise = require('bluebird');
var NoopStream = require('../lib/NoopStream');

test("get content a docx file without errors", async function (t) {
var archive = path.join(__dirname, '../testData/office/testfile.docx');
Expand All @@ -18,4 +19,18 @@ test("get content a xlsx file without errors", async function (t) {

const directory = await unzip.Open.file(archive);
await Promise.all(directory.files.map(file => file.buffer()));
});

test("stream retries when the local file header indicates bigger size than central directory", async function (t) {
var archive = path.join(__dirname, '../testData/office/testfile.xlsx');
let retries = 0, size;
const directory = await unzip.Open.file(archive, {padding: 10});
const stream = directory.files[0].stream();
stream.on('streamRetry', _size => {
retries += 1;
size = _size;
});
await new Promise(resolve => stream.pipe(NoopStream()).on('finish', resolve));
t.ok(retries === 1, 'retries once');
t.ok(size > 0, 'size is set');
});

0 comments on commit 51692b4

Please sign in to comment.