In this recipe, we will look at creating archives of our files and directories.
We will be using the
archiver
NPM module. Install it with the following command:
npm install archiver --save
With the archiver
module installed, we can use it to create a backup file of our entire workspace tree by following these steps:
Load the
fs
andarchiver
modules:fs = require 'fs' archiver = require 'archiver'
Create a write stream:
output = fs.createWriteStream 'backup.zip'
Create an instance of a ZIP archive:
archive = archiver 'zip'
Add event handlers to the stream and archive:
output.on 'close', -> console.log "Total bytes: #{archive.pointer()}" archive.on 'error', (err) -> console.error err
Set the archive's output pipe to our stream writer:
archive.pipe output
Perform the compression:
archive.bulk expand: yes cwd: 'workspace' src: ['**'] dest: 'src' archive.finalize()