dir-archiver
Version:
Compress a whole directory (including subdirectories) into a zip file, with options to exclude specific files, or directories.
126 lines (93 loc) • 4.29 kB
Markdown
[![npm][npm-image]][npm-url] [![license][license-image]][license-url]
[![changelog][changelog-image]][changelog-url]
Compress a whole directory (including subdirectories) into a zip file, with options to exclude specific files or directories.
# Installation
```sh
$ npm install dir-archiver
```
Requires Node.js >=18.
Quick start (async/await):
```javascript
const DirArchiver = require('dir-archiver');
const archive = new DirArchiver(
'./my-project',
'./my-project.zip',
true,
['node_modules', 'dist', 'nested/secret.txt'],
false
);
await archive.createZip();
```
Signature:
```ts
new DirArchiver(
directoryPath: string,
zipPath: string,
includeBaseDirectory?: boolean,
excludes?: string[],
followSymlinks?: boolean
)
```
Parameters:
- `directoryPath`: Root folder to archive (must exist).
- `zipPath`: Destination zip file path (parent directory must exist).
- `includeBaseDirectory`: When true, the archive contains the source folder as the top-level directory.
- `excludes`: Names or relative paths to skip. Names without path separators match anywhere; use a relative path
(for example, `nested/file.txt`) to target a specific entry. Trailing slashes can target directories (for example, `cache/`).
Windows-style backslashes are accepted and normalized. Absolute paths inside the source tree are accepted and converted
to relative excludes. Matching is case-insensitive on Windows.
- `followSymlinks`: Follow symlinks when traversing directories. Default: `false`.
`createZip()` returns a Promise that resolves with the zip path when the archive is finalized and rejects on failure.
Zip entries always use forward slashes, regardless of OS, and are added in deterministic order.
## Command Line Interface
```sh
Usage: dir-archiver --src <path-to-directory> --dest <path-to-file>.zip --includebasedir true|false --exclude folder-name file-name.extension
Options:
--src The path of the folder to archive. [string][required]
--dest The path of the zip file to create. [string][required]
--includebasedir Includes a base directory at the root of the archive.
For example, if the root folder of your project is named
"your-project", setting this option to true will create
an archive that includes this base directory.
If this option is set to false the archive created will
unzip its content to the current directory. [bool]
--followsymlinks Follow symlinks when traversing directories. [bool]
--exclude A list with the names of the files and folders to exclude. Names without
path separators match anywhere; use a relative path to target a specific
entry. Windows-style backslashes are accepted and normalized. [array]
```
Inline values are supported for flags (for example, `--includebasedir=true` or `--exclude=cache`).
# CLI examples
```sh
# Basic
dir-archiver --src ./my-project --dest ./my-project.zip
# Include base directory and exclude node_modules anywhere
dir-archiver --src ./my-project --dest ./my-project.zip --includebasedir=true --exclude node_modules
dir-archiver --src ./my-project --dest ./my-project.zip --exclude nested/secret.txt
dir-archiver --src . --dest archive.zip --exclude .\\nested\\skip.txt
dir-archiver --src ./my-project --dest ./my-project.zip --followsymlinks=true
```
```sh
$ npm test
```
```sh
$ npm install
$ npm run typecheck
$ npm run build
$ npm run lint
```
Linting runs TypeScript typechecking and ESLint. CI runs lint and tests on Node 18/20/22 across Linux, macOS, and Windows.
[]: https://img.shields.io/badge/changelog-md-blue.svg?style=flat-square
[]: CHANGELOG.md
[]: https://img.shields.io/npm/l/dir-archiver.svg?style=flat-square
[]: LICENSE
[]: https://img.shields.io/npm/v/dir-archiver.svg?style=flat-square
[]: https://www.npmjs.com/package/dir-archiver