GZipStream fails if whole file not read #467

Open
opened 2026-01-29 22:12:33 +00:00 by claunia · 1 comment
Owner

Originally created by @neumatho on GitHub (Jul 21, 2021).

I have the following simple code:

using (FileStream fs = new FileStream(@"<path to file>.gz", FileMode.Open, FileAccess.Read))
{
	GZipStream gz = new GZipStream(fs, CompressionMode.Decompress);

	byte[] buf = new byte[601565];  // Full decompressed length of file
//	byte[] buf = new byte[16];
	gz.Read(buf, 0, buf.Length);

	gz.Dispose();
}

If only part of the file is read, the gz.Dispose() fails with either a "Destination too short" or "CRC checksum mismatch" exception. If the whole file is read, everything works fine.

The file to open, is a single file compressed with GZip. No archive.

Originally created by @neumatho on GitHub (Jul 21, 2021). I have the following simple code: ``` using (FileStream fs = new FileStream(@"<path to file>.gz", FileMode.Open, FileAccess.Read)) { GZipStream gz = new GZipStream(fs, CompressionMode.Decompress); byte[] buf = new byte[601565]; // Full decompressed length of file // byte[] buf = new byte[16]; gz.Read(buf, 0, buf.Length); gz.Dispose(); } ``` If only part of the file is read, the gz.Dispose() fails with either a "Destination too short" or "CRC checksum mismatch" exception. If the whole file is read, everything works fine. The file to open, is a single file compressed with GZip. No archive.
Author
Owner

@adamhathcock commented on GitHub (Aug 17, 2021):

This is how the current GZipStream works because it's trying to validate the data. An option to skip this could possibly be added.

If you don't want to read more, you can just copy the rest of the stream to Stream.Null

@adamhathcock commented on GitHub (Aug 17, 2021): This is how the current GZipStream works because it's trying to validate the data. An option to skip this could possibly be added. If you don't want to read more, you can just copy the rest of the stream to Stream.Null
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#467