Node.js读取zip中的文件而不解压缩它

Node.js read a file in a zip without unzipping it

本文关键字:解压缩 文件 js 读取 zip Node      更新时间:2023-09-26

我有一个zip文件(实际上是一个epub文件(,我需要循环浏览其中的文件并读取它们,而无需将它们解压缩到磁盘。

我尝试使用一个名为JSZip的Node.js库,但每个文件的内容都存储在Buffer中的内存中,每当我尝试解码缓冲区内容以字符串时,返回的内容都是不可读的

这是我尝试过的代码:

const zip = new JSZip();
  // read a zip file
    fs.readFile(epubFile, function (err, data) {
        if (err) throw err;
        zip.loadAsync(data).then(function (zip) {
            async.eachOf(zip.files, function (content, fileName, callback) {
                if (fileName.match(/json/)) {
                    var buf = content._data.compressedContent;
                    console.log(fileName);
                    console.log((new Buffer(buf)).toString('utf-8'));
                }
                callback();
            }, function (err) {
                if (err) {
                    console.log(err);
                }
            });
        });
    });

由于unzip似乎被放弃了,我使用了节点流zip,并取得了相当好的成功。

npm install node-stream-zip

读取文件就像:

const StreamZip = require('node-stream-zip');
const zip = new StreamZip({
    file: 'archive.zip',
    storeEntries: true
});
zip.on('ready', () => {
    // Take a look at the files
    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}`);
    }
    // Read a file in memory
    let zipDotTxtContents = zip.entryDataSync('path/inside/zip.txt').toString('utf8');
    console.log("The content of path/inside/zip.txt is: " + zipDotTxtContents);
    // Do not forget to close the file once you're done
    zip.close()
});
npm install unzip

https://www.npmjs.com/package/unzip

    fs.createReadStream('path/to/archive.zip')
  .pipe(unzip.Parse())
  .on('entry', function (entry) {
    var fileName = entry.path;
    var type = entry.type; // 'Directory' or 'File' 
    var size = entry.size;
    if (fileName === "this IS the file I'm looking for") {
      entry.pipe(fs.createWriteStream('output/path'));
    } else {
      entry.autodrain();
    }
  });