Tar for files?

Hello everyone. I really liked the idea of the Tar.jl package. Can you use them for files instead of a complete folder? I have code that generates many files, and it would be interesting to compress these files into a single tar file and also access them later.

Note that a tar file isn’t compressed in the sense of a zip or other file. It merely wraps the given files into a single tarball, it doesn’t try to minimize its size.

Tar.create accepts a predicate argument, so you can do

Tar.create(dir_containing_files, tarball_destination) do path
  basename(path) in ["myfile1", "myfile2"]
end

This will look in dir_containing_files, but only include myfile1 and myfile2 in the created tarball, and exclude any other files in the directory.

You can include any logic in the do block, so if your generated filenames have a pattern to them, you can select based on that too.

1 Like

Very good, I will use it that way. Thanks for the answer. Happy birthday!

1 Like

Before asking a question I always research, but sometimes it is hard to find the answer except with a direct question here on the forum. In my code, I have several Julia scripts running on the same machine and adding files to the same tar file. To do this I use the following code:

function tar_add_files(tar_file, new_file, rmf=true)
	if !isfile(tar_file)
		run(`tar -cf $tar -T /dev/null`) # create an empty tar file
	end
	run(`tar -rf $tar $new_file`) # add new_file to an existing .tar
	if rmf rm(new_file) end # remove new_file
end

scritp1.jl => tar_add_files("results.tar", "file1.data")
scritp2.jl => tar_add_files("results.tar", "file2.data")
scritp3.jl => tar_add_files("results.tar", "file3.data")
... many scripts.jl
  1. Can you add files to an empty tarball using Tar.jl?
  2. Instead of extracting a tarball, can you read the contents of the tarball into a buffer and then read it with readdlm?
1 Like

The two answers are complementary, as I cannot mark both as a solution, I will mark the first one. Thank you very much.