Zip a folder and excluding some folder and files

I am trying to create the zip folder and ignore some particular files and folders like node_modules, access.log, and other hidden folders.
in my case, I can able to ignore the .log file but my code is not working for folder and hidden files/folder.

Please help how do I achieve the code and ignore the node_modules folder and hidden files and folder including .log files

as per the documentation page: https://www.npmjs.com/package/archiver

const fs = require("fs");
const archiver = require("archiver");

const output = fs.createWriteStream(__dirname + "/example.zip");
const archive = archiver("zip", {
	zlib: { level: 9 }, // Sets the compression level.
});

output.on("close", function () {
	console.log(archive.pointer() + " total bytes");
	console.log("archiver has been finalized and the output file descriptor has closed.");
});

output.on("end", function () {
	console.log("Data has been drained");
});

archive.on("warning", function (err) {
	if (err.code === "ENOENT") {
		// log warning
	} else {
		// throw error
		throw err;
	}
});

archive.on("error", function (err) {
	throw err;
});

archive.pipe(output);
archive.glob("**", { cwd: __dirname, ignore: ["node_modules", "*.log"] }, {});

archive.finalize();

After quite a bit of digging found this link, which suggests that the folder name should be followed by a slash and two asterixis

A shot in the dark, but does this work?

archive.glob("**", { cwd: __dirname, ignore: ["node_modules/**", "*.log"] }, {});
1 Like

Thank You very much…

can we include multiple path ? in glob function ?

I can’t answer with any authority on this, maybe someone else here can better comment.

cwd is current working directory not directories, and actually look at the the link I gave you cwd has been omitted.

In the example they have selected multiple folders though.

glob('**/*.js', { ignore: '{c,d}/**' }, callback) //  (folders c and d)

I wonder therefore if you could do something similar

archive.glob("**", { ignore: ["{path1, path2}/node_modules/**", "*.log"] }, {});

I’m sorry I can’t be more helpful. Guesswork at this point.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.