Reputation: 6002
I have an express route, that uploads files, that are sent to the server via formData
.
Supposing the file is a .rar
file, my goal is to extract all file names, that are inside this archive or it's subfolders.
This is how my express route currently looks like:
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') }
}
Here is what the console.log(fileData)
looks like:
{
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
}
Inside filename.rar
are a few files like texture.png
and info.txt
. And my goal is to fetch those names.
Upvotes: 2
Views: 1915
Reputation: 104870
Steps could be:
npm install -g node-rar
or
npm install node-rar
and
node-rar --version
in your file:
var rar = require('node-rar');
/// list archive entries
rar.list('path/to/file.rar', 'optional password');
// => [entries]
/// test archive entries
rar.test('path/to/file.rar', 'dir/to/test', 'optional password');
// => [entries]
/// extract archive entries
rar.extract('path/to/file.rar', 'dir/to/extract', 'optional password');
// => [entries]
Upvotes: 0
Reputation: 6002
I eventually found an solution using 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') }
}
Upvotes: 5
Reputation: 1762
You can use decompress which accepts Buffer
as an argument:
const Busboy = require('busboy');
const decompress = require('decompress');
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
decompress(fileData, 'dist').then(files => {
req.send(files.map(f => f.path))
});
})
req.pipe(busboy)
} catch (err) { return response.error(req, res, err, 'uploadProductFile_unexpected') }
}
Upvotes: 0
Reputation: 101
Use: npm i node-rar
const rar = require('node-rar')
rar.list('path/to/file.rar', 'optional password')
Reference: https://www.npmjs.com/package/node-rar
Upvotes: 0