gpt4 book ai didi

javascript - 从 Node.js 中的 .rar 文件读取所有文件名

转载 作者:行者123 更新时间:2023-11-30 09:17:44 26 4
gpt4 key购买 nike

我有一个快速路由,可以上传文件,并通过 formData 发送到服务器。

假设该文件是 .rar 文件,我的目标是提取此存档或其子文件夹内的所有文件名。

这就是我目前的快速路线:

module.exports = async (req, res) => {
try {
const busboy = new Busboy({ headers: req.headers })
busboy.on('finish', async () => {
const fileData = req.files.file
console.log(fileData)
// upload file
// send back response
})
req.pipe(busboy)
} catch (err) { return response.error(req, res, err, 'uploadProductFile_unexpected') }
}

这是 console.log(fileData) 的样子:

{
data:
<Buffer 52 61 72 21 1a 07 01 00 56 0c 22 93 0c 01 05 08 00 07 01 01 8d d6 8d 80 00 85 76 33 e4 49 02 03 0b fc d4 0d 04 b1 8c 1e 20 bc 86 da 2e 80 13
00 2b 66 ... >,
name: 'filename.rar',
encoding: '7bit',
mimetype: 'application/octet-stream',
truncated: false,
size: 224136
}

filename.rar里面有一些文件,比如texture.pnginfo.txt。我的目标是获取这些名称。

最佳答案

我最终使用 node-unrar-js 找到了解决方案:

const unrar = require('node-unrar-js')

module.exports = async (req, res) => {
try {
const busboy = new Busboy({ headers: req.headers })
busboy.on('finish', async () => {

const fileData = req.files.file
const extractor = unrar.createExtractorFromData(fileData.data)
const list = extractor.getFileList()
if (list[0].state === 'SUCCESS')
// Here I have the file names
const fileNmes = list[1].fileHeaders.map(header => header.name)
// ...

})
req.pipe(busboy)
} catch (err) { return response.error(req, res, err, 'uploadProductFile_unexpected') }
}

关于javascript - 从 Node.js 中的 .rar 文件读取所有文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53914135/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com