-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how to use generateInternalStream? #522
Comments
I have the same problem here. did you solve? |
We made it using TypeScript and Cordova for an Android app: async function writeZipFile(dirEntry: DirectoryEntry, zip: JSZip, fileName: string): Promise<void> {
return new Promise((ok, err) => {
dirEntry.getFile(
fileName,
{ create: true, exclusive: false },
function (fileEntry: FileEntry) {
fileEntry.createWriter((fileWriter: FileWriter) => {
let chunksToAdd: Blob[] = [];
let flushWriteHandler: ReturnType<typeof setTimeout> = null;
const stream = zip.generateInternalStream({
type: "blob",
compression: "DEFLATE",
compressionOptions: {
// 1: Best speed
// ...
// 9: Best compression
level: 5,
},
streamFiles: true,
});
fileWriter.onwrite = () => {
stream.resume();
};
fileWriter.onerror = (e: ProgressEvent<EventTarget>) => {
console.error(`Failed zip file write: ${e.toString()}`);
// Here you should delete the file
};
stream.on("data", (chunk: Blob, _metadata: JSZip.JSZipMetadata) => {
stream.pause();
chunksToAdd.push(chunk);
if (!flushWriteHandler) {
flushWriteHandler = setTimeout(() => {
if (chunksToAdd.length === 0) {
return;
}
const blobToAdd = new Blob(chunksToAdd);
chunksToAdd = [];
flushWriteHandler = null;
fileWriter.write(blobToAdd);
}, 0);
}
});
stream.on("end", () => {
function finishIfReady() {
if (fileWriter.readyState === FileWriter.DONE) {
ok();
} else {
setTimeout(finishIfReady, 100);
}
}
setTimeout(finishIfReady, 0);
});
stream.on("error", (e: Error) => {
stream.pause();
console.error(`Failed zip stream: ${e.toString()}`);
// Here you should delete the file
});
stream.resume();
});
},
function (error: FileError) {
err(error);
}
);
});
}
// Use
await writeZipFile(dirEntry, zip, fileName); Our issue was that So to force an order when adding data to the file chunk by chunk, I'm not sure if this is the case for browser, but may be similar. |
Hi,
I want to use jszip in browser with some large file.
For now I use
generateInternalStream
and write zip data to chrome sandbox file system, then trigger a click event to download the zip file, however, the generated zip file seems broken.here is the code:
Thank you!
The text was updated successfully, but these errors were encountered: