mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-02-03 21:23:38 +00:00
a null exception occur with some gzip files #342
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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;
2.add a Name property for NonDisposingStream
public class NonDisposingStream : Stream
{
public NonDisposingStream(Stream stream, bool throwOnDispose = false)
{
Stream = stream;
ThrowOnDispose = throwOnDispose;
}
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";
}
}
@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 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.
@rickyelqasem commented on GitHub (Mar 19, 2019):
In which file do you make those changes?
@adamhathcock commented on GitHub (Mar 18, 2022):
A PR with fixes would help everyone. I can more easily help guide with a PR.