Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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);                 }             });         });     }); 
like image 212
Amaynut Avatar asked Sep 26 '16 14:09

Amaynut


People also ask

How do I open a zip file without extracting it?

unzip -l archive. zip lists the contents of a ZIP archive to ensure your file is inside. Use the -p option to write the contents of named files to stdout (screen) without having to uncompress the entire archive.

How do I read a zip file in node JS?

Inside the loop, you convert the object into a string that represents the object using the Node. js toString() method, then log it in the console using the console. log() method. Finally, you invoke the readZipArchive() function with the ZIP archive file path as an argument.

How do I open a zip file without unzipping it in Linux?

If you need to check the contents of a compressed text file on Linux, you don't have to uncompress it first. Instead, you can use a zcat or bzcat command to extract and display file contents while leaving the file intact. The "cat" in each command name tells you that the command's purpose is to display content.

How can I read the content of a Zip file without unzipping it in Java?

Methods. getComment(): String – returns the zip file comment, or null if none. getEntry(String name): ZipEntry – returns the zip file entry for the specified name, or null if not found. getInputStream(ZipEntry entry) : InputStream – Returns an input stream for reading the contents of the specified zip file entry.


2 Answers

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() }); 
like image 90
Ryan Shillington Avatar answered Sep 16 '22 16:09

Ryan Shillington


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();     }   }); 
like image 27
Kacper Polak Avatar answered Sep 19 '22 16:09

Kacper Polak