Skip to content

Commit

Permalink
Improve the behaviour of ZipInputStream when reading zip entries with…
Browse files Browse the repository at this point in the history
… BZip2 compression.
  • Loading branch information
Numpsy committed Apr 1, 2019
1 parent afcccb0 commit b02b844
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/ICSharpCode.SharpZipLib/Zip/ZipInputStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,21 @@ public bool CanDecompressEntry
{
get
{
return (entry != null) && entry.CanDecompress;
return (entry != null) && IsEntryCompressionMethodSupported(entry) && entry.CanDecompress;
}
}

/// <summary>
/// Is the compression method for the specified entry supported?
/// </summary>
/// <param name="entry"></param>
/// <returns></returns>
private static bool IsEntryCompressionMethodSupported(ZipEntry entry)
{
return entry.CompressionMethod == CompressionMethod.Deflated ||
entry.CompressionMethod == CompressionMethod.Stored;
}

/// <summary>
/// Advances to the next entry in the archive
/// </summary>
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
Expand Down
28 changes: 28 additions & 0 deletions test/ICSharpCode.SharpZipLib.Tests/Zip/StreamHandling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,5 +324,33 @@ public void SingleLargeEntry()
}
);
}

const string BZip2CompressedZip =
"UEsDBC4AAAAMAEyxgU5p3ou9JwAAAAcAAAAFAAAAYS5kYXRCWmg5MUFZJlNZ0buMcAAAAkgACABA" +
"ACAAIQCCCxdyRThQkNG7jHBQSwECMwAuAAAADABMsYFOad6LvScAAAAHAAAABQAAAAAAAAAAAAAA" +
"AAAAAAAAYS5kYXRQSwUGAAAAAAEAAQAzAAAASgAAAAAA";

/// <summary>
/// Should fail to read a zip with BZip2 compression
/// </summary>
[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<ZipException>(() => zis.Read(buffer, 0, 1), "Trying to read the stream should throw");
}
}
}
}

0 comments on commit b02b844

Please sign in to comment.