#anyone know how to extract files with JSZip or point me to an understandable example?
37 messages · Page 1 of 1 (latest)
show what you've tried
I believe this is the part you are looking for: https://stuk.github.io/jszip/documentation/api_jszip/file_name.html
(the library uses the horribly outdated var in their doc. Use const instead.)
FWIW all the zip libraries are garbage... I've used adm-zip for a few projects. I had tried jszip at one point but it was busted for what I needed.
do you really need it client-side?
current state of my function:
// Import the storage archive
function importStorageArchive(storageObj){
console.log("Importing storage archive...");
let loadfile = document.getElementById('importObj').files[0];
let freader = new FileReader();
console.log("364-import file: ", loadfile.name);
// Clear the storage viewer and storage data
storageObj.clearView();
storageObj.fileIndex = [];
alert("Importing files....\nThis may take some time.\nPress OK to start.");
// File read handler
freader.onload = function(ev) {
JSZip.loadAsync(ev.target.result).then(function(zipcontainer) {
let i = 0;
let tflg = 0;
for(let [filename, fileObj] of Object.entries(zipcontainer.files)) {
console.log(filename);
console.log(fileObj);
if (tflg) { // File content
const fileData = new arrayBuffer(await fileObj.arrayBuffer());
fileIndex[idx].push(fileData);
tflg = 0; // Next file is header
}else{ // File header
const fileData = new arrayBuffer(await fileObj.arrayBuffer());
fileIndex.push(JSON.parse(fileData));
tflg = 1; // Next file is content
}
}
}).catch(function(err) {
alert("Failed to open file: ", loadfile.name);
})
}
freader.readAsArrayBuffer(loadfile);
}```
unfortunately for what I am trying to do, yes. I am working on an emulator that runs in a browser locally. This is not the best way to implement it perhaps as there are limitations to accessing the local filesystem, but for now I am having to work with it as is. Long term might be better to build an Electron app or something.
I am currently manipulating things in memory (programs are only kb's so not an issue) and as a workaround writing out the session state to a zip file. The problem is how to read it back in. I can open the zip file, get the name of the enclosed compressed file. The trick is how to extract the compressed content.
thanks for that and I think this is along the right line, and thisa is probably the bit I need:
zip.file("amount.txt").async("arraybuffer")
I do have the zip file object so can do fileObj.async("arraybuffer", )but, to where is the content extracted? "arraybuffer" seems to be a type parameter rather than an array object?
I assume the file is not actually unzipped, but rather the file's contents are returned by the .file method instead. Which makes sense, as you don't have control over the user's file system in a client side context.
Also, check #faq on how to share code.
a promise of an ArrayBuffer containing €15 encoded as utf8
return value
three backticks for code blocks. add "js" for syntax highlighting. see #faq.
thanks for the note on sharing code. I would have thought that the code [<>] tool would have automatically applied the correct markup. Have adjusted it accordingly.
thanks. I read that. I know what a promise, Array buffer and utf8 is but what on earth is "€15 encoded"? Please for give my ignorance and I will re-read, but where is this "return value" returned?
is this saying that in the statement:
return zip.file("content.txt").async("string");```
zip.file is the actual returned extracted uncompressed data? (In string format in this case) ?
So can I do something like:
```js
myArray = [];
myArray = zip.file("content.txt").async("arrayBuffer");```
But what's "content.txt" in the above example statement?
(OK, I think I have the filename instance within the ZIP?)
sorry, I don't understand the purpose of the link?
You need to await promises
thought that was too easy....
It’s still easy
Ok, so looking at this example again:
JSZip.loadAsync(body).then(function (zip) {
return zip.file("content.txt").async("string");
}).then(function (text) {
console.log(text);
});```
So is the 'awaiting part the then statement? If so, is the variable 'text' where the return value of the string get put? (Here being output to the console)
My god. Is that really an example they give?
Yes. Right at the bottom of the page: https://stuk.github.io/jszip/documentation/howto/read_zip.html
Create .zip files using JavaScript. Provides a simple API to place any content generated by JavaScript into a .zip file for your users.
.then() is related to await, yes. You should read through this whole section: https://javascript.info/async
The local example is just:
JSZip.loadAsync(data).then(function () {
// ...
});```
Anyway thanks for the link.
at a keyboard now, so I can actually type...
I find the whole promises syntax so convoluted. I kind of understand the principle, but struggle unravelling all the nested functions and what passes what objects/values to what.
this is just nasty... no idea why they have that return there... re-written with async/await would be:
const zip = await JSZip.loadAsync(body);
const text = await zip.file('content.txt').async('string');
console.log(text);
this is why you should learn async/await
.then() and .catch() is nasty
const myArray = await zip.file("content.txt").async("arrayBuffer");
ok, that looks simpler. The process waits until the promise it complete before assigning the result to const text before moving on?
correct. read through the link. it'll help you a great deal.
thanks for the link and taking the time and trouble to explain.
Btw, if you're convinced that there's no decent library for what you want to do, but there's a command line tool, you can just write your own wrapper. There's packages like execa which help you do this fairly easily.