GZipStream appears to malfunction (24K maximum)
-
I am getting an odd result when trying to decompress .gz files... The method that I am using only decompresses the first 24KB of the file, but the actual content is over 100 MB. I am using .NET 3.5, with the decompression method from the MSDN article (copied below). Has anyone seen this odd behavior before..?
/// <summary>
/// G-ZIP (.gz) Decompression method. Places new file in the same folder.
/// </summary>
/// <see cref="http://msdn2.microsoft.com/en-us/library/system.io.compression.gzipstream.aspx"/>
/// <param name="file">File to be decompressed.</param>
private static void gDecompress(FileInfo file)
{
// Get the stream of the source file.
using (FileStream inFile = file.OpenRead())
{
// Get original file extension, for example "doc" from report.doc.gz.
string curFile = file.FullName;
string origName = curFile.Remove(curFile.Length - file.Extension.Length);//Create the decompressed file. using (FileStream outFile = File.Create(origName)) { using (GZipStream Decompress = new GZipStream(inFile, CompressionMode.Decompress)) { //Copy the decompression stream into the output file. byte\[\] buffer = new byte\[4096\]; int numRead; while ((numRead = Decompress.Read(buffer, 0, buffer.Length)) != 0) { outFile.Write(buffer, 0, numRead); } } } }
}
-
I am getting an odd result when trying to decompress .gz files... The method that I am using only decompresses the first 24KB of the file, but the actual content is over 100 MB. I am using .NET 3.5, with the decompression method from the MSDN article (copied below). Has anyone seen this odd behavior before..?
/// <summary>
/// G-ZIP (.gz) Decompression method. Places new file in the same folder.
/// </summary>
/// <see cref="http://msdn2.microsoft.com/en-us/library/system.io.compression.gzipstream.aspx"/>
/// <param name="file">File to be decompressed.</param>
private static void gDecompress(FileInfo file)
{
// Get the stream of the source file.
using (FileStream inFile = file.OpenRead())
{
// Get original file extension, for example "doc" from report.doc.gz.
string curFile = file.FullName;
string origName = curFile.Remove(curFile.Length - file.Extension.Length);//Create the decompressed file. using (FileStream outFile = File.Create(origName)) { using (GZipStream Decompress = new GZipStream(inFile, CompressionMode.Decompress)) { //Copy the decompression stream into the output file. byte\[\] buffer = new byte\[4096\]; int numRead; while ((numRead = Decompress.Read(buffer, 0, buffer.Length)) != 0) { outFile.Write(buffer, 0, numRead); } } } }
}
Timothy CIAN wrote:
Has anyone seen this odd behavior before..?
I haven't. I did look into MSDN a bit, and I noticed the code examples for 2.0, 3.5, and 4.0 are all different, although I can't tell why; they all look good to me. [ADDED] I also ran the 2.0 example on a 1.5MB file, and all was well.[/ADDED] What makes you think the decompression terminates early? Are you sure the input file is what you intend it to be, maybe you are using similar code and it has a bug somewhere. I suggest you add (re-insert) debug statements, then compare the original uncompressed file size, the compressed one, and the final decompressed one. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
Timothy CIAN wrote:
Has anyone seen this odd behavior before..?
I haven't. I did look into MSDN a bit, and I noticed the code examples for 2.0, 3.5, and 4.0 are all different, although I can't tell why; they all look good to me. [ADDED] I also ran the 2.0 example on a 1.5MB file, and all was well.[/ADDED] What makes you think the decompression terminates early? Are you sure the input file is what you intend it to be, maybe you are using similar code and it has a bug somewhere. I suggest you add (re-insert) debug statements, then compare the original uncompressed file size, the compressed one, and the final decompressed one. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
Well, I have it attempting to decompress a collection of .gz files which are generated as output from a 3rd-party application. However, since I don't see anyone else (via Google search) having this problem, I shall assume it is either (1) something odd about how the 3rd-party source is (mis)creating .gz files, or (2) something is wonky elsewhere with the code. #2 seems unlikely, as this is a static method that only takes a FileInfo as input. It seems fairly self-contained. However, my next step is to create a tiny app that I can test this with (in better isolation from the "other stuff" in my code), as well as a parallel test using something like SharpZIPlib to see if there is any difference. Thanks...
-
Well, I have it attempting to decompress a collection of .gz files which are generated as output from a 3rd-party application. However, since I don't see anyone else (via Google search) having this problem, I shall assume it is either (1) something odd about how the 3rd-party source is (mis)creating .gz files, or (2) something is wonky elsewhere with the code. #2 seems unlikely, as this is a static method that only takes a FileInfo as input. It seems fairly self-contained. However, my next step is to create a tiny app that I can test this with (in better isolation from the "other stuff" in my code), as well as a parallel test using something like SharpZIPlib to see if there is any difference. Thanks...
Update... The .gz appears fine; I was able to extract it using WinZip (224 MB -> 877 MB). I tested Ionic's version of GZipStream, and it also resulted in a 24K file, along with the following error message:
Destination array was not long enough. Check destIndex and length, and the array's lower bounds.
Here is the Ionic-GZipStream version (which is almost identical to the previous):/// <summary>
/// G-ZIP (.gz) Decompression method, using Ionic library. Places new file in the same folder.
/// </summary>
/// <param name="file">File to be decompressed.</param>
private static void gDecompressIonic(FileInfo file)
{
// Get the stream of the source file.
using (FileStream inFile = file.OpenRead())
{
// Get original file extension, for example "doc" from report.doc.gz.
string curFile = file.FullName;
string origName = curFile.Remove(curFile.Length - file.Extension.Length);//Create the decompressed file. using (FileStream outFile = File.Create(origName)) { using (Ionic.Zlib.GZipStream Decompress = new Ionic.Zlib.GZipStream(inFile, Ionic.Zlib.CompressionMode.Decompress)) { //Copy the decompression stream into the output file. byte\[\] buffer = new byte\[4096\]; int numRead; while ((numRead = Decompress.Read(buffer, 0, buffer.Length)) != 0) { outFile.Write(buffer, 0, numRead); } } } }
}
-
Update... The .gz appears fine; I was able to extract it using WinZip (224 MB -> 877 MB). I tested Ionic's version of GZipStream, and it also resulted in a 24K file, along with the following error message:
Destination array was not long enough. Check destIndex and length, and the array's lower bounds.
Here is the Ionic-GZipStream version (which is almost identical to the previous):/// <summary>
/// G-ZIP (.gz) Decompression method, using Ionic library. Places new file in the same folder.
/// </summary>
/// <param name="file">File to be decompressed.</param>
private static void gDecompressIonic(FileInfo file)
{
// Get the stream of the source file.
using (FileStream inFile = file.OpenRead())
{
// Get original file extension, for example "doc" from report.doc.gz.
string curFile = file.FullName;
string origName = curFile.Remove(curFile.Length - file.Extension.Length);//Create the decompressed file. using (FileStream outFile = File.Create(origName)) { using (Ionic.Zlib.GZipStream Decompress = new Ionic.Zlib.GZipStream(inFile, Ionic.Zlib.CompressionMode.Decompress)) { //Copy the decompression stream into the output file. byte\[\] buffer = new byte\[4096\]; int numRead; while ((numRead = Decompress.Read(buffer, 0, buffer.Length)) != 0) { outFile.Write(buffer, 0, numRead); } } } }
}
-
Update... The .gz appears fine; I was able to extract it using WinZip (224 MB -> 877 MB). I tested Ionic's version of GZipStream, and it also resulted in a 24K file, along with the following error message:
Destination array was not long enough. Check destIndex and length, and the array's lower bounds.
Here is the Ionic-GZipStream version (which is almost identical to the previous):/// <summary>
/// G-ZIP (.gz) Decompression method, using Ionic library. Places new file in the same folder.
/// </summary>
/// <param name="file">File to be decompressed.</param>
private static void gDecompressIonic(FileInfo file)
{
// Get the stream of the source file.
using (FileStream inFile = file.OpenRead())
{
// Get original file extension, for example "doc" from report.doc.gz.
string curFile = file.FullName;
string origName = curFile.Remove(curFile.Length - file.Extension.Length);//Create the decompressed file. using (FileStream outFile = File.Create(origName)) { using (Ionic.Zlib.GZipStream Decompress = new Ionic.Zlib.GZipStream(inFile, Ionic.Zlib.CompressionMode.Decompress)) { //Copy the decompression stream into the output file. byte\[\] buffer = new byte\[4096\]; int numRead; while ((numRead = Decompress.Read(buffer, 0, buffer.Length)) != 0) { outFile.Write(buffer, 0, numRead); } } } }
}
-
I am getting an odd result when trying to decompress .gz files... The method that I am using only decompresses the first 24KB of the file, but the actual content is over 100 MB. I am using .NET 3.5, with the decompression method from the MSDN article (copied below). Has anyone seen this odd behavior before..?
/// <summary>
/// G-ZIP (.gz) Decompression method. Places new file in the same folder.
/// </summary>
/// <see cref="http://msdn2.microsoft.com/en-us/library/system.io.compression.gzipstream.aspx"/>
/// <param name="file">File to be decompressed.</param>
private static void gDecompress(FileInfo file)
{
// Get the stream of the source file.
using (FileStream inFile = file.OpenRead())
{
// Get original file extension, for example "doc" from report.doc.gz.
string curFile = file.FullName;
string origName = curFile.Remove(curFile.Length - file.Extension.Length);//Create the decompressed file. using (FileStream outFile = File.Create(origName)) { using (GZipStream Decompress = new GZipStream(inFile, CompressionMode.Decompress)) { //Copy the decompression stream into the output file. byte\[\] buffer = new byte\[4096\]; int numRead; while ((numRead = Decompress.Read(buffer, 0, buffer.Length)) != 0) { outFile.Write(buffer, 0, numRead); } } } }
}
FWIW: I just finished a WinZip-like (but incompatible) utility based on System.IO.Compression.GZipStream and it perfectly decompresses files it compressed before, whatever the file size is (tried up to 2MB). Typical compression ratio is about 3 except of course for PDF, JPEG, and very small files. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum