Tengo un archivo .Zip que está en la memoria como un objeto de archivo . Quiero acceder a los archivos individuales y agregarlos a una matriz de objetos de archivo en la memoria . Veo varias opciones en línea, pero todo requiere acceder a un archivo .zip físico en una computadora. ¿Cómo puedo hacerlo sin guardarlo como un archivo físico?
Ciertamente puede hacer esto con JSZip, dado que File implementa Blob , probablemente solo pueda hacer JSZip.loadAsync(yourFileObject).then(zip => { /* do something */ } . Consulte los documentos . Querrá iterar sobre cada archivo en el archivo y cree blobs, de manera óptima con Promise.all() .
Sin embargo, para un rendimiento mucho mejor y un tamaño más pequeño, me gustaría señalarle mi biblioteca fflate . Si está tratando de obtener una matriz de objetos de archivo en fflate :
// If you aren't using a bundler, see the CDN instructions in the docs import { unzipSync, unzip } from 'fflate'; // multithreaded = false is slower and blocks the UI thread if the files // inside are compressed, but it can be faster if they are not. const getFiles = async (zipFile, multithreaded = true) => { const zipBuffer = new Uint8Array(await zipFile.arrayBuffer()); const unzipped = multithreaded ? await new Promise((resolve, reject) => unzip( zipBuffer, (err, unzipped) => err ? reject(err) : resolve(unzipped) )) : unzipSync(zipBuffer); const fileArray = Object.keys(unzipped) .filter(filename => unzipped[filename].length > 0) .map(filename => new File([unzipped[filename]], filename)); return fileArray; } console.log(someFileObject); // File { ... } getFiles(someFileObject).then(console.log) // [File { ... }, File { ... }, ...]