Im trying to read a zip file and then parse a json file at the root of the zip.
The json file is called manifest.json and will be called this in every zip file i read.
Currently i have the following function
function getFileContents(directory){
// reading archives
var zip = new AdmZip(directory);
var zipEntries = zip.getEntries(); // an array of ZipEntry records
zipEntries.forEach(function (zipEntry) {
if (zipEntry.entryName == "manifest.json") {
console.log('getData', zipEntry.getData());
console.log('data',zipEntry.data.toString('utf8'));
}
});
}
However i get the following exception in the console
getData <Buffer ff fe 7b 00 0a 00 20 00 20 00 22 00 62 00 75 00 69 00 6c 00 64 0
0 22 00 3a 00 20 00 22 00 34 00 2e 00 38 00 2e 00 37 00 32 00 31 00 39 00 22 00
2c 00 0a ...>
TypeError: Cannot call method 'toString' of undefined
at c:\direc\Custom_Modules\readZipFileModule\readZipFileModule.js:18:46
at Array.forEach (native)
Of the back of this ive tried:
function getFileContents(directory){
// reading archives
var zip = new AdmZip(directory);
var zipEntries = zip.getEntries(); // an array of ZipEntry records
zipEntries.forEach(function (zipEntry) {
if (zipEntry.entryName == "manifest.json") {
var decompressedData = zip.readFile(zipEntry);
var data = zip.readAsText(zipEntry)
console.log(JSON.parse(data));
}
});
}
if i console.log data i get:
??{
" b u i l d " : " 4 . 8 . 7 2 1 9 " ,
" b r a n c h " : " s t e p h e n " ,
" t i m e s t a m p " : " 1 5 - 0 1 - 2 0 1 4 0 9 : 0 6 : 2 7 "
}
That is the correct data from the file however there are not spaces inbetween every character. But when i try and parse it it obviously throws an error about '??' Where are the question marks coming from? I don't fully understand how to use adm-zip correctly what exactly am i doing wrong to read a json file from a zip in nodejs? It doesn't need to save the file just parse its data into an object.
Thanks for any and all assistance with this.