Unpacking Stream in C #

I am working in C # and I am uploading a zip file with one XML file for the Internet. and I want to download this XML file. This is what I have so far:

byte[] data; WebClient webClient = new WebClient(); try { data = webClient.DownloadData(downloadUrl); } catch (Exception ex) { Console.WriteLine("Error in DownloadData (Ex:{0})", ex.Message); throw; } if (data == null) { Console.WriteLine("Bulk data is null"); throw new Exception("Bulk data is null"); } //Create the stream MemoryStream stream = new MemoryStream(data); XmlDocument document = new XmlDocument(); //Gzip GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress); //Load report straight from the gzip stream try { document.Load(gzipStream); } catch (Exception ex) { Console.WriteLine("Error in Load (Ex:{0})", ex.Message); throw; } 

in document.Load I always get the following exception:
The magic number in the GZip header is incorrect. Make sure you stream the GZip stream.

What am I doing wrong?

+6
source share
5 answers

I am using SharpZipLib and it works great!

Below is a function that encapsulates a library

  public static void Compress(FileInfo sourceFile, string destinationFileName,string destinationTempFileName) { Crc32 crc = new Crc32(); string zipFile = Path.Combine(sourceFile.Directory.FullName, destinationTempFileName); zipFile = Path.ChangeExtension(zipFile, ZIP_EXTENSION); using (FileStream fs = File.Create(zipFile)) { using (ZipOutputStream zOut = new ZipOutputStream(fs)) { zOut.SetLevel(9); ZipEntry entry = new ZipEntry(ZipEntry.CleanName(destinationFileName)); entry.DateTime = DateTime.Now; entry.ZipFileIndex = 1; entry.Size = sourceFile.Length; using (FileStream sourceStream = sourceFile.OpenRead()) { crc.Reset(); long len = sourceFile.Length; byte[] buffer = new byte[bufferSize]; while (len > 0) { int readSoFar = sourceStream.Read(buffer, 0, buffer.Length); crc.Update(buffer, 0, readSoFar); len -= readSoFar; } entry.Crc = crc.Value; zOut.PutNextEntry(entry); len = sourceStream.Length; sourceStream.Seek(0, SeekOrigin.Begin); while (len > 0) { int readSoFar = sourceStream.Read(buffer, 0, buffer.Length); zOut.Write(buffer, 0, readSoFar); len -= readSoFar; } } zOut.Finish(); zOut.Close(); } fs.Close(); } } 
+5
source

Obviously, SharpZipLib is now not supported, and you probably want to avoid it: fooobar.com/questions/166780 / ...

.NET 4.5 now has built-in support for zip files , so for your example, this would be:

 var data = new WebClient().DownloadData(downloadUrl); //Create the stream var stream = new MemoryStream(data); var document = new XmlDocument(); //zip var zipArchive = new ZipArchive(stream); //Load report straight from the zip stream document.Load(zipArchive.Entries[0].Open()); 
+4
source

If you have a byte array containing a zip archive with one file, you can use the ZipArchive class to get the unpacked byte array with the file data. ZipArchive contained in .NET 4.5, in the assembly System.IO.Compression.FileSystem (you need to explicitly reference it).

The following function adapted from this answer works for me:

 public static byte[] UnzipSingleEntry(byte[] zipped) { using (var memoryStream = new MemoryStream(zipped)) { using (var archive = new ZipArchive(memoryStream)) { foreach (ZipArchiveEntry entry in archive.Entries) { using (var entryStream = entry.Open()) { using (var reader = new BinaryReader(entryStream)) { return reader.ReadBytes((int)entry.Length); } } } } } return null; // To quiet my compiler } 
+3
source

As already mentioned, GZip and Zip do not match, so you may need to use the zip library. I am using a library called DotNetZip, available at the following site:

http://dotnetzip.codeplex.com/

+2
source

From the description of the GZipStream class :

GZipStream compressed objects written to a file with the .gz extension can be unpacked using many common compression tools; however, this class does not inherently provide functionality for adding or extracting files to .zip files.

Therefore, if you do not control the files on the server side, I would suggest looking for a specific zip target library (e.g. SharpZipLib).

+1
source

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


All Articles