How to list files inside a ZIP archive using JSZip
In JSZip, you can list files inside a ZIP archive using zip.files
which is an Object with key = filename and value = file metadata object.
JSZip.loadAsync(fileContent).then(function(zip) {
for(let [filename, file] of Object.entries(zip.files)) {
// TODO Your code goes here
console.log(filename);
}
}).catch(function(err) {
console.error("Failed to open", filename, " as ZIP file:", err);
})
Full example
This is based on our post on How to read local ZIP in HTML5/Javascript using JSZip
<html>
<body>
<input type="file" id="myfile" onchange="onMyfileChange(this)" />
<script src="https://unpkg.com/[email protected]/dist/jszip.js" type="text/javascript"></script>
<script type="text/javascript">
function onMyfileChange(fileInput) {
if(fileInput.files[0] == undefined) {
return ;
}
var filename = fileInput.files[0].name;
// var filesize = fileInput.files[0].size;
var reader = new FileReader();
reader.onload = function(ev) {
JSZip.loadAsync(ev.target.result).then(function(zip) {
console.log(zip)
}).catch(function(err) {
console.error("Failed to open", filename, " as ZIP file");
})
};
reader.onerror = function(err) {
console.error("Failed to read file", err);
}
reader.readAsArrayBuffer(fileInput.files[0]);
}
</script>
</body>
</html>