JSZip提取文件对象

tgr*_*een 2 javascript extract node.js jszip

我通过执行以下操作使用JSZip提取一个zip文件:

jszip.loadAsync(zipFile)
['then'](function(zip) {
    return bluebird.map(Object.keys(zip.files), function (filename) {
        // converts the compressed file to a string of its contents
        return zip.files[filename].async('string').then(function (fileData) {
            // fileData is a string of the contents
        })
    })
})
Run Code Online (Sandbox Code Playgroud)

但是,此提取的输出是文件内容的字符串数组。我想知道是否有可能获取文件对象数组作为输出,因为稍后需要文件对象。

我试图做

new File(fileData.split('\n'), filename)
Run Code Online (Sandbox Code Playgroud)

但是它丢失了原始文件格式。

有什么建议么?

Dav*_*hel 7

File构造需要 BufferSource(ArrayBuffer,Uint8Array等),BLOB或字符串列表。如果分割内容\n,则将其删除\nFile然后将连接每个字符串,而无需重新添加新行。

请改用Blob:

return zip.files[filename].async('blob').then(function (fileData) {
    return new File([fileData], filename);
})
Run Code Online (Sandbox Code Playgroud)