Stuk / jszip

Create, read and edit .zip files with Javascript
https://stuk.github.io/jszip/
Other
9.8k stars 1.3k forks source link

Convert JSZipObject to File object #667

Open shakthifuture opened 4 years ago

shakthifuture commented 4 years ago

Hi,

Please let me know how to convert "JSZipObject" to a standard "File" object?

Thank you.

Semoralesga commented 4 years ago

I did this:

async compressXmlFile(file: File): Promise<File> {
    let count = 0;
    var zip = new JSZip();
    let newFile: File;
    zip.file(file.name, file);
    await zip.generateAsync({ type: 'blob' }).then((blob: Blob) => {
      newFile = new File([blob], file.name.split('.')[0] + '.zip', {
        lastModified: file.lastModified,
        type: 'application/zip'
      });
    });
    return newFile;
  }
willstott101 commented 4 years ago

zip.generateAsync({ type: 'file' }) and ZipObject#loadAsync('file') would both be very helpful.

That snippet above shows that it ought to be fairly trivial.

willstott101 commented 4 years ago

FWIW I used the following for reading zipped files as File objects.

    const zip = await JSZip.loadAsync(file);
    for (let zobj of Object.values(zip.files)) {
      if (zobj.dir) continue;
      const zblob = await zobj.async("blob");
      const zfile = new File([zblob], path.basename(zobj.name), {
        lastModified: zobj.date.getTime(),
        type: 'application/zip'
      });
      processFile(zfile, fp + "/" + zobj.name);
    }
keatkeat87 commented 4 years ago

await zobj.async("blob"); the blob object doesn't included type. application/zip is not the file type.

willstott101 commented 4 years ago

There is a separate issue mentioning mime types if you're having trouble: https://github.com/Stuk/jszip/issues/626