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

五迷三道 提交于 2021-01-20 16:38:32

问题


I have a zip file (actually it's an epub file) I need to loop through the files in it and read them without unzipping them to the disk.

I tried to use a Node.js library called JSZip but the content of each file is stored in memory in Buffer and whenever I try to decode the buffer content to string the content returned is unreadable

Here's the code I tried:

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

回答1:


Since unzip seems to be abandoned, I used node-stream-zip with pretty good success.

npm install node-stream-zip

Reading files be all like:

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



回答2:


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


来源:https://stackoverflow.com/questions/39705209/node-js-read-a-file-in-a-zip-without-unzipping-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!