I am trying read list of files stored in zip file and parse it according to the requirement.
zip Structure
folder 1 -> file1.json
folder 2 -> file2.json
to an object
{
folder1: {file1: {file1json Data}},
folder2: {file2: {file1json Data}}
}
How to wait in
lis.reduce till
return oFileReader.readFile(new File([current[1]], path[1]))
.then(oResult => {
accumulator[path[0]].data = oResult
})
returns result and store it in accumulator object
readZipFile: function(oFile) {
return new Promise(function(resolve, reject) {
return JSZip.loadAsync(oFile).then(function(zip) {
var entries = Object.keys(zip.files).map(function(name) {
return zip.files[name];
});
var listOfPromises = entries.map(function(entry) {
return entry.async("blob").then(function(data) {
return [entry.name, data];
});
});
var promiseOfList = Promise.all(listOfPromises);
promiseOfList.then(function(list) {
var result = list.reduce(function(accumulator, current) {
let currentName = current[0];
let path = currentName.split("/");
if (currentName.includes('.json')) {
let oFileReader = new new FileReader();
return oFileReader.readFile(new File([current[1]], path[1]))
.then(oResult => {
accumulator[path[0]].data = oResult
})
}
return accumulator;
}, {});
resolve(result);
});
});
}.bind(this));
},