which-electron/src/zip.js

42 lines
1.0 KiB
JavaScript

const Seven = require("node-7z");
const sevenBin = require('7zip-bin').path7za;
const path = require("path");
const fs = require("fs");
module.exports = {
readFileContents: function(archive, filepath, cb) {
// TODO: Create a new temp directory
let stream = Seven.extract(archive, "/tmp", {
recursive: true,
$cherryPick: filepath,
$bin: sevenBin
});
let fn = path.basename(filepath);
stream.on("end", ()=>{
cb(fs.readFileSync(`/tmp/${fn}`, {encoding: 'utf8'}))
});
},
extractSomeFiles: function(archive, list, cb) {
let dir = fs.mkdtempSync('/tmp/which-electron')
let stream = Seven.extract(archive, dir, {
$cherryPick: list,
$bin: sevenBin
})
stream.on('end', ()=>{
cb(dir)
})
},
listFileContents: function(archive, cb) {
let zip = Seven.list(archive, {
$bin: sevenBin
});
let entries = [];
zip.on("data", (data) => {
entries.push(data);
});
zip.on("end", () => {
cb(entries);
});
},
};