Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ICSharpCode.SharpZipLib/Zip/ZipOutputStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public void PutNextPassthroughEntry(ZipEntry entry)

if(entry.CompressionMethod != CompressionMethod.Deflated)
{
throw new ZipException("Only Deflated entries are supported for passthrough");
throw new NotImplementedException("Only Deflated entries are supported for passthrough");
}

if(!string.IsNullOrEmpty(Password))
Expand Down
37 changes: 0 additions & 37 deletions test/ICSharpCode.SharpZipLib.Tests/Zip/GeneralHandling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,43 +386,6 @@ public void StoredNonSeekableKnownSizeNoCrc()
Assert.IsTrue(ZipTesting.TestArchive(ms.ToArray()));
}

[Test]
[Category("Zip")]
public void StoredNonSeekablePrecompressed()
{
var data = Encoding.UTF8.GetBytes("Hello, world");

var crc = new Crc32();
crc.Update(data);

var compressedData = new MemoryStream();
using(var gz = new System.IO.Compression.DeflateStream(compressedData, System.IO.Compression.CompressionMode.Compress, true))
{
gz.Write(data, 0, data.Length);
}
compressedData.Position = 0;

MemoryStream ms = new MemoryStreamWithoutSeek();

using (ZipOutputStream outStream = new ZipOutputStream(ms))
{
outStream.IsStreamOwner = false;

var entry = new ZipEntry("dummyfile.tst");

entry.CompressionMethod = CompressionMethod.Deflated;
entry.Size = data.Length;
entry.Crc = (uint)crc.Value;
entry.CompressedSize = compressedData.Length;

outStream.PutNextPassthroughEntry(entry);

compressedData.CopyTo(outStream);
}

Assert.IsTrue(ZipTesting.TestArchive(ms.ToArray()));
}

[Test]
[Category("Zip")]
public void StoredNonSeekableKnownSizeNoCrcEncrypted()
Expand Down
140 changes: 140 additions & 0 deletions test/ICSharpCode.SharpZipLib.Tests/Zip/PassthroughTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
using System;
using System.IO;
using System.IO.Compression;
using System.Text;
using ICSharpCode.SharpZipLib.Checksum;
using ICSharpCode.SharpZipLib.Tests.TestSupport;
using ICSharpCode.SharpZipLib.Zip;
using NUnit.Framework;

namespace ICSharpCode.SharpZipLib.Tests.Zip
{
[TestFixture]
public class PassthroughTests
{
[Test]
[Category("Zip")]
public void AddingValidPrecompressedEntryToZipOutputStream()
{
using var ms = new MemoryStream();

using (var outStream = new ZipOutputStream(ms){IsStreamOwner = false})
{
var (compressedData, crc, size) = CreateDeflatedData();
var entry = new ZipEntry("dummyfile.tst")
{
CompressionMethod = CompressionMethod.Deflated,
Size = size,
Crc = (uint)crc.Value,
CompressedSize = compressedData.Length,
};

outStream.PutNextPassthroughEntry(entry);

compressedData.CopyTo(outStream);
}

Assert.IsTrue(ZipTesting.TestArchive(ms.ToArray()));
}

private static (MemoryStream, Crc32, int) CreateDeflatedData()
{
var data = Encoding.UTF8.GetBytes("Hello, world");

var crc = new Crc32();
crc.Update(data);

var compressedData = new MemoryStream();
using(var gz = new DeflateStream(compressedData, CompressionMode.Compress, leaveOpen: true))
{
gz.Write(data, 0, data.Length);
}
compressedData.Position = 0;

return (compressedData, crc, data.Length);
}

[Test]
[Category("Zip")]
public void AddingPrecompressedEntryToZipOutputStreamWithInvalidSize()
{
using var outStream = new ZipOutputStream(new MemoryStream());
var (compressedData, crc, size) = CreateDeflatedData();
outStream.Password = "mockpassword";
var entry = new ZipEntry("dummyfile.tst")
{
CompressionMethod = CompressionMethod.Stored,
Crc = (uint)crc.Value,
CompressedSize = compressedData.Length,
};

Assert.Throws<ZipException>(() =>
{
outStream.PutNextPassthroughEntry(entry);
});
}


[Test]
[Category("Zip")]
public void AddingPrecompressedEntryToZipOutputStreamWithInvalidCompressedSize()
{
using var outStream = new ZipOutputStream(new MemoryStream());
var (compressedData, crc, size) = CreateDeflatedData();
outStream.Password = "mockpassword";
var entry = new ZipEntry("dummyfile.tst")
{
CompressionMethod = CompressionMethod.Stored,
Size = size,
Crc = (uint)crc.Value,
};

Assert.Throws<ZipException>(() =>
{
outStream.PutNextPassthroughEntry(entry);
});
}

[Test]
[Category("Zip")]
public void AddingPrecompressedEntryToZipOutputStreamWithNonSupportedMethod()
{
using var outStream = new ZipOutputStream(new MemoryStream());
var (compressedData, crc, size) = CreateDeflatedData();
outStream.Password = "mockpassword";
var entry = new ZipEntry("dummyfile.tst")
{
CompressionMethod = CompressionMethod.LZMA,
Size = size,
Crc = (uint)crc.Value,
CompressedSize = compressedData.Length,
};

Assert.Throws<NotImplementedException>(() =>
{
outStream.PutNextPassthroughEntry(entry);
});
}

[Test]
[Category("Zip")]
public void AddingPrecompressedEntryToZipOutputStreamWithEncryption()
{
using var outStream = new ZipOutputStream(new MemoryStream());
var (compressedData, crc, size) = CreateDeflatedData();
outStream.Password = "mockpassword";
var entry = new ZipEntry("dummyfile.tst")
{
CompressionMethod = CompressionMethod.Deflated,
Size = size,
Crc = (uint)crc.Value,
CompressedSize = compressedData.Length,
};

Assert.Throws<NotImplementedException>(() =>
{
outStream.PutNextPassthroughEntry(entry);
});
}
}
}