0

I am trying to extract a .tgz file from my C# project, and I tried following the answers to this question:

Decompress tar files using C#

However, this code example does not work for me:

using (Stream stream = File.OpenRead(tarPath))
{
    var reader = ReaderFactory.Open(stream);
    while (reader.MoveToNextEntry())
    {
        if (!reader.Entry.IsDirectory)
        {
            reader.WriteEntryToDirectory(extractPath, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
        }
    }
}

I get two errors:

1) ExtractOptions "does not exist in the current context". I am successfully using System.IO and SharpCompress.Readers, but I fail to find where ExtractOptions is located.

2) File "is a method, which is not valid in the given context". I have no idea why this happens!

In case it helps, I can successfully extract .zip files from the same path with a simple:

System.IO.Compression.ZipFile.ExtractToDirectory(zipPath, extractPath);

If there is a better way to extract the .tgz file that would also help!

Thanks

Pablo
  • 1,373
  • 16
  • 36

1 Answers1

2

After a good night I was finally able to make it work.

Problem 1, "ExtractOptions does not exist".

As you can see here, ExtractOptions is actually an enum, which needs to be initialized. This is not shown in the example I was using (perhaps it worked differently in an earlier version? Maybe the naming convention used in the example was throwing me off?)

Problem 2, "File is not valid in the given context"

Actually I had not given enough information to answer this one! The problem is that Visual Studio was taking for granted I was trying to use Controller.File instead of System.IO.File (but complained about the first not being valid in the context, instead of about a possible conflict). Controller.File is used in the web application framework ASP.NET MVC (which is what I am working on) to create FileContentResult objects.

Fixed code

(using System.IO; using SharpCompress.Readers;)

using (Stream stream = System.IO.File.OpenRead(tarPath))
{
    var reader = ReaderFactory.Open(stream);
    while (reader.MoveToNextEntry())
    {
        if (!reader.Entry.IsDirectory)
        {
           reader.WriteEntryToDirectory(extractPath, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true });
        }
    }
}
Community
  • 1
  • 1
Pablo
  • 1,373
  • 16
  • 36