Unzip .tar.gz with SharpZipLib

J.Calmet picture J.Calmet · Jun 30, 2016 · Viewed 8.3k times · Source

I am trying to unzip a tar.gz inside one zip but I can't

It shows me an error “cannot find central directory” and I don’t know what to do.

First I unzip the zip on a temporal folder, then search for a .tar.gz on that folder and try to unzip but crash with that error, but I can open it with winrar application, the .tar.gz have some folder and some files inside.

This is my code:

var trash = Path.Combine(_temporalPath, "Trash");
        try
        {
            var zip = new FastZip();
            Directory.CreateDirectory(trash);
            zip.ExtractZip(_origin, trash, "");
            var gzip = Directory.GetFiles(trash, "*.tar.gz")[0];
            zip.ExtractZip(gzip, trash, FastZip.Overwrite.Always.ToString());
            File.Delete(gzip);
        }
        catch (Exception)
        {
            //IGNORE
        }

I search information but I only find for unzip one file. enter image description here

What I need is open the tar.gz and get the files from inside.

Answer

Brissles picture Brissles · Sep 6, 2018

Use SharpZipLib:

using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Tar;

public void ExtractTGZ(String gzArchiveName, String destFolder)
{
    Stream inStream = File.OpenRead(gzArchiveName);
    Stream gzipStream = new GZipInputStream(inStream);

    TarArchive tarArchive = TarArchive.CreateInputTarArchive(gzipStream);
    tarArchive.ExtractContents(destFolder);
    tarArchive.Close();

    gzipStream.Close();
    inStream.Close();
}