Skip to content

Commit 446cebd

Browse files
committed
Unit tests for zip files with BZip2 compression.
1 parent 14865a7 commit 446cebd

File tree

2 files changed

+132
-1
lines changed

2 files changed

+132
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public void UnsupportedCompressionMethod()
141141
var ze = new ZipEntry("HumblePie");
142142
//ze.CompressionMethod = CompressionMethod.BZip2;
143143

144-
Assert.That(() => ze.CompressionMethod = CompressionMethod.BZip2,
144+
Assert.That(() => ze.CompressionMethod = CompressionMethod.Deflate64,
145145
Throws.TypeOf<NotSupportedException>());
146146
}
147147

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

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,5 +1611,136 @@ public void AddFileWithAlternateName()
16111611
}
16121612
}
16131613
}
1614+
1615+
/// <summary>
1616+
/// Test a zip file using BZip2 compression.
1617+
/// </summary>
1618+
[Test]
1619+
[Category("Zip")]
1620+
public void ZipWithBZip2Compression()
1621+
{
1622+
using (var memStream = new MemoryStream())
1623+
{
1624+
using (ZipFile f = new ZipFile(memStream, leaveOpen: true))
1625+
{
1626+
f.BeginUpdate(new MemoryArchiveStorage());
1627+
1628+
var m = new StringMemoryDataSource("BZip2Compressed");
1629+
f.Add(m, "a.dat", CompressionMethod.BZip2);
1630+
1631+
var m2 = new StringMemoryDataSource("DeflateCompressed");
1632+
f.Add(m2, "b.dat", CompressionMethod.Deflated);
1633+
f.CommitUpdate();
1634+
Assert.IsTrue(f.TestArchive(true));
1635+
}
1636+
1637+
memStream.Seek(0, SeekOrigin.Begin);
1638+
1639+
using (ZipFile f = new ZipFile(memStream))
1640+
{
1641+
{
1642+
var entry = f.GetEntry("a.dat");
1643+
Assert.That(entry.CompressionMethod, Is.EqualTo(CompressionMethod.BZip2), "Compression method should be BZip2");
1644+
Assert.That(entry.Version, Is.EqualTo(ZipConstants.VersionBZip2), "Entry version should be 46");
1645+
1646+
using (var reader = new StreamReader(f.GetInputStream(entry)))
1647+
{
1648+
string contents = reader.ReadToEnd();
1649+
Assert.That(contents, Is.EqualTo("BZip2Compressed"), "extract string must match original string");
1650+
}
1651+
}
1652+
1653+
{
1654+
var entry = f.GetEntry("b.dat");
1655+
Assert.That(entry.CompressionMethod, Is.EqualTo(CompressionMethod.Deflated), "Compression method should be Deflated");
1656+
1657+
using (var reader = new StreamReader(f.GetInputStream(entry)))
1658+
{
1659+
string contents = reader.ReadToEnd();
1660+
Assert.That(contents, Is.EqualTo("DeflateCompressed"), "extract string must match original string");
1661+
}
1662+
}
1663+
}
1664+
}
1665+
}
1666+
1667+
/// <summary>
1668+
/// We should be able to read a bzip2 compressed zip file created by 7-zip.
1669+
/// </summary>
1670+
[Test]
1671+
[Category("Zip")]
1672+
public void ShouldReadBZip2ZipCreatedBy7Zip()
1673+
{
1674+
const string BZip2CompressedZipCreatedBy7Zip =
1675+
"UEsDBC4AAAAMAIa50U4/rHf5qwAAAK8AAAAJAAAASGVsbG8udHh0QlpoOTFBWSZTWTL8pwYAA" +
1676+
"BWfgEhlUAAiLUgQP+feMCAAiCKaeiaBobU9JiaAMGmoak9GmRNqPUDQ9T1PQsz/t9B6YvEdvF" +
1677+
"5dhwXzGE1ooO41A6TtATBEFxFUq6trGtUcSJDyWWWj/S2VwY15fy3IqHi3hHUS+K76zdoDzQa" +
1678+
"VGE/4YkYZe3JAtv1EsIqIsiTnnktIbBo1R4xY3JZEOm2BvwLuSKcKEgZflODAUEsBAj8ALgAA" +
1679+
"AAwAhrnRTj+sd/mrAAAArwAAAAkAJAAAAAAAAAAgAAAAAAAAAEhlbGxvLnR4dAoAIAAAAAAAA" +
1680+
"QAYAO97MLZZJdUB73swtlkl1QEK0UTFWCXVAVBLBQYAAAAAAQABAFsAAADSAAAAAAA=";
1681+
1682+
const string OriginalText =
1683+
"SharpZipLib (#ziplib, formerly NZipLib) is a compression library that supports Zip files using both stored and deflate compression methods, PKZIP 2.0 style and AES encryption.";
1684+
1685+
var fileBytes = System.Convert.FromBase64String(BZip2CompressedZipCreatedBy7Zip);
1686+
1687+
using (var input = new MemoryStream(fileBytes, false))
1688+
{
1689+
using (ZipFile f = new ZipFile(input))
1690+
{
1691+
var entry = f.GetEntry("Hello.txt");
1692+
Assert.That(entry.CompressionMethod, Is.EqualTo(CompressionMethod.BZip2), "Compression method should be BZip2");
1693+
Assert.That(entry.Version, Is.EqualTo(ZipConstants.VersionBZip2), "Entry version should be 46");
1694+
1695+
using (var reader = new StreamReader(f.GetInputStream(entry)))
1696+
{
1697+
string contents = reader.ReadToEnd();
1698+
Assert.That(contents, Is.EqualTo(OriginalText), "extract string must match original string");
1699+
}
1700+
}
1701+
}
1702+
}
1703+
1704+
/// <summary>
1705+
/// We should be able to read a bzip2 compressed / AES encrypted zip file created by 7-zip.
1706+
/// </summary>
1707+
[Test]
1708+
[Category("Zip")]
1709+
public void ShouldReadAESBZip2ZipCreatedBy7Zip()
1710+
{
1711+
const string BZip2CompressedZipCreatedBy7Zip =
1712+
"UEsDBDMAAQBjAIa50U4AAAAAxwAAAK8AAAAJAAsASGVsbG8udHh0AZkHAAIAQUUDDAAYg6jqf" +
1713+
"kvZClVMOtgmqKT0/8I9fMPgo96myxw9hLQUhKj1Qczi3fT7QIhAnAKU+u03nA8rCKGWmDI5Qz" +
1714+
"qPREy95boQVDPwmwEsWksv3GAWzMfzZUhmB/TgIJlA34a4yP0f2ucy3/QCQYo8QcHjBtjWX5b" +
1715+
"dZn0+fwY9Ci7q8JSI8zNSbgQ0Ert/lIJ9MxQ4lzBxMl4LySkd104cDPh/FslTAcPtHoy8Mf1c" +
1716+
"vnI1uICMgjWVeTqYrvSvt2uuHnqr4AiehArFiXTnUEsBAj8AMwABAGMAhrnRTgAAAADHAAAAr" +
1717+
"wAAAAkALwAAAAAAAAAgAAAAAAAAAEhlbGxvLnR4dAoAIAAAAAAAAQAYAO97MLZZJdUBYdnjul" +
1718+
"kl1QEK0UTFWCXVAQGZBwACAEFFAwwAUEsFBgAAAAABAAEAZgAAAPkAAAAAAA==";
1719+
1720+
const string OriginalText =
1721+
"SharpZipLib (#ziplib, formerly NZipLib) is a compression library that supports Zip files using both stored and deflate compression methods, PKZIP 2.0 style and AES encryption.";
1722+
1723+
var fileBytes = System.Convert.FromBase64String(BZip2CompressedZipCreatedBy7Zip);
1724+
1725+
using (var input = new MemoryStream(fileBytes, false))
1726+
{
1727+
using (ZipFile f = new ZipFile(input))
1728+
{
1729+
f.Password = "password";
1730+
1731+
var entry = f.GetEntry("Hello.txt");
1732+
Assert.That(entry.CompressionMethod, Is.EqualTo(CompressionMethod.BZip2), "Compression method should be BZip2");
1733+
Assert.That(entry.Version, Is.EqualTo(ZipConstants.VERSION_AES), "Entry version should be 51");
1734+
Assert.That(entry.IsCrypted, Is.True, "Entry should be encrypted");
1735+
Assert.That(entry.AESKeySize, Is.EqualTo(256), "AES Keysize should be 256");
1736+
1737+
using (var reader = new StreamReader(f.GetInputStream(entry)))
1738+
{
1739+
string contents = reader.ReadToEnd();
1740+
Assert.That(contents, Is.EqualTo(OriginalText), "extract string must match original string");
1741+
}
1742+
}
1743+
}
1744+
}
16141745
}
16151746
}

0 commit comments

Comments
 (0)