9

How can I compress a file/files and save into a .zip extension file with the provided node.js zlib module? I figured how to save into .gz using gzip like the following:

const gzip = zlib.createGzip();
const fs = require('fs');
const input = fs.createReadStream('myfile.txt');
const out = fs.createWriteStream('myfile.txt.gz');

input.pipe(gzip).pipe(out);
Nafis Abdullah Khan
  • 2,062
  • 3
  • 22
  • 39
  • @Raptor: Gunzip, Gzip but they seems to be making tar.gz, not .zip. Also looked for linux commands and seems like there is a zip command. There are other modules available but I was hoping it must be possible with the provided zlib module, but couldn't find in google so far. – Nafis Abdullah Khan Feb 06 '18 at 08:59
  • You mentioned about `node.js`, how do you want to use it with compressing file? – Raptor Feb 06 '18 at 09:20
  • @Raptor: By openning a readfilestream and a writefilestream, piping the readstream through some function in zlib to the writefilestream? But what function do I use for .zip files? – Nafis Abdullah Khan Feb 06 '18 at 09:34
  • So, where's your code ? – Raptor Feb 06 '18 at 09:36
  • @Raptor: It's in here as an example of gzip: https://nodejs.org/api/zlib.html – Nafis Abdullah Khan Feb 06 '18 at 09:37
  • Your question should relate to your implementation, instead of example codes from documentation. – Raptor Feb 06 '18 at 09:42
  • @Raptor: Alright, I've edited the question with an implementation. – Nafis Abdullah Khan Feb 06 '18 at 09:49
  • 5
    A `.zip` file can contain multiple files, and has directory information. It seems that you would need an npm library that handles zip files (there are several) as it is doubtful a transform stream can handle more than one file in this way. A `.gz` file contains gzipped data of one file with no metadata about the file, so a transform stream could do those... but not .zip ... Perhaps read https://stackoverflow.com/questions/20762094/how-are-zlib-gzip-and-zip-related-what-do-they-have-in-common-and-how-are-they especially the PKZIP spec he cites. – Paul Feb 06 '18 at 09:58
  • Try [jszip](http://stuk.github.io/jszip/). – Mark Adler Feb 07 '18 at 05:36

0 Answers0