Rado86
Rado86

Reputation: 13

Ignore certain files while packing a .tar.gz file in node JS

So I already have some code from the "targz" package that packs all the files inside of a directory. Now I have seen that you can somehow ignore files (not pack them) and I wanted to do it as well. I just can't figure out how I should write the ignore section. Here is my code:

targz.compress({
    src: "./" + this.sourcePath + "/",
    dest: "./" + this.targetPath + "/" + "result.tar.gz",
    tar: {
        entries: this.fileArray,
        ignore: function(name) {
            return path.extname(name) === '.pdf'
        }
    },
    gz: {
        level: 6,
        memLevel: 6,
    }
}, function(err){
    if(err) {
        console.log(err);
        reject(err);
    } else {
        resolve();
    }
});

Can somebody tell me how I need to write that section so it works? It would be greatly appreciated

Upvotes: 0

Views: 805

Answers (2)

fknx
fknx

Reputation: 1785

I think the combination of entries and ignore is not working as you expect. If you include a file in entries it will be added to your archive, no matter what ignore does.

I don't think you need to specify the entries manually since you already specify a src. So removing the entries should do the trick.

Upvotes: 1

Alexandru Olaru
Alexandru Olaru

Reputation: 7092

You got the ignore function which acts as a filter, for example from your code you are filtering all the files that have the .pdf extension.

You can rewrite this function to filter all the files and not your specific ones:

ignore: function filter(name) {
  const specificFilesToIgnore = ['some_specific.pdf', 'other_specific.pdf'];

  return path.extname(file) === '.pdf' && !specificFilesToIgnore.includes(name); 
}

Upvotes: 0

Related Questions