diff --git a/src/ICSharpCode.SharpZipLib/Zip/ZipInputStream.cs b/src/ICSharpCode.SharpZipLib/Zip/ZipInputStream.cs index fc783caa0..472407609 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/ZipInputStream.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/ZipInputStream.cs @@ -131,10 +131,21 @@ public bool CanDecompressEntry { get { - return (entry != null) && entry.CanDecompress; + return (entry != null) && IsEntryCompressionMethodSupported(entry) && entry.CanDecompress; } } + /// + /// Is the compression method for the specified entry supported? + /// + /// + /// + private static bool IsEntryCompressionMethodSupported(ZipEntry entry) + { + return entry.CompressionMethod == CompressionMethod.Deflated || + entry.CompressionMethod == CompressionMethod.Stored; + } + /// /// Advances to the next entry in the archive /// @@ -206,10 +217,9 @@ public ZipEntry GetNextEntry() string name = ZipStrings.ConvertToStringExt(flags, buffer); - entry = new ZipEntry(name, versionRequiredToExtract) + entry = new ZipEntry(name, versionRequiredToExtract, ZipConstants.VersionMadeBy, (CompressionMethod)method) { - Flags = flags, - CompressionMethod = (CompressionMethod)method + Flags = flags }; if ((flags & 8) == 0) @@ -272,7 +282,7 @@ public ZipEntry GetNextEntry() } // Determine how to handle reading of data if this is attempted. - if (entry.IsCompressionMethodSupported()) + if (IsEntryCompressionMethodSupported(entry)) { internalReader = new ReadDataHandler(InitialRead); } diff --git a/test/ICSharpCode.SharpZipLib.Tests/Zip/StreamHandling.cs b/test/ICSharpCode.SharpZipLib.Tests/Zip/StreamHandling.cs index 43676f77b..4b8a16b2c 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Zip/StreamHandling.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Zip/StreamHandling.cs @@ -324,5 +324,33 @@ public void SingleLargeEntry() } ); } + + const string BZip2CompressedZip = + "UEsDBC4AAAAMAEyxgU5p3ou9JwAAAAcAAAAFAAAAYS5kYXRCWmg5MUFZJlNZ0buMcAAAAkgACABA" + + "ACAAIQCCCxdyRThQkNG7jHBQSwECMwAuAAAADABMsYFOad6LvScAAAAHAAAABQAAAAAAAAAAAAAA" + + "AAAAAAAAYS5kYXRQSwUGAAAAAAEAAQAzAAAASgAAAAAA"; + + /// + /// Should fail to read a zip with BZip2 compression + /// + [Test] + [Category("Zip")] + public void ShouldReadBZip2EntryButNotDecompress() + { + var fileBytes = System.Convert.FromBase64String(BZip2CompressedZip); + + using (var input = new MemoryStream(fileBytes, false)) + { + var zis = new ZipInputStream(input); + var entry = zis.GetNextEntry(); + + Assert.That(entry.Name, Is.EqualTo("a.dat"), "Should be able to get entry name"); + Assert.That(entry.CompressionMethod, Is.EqualTo(CompressionMethod.BZip2), "Entry should be BZip2 compressed"); + Assert.That(zis.CanDecompressEntry, Is.False, "Should not be able to decompress BZip2 entry"); + + var buffer = new byte[1]; + Assert.Throws(() => zis.Read(buffer, 0, 1), "Trying to read the stream should throw"); + } + } } }