How can I compress/decompress a buffer via yazl? https://www.npmjs.com/package/yazl
I don't want to create/save a zip file but just compress a buffer and forward it to another service. Any sample code would be helpful
var yazl = require("yazl");
var buf = fs.readFileSync(__dirname + '/testArchive.txt');
var zipfile = new yazl.ZipFile();
zipfile.addBuffer(buf, "TEMPENTRY");
zipfile.end();
So has it been archived at this point? How to I inflate that buffer by using yauzl then?
This kind of function should help, manually retrieving the stream's packets, then concatenating them into one single Buffer
:
function stream2buffer(readableStream) {
return new Promise((resolve, reject) => {
const chunks = [];
readableStream.on('data', (chunk) => {
chunks.push(chunk);
}).on('end', () => {
resolve(Buffer.concat(chunks));
}).on('error', (err) => {
reject(err);
});
});
}
Then you can use it on yazl.ZipFile.outputStream
, and on the other side on the readable stream provided by yauzl.ZipFile.openReadStream
's callback.
What I meant is more like using this function on yazl
's outputStream
, not on the source files you want to compress. More like this:
function zipper(mapping) { // mapping is expected to be a Map here
const handler = new yazl.ZipFile(); // yazl = require('yazl');
for (mapItem of mapping) {
if (typeof mapItem[1] === 'string' || mapItem[1] instanceof String) {
handler.addFile(mapItem[1], mapItem[0]);
} else if (mapItem[1] instanceof Buffer) {
handler.addBuffer(mapItem[1], mapItem[0]);
} else if (mapItem[1] instanceof stream.Readable) { // stream = require('stream');
handler.addReadStream(mapItem[1], mapItem[0]);
} else throw new Error('unsupported type');
}
handler.end();
return stream2buffer(handler.outputStream);
}
And it's alike for the unzipping:
function unzipper(buffer) {
return new Promise((resolve, reject) => {
unzippedContents = {};
yauzl.fromBuffer(buffer, {lazyEntries: true}, (err, zip) => { // yauzl = require('yauzl')
if (err) return reject(err);
zip.on('entry', (entry) => {
if (/\/$/.test(entry.fileName)) return zip.readEntry(); // no directories
zip.openReadStream(entry, async (err, rs) => {
try {
if (err) throw err;
unzippedContents[entry.fileName] = await stream2buffer(rs);
} catch (err) {
zip.close();
return reject(err);
}
zip.readEntry();
});
}).on('end', () => {
zip.close();
resolve(unzippedContents);
});
zip.readEntry(); // start the process
});
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.