2

I've a embedded zip file in my project. I need to unzip it to some location.

I prefer not to use external libraries.

    Stream _pluginZipResourceStream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("plugin.zip");

I tried GZipStream but ended up with errors!

        using (FileStream decompressedFileStream = File.Create(outputFileName))
        {
            using (GZipStream decompressionStream = new GZipStream(fileStream, CompressionMode.Decompress))
            {
                decompressionStream.CopyTo(decompressedFileStream);
            }
        }

InvalidDataException was unhandled: The magic number in GZip header is not correct. Make sure you are passing in a GZip stream.

How to unzip it ???

tarzanbappa
  • 4,930
  • 22
  • 75
  • 117
Dineshkumar
  • 4,165
  • 5
  • 29
  • 43

2 Answers2

1

GZip is a specific compression algorithm, which can but most likely isn't used in your zip file.

If you're on .NET 4.5, you can use the ZipArchive class. If not, see How to extract ZIP file in C#.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
1

If you are fortunate to use framework 4.5 or higher, you can use ZipArchive see:

http://msdn.microsoft.com/de-de/library/System.IO.Compression%28v=vs.110%29.aspx

If you are stuck with 4.0 or older, you will have to use an external library, or convert your archive to GZip.

Omnibus
  • 108
  • 4
  • should i use ZipArchiveEntry.ExtracttoFile ? – Dineshkumar Jan 28 '14 at 11:26
  • Depends on what you are storing in your zip file. If you have more than one file zipped there and you want to get just that one, then ZipArchiveEntry.ExtractToFile ist your choice. If you just want to unzip all files, even if you have just one file there, I would prefer ZipArchive.ExtractToDirectory. Hope it helps. – Omnibus Jan 28 '14 at 12:41
  • Glad to hear that. Marking the answer as solved or at least an upvote would be a nice token of appreciation. – Omnibus Jan 29 '14 at 10:16