Harish
Harish

Reputation: 101

How do you unzip a string in React

The response from an API is a zipped folder of three small files. I've got this response in a string. I want to display the contents of one of the files and store another in the browser's local storage for later use. I'm having trouble unzipping. How do I do this without accessing the file system?

fetch(URL,
        {
            method: 'GET',
            headers: {
                'API-KEY': API_Key,
            },
        }).then(response => response.text()).then(zippedFolderAsString => {
            // Need to unzip
        });

Upvotes: 1

Views: 5178

Answers (1)

Harish
Harish

Reputation: 101

Here's how I solved it. I used JSZip which can take blobs as input as opposed to the path to a file like most other libraries.

import JSZip from 'jszip';
...
var new_zip = new JSZip();
new_zip.loadAsync(zippedFolderAsBlob).then(async function(zipped) {
    var jsonFile = await zipped.file("theJsonFile.json").async("text");
})

Upvotes: 6

Related Questions