3

I am working on a project where I need to deal with some .taz compressed files. Is there a way to decompress and extract those kind of files, in C# dotnet core ?

Thank you !

Orlyyn
  • 2,296
  • 2
  • 20
  • 32
  • Are you by any chance referring to a `.tar.gz` file? – silkfire Nov 09 '18 at 17:40
  • Unfortunately I am not :( It's more likely to be equivalent to .tar.z extension (I think so but I'm not sure) – Orlyyn Nov 09 '18 at 17:41
  • Have a look at - https://code.google.com/archive/p/tar-cs/ can - That can handle tar format, The decompression would be another story though... – Sam Nov 09 '18 at 17:43
  • I saw that the first step would be to decompress to .taz into a .tar first, but I haven't found how to do that yet :p (Therefore the question) – Orlyyn Nov 09 '18 at 17:51
  • Since .taz is show for .tar.z, Possible way to [uncompress .Z files](https://social.msdn.microsoft.com/Forums/vstudio/en-US/4579180d-6a4c-402f-b333-b6eca590afdc/how-to-extract-z-files-using-c-?forum=csharpgeneral) , Possible way to [extract .tar files](https://stackoverflow.com/questions/8863875/decompress-tar-files-using-c-sharp).. Good luck! – Jack Nov 09 '18 at 18:02

2 Answers2

0

tar.Z is an archive file that can be created with the help of the UNIX compress utility. Its files are compressed into a tarball (.tar) and then further compressed by use of the now rather antiquated 80s compression algorithm known as LZW.

You can extract these packages with help of the popular SharpZipLib library.

Here's an example of how to open the TAZ file and then write the TAR file it contains to disk. You can of course extract the TAR file in memory right away if the file is feasible in size to be kept in memory in its entirety.

using (var inputStream = File.Open(PATH_TO_TARZ_FILE, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
        using (var tazStream = new LzwInputStream(inputStream))
        {
               using (var outputStream = PATH_TO_OUTPUT_TAR_FILE, 4096))
               {
                     tazStream.CopyTo(outputStream);
               }
        }
}
silkfire
  • 24,585
  • 15
  • 82
  • 105
  • I tried your solution, but the output was a file that was unusable (I tried forcing the extension to .tar, but it wouldn't do). Are you sure that the library GZipStream is suited for files like .taz ? It looks like it only works for .gz file extensions – Orlyyn Nov 12 '18 at 11:48
  • I actually tried with some files I found online which worked for me (they were all `tar.Z`). But there might be some other types of TAZ in the wild. Is there any way you could send / upload one of those files (unless confidential) so I can determine what's going on? – silkfire Nov 12 '18 at 16:54
  • @Orlyyn See my edit. Looks like I really had a GZip file, despite it having the `tar.Z` extension. I found myself a better test example and was successful in using SharpZipLib to open it. – silkfire Nov 12 '18 at 19:33
0

I found a workaround by using the UNIX command line "gunzip"

gunzip myfilename.taz

gunzip works with files with the following extensions : .gz, -gz, .z, -z, _z or .Z (and also recognizes the special extensions .tgz and .taz as shorthands for .tar.gz and .tar.Z respectively.)

To run this process, I wrote the following code :

public string ExtractFileFromTaz (string tazFilePath)
{
    var process = new Process()
    {
        StartInfo = new ProcessStartInfo
        {
            FileName = "gunzip",
            Arguments = tazFilePath,
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = true
        }
    };

    process.Start();
    string result = process.StandardOutput.ReadToEnd();
    process.WaitForExit();

    return result;
}

The resulting output is the file with the .tar extension, which replaced the original .taz file in the same directory.

Orlyyn
  • 2,296
  • 2
  • 20
  • 32