从ZIP文件检索数据-NodeJS

2022-08-07 node.js nodes javascript

我问了自己一个问题

我可以在云平台上阅读文件(主要是CSV),但当它是压缩文件时,我只能得到一堆:

j�lȜ��&��3+xT��J��=��y��7���vu�  {d�T���?��!�

这很正常,所以我想知道是否有办法将其放在一个变量中,然后使用lib或类似的东西将其解压缩。

感谢您抽出时间


解决方案

您应该使用npm install node-stream-zip

const StreamZip = require('node-stream-zip');
const zip = new StreamZip({
    file: 'archive.zip',
    storeEntries: true
});

并像这样获取信息

zip.on('ready', () => {
    console.log('Entries read: ' + zip.entriesCount);
    for (const entry of Object.values(zip.entries())) {
        const desc = entry.isDirectory ? 'directory' : `${entry.size} bytes`;
        console.log(`Entry ${entry.name}: ${desc}`);
    }
    // Do not forget to close the file once you're done
    zip.close()
});

希望对您有所帮助:-)

相关文章