Skip to content

Commit

Permalink
Merge pull request adamhathcock#417 from KyotoFox/fix-entrystream-flush
Browse files Browse the repository at this point in the history
Allow Flush on EntryStream
  • Loading branch information
adamhathcock authored Oct 4, 2018
2 parents f1facc5 + 53ad00c commit 0941239
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/SharpCompress/Common/EntryStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ protected override void Dispose(bool disposing)

public override bool CanWrite => false;

public override void Flush()
{
throw new NotSupportedException();
public override void Flush() {
}

public override long Length => _stream.Length;
Expand Down
57 changes: 57 additions & 0 deletions tests/SharpCompress.Test/Mocks/FlushOnDisposeStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.IO;

namespace SharpCompress.Test.Mocks
{
// This is a simplified version of CryptoStream that always flushes the inner stream on Dispose to trigger an error in EntryStream
// CryptoStream doesn't always trigger the Flush, so this class is used instead
// See https://referencesource.microsoft.com/#mscorlib/system/security/cryptography/cryptostream.cs,141

public class FlushOnDisposeStream : Stream, IDisposable
{
private Stream inner;

public FlushOnDisposeStream(Stream innerStream) {
this.inner = innerStream;
}

public override bool CanRead => this.inner.CanRead;

public override bool CanSeek => false;

public override bool CanWrite => false;

public override long Length => this.inner.Length;

public override long Position { get => this.inner.Position; set => this.inner.Position = value; }

public override void Flush() {
throw new NotImplementedException();
}

public override int Read(byte[] buffer, int offset, int count) {
return this.inner.Read(buffer, offset, count);
}

public override long Seek(long offset, SeekOrigin origin) {
throw new NotImplementedException();
}

public override void SetLength(long value) {
throw new NotImplementedException();
}

public override void Write(byte[] buffer, int offset, int count) {
throw new NotImplementedException();
}

protected override void Dispose(bool disposing) {
if(disposing) {
this.inner.Flush();
this.inner.Close();
}

base.Dispose(disposing);
}
}
}
26 changes: 26 additions & 0 deletions tests/SharpCompress.Test/Tar/TarReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,31 @@ public void Tar_Containing_Rar_Reader()
Assert.True(reader.ArchiveType == ArchiveType.Tar);
}
}

[Fact]
public void Tar_With_TarGz_With_Flushed_EntryStream()
{
string archiveFullPath = Path.Combine(TEST_ARCHIVES_PATH, "Tar.ContainsTarGz.tar");
using(Stream stream = File.OpenRead(archiveFullPath))
using(IReader reader = ReaderFactory.Open(stream))
{
Assert.True(reader.MoveToNextEntry());
Assert.Equal("inner.tar.gz", reader.Entry.Key);

using(var entryStream = reader.OpenEntryStream()) {

using(FlushOnDisposeStream flushingStream = new FlushOnDisposeStream(entryStream)) {

// Extract inner.tar.gz
using(var innerReader = ReaderFactory.Open(flushingStream)) {

Assert.True(innerReader.MoveToNextEntry());
Assert.Equal("test", innerReader.Entry.Key);

}
}
}
}
}
}
}
Binary file not shown.

0 comments on commit 0941239

Please sign in to comment.