Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test with ForwardOnlyStream. RewindableStream shouldn't corrupt a Fo… #161

Merged
merged 1 commit into from
Aug 12, 2016
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
7 changes: 5 additions & 2 deletions src/SharpCompress/Common/Zip/StreamingZipHeaderFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal IEnumerable<ZipHeader> ReadStreamHeader(Stream stream)
FlagUtility.HasFlag(lastEntryHeader.Flags, HeaderFlags.UsePostDataDescriptor))
{
reader = (lastEntryHeader.Part as StreamingZipFilePart).FixStreamedFileLocation(ref rewindableStream);
long pos = rewindableStream.Position;
long? pos = rewindableStream.CanSeek ? (long?)rewindableStream.Position : null;
uint crc = reader.ReadUInt32();
if (crc == POST_DATA_DESCRIPTOR)
{
Expand All @@ -40,7 +40,10 @@ internal IEnumerable<ZipHeader> ReadStreamHeader(Stream stream)
lastEntryHeader.Crc = crc;
lastEntryHeader.CompressedSize = reader.ReadUInt32();
lastEntryHeader.UncompressedSize = reader.ReadUInt32();
lastEntryHeader.DataStartPosition = pos - lastEntryHeader.CompressedSize;
if (pos.HasValue)
{
lastEntryHeader.DataStartPosition = pos - lastEntryHeader.CompressedSize;
}
}
lastEntryHeader = null;
uint headerBytes = reader.ReadUInt32();
Expand Down
5 changes: 1 addition & 4 deletions src/SharpCompress/IO/RewindableStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,7 @@ public override bool CanRead
get { return true; }
}

public override bool CanSeek
{
get { return false; }
}
public override bool CanSeek => stream.CanSeek;

public override bool CanWrite
{
Expand Down
64 changes: 64 additions & 0 deletions test/SharpCompress.Test/ForwardOnlyStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.IO;

namespace SharpCompress.Test
{
public class ForwardOnlyStream : Stream
{
private readonly Stream stream;

public bool IsDisposed { get; private set; }

public ForwardOnlyStream(Stream stream)
{
this.stream = stream;
}

protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
stream.Dispose();
IsDisposed = true;
}

public override bool CanRead => true;

public override bool CanSeek => false;
public override bool CanWrite => false;

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

public override long Length
{
get { throw new NotSupportedException(); }
}

public override long Position
{
get { throw new NotSupportedException(); }
set { throw new NotSupportedException(); }
}

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

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

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

public override void Write(byte[] buffer, int offset, int count)
{throw new NotSupportedException();
}
}
}
2 changes: 1 addition & 1 deletion test/SharpCompress.Test/ReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ protected void Read(IEnumerable<string> testArchives, CompressionType expectedCo
{
foreach (var path in testArchives)
{
using (Stream stream = File.OpenRead(path))
using (Stream stream = new ForwardOnlyStream(File.OpenRead(path)))
using (IReader reader = ReaderFactory.Open(stream))
{
UseReader(this, reader, expectedCompression);
Expand Down