Skip to content

Commit 1a130a0

Browse files
authored
Merge PR #390: Override Ensure GZipOutputStream headers are written before flush
* Add unit tests to repro #382 * Add an override of Flush() to GZipOutputStream to ensure the headers is writen before flushing
1 parent 73aa23a commit 1a130a0

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

src/ICSharpCode.SharpZipLib/GZip/GzipOutputStream.cs

+14
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,20 @@ protected override void Dispose(bool disposing)
160160
}
161161
}
162162

163+
/// <summary>
164+
/// Flushes the stream by ensuring the header is written, and then calling <see cref="DeflaterOutputStream.Flush">Flush</see>
165+
/// on the deflater.
166+
/// </summary>
167+
public override void Flush()
168+
{
169+
if (state_ == OutputState.Header)
170+
{
171+
WriteHeader();
172+
}
173+
174+
base.Flush();
175+
}
176+
163177
#endregion Stream overrides
164178

165179
#region DeflaterOutputStream overrides

test/ICSharpCode.SharpZipLib.Tests/GZip/GZipTests.cs

+63
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,37 @@ public void DelayedHeaderWriteNoData()
7676
Assert.IsTrue(data.Length > 0);
7777
}
7878

79+
80+
/// <summary>
81+
/// Variant of DelayedHeaderWriteNoData testing flushing for https://github.com/icsharpcode/SharpZipLib/issues/382
82+
/// </summary>
83+
[Test]
84+
[Category("GZip")]
85+
public void DelayedHeaderWriteFlushNoData()
86+
{
87+
var ms = new MemoryStream();
88+
Assert.AreEqual(0, ms.Length);
89+
90+
using (GZipOutputStream outStream = new GZipOutputStream(ms) { IsStreamOwner = false })
91+
{
92+
// #382 - test flushing the stream before writing to it.
93+
outStream.Flush();
94+
}
95+
96+
ms.Seek(0, SeekOrigin.Begin);
97+
98+
// Test that the gzip stream can be read
99+
var readStream = new MemoryStream();
100+
using (GZipInputStream inStream = new GZipInputStream(ms))
101+
{
102+
inStream.CopyTo(readStream);
103+
}
104+
105+
byte[] data = readStream.ToArray();
106+
107+
Assert.That(data, Is.Empty, "Should not have any decompressed data");
108+
}
109+
79110
/// <summary>
80111
/// Writing GZip headers is delayed so that this stream can be used with HTTP/IIS.
81112
/// </summary>
@@ -99,6 +130,38 @@ public void DelayedHeaderWriteWithData()
99130
Assert.IsTrue(data.Length > 0);
100131
}
101132

133+
/// <summary>
134+
/// variant of DelayedHeaderWriteWithData to test https://github.com/icsharpcode/SharpZipLib/issues/382
135+
/// </summary>
136+
[Test]
137+
[Category("GZip")]
138+
public void DelayedHeaderWriteFlushWithData()
139+
{
140+
var ms = new MemoryStream();
141+
Assert.AreEqual(0, ms.Length);
142+
using (GZipOutputStream outStream = new GZipOutputStream(ms) { IsStreamOwner = false })
143+
{
144+
Assert.AreEqual(0, ms.Length);
145+
146+
// #382 - test flushing the stream before writing to it.
147+
outStream.Flush();
148+
outStream.WriteByte(45);
149+
}
150+
151+
ms.Seek(0, SeekOrigin.Begin);
152+
153+
// Test that the gzip stream can be read
154+
var readStream = new MemoryStream();
155+
using (GZipInputStream inStream = new GZipInputStream(ms))
156+
{
157+
inStream.CopyTo(readStream);
158+
}
159+
160+
// Check that the data was read
161+
byte[] data = readStream.ToArray();
162+
CollectionAssert.AreEqual(new byte[] { 45 }, data, "Decompressed data should match initial data");
163+
}
164+
102165
[Test]
103166
[Category("GZip")]
104167
public void ZeroLengthInputStream()

0 commit comments

Comments
 (0)