Skip to content

Conversation

sharoninator
Copy link

This is a helper function for more shorthand compression. It detects whether the data its compressing is a file or a directory, and uses the appropriate functions based on that. Here's a usage example:

const archive = archiver('zip');
archive.compress('dir/', 'archive.zip');

fs is already a dependency, so it skips the step of manually passing in the file input stream.

Without this helper function, say I want to compress a directory. In the example, I am to do it as so:

// require modules
const fs = require('fs');
const archiver = require('archiver');

// create a file to stream archive data to.
const output = fs.createWriteStream(__dirname + '/example.zip');
const archive = archiver('zip', {
  zlib: { level: 9 } // Sets the compression level.
});

// pipe archive data to the file
archive.pipe(output);

// append files from a sub-directory and naming it `new-subdir` within the archive
archive.directory('subdir/', 'new-subdir');
archive.finalize();

This is quite verbose, just to compress a directory. This function makes it so it can be done in one line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant