Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Bugfix in OffsetStream.Seek for reading embedded zip archives #283

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
23 changes: 23 additions & 0 deletions src/Zip Tests/Streams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,29 @@ public void ReadZip_OpenReader()
}


// Read a file consisting of a 16 byte header and one embedded zip archive
[TestMethod]
public void ReadZip_WithOffset()
{
string filename = Path.Combine(CurrentDir, "zips\\offset.bin");
string header = null;
int entries = 0;

using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
using (BinaryReader br = new BinaryReader(fs))
{
header = Encoding.ASCII.GetString(br.ReadBytes(16));
using (ZipFile zip = ZipFile.Read(fs))
{
entries = zip.Entries.Count;
}
}

Assert.AreEqual<string>("16-byte header\0\0", header, "Error reading header");
Assert.AreEqual<int>(1, entries, "Error reading embedded zip file");
}


[TestMethod]
public void ZOS_Create_WithComment_wi10339()
{
Expand Down
3 changes: 3 additions & 0 deletions src/Zip Tests/Zip Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@
<Content Include="zips\relative-paths-in-subdir-outside.zip">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<None Include="zips\offset.bin">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<Content Include="zips\winzip-sfx.exe">
Expand Down
Binary file added src/Zip Tests/zips/offset.bin
Binary file not shown.
13 changes: 12 additions & 1 deletion src/Zip.Shared/OffsetStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,18 @@ public override long Position

public override long Seek(long offset, System.IO.SeekOrigin origin)
{
return _innerStream.Seek(_originalPosition + offset, origin) - _originalPosition;
switch (origin)
{
case SeekOrigin.Begin:
return _innerStream.Seek(_originalPosition + offset, origin) - _originalPosition;

case SeekOrigin.Current:
case SeekOrigin.End:
return _innerStream.Seek(offset, origin) - _originalPosition;

default:
throw new ArgumentOutOfRangeException(nameof(origin), origin, "Invalid seek origin");
}
}


Expand Down