a null exception occur with some gzip files #342

Open
opened 2026-01-29 22:10:25 +00:00 by claunia · 4 comments
Owner

Originally created by @Wildcatii on GitHub (Jan 15, 2019).

GZIP file format specification version 4.3
https://tools.ietf.org/html/rfc1952
+---+---+---+---+---+---+---+---+---+---+
|ID1|ID2|CM |FLG| MTIME |XFL|OS | (more-->)
+---+---+---+---+---+---+---+---+---+---+

FLG (FLaGs)
This flag byte is divided into individual bits as follows:
bit 0 FTEXT
bit 1 FHCRC
bit 2 FEXTRA
bit 3 FNAME
bit 4 FCOMMENT
bit 5 reserved
bit 6 reserved
bit 7 reserved

if bit 3 is 0, the Entry.Key will be null, so WriteEntryToDirectory can't get the destinationFileName.
because gzip is a single-file/stream lossless data compression utility(see http://www.gzip.org/)
so we can use the gzip file name without .gz.

I change it like this:

1.add a Name property for RewindableStream
internal class RewindableStream : Stream
{
private readonly Stream stream;
private MemoryStream bufferStream = new MemoryStream();
private bool isRewound;
private bool isDisposed;

    public string Name => (stream as FileStream)?.Name;

2.add a Name property for NonDisposingStream
public class NonDisposingStream : Stream
{
public NonDisposingStream(Stream stream, bool throwOnDispose = false)
{
Stream = stream;
ThrowOnDispose = throwOnDispose;
}

    public string Name => (Stream as RewindableStream)?.Name;

3.in class GZipFilePart's function ReadAndValidateGzipHeader(Stream stream)
in this section, anyway we should supply a name.
if ((header[3] & 0x08) == 0x08)
{
_name = ReadZeroTerminatedString(stream);
}
else
{
var gzFileName = (stream as SharpCompress.IO.NonDisposingStream)?.Name;
var length = gzFileName?.LastIndexOf('.');
if (length.HasValue && length.Value > 0)
{
_name = gzFileName.Substring(0, length.Value);
}
else
{
_name = gzFileName + ".dat";
}
}

Originally created by @Wildcatii on GitHub (Jan 15, 2019). GZIP file format specification version 4.3 https://tools.ietf.org/html/rfc1952 +---+---+---+---+---+---+---+---+---+---+ |ID1|ID2|CM |FLG| MTIME |XFL|OS | (more-->) +---+---+---+---+---+---+---+---+---+---+ FLG (FLaGs) This flag byte is divided into individual bits as follows: bit 0 FTEXT bit 1 FHCRC bit 2 FEXTRA bit 3 FNAME bit 4 FCOMMENT bit 5 reserved bit 6 reserved bit 7 reserved if bit 3 is 0, the Entry.Key will be null, so WriteEntryToDirectory can't get the destinationFileName. because gzip is a single-file/stream lossless data compression utility(see http://www.gzip.org/) so we can use the gzip file name without .gz. I change it like this: 1.add a Name property for RewindableStream internal class RewindableStream : Stream { private readonly Stream stream; private MemoryStream bufferStream = new MemoryStream(); private bool isRewound; private bool isDisposed; public string Name => (stream as FileStream)?.Name; 2.add a Name property for NonDisposingStream public class NonDisposingStream : Stream { public NonDisposingStream(Stream stream, bool throwOnDispose = false) { Stream = stream; ThrowOnDispose = throwOnDispose; } public string Name => (Stream as RewindableStream)?.Name; 3.in class GZipFilePart's function ReadAndValidateGzipHeader(Stream stream) in this section, anyway we should supply a name. if ((header[3] & 0x08) == 0x08) { _name = ReadZeroTerminatedString(stream); } else { var gzFileName = (stream as SharpCompress.IO.NonDisposingStream)?.Name; var length = gzFileName?.LastIndexOf('.'); if (length.HasValue && length.Value > 0) { _name = gzFileName.Substring(0, length.Value); } else { _name = gzFileName + ".dat"; } }
Author
Owner

@prpercival commented on GitHub (Mar 12, 2019):

I also ran into this issue with a file containing header "1f8b 0800"

@prpercival commented on GitHub (Mar 12, 2019): I also ran into this issue with a file containing header "1f8b 0800"
Author
Owner

@prpercival commented on GitHub (Mar 12, 2019):

I tried to implement the changes Wildcatii described and it worked but there was an issue with it never receiving the name so the extracted file is always named the default of ".dat". Also the FileStream type is unsupported on .NET Standard 1.0 so some changes would need to be made for the implementation to support it.

@prpercival commented on GitHub (Mar 12, 2019): I tried to implement the changes Wildcatii described and it worked but there was an issue with it never receiving the name so the extracted file is always named the default of ".dat". Also the FileStream type is unsupported on .NET Standard 1.0 so some changes would need to be made for the implementation to support it.
Author
Owner

@rickyelqasem commented on GitHub (Mar 19, 2019):

In which file do you make those changes?

@rickyelqasem commented on GitHub (Mar 19, 2019): In which file do you make those changes?
Author
Owner

@adamhathcock commented on GitHub (Mar 18, 2022):

A PR with fixes would help everyone. I can more easily help guide with a PR.

@adamhathcock commented on GitHub (Mar 18, 2022): A PR with fixes would help everyone. I can more easily help guide with a PR.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#342