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

时间:2016-09-26 14:12:31

标签: javascript node.js zip

我有一个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);
                }
            });
        });
    });

2 个答案:

答案 0 :(得分:7)

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();
    }
  });

答案 1 :(得分:2)

由于解压缩似乎已被放弃,因此我使用node-stream-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()
});
相关问题