I need to use nodejs to create a tar
file that isn't encompassed in a parent directory.
For example, here is the file system:
/tmp/mydir
/tmp/mydir/Dockerfile
/tmp/mydir/anotherfile
What I'm looking to do is the equivalent to this:
cd /tmp/mydir
tar -cvf archive.tar *
So, when I extract archive.tar
, Dockerfile
will end up in the same directory I execute the command.
I've tried tar.gz
and a few others, but all the examples are compressing an entire directory, and not just files.
I'm doing this so I can utilize the Docker REST API to send builds.
Tape Archive or tar is a file format for creating files and directories into an archive while preserving filesystem information such as permissions. We can use the tar command to create tar archives, extract the archives, view files and directories stored in the archives, and append files to an existing archive.
With a modern module node-tar you can create a .tar file like this:
tar.create(
{ file: 'archive.tar' },
['/tmp/mydir']
).then(_ => { .. tarball has been created .. })
The tar.gz module referenced in other answers is deprecated.
Use tar.gz module. Here is a sample code
var targz = require('tar.gz');
var compress = new targz().compress('/path/to/compress', '/path/to/store.tar.gz',
function(err){
if(err)
console.log(err);
console.log('The compression has ended!');
});
This package is now deprecated. Check the answer provided by @Kelin.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With