4

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.

J.Calmet
  • 61
  • 1
  • 1
  • 7
  • related : http://stackoverflow.com/questions/24138373/unzip-gz-file-using-c-sharp –  Jun 30 '16 at 10:38
  • in that question they only Decompress one file not a group of files – J.Calmet Jun 30 '16 at 10:41
  • How about this FastZip : https://github.com/icsharpcode/SharpZipLib/wiki/FastZip –  Jun 30 '16 at 10:46
  • `.gz` files don't contain directories/multiple files. GZip doesn't have support for multiple entries. This is why, when using gzip on multiple files, it's necessary to use `tar` to bundle multiple files/directories into a single file. The file suffix would then be `.tar.gz`. Is your question missing something? – spender Jun 30 '16 at 12:55
  • yes sorry it's tar.gz, sorry – J.Calmet Jun 30 '16 at 12:58
  • Please can you return to your question and take the time to be ***very explicit*** about ***exactly*** what the structure of your files are. You're wasting everyone's time by missing out these important details. – spender Jun 30 '16 at 13:00
  • "central directory" is a *zip* concept, not a *gzip* concept. You need to use something that can extract tar files and gzip streams. A "Zip" library won't work. – Lasse V. Karlsen Jun 30 '16 at 13:04

4 Answers4

6

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();
}
Brissles
  • 3,833
  • 23
  • 31
1

Finally I found the form.

var trash = Path.Combine(_temporalPath, "Trash");
        try
        {
            var zip = new ICSharpCode.SharpZipLib.Zip.FastZip();
            Directory.CreateDirectory(trash);
            zip.ExtractZip(_origin, trash, null);
            var gzip = Directory.GetFiles(trash, "*.gz")[0];
            UnGzFile(gzip, Path.Combine(trash, Path.GetFileNameWithoutExtension(gzip)));
            File.Delete(gzip);
            var tar = Directory.GetFiles(trash, "*.tar")[0];
            var stream = File.OpenRead(tar);
            var tarArchive = ICSharpCode.SharpZipLib.Tar.TarArchive.CreateInputTarArchive(stream);
            tarArchive.ExtractContents(trash);
            tarArchive.Close();
            stream.Close();
            File.Delete(tar);
        }
        catch (Exception ex)
        {
            //IGNORE
        }

Sorry and Thanks

J.Calmet
  • 61
  • 1
  • 1
  • 7
0

You can simply do this using the ZipFile Class:

File.Move(source, Path.ChangeExtension(source, ".zip"));
ZipFile.ExtractToDirectory(Path.ChangeExtension(source, ".zip"), destination);
Mohammad Almasi
  • 400
  • 6
  • 19
-1

https://github.com/icsharpcode/SharpZipLib/wiki/FastZip

using System;
using ICSharpCode.SharpZipLib.Zip;

public void TestFastZipUnpack(string zipFileName, string targetDir) {

    FastZip fastZip = new FastZip();
    string fileFilter = null;

    // Will always overwrite if target filenames already exist
    fastZip.ExtractZip(zipFileName, targetDir, fileFilter);
}