GZipStream does not read the entire file

I have code that downloads gzipped files and unpacks them. The problem is that I can not get it to unpack the entire file, it only reads the first 4096 bytes, and then about 500 more.

Byte[] buffer = new Byte[4096]; int count = 0; FileStream fileInput = new FileStream("input.gzip", FileMode.Open, FileAccess.Read, FileShare.Read); FileStream fileOutput = new FileStream("output.dat", FileMode.Create, FileAccess.Write, FileShare.None); GZipStream gzipStream = new GZipStream(fileInput, CompressionMode.Decompress, true); // Read from gzip steam while ((count = gzipStream.Read(buffer, 0, buffer.Length)) > 0) { // Write to output file fileOutput.Write(buffer, 0, count); } // Close the streams ... 

I checked the downloaded file; it is 13 MB in compression and contains one XML file. I manually unpacked the XML file, and all the content is there. But when I do this with this code, it only outputs the beginning of the XML file.

Does anyone have any idea why this might be happening?

+8
c # gzipstream
source share
4 answers

I ended up using the gzip executable for decompression instead of GZipStream. For some reason, it cannot process the file, but the executable can.

+1
source share

EDIT

Try not to open GZipStream:

 GZipStream gzipStream = new GZipStream(fileInput, CompressionMode.Decompress, false); 

or

 GZipStream gzipStream = new GZipStream(fileInput, CompressionMode.Decompress); 
+4
source share

The same thing happened to me. In my case, only 6 lines are read, and then it reaches the end of the file. Therefore, I realized that although the extension is gz, it was compressed by another algorithm not supported by GZipStream. So I used the SevenZipSharp library, and it worked. This is my code.

You can use the SevenZipSharp library

 using (var input = File.OpenRead(lstFiles[0])) { using (var ds = new SevenZipExtractor(input)) { //ds.ExtractionFinished += DsOnExtractionFinished; var mem = new MemoryStream(); ds.ExtractFile(0, mem); using (var sr = new StreamReader(mem)) { var iCount = 0; String line; mem.Position = 0; while ((line = sr.ReadLine()) != null && iCount < 100) { iCount++; LstOutput.Items.Add(line); } } } } 
+1
source share

Are you calling Close or Flush on fileOutput ? (Or simply wrap it in using , which is recommended for practice.) If you do not delete the file, you will not be deleted to disk when your program ends.

0
source share

All Articles