adamhathcock / sharpcompress

SharpCompress is a fully managed C# library to deal with many compression types and formats.
MIT License
2.22k stars 476 forks source link

WriteEntryToDirectory with GZipReader fails #831

Closed edwardmjackson closed 2 months ago

edwardmjackson commented 2 months ago

The generic extraction mechanism implemented in ExtractionMethods.cs appears to fail for GZip files.

Path.Combine throws an exception at https://github.com/adamhathcock/sharpcompress/blob/900190cf54423ebc1187b647094b118881ab9485/src/SharpCompress/Common/ExtractionMethods.cs#L60

due to fileName being null, which is set here

https://github.com/adamhathcock/sharpcompress/blob/900190cf54423ebc1187b647094b118881ab9485/src/SharpCompress/Common/ExtractionMethods.cs#L39

StackTrace:
   at System.IO.Path.Combine(String path1, String path2) in System.IO\Path.cs:line 310
   at SharpCompress.Common.ExtractionMethods.WriteEntryToDirectory(IEntry entry, String destinationDirectory, ExtractionOptions options, Action`2 write) in SharpCompress.Common\ExtractionMethods.cs:line 44
   at SharpCompress.Readers.IReaderExtensions.WriteEntryToDirectory(IReader reader, String destinationDirectory, ExtractionOptions options) in SharpCompress.Readers\IReaderExtensions.cs:line 31
   at SharpCompress.Readers.IReaderExtensions.WriteAllToDirectory(IReader reader, String destinationDirectory, ExtractionOptions options) in SharpCompress.Readers\IReaderExtensions.cs:line 22
edwardmjackson commented 2 months ago

As a follow up, this is probably a sketchy file header in the gzip file, looking at https://github.com/adamhathcock/sharpcompress/blob/900190cf54423ebc1187b647094b118881ab9485/src/SharpCompress/Common/GZip/GZipFilePart.cs#L94

Trivial workaround for now:

using IReader reader = ReaderFactory.Open(sourceStream);

while (reader.MoveToNextEntry())
{
  if (!reader.Entry.IsDirectory)
  {
    reader.WriteEntryToFile(decompressedFileLocation, new ExtractionOptions { ExtractFullPath = false, Overwrite = true });
  }
}
adamhathcock commented 2 months ago

you're saying the entry.Key can be null?

edwardmjackson commented 2 months ago

Correct. The ! on this line

https://github.com/adamhathcock/sharpcompress/blob/900190cf54423ebc1187b647094b118881ab9485/src/SharpCompress/Common/GZip/GZipFilePart.cs#L37

is masking it, but it can be null (in possibly wierdly-formed files that open fine in 7Zip)

adamhathcock commented 2 months ago

0.37.0 has fixed null support so maybe this isn't an issue now?