Technical Note/Node.js

ZIP a Folder in NodeJS

Here is a simple way to archive and pipe a folder in NodeJS.
First, get the fstream and tar modules:
  • npm install fstream
  • npm install tar
Do something like this on your server:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var fstream = require('fstream'),
    tar = require('tar'),
    zlib = require('zlib');
 
    res.writeHead(200, {
      'Content-Type'        'application/octet-stream',
      'Content-Disposition' 'attachment; filename=myArchive.zip',
      'Content-Encoding'    'gzip'
    });
 
    var folderWeWantToZip = '/foo/bar';
 
    /* Read the source directory */
    fstream.Reader({ 'path' : folderWeWantToZip, 'type' 'Directory' })
        .pipe(tar.Pack())/* Convert the directory to a .tar file */
        .pipe(zlib.Gzip())/* Compress the .tar file */
        .pipe(response); // Write back to the response, or wherever else...
This solution is based on an answer on StackOverflow


0 0

1 2 3 4 5 6 ··· 237
블로그 이미지

zzikjh