GZipStream ends too early
-
For some reason, while reading a GZip file I am having an issue where it stops after about 24K characters; it never reaches the end of file (I have tried it on several Gzip files of differing sizes, each containing one text file). I created a small console app to replicate the problem, see below. Any insight would be welcomed... Framework is .NET 3.5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Compression;namespace GZipDump
{
class Program
{
static void Main(string[] args)
{
UInt64 charsRead = 0;
string file;
if (args.Length > 0)
file = args[0];
else
{
Console.Write("GZip file to be read (full path): ");
file = Console.ReadLine();
}using (GZipStream gz = new GZipStream(new FileInfo(file).OpenRead(), CompressionMode.Decompress)) { byte\[\] bytes = new byte\[1\]; while (gz.Read(bytes, 0, 1) > 0) { Console.Write(Convert.ToChar(bytes\[0\])); charsRead++; } } Console.WriteLine("\\n\\nCompleted."); Console.WriteLine("Characters Read: " + charsRead); Console.ReadLine(); } }
}
-
For some reason, while reading a GZip file I am having an issue where it stops after about 24K characters; it never reaches the end of file (I have tried it on several Gzip files of differing sizes, each containing one text file). I created a small console app to replicate the problem, see below. Any insight would be welcomed... Framework is .NET 3.5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Compression;namespace GZipDump
{
class Program
{
static void Main(string[] args)
{
UInt64 charsRead = 0;
string file;
if (args.Length > 0)
file = args[0];
else
{
Console.Write("GZip file to be read (full path): ");
file = Console.ReadLine();
}using (GZipStream gz = new GZipStream(new FileInfo(file).OpenRead(), CompressionMode.Decompress)) { byte\[\] bytes = new byte\[1\]; while (gz.Read(bytes, 0, 1) > 0) { Console.Write(Convert.ToChar(bytes\[0\])); charsRead++; } } Console.WriteLine("\\n\\nCompleted."); Console.WriteLine("Characters Read: " + charsRead); Console.ReadLine(); } }
}
I've never treated the
GZipStream
like that, but you could try using one of the decompression example out of MSDN found here[^]. If you don't retrieve the original file out of the zip, you may be dealing with a different compression that isn't supported. If you do, then it'd be something to do with how you're reading it out here. Give it a whirl and see what happens. -
I've never treated the
GZipStream
like that, but you could try using one of the decompression example out of MSDN found here[^]. If you don't retrieve the original file out of the zip, you may be dealing with a different compression that isn't supported. If you do, then it'd be something to do with how you're reading it out here. Give it a whirl and see what happens.Unfortunately, that example is .NET 4.0 specific. This line:
Decompress.CopyTo(outFile);
Will not work in 3.5, because the CopyTo method does not exist (in fact, if you change the framework version in the link you provided, the example disappears). The example at this url at MSDN (the class page itself) does use the 3-parameter Read method (it is 3.5). Part of my objective is to "read" the contents of a Gzip file *without* extracting it to disk. However, having it stop early is a problem...
-
Unfortunately, that example is .NET 4.0 specific. This line:
Decompress.CopyTo(outFile);
Will not work in 3.5, because the CopyTo method does not exist (in fact, if you change the framework version in the link you provided, the example disappears). The example at this url at MSDN (the class page itself) does use the 3-parameter Read method (it is 3.5). Part of my objective is to "read" the contents of a Gzip file *without* extracting it to disk. However, having it stop early is a problem...
Now that I have access to VS today, I can test your code. After creating a GZip file with .NET, via the following code:
using (GZipStream gz = new GZipStream(new FileInfo(file).Create(), CompressionMode.Compress))
{
byte[] bytes = File.ReadAllBytes(file);gz.Write(bytes, 0, bytes.Length);
}I then ran your code against it and it worked just fine. The question I have is are you accessing a file that isn't local? If so, this may be the source of your problem. Otherwise, you might want to create the zip file with .NET and see if that solves your problem.