jszip icon indicating copy to clipboard operation
jszip copied to clipboard

How to read the content of the unzipped file ?

Open PrashantHavanagi opened this issue 7 years ago • 1 comments

Hi,

I am pretty new to JSZip and I am able to extract all the zipped files in a .pptx using the example given in: [(http://stuk.github.io/jszip/documentation/examples/read-local-file-api.html)]

I believe this is the code which prints the unzipped path/filename. I would like to read the actual content of the files. zip.forEach(function (relativePath, zipEntry) { $fileContent.append($("<li>", { text : zipEntry.name })); Any way I could do this ?

ppt/slideLayouts/slideLayout1.xml ppt/slideLayouts/_rels/slideLayout1.xml.rels ppt/slides/slide1.xml ppt/slides/_rels/slide1.xml.rels

These are some of my files, suppose I want the actual content of ppt/slides/slide1.xml. Is there any way?

Thank you !

PrashantHavanagi avatar Mar 26 '18 20:03 PrashantHavanagi

To get the actual content of a file called 'ppt/slides/slide1.xml', try using

zip.files['ppt/slides/slide1.xml'].async('blob').then(function(blob) {
    /*do something with 'blob' (the data)*/
}

You could turn that into zip.files[filename] and run it inside a function. I'm not sure about .pptx files but if you know that the folder ppt/slides is always going to be there you could do something like:

var slidesobj = {};
zip.forEach(function (relativePath, zipEntry) {
    if (relativePath.indexOf('ppt/slides/') != -1) {
        zip.files[relativePath].async('blob').then(function(data) {
            //https://stuk.github.io/jszip/documentation/api_zipobject/async.html
            var name = relativePath.split('/').pop();
            //I'm pretty sure this splits the path by the slashes, then picks the last part.
            //e.g. 'ppt/slides/slide1.xml' -> ['ppt', 'slides', 'slide1.xml'] -> 'slide1.xml'
            slidesobj[name] = data;
        }
    }
});
//do something with the slides in slideobj

turquoise-turtle avatar May 29 '18 09:05 turquoise-turtle