Last active
October 12, 2017 06:35
-
-
Save izinin/94fd1b78a1a39a1518a2b7dcc7d2dcaf to your computer and use it in GitHub Desktop.
JSZip (https://stuk.github.io/jszip/) use for updating a zip archive. If the archive contains FName, its content will be updated, otherwise new item FName will be added
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var JSZip = require("jszip"); | |
var fs = require('fs'); | |
module.exports = { | |
// Replaces or adds 'replacementFname' into archive 'zipFname' with | |
// content obtained from 'contentFname' file. | |
// Creates new file 'updated.zip' in the current directory | |
// for example : | |
// xxx.updatezip('test_suite.zip', 'scenario.xml', 'rooster/scenarios/rooster.xml') | |
updatezip: function(zipFname, contentFname, replacementFname){ | |
var scenario = new Promise((resolve, reject) =>{ | |
fs.readFile(contentFname, (err, data)=>{ | |
if(err) reject(err) | |
else resolve(data) | |
}) | |
}); | |
var suite = new Promise((resolve, reject) =>{ | |
fs.readFile(zipFname, (err, data)=>{ | |
if(err) reject(err) | |
else resolve(data) | |
}) | |
}); | |
suite.then(data => { | |
return JSZip.loadAsync(data); | |
}).then(zip => { | |
//zip.remove('rooster/scenarios/rooster.xml'); | |
return zip.file(replacementFname, scenario); | |
}).then(zip => { | |
return zip.generateAsync({type:"nodebuffer"}) | |
}).then(content => { | |
fs.writeFileSync('./updated.zip', content); | |
console.log("done!"); | |
}).catch(err => { | |
console.log(err); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment