JSZip提取文件对象

3

我正在使用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
        })
    })
})

然而,这种提取的输出结果是文件内容字符串数组。我想知道是否有可能将文件对象数组作为输出结果,因为我之后需要使用文件对象。
我尝试过:
new File(fileData.split('\n'), filename)

但它会丢失原始文件格式。有什么建议吗?
1个回答

10

File 构造函数接受一个 BufferSource(ArrayBuffer,Uint8Array等)、Blob 或字符串的列表。如果您在 \n 上拆分内容,则会删除这些 \nFile 然后将每个字符串连接起来而不重新添加新行。

使用 Blob 替代:

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

谢谢,这正是我所需要的。 对于将来可能使用它的人,请注意该行应为 return new File([fileData], filename) - tgreen
你说得对,我弄错了变量名。我会编辑我的答案来修复它。 - David Duponchel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接