How to unzip a c # zip file

I want to programmatically extract a zip file.

I searched google but I did not find it. I use this code, but I get this error

The magic number in the GZip header is incorrect. Make sure you are passing the gzip stream.

the code:

public static void Decompress(FileInfo fi) { using (FileStream inFile = fi.OpenRead()) { string curFile = fi.FullName; string origName = curFile.Remove(curFile.Length - fi.Extension.Length); using (FileStream outFile = File.Create(origName)) { using (GZipStream Decompress = new GZipStream(inFile, CompressionMode.Decompress)) { byte[] buffer = new byte[4096]; int numRead; while ((numRead = Decompress.Read(buffer, 0, buffer.Length)) != 0) { outFile.Write(buffer, 0, numRead); } Console.WriteLine("Decompressed: {0}", fi.Name); } } } } 

It would be very nice if someone helped me.

Thanks in advance.

+6
source share
1 answer

The error indicates that you are not opening the GZip file. The GZip library cannot open standard ZIP archives.

See gzip format on wikipedia

You can use DotNetZip to open / read / write standard zip archives and even write encrypted password-protected postal codes. It is also on nuget .

+11
source

Source: https://habr.com/ru/post/901938/


All Articles