Skip to content

Commit 2e05458

Browse files
authored
Merge pull request #1 from icsharpcode/pr/683-suggestions
Pr/683 suggestions
2 parents af7187d + 89f925d commit 2e05458

File tree

3 files changed

+141
-38
lines changed

3 files changed

+141
-38
lines changed

src/ICSharpCode.SharpZipLib/Zip/ZipOutputStream.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ public void PutNextPassthroughEntry(ZipEntry entry)
328328

329329
if(entry.CompressionMethod != CompressionMethod.Deflated)
330330
{
331-
throw new ZipException("Only Deflated entries are supported for passthrough");
331+
throw new NotImplementedException("Only Deflated entries are supported for passthrough");
332332
}
333333

334334
if(!string.IsNullOrEmpty(Password))

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

-37
Original file line numberDiff line numberDiff line change
@@ -386,43 +386,6 @@ public void StoredNonSeekableKnownSizeNoCrc()
386386
Assert.IsTrue(ZipTesting.TestArchive(ms.ToArray()));
387387
}
388388

389-
[Test]
390-
[Category("Zip")]
391-
public void StoredNonSeekablePrecompressed()
392-
{
393-
var data = Encoding.UTF8.GetBytes("Hello, world");
394-
395-
var crc = new Crc32();
396-
crc.Update(data);
397-
398-
var compressedData = new MemoryStream();
399-
using(var gz = new System.IO.Compression.DeflateStream(compressedData, System.IO.Compression.CompressionMode.Compress, true))
400-
{
401-
gz.Write(data, 0, data.Length);
402-
}
403-
compressedData.Position = 0;
404-
405-
MemoryStream ms = new MemoryStreamWithoutSeek();
406-
407-
using (ZipOutputStream outStream = new ZipOutputStream(ms))
408-
{
409-
outStream.IsStreamOwner = false;
410-
411-
var entry = new ZipEntry("dummyfile.tst");
412-
413-
entry.CompressionMethod = CompressionMethod.Deflated;
414-
entry.Size = data.Length;
415-
entry.Crc = (uint)crc.Value;
416-
entry.CompressedSize = compressedData.Length;
417-
418-
outStream.PutNextPassthroughEntry(entry);
419-
420-
compressedData.CopyTo(outStream);
421-
}
422-
423-
Assert.IsTrue(ZipTesting.TestArchive(ms.ToArray()));
424-
}
425-
426389
[Test]
427390
[Category("Zip")]
428391
public void StoredNonSeekableKnownSizeNoCrcEncrypted()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
using System;
2+
using System.IO;
3+
using System.IO.Compression;
4+
using System.Text;
5+
using ICSharpCode.SharpZipLib.Checksum;
6+
using ICSharpCode.SharpZipLib.Tests.TestSupport;
7+
using ICSharpCode.SharpZipLib.Zip;
8+
using NUnit.Framework;
9+
10+
namespace ICSharpCode.SharpZipLib.Tests.Zip
11+
{
12+
[TestFixture]
13+
public class PassthroughTests
14+
{
15+
[Test]
16+
[Category("Zip")]
17+
public void AddingValidPrecompressedEntryToZipOutputStream()
18+
{
19+
using var ms = new MemoryStream();
20+
21+
using (var outStream = new ZipOutputStream(ms){IsStreamOwner = false})
22+
{
23+
var (compressedData, crc, size) = CreateDeflatedData();
24+
var entry = new ZipEntry("dummyfile.tst")
25+
{
26+
CompressionMethod = CompressionMethod.Deflated,
27+
Size = size,
28+
Crc = (uint)crc.Value,
29+
CompressedSize = compressedData.Length,
30+
};
31+
32+
outStream.PutNextPassthroughEntry(entry);
33+
34+
compressedData.CopyTo(outStream);
35+
}
36+
37+
Assert.IsTrue(ZipTesting.TestArchive(ms.ToArray()));
38+
}
39+
40+
private static (MemoryStream, Crc32, int) CreateDeflatedData()
41+
{
42+
var data = Encoding.UTF8.GetBytes("Hello, world");
43+
44+
var crc = new Crc32();
45+
crc.Update(data);
46+
47+
var compressedData = new MemoryStream();
48+
using(var gz = new DeflateStream(compressedData, CompressionMode.Compress, leaveOpen: true))
49+
{
50+
gz.Write(data, 0, data.Length);
51+
}
52+
compressedData.Position = 0;
53+
54+
return (compressedData, crc, data.Length);
55+
}
56+
57+
[Test]
58+
[Category("Zip")]
59+
public void AddingPrecompressedEntryToZipOutputStreamWithInvalidSize()
60+
{
61+
using var outStream = new ZipOutputStream(new MemoryStream());
62+
var (compressedData, crc, size) = CreateDeflatedData();
63+
outStream.Password = "mockpassword";
64+
var entry = new ZipEntry("dummyfile.tst")
65+
{
66+
CompressionMethod = CompressionMethod.Stored,
67+
Crc = (uint)crc.Value,
68+
CompressedSize = compressedData.Length,
69+
};
70+
71+
Assert.Throws<ZipException>(() =>
72+
{
73+
outStream.PutNextPassthroughEntry(entry);
74+
});
75+
}
76+
77+
78+
[Test]
79+
[Category("Zip")]
80+
public void AddingPrecompressedEntryToZipOutputStreamWithInvalidCompressedSize()
81+
{
82+
using var outStream = new ZipOutputStream(new MemoryStream());
83+
var (compressedData, crc, size) = CreateDeflatedData();
84+
outStream.Password = "mockpassword";
85+
var entry = new ZipEntry("dummyfile.tst")
86+
{
87+
CompressionMethod = CompressionMethod.Stored,
88+
Size = size,
89+
Crc = (uint)crc.Value,
90+
};
91+
92+
Assert.Throws<ZipException>(() =>
93+
{
94+
outStream.PutNextPassthroughEntry(entry);
95+
});
96+
}
97+
98+
[Test]
99+
[Category("Zip")]
100+
public void AddingPrecompressedEntryToZipOutputStreamWithNonSupportedMethod()
101+
{
102+
using var outStream = new ZipOutputStream(new MemoryStream());
103+
var (compressedData, crc, size) = CreateDeflatedData();
104+
outStream.Password = "mockpassword";
105+
var entry = new ZipEntry("dummyfile.tst")
106+
{
107+
CompressionMethod = CompressionMethod.LZMA,
108+
Size = size,
109+
Crc = (uint)crc.Value,
110+
CompressedSize = compressedData.Length,
111+
};
112+
113+
Assert.Throws<NotImplementedException>(() =>
114+
{
115+
outStream.PutNextPassthroughEntry(entry);
116+
});
117+
}
118+
119+
[Test]
120+
[Category("Zip")]
121+
public void AddingPrecompressedEntryToZipOutputStreamWithEncryption()
122+
{
123+
using var outStream = new ZipOutputStream(new MemoryStream());
124+
var (compressedData, crc, size) = CreateDeflatedData();
125+
outStream.Password = "mockpassword";
126+
var entry = new ZipEntry("dummyfile.tst")
127+
{
128+
CompressionMethod = CompressionMethod.Deflated,
129+
Size = size,
130+
Crc = (uint)crc.Value,
131+
CompressedSize = compressedData.Length,
132+
};
133+
134+
Assert.Throws<NotImplementedException>(() =>
135+
{
136+
outStream.PutNextPassthroughEntry(entry);
137+
});
138+
}
139+
}
140+
}

0 commit comments

Comments
 (0)