tar (on Windows) a list of files in c#
Question
I then to tar and then gzip a list of files in C#.
I need some help with how to set the arguments to tar.
Say I have tar.exe in a folder c:\tar\tar.exe and a method like the following:
private void RunTar(string outputFileName, List<string> fileNamePaths)
{
using (Process p = new Process())
{
p.StartInfo.FileName = @"c:\tar\tar.exe";
p.StartInfo.Arguments = //;
p.Start();
p.WaitForExit();
}
}
Note: the fileNamePathsToTar list has the full unc filename paths and the files can be in different folders.
Can anyone please help with what arguments to supply.
Also I notice in the documentation:
-z, --gzip, --ungzip
filter the archive through gzip
-Z, --compress, --uncompress
filter the archive through compress
--use-compress-program=PROG
filter through PROG (must accept -d)
Not sure how to use this but if I place the gzip.exe in the same folder as tar.exe can I perform my tar and then gzip of these files all in one step?
Update
I can only seem to get tar to work on files in the same directory as tar.exe if I try a full path name I get something like:
C:\Tar Test>tar -cf out.tar c:/Tar Test/Text/t1.txt
tar: Cannot add file c:/Tar: No such file or directory
tar: Cannot add file Test/Text/t1.txt: No such file or directory
tar: Error exit delayed from previous errors
I've tried with the slashes both ways \ or / and with quotes around the full path no joy.
Thanks
Solution
to create an archive and gzip it, you should use czf as arguments, so
p.StartInfo.Arguments = "czf";
or
p.StartInfo.Arguments = "-czf";
depending on tar version.
to avoid gzipping, remove the 'z' from the arguments.
ah, and you'd better tar an entire folder, like put all your file in a folder named, e.g., myfolder and tar that folder, not it's content.
OTHER TIPS
FYI, there's a managed tar library with full source available at http://cheeso.members.winisp.net/srcview.aspx?dir=Tar&file=Tar.cs
There's a makefile to allow you to build Tar.dll or Tar.exe. There's also a sample app in VB.