Skip to content

Puzzled with uploading a big zip file in chunks to back-end #198

Answered by 101arrowz
alexandis asked this question in Q&A
Discussion options

You must be logged in to vote

You should probably make use of the WHATWG streams API to maximize performance here. If fetch supported ReadableStream in all browsers you could do the entire thing with WHATWG streams, but as it does not, you can do it as follows.

First you need a way to read the file into a zip entry. Since you're using level 0 compression, I've used a ZipPassThrough instead of ZipDeflate (it's a bit faster).

import { Zip, ZipPassThrough } from 'fflate';

const addFileToZip = async (zip: Zip, file: File) => {
  const zipEntry = new ZipPassThrough(file.name.replace(/^\/+/, ''));
  zip.add(zipEntry);
  const reader = file.stream().getReader();

  while (true) {
    const { done, value } = await reader.read()

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@101arrowz
Comment options

Comment options

You must be logged in to vote
1 reply
@alexandis
Comment options

Answer selected by 101arrowz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants