In my Angular 7 and Typescript project, I am faced with the challenge of reading a json file from within a zip file. Although I am able to successfully display the correct json output on the console, I am struggling to return this json data from the function.
My approach involves downloading the zip file using a separate service that utilizes an observable. Within this observable, I make use of a promise provided by the JSZip library to extract the zip file and access the json data. Below is the code snippet for the function responsible for this task:
extract-zip.service.ts:
async getJSONfromZip(zip_filename: string, json_filename: string) {
return this.fileService.downloadResource(zip_filename)
.pipe(
map (async (data: any) => {
return JSZip.loadAsync(data)
.then((zip: any) => {
Object.keys(zip.files)
// iterate over all containing files
.forEach((filename) => {
// look for json file
if (filename === json_filename) {
return zip.files[filename].async('string')
.then((fileData: any) => {
console.log(fileData);
return fileData;
});
}
});
});
}))
.toPromise();
}
Although the function currently returns a Promise, my goal is to actually retrieve and return the content of the json file (fileData). Despite trying various approaches such as converting it to a Promise or utilizing and returning a class member variable, I have yet to find a solution.