So considering I have a multiple files in multiple folders.
/parentDir/
|__dirA/
|__file1
|__file2
|...
|__dirB/
|__file3
|...
|...
And I want to compress them into an archive. I wanted to use node-tar
but that's not necessarily a constraint. To be noted that the archive could be extremely large, in the hundreds of gigabytes.
I also have a map of how I want to organize these files in different sub-directories.
In the end I should have an archive that looks something like this:
/rootArchive/
|__dir1/
|__subDir1/
|__subSubDir1/
|__file1
|__...
|__subSubDir2/
|__...
|__subDir2/
|__...
|__dir2/
|__...
I know something like this can be done with --transform
but I didn't see anything this complex and the node-tar
doesn't even seem to be able to do that on creating an archive, only on extracting.
So is something like this possible without first creating the folders and copying the files, archiving and compressing it and then deleting the folder?
Though your question is not very descriptive on how you want to re-structure your archive. I think Archiver might be able to help you.
Commands that you'd have to play with from the docs of archiver:
// require modules
const fs = require('fs');
const archiver = require('archiver');
// creating an archive instance
const archive = archiver('zip', {
zlib: { level: 9 } // Sets the compression level.
});
// append a file
archive.file('file1.txt', { name: 'file4.txt' });
// append files from a sub-directory and naming it `new-subdir` within the archive
archive.directory('subdir/', 'new-subdir');
// append files from a sub-directory, putting its contents at the root of archive
archive.directory('subdir/', false);
// append files from a glob pattern
archive.glob('file*.txt', {cwd:__dirname});
// finalize the archive (ie we are done appending files but streams have to finish yet)
// 'close', 'end' or 'finish' may be fired right after calling this method so register to them beforehand
archive.finalize();
// create a file to stream archive data to.
const output = fs.createWriteStream(__dirname + '/example.zip');
// pipe archive data to the file
archive.pipe(output);