Skip to content

Commit 27ac139

Browse files
Numpsypiksel
authored andcommitted
Merge PR #352: Change ZipInputStream.GetNextEntry to set the entry compression method via its constructor
* Add a unit test for ZipInputStream reading an entry with an unsupported compression type * Change ZipInputStream.GetNextEntry to pass the compression method to the ZipEntry constructor.
1 parent 5cca93d commit 27ac139

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/ICSharpCode.SharpZipLib/Zip/ZipInputStream.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,9 @@ public ZipEntry GetNextEntry()
206206

207207
string name = ZipStrings.ConvertToStringExt(flags, buffer);
208208

209-
entry = new ZipEntry(name, versionRequiredToExtract)
209+
entry = new ZipEntry(name, versionRequiredToExtract, ZipConstants.VersionMadeBy, (CompressionMethod)method)
210210
{
211211
Flags = flags,
212-
CompressionMethod = (CompressionMethod)method
213212
};
214213

215214
if ((flags & 8) == 0)

test/ICSharpCode.SharpZipLib.Tests/Zip/StreamHandling.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,5 +324,33 @@ public void SingleLargeEntry()
324324
}
325325
);
326326
}
327+
328+
const string BZip2CompressedZip =
329+
"UEsDBC4AAAAMAEyxgU5p3ou9JwAAAAcAAAAFAAAAYS5kYXRCWmg5MUFZJlNZ0buMcAAAAkgACABA" +
330+
"ACAAIQCCCxdyRThQkNG7jHBQSwECMwAuAAAADABMsYFOad6LvScAAAAHAAAABQAAAAAAAAAAAAAA" +
331+
"AAAAAAAAYS5kYXRQSwUGAAAAAAEAAQAzAAAASgAAAAAA";
332+
333+
/// <summary>
334+
/// Should fail to read a zip with BZip2 compression
335+
/// </summary>
336+
[Test]
337+
[Category("Zip")]
338+
public void ShouldReadBZip2EntryButNotDecompress()
339+
{
340+
var fileBytes = System.Convert.FromBase64String(BZip2CompressedZip);
341+
342+
using (var input = new MemoryStream(fileBytes, false))
343+
{
344+
var zis = new ZipInputStream(input);
345+
var entry = zis.GetNextEntry();
346+
347+
Assert.That(entry.Name, Is.EqualTo("a.dat"), "Should be able to get entry name");
348+
Assert.That(entry.CompressionMethod, Is.EqualTo(CompressionMethod.BZip2), "Entry should be BZip2 compressed");
349+
Assert.That(zis.CanDecompressEntry, Is.False, "Should not be able to decompress BZip2 entry");
350+
351+
var buffer = new byte[1];
352+
Assert.Throws<ZipException>(() => zis.Read(buffer, 0, 1), "Trying to read the stream should throw");
353+
}
354+
}
327355
}
328356
}

0 commit comments

Comments
 (0)