Skip to content

Commit 220ee4e

Browse files
committed
Unit tests for zip files with BZip2 compression.
1 parent 1c273af commit 220ee4e

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
@@ -1481,5 +1481,136 @@ public void HostSystemPersistedFromZipFile()
14811481
}
14821482
}
14831483
}
1484+
1485+
/// <summary>
1486+
/// Test a zip file using BZip2 compression.
1487+
/// </summary>
1488+
[Test]
1489+
[Category("Zip")]
1490+
public void ZipWithBZip2Compression()
1491+
{
1492+
using (var memStream = new MemoryStream())
1493+
{
1494+
using (ZipFile f = new ZipFile(memStream, leaveOpen: true))
1495+
{
1496+
f.BeginUpdate(new MemoryArchiveStorage());
1497+
1498+
var m = new StringMemoryDataSource("BZip2Compressed");
1499+
f.Add(m, "a.dat", CompressionMethod.BZip2);
1500+
1501+
var m2 = new StringMemoryDataSource("DeflateCompressed");
1502+
f.Add(m2, "b.dat", CompressionMethod.Deflated);
1503+
f.CommitUpdate();
1504+
Assert.IsTrue(f.TestArchive(true));
1505+
}
1506+
1507+
memStream.Seek(0, SeekOrigin.Begin);
1508+
1509+
using (ZipFile f = new ZipFile(memStream))
1510+
{
1511+
{
1512+
var entry = f.GetEntry("a.dat");
1513+
Assert.That(entry.CompressionMethod, Is.EqualTo(CompressionMethod.BZip2), "Compression method should be BZip2");
1514+
Assert.That(entry.Version, Is.EqualTo(ZipConstants.VersionBZip2), "Entry version should be 46");
1515+
1516+
using (var reader = new StreamReader(f.GetInputStream(entry)))
1517+
{
1518+
string contents = reader.ReadToEnd();
1519+
Assert.That(contents, Is.EqualTo("BZip2Compressed"), "extract string must match original string");
1520+
}
1521+
}
1522+
1523+
{
1524+
var entry = f.GetEntry("b.dat");
1525+
Assert.That(entry.CompressionMethod, Is.EqualTo(CompressionMethod.Deflated), "Compression method should be Deflated");
1526+
1527+
using (var reader = new StreamReader(f.GetInputStream(entry)))
1528+
{
1529+
string contents = reader.ReadToEnd();
1530+
Assert.That(contents, Is.EqualTo("DeflateCompressed"), "extract string must match original string");
1531+
}
1532+
}
1533+
}
1534+
}
1535+
}
1536+
1537+
/// <summary>
1538+
/// We should be able to read a bzip2 compressed zip file created by 7-zip.
1539+
/// </summary>
1540+
[Test]
1541+
[Category("Zip")]
1542+
public void ShouldReadBZip2ZipCreatedBy7Zip()
1543+
{
1544+
const string BZip2CompressedZipCreatedBy7Zip =
1545+
"UEsDBC4AAAAMAIa50U4/rHf5qwAAAK8AAAAJAAAASGVsbG8udHh0QlpoOTFBWSZTWTL8pwYAA" +
1546+
"BWfgEhlUAAiLUgQP+feMCAAiCKaeiaBobU9JiaAMGmoak9GmRNqPUDQ9T1PQsz/t9B6YvEdvF" +
1547+
"5dhwXzGE1ooO41A6TtATBEFxFUq6trGtUcSJDyWWWj/S2VwY15fy3IqHi3hHUS+K76zdoDzQa" +
1548+
"VGE/4YkYZe3JAtv1EsIqIsiTnnktIbBo1R4xY3JZEOm2BvwLuSKcKEgZflODAUEsBAj8ALgAA" +
1549+
"AAwAhrnRTj+sd/mrAAAArwAAAAkAJAAAAAAAAAAgAAAAAAAAAEhlbGxvLnR4dAoAIAAAAAAAA" +
1550+
"QAYAO97MLZZJdUB73swtlkl1QEK0UTFWCXVAVBLBQYAAAAAAQABAFsAAADSAAAAAAA=";
1551+
1552+
const string OriginalText =
1553+
"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.";
1554+
1555+
var fileBytes = System.Convert.FromBase64String(BZip2CompressedZipCreatedBy7Zip);
1556+
1557+
using (var input = new MemoryStream(fileBytes, false))
1558+
{
1559+
using (ZipFile f = new ZipFile(input))
1560+
{
1561+
var entry = f.GetEntry("Hello.txt");
1562+
Assert.That(entry.CompressionMethod, Is.EqualTo(CompressionMethod.BZip2), "Compression method should be BZip2");
1563+
Assert.That(entry.Version, Is.EqualTo(ZipConstants.VersionBZip2), "Entry version should be 46");
1564+
1565+
using (var reader = new StreamReader(f.GetInputStream(entry)))
1566+
{
1567+
string contents = reader.ReadToEnd();
1568+
Assert.That(contents, Is.EqualTo(OriginalText), "extract string must match original string");
1569+
}
1570+
}
1571+
}
1572+
}
1573+
1574+
/// <summary>
1575+
/// We should be able to read a bzip2 compressed / AES encrypted zip file created by 7-zip.
1576+
/// </summary>
1577+
[Test]
1578+
[Category("Zip")]
1579+
public void ShouldReadAESBZip2ZipCreatedBy7Zip()
1580+
{
1581+
const string BZip2CompressedZipCreatedBy7Zip =
1582+
"UEsDBDMAAQBjAIa50U4AAAAAxwAAAK8AAAAJAAsASGVsbG8udHh0AZkHAAIAQUUDDAAYg6jqf" +
1583+
"kvZClVMOtgmqKT0/8I9fMPgo96myxw9hLQUhKj1Qczi3fT7QIhAnAKU+u03nA8rCKGWmDI5Qz" +
1584+
"qPREy95boQVDPwmwEsWksv3GAWzMfzZUhmB/TgIJlA34a4yP0f2ucy3/QCQYo8QcHjBtjWX5b" +
1585+
"dZn0+fwY9Ci7q8JSI8zNSbgQ0Ert/lIJ9MxQ4lzBxMl4LySkd104cDPh/FslTAcPtHoy8Mf1c" +
1586+
"vnI1uICMgjWVeTqYrvSvt2uuHnqr4AiehArFiXTnUEsBAj8AMwABAGMAhrnRTgAAAADHAAAAr" +
1587+
"wAAAAkALwAAAAAAAAAgAAAAAAAAAEhlbGxvLnR4dAoAIAAAAAAAAQAYAO97MLZZJdUBYdnjul" +
1588+
"kl1QEK0UTFWCXVAQGZBwACAEFFAwwAUEsFBgAAAAABAAEAZgAAAPkAAAAAAA==";
1589+
1590+
const string OriginalText =
1591+
"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.";
1592+
1593+
var fileBytes = System.Convert.FromBase64String(BZip2CompressedZipCreatedBy7Zip);
1594+
1595+
using (var input = new MemoryStream(fileBytes, false))
1596+
{
1597+
using (ZipFile f = new ZipFile(input))
1598+
{
1599+
f.Password = "password";
1600+
1601+
var entry = f.GetEntry("Hello.txt");
1602+
Assert.That(entry.CompressionMethod, Is.EqualTo(CompressionMethod.BZip2), "Compression method should be BZip2");
1603+
Assert.That(entry.Version, Is.EqualTo(ZipConstants.VERSION_AES), "Entry version should be 51");
1604+
Assert.That(entry.IsCrypted, Is.True, "Entry should be encrypted");
1605+
Assert.That(entry.AESKeySize, Is.EqualTo(256), "AES Keysize should be 256");
1606+
1607+
using (var reader = new StreamReader(f.GetInputStream(entry)))
1608+
{
1609+
string contents = reader.ReadToEnd();
1610+
Assert.That(contents, Is.EqualTo(OriginalText), "extract string must match original string");
1611+
}
1612+
}
1613+
}
1614+
}
14841615
}
14851616
}

0 commit comments

Comments
 (0)