Skip to content

Commit

Permalink
Replace fstream with fs-extra
Browse files Browse the repository at this point in the history
  • Loading branch information
ZJONSSON committed Jun 8, 2024
1 parent 1149855 commit b1abffa
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
14 changes: 11 additions & 3 deletions lib/Open/directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const unzip = require('./unzip');
const BufferStream = require('../BufferStream');
const parseExtraField = require('../parseExtraField');
const path = require('path');
const Writer = require('fstream').Writer;
const fs = require('fs-extra');
const parseDateTime = require('../parseDateTime');
const parseBuffer = require('../parseBuffer');
const Bluebird = require('bluebird');
Expand Down Expand Up @@ -153,7 +153,7 @@ module.exports = function centralDirectory(source, options) {
// make sure path is normalized before using it
opts.path = path.resolve(path.normalize(opts.path));
return vars.files.then(function(files) {
return Bluebird.map(files, function(entry) {
return Bluebird.map(files, async function(entry) {
if (entry.type == 'Directory') return;

// to avoid zip slip (writing outside of the destination), we resolve
Expand All @@ -163,7 +163,15 @@ module.exports = function centralDirectory(source, options) {
if (extractPath.indexOf(opts.path) != 0) {
return;
}
const writer = opts.getWriter ? opts.getWriter({path: extractPath}) : Writer({ path: extractPath });

let writer;

if (opts.getWriter) {
writer = opts.getWriter({path: extractPath});
} else {
await fs.ensureDir(path.dirname(extractPath));
writer = fs.createWriteStream(extractPath);
}

return new Promise(function(resolve, reject) {
entry.stream(opts.password)
Expand Down
13 changes: 10 additions & 3 deletions lib/extract.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = Extract;

const Parse = require('./parse');
const Writer = require('fstream').Writer;
const fs = require('fs-extra');
const path = require('path');
const stream = require('stream');
const duplexer2 = require('duplexer2');
Expand All @@ -13,7 +13,7 @@ function Extract (opts) {
const parser = new Parse(opts);

const outStream = new stream.Writable({objectMode: true});
outStream._write = function(entry, encoding, cb) {
outStream._write = async function(entry, encoding, cb) {

if (entry.type == 'Directory') return cb();

Expand All @@ -27,7 +27,14 @@ function Extract (opts) {
return cb();
}

const writer = opts.getWriter ? opts.getWriter({path: extractPath}) : Writer({ path: extractPath });
let writer;

if (opts.getWriter) {
writer = opts.getWriter({path: extractPath});
} else {
await fs.ensureDir(path.dirname(extractPath));
writer = fs.createWriteStream(extractPath);
}

entry.pipe(writer)
.on('error', cb)
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
"big-integer": "^1.6.17",
"bluebird": "~3.4.1",
"duplexer2": "~0.1.4",
"fstream": "^1.0.12",
"fs-extra": "^11.2.0",
"graceful-fs": "^4.2.2"
},
"devDependencies": {
"@eslint/js": "^9.2.0",
"aws-sdk": "^2.77.0",
"aws-sdk": "^2.1636.0",
"dirdiff": ">= 0.0.1 < 1",
"eslint": "^9.2.0",
"globals": "^15.2.0",
Expand Down

0 comments on commit b1abffa

Please sign in to comment.