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

Commit

Permalink
Prepare for releasing 1.6.0, and finished implementing #174.
Browse files Browse the repository at this point in the history
  • Loading branch information
squid-box committed Mar 31, 2023
1 parent 8115c34 commit cdcae1e
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ on:
branches: [ "master" ]

env:
VERSION: '1.5.2.${{ github.run_number }}'
VERSION: '1.6.0.${{ github.run_number }}'

jobs:
build:
Expand Down
19 changes: 19 additions & 0 deletions SevenZip.Tests/SevenZipExtractorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,25 @@ public void ExtractionFromStreamTest()
Assert.AreEqual(3, Directory.GetFiles(OutputDirectory).Length);
}

[Test]
public void ExtractionFromStream_LeaveStreamOpenTest()
{
using var fileStream = new FileStream(@"TestData\multiple_files.7z", FileMode.Open);
using var extractor1 = new SevenZipExtractor(fileStream, leaveOpen: true);

extractor1.ExtractArchive(OutputDirectory);

Assert.IsTrue(fileStream.CanRead);

extractor1.Dispose();

using var extractor2 = new SevenZipExtractor(fileStream, leaveOpen: false);

extractor2.ExtractArchive(OutputDirectory);

Assert.IsFalse(fileStream.CanRead);
}

[Test]
public void ExtractionToStreamTest()
{
Expand Down
47 changes: 35 additions & 12 deletions SevenZip/ArchiveEmulationStreamProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,76 +8,99 @@
/// </summary>
internal class ArchiveEmulationStreamProxy : Stream, IDisposable
{
/// <summary>
/// Gets the file offset.
/// </summary>
public int Offset { get; }

/// <summary>
/// The source wrapped stream.
/// </summary>
public Stream Source { get; }
private readonly bool _leaveOpen;

/// <summary>
/// Initializes a new instance of the ArchiveEmulationStream class.
/// </summary>
/// <param name="stream">The stream to wrap.</param>
/// <param name="offset">The stream offset.</param>
public ArchiveEmulationStreamProxy(Stream stream, int offset)
/// <param name="leaveOpen">Whether or not the stream should be closed after operation completes.</param>
public ArchiveEmulationStreamProxy(Stream stream, int offset, bool leaveOpen = false)
{
Source = stream;
Offset = offset;
Source.Position = offset;

_leaveOpen = leaveOpen;
}

/// <summary>
/// Gets the file offset.
/// </summary>
public int Offset { get; }

/// <summary>
/// The source wrapped stream.
/// </summary>
public Stream Source { get; }

/// <inheritdoc />
public override bool CanRead => Source.CanRead;

/// <inheritdoc />
public override bool CanSeek => Source.CanSeek;

/// <inheritdoc />
public override bool CanWrite => Source.CanWrite;

/// <inheritdoc />
public override void Flush()
{
Source.Flush();
}

/// <inheritdoc />
public override long Length => Source.Length - Offset;

/// <inheritdoc />
public override long Position
{
get => Source.Position - Offset;
set => Source.Position = value;
}

/// <inheritdoc />
public override int Read(byte[] buffer, int offset, int count)
{
return Source.Read(buffer, offset, count);
}

/// <inheritdoc />
public override long Seek(long offset, SeekOrigin origin)
{
return Source.Seek(origin == SeekOrigin.Begin ? offset + Offset : offset,
origin) - Offset;
}

/// <inheritdoc />
public override void SetLength(long value)
{
Source.SetLength(value);
}

/// <inheritdoc />
public override void Write(byte[] buffer, int offset, int count)
{
Source.Write(buffer, offset, count);
}

/// <inheritdoc />
public new void Dispose()
{
Source.Dispose();
if (!_leaveOpen)
{
Source.Dispose();
}
}

/// <inheritdoc />
public override void Close()
{
Source.Close();
if (!_leaveOpen)
{
Source.Close();
}
}
}
}
22 changes: 11 additions & 11 deletions SevenZip/SevenZipExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private void Init(Stream stream)

try
{
_inStream = new ArchiveEmulationStreamProxy(stream, _offset);
_inStream = new ArchiveEmulationStreamProxy(stream, _offset, _leaveOpen);
_packedSize = stream.Length;
_archive = SevenZipLibraryManager.InArchive(_format, this);
}
Expand All @@ -137,7 +137,7 @@ private void Init(Stream stream)

try
{
_inStream = new ArchiveEmulationStreamProxy(stream, _offset);
_inStream = new ArchiveEmulationStreamProxy(stream, _offset, _leaveOpen);
_packedSize = stream.Length;
_archive = SevenZipLibraryManager.InArchive(_format, this);
}
Expand Down Expand Up @@ -413,10 +413,11 @@ private IInStream GetArchiveStream(bool dispose)
{
if (_archiveStream != null)
{
if (_archiveStream is DisposeVariableWrapper)
if (_archiveStream is DisposeVariableWrapper wrapper)
{
(_archiveStream as DisposeVariableWrapper).DisposeStream = dispose;
wrapper.DisposeStream = dispose;
}

return _archiveStream;
}

Expand All @@ -432,13 +433,13 @@ private IInStream GetArchiveStream(bool dispose)
_archiveStream = new InStreamWrapper(
new ArchiveEmulationStreamProxy(new FileStream(
_fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite),
_offset),
_offset, _leaveOpen),
dispose);
}
else
{
_archiveStream = new InMultiStreamWrapper(_fileName, dispose);
_packedSize = (_archiveStream as InMultiStreamWrapper).Length;
_packedSize = (_archiveStream as InMultiStreamWrapper)?.Length;
}
}

Expand Down Expand Up @@ -792,16 +793,16 @@ private void CommonDispose()

if (_archiveStream != null && !_leaveOpen)
{
if (_archiveStream is IDisposable)
if (_archiveStream is IDisposable disposable)
{
try
{
if (_archiveStream is DisposeVariableWrapper)
if (disposable is DisposeVariableWrapper wrapper)
{
(_archiveStream as DisposeVariableWrapper).DisposeStream = true;
wrapper.DisposeStream = true;
}

(_archiveStream as IDisposable).Dispose();
disposable.Dispose();
}
catch (ObjectDisposedException) { }
_archiveStream = null;
Expand All @@ -827,7 +828,6 @@ public void Dispose()
}

_disposed = true;
GC.SuppressFinalize(this);
}

#endregion
Expand Down
10 changes: 8 additions & 2 deletions package.lite.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@
<projectUrl>https://github.com/squid-box/SevenZipSharp</projectUrl>
<tags>7z sevenzip sevenzipsharp 7-zip</tags>
<releaseNotes>
Fixed an issue when seeking in streams with SeekOrigin.End, thanks to GitHub user bneidhold.
Fixed an issue when checking multi-volume 7z archives, thanks to GitHub user panda73111.
Added option to leave streams open after extraction, thanks to GitHub user acrilly-msft.
Added support for GPT and NTFS formats, thanks to GitHub user acrilly-msft.
Added support for the CPIO format, thanks to GitHub user doug24.
Changed how the Unique ID's are generated, thanks to GitHub user RoadTrain.
Fixed an issue with multi-volume archives of more than 1000 parts, thanks to GitHub user in1gma.
Fixed an issue when extracting from a read-only file, thanks to GitHub user benjicoh.
Fixed an issue with appending to archives with encrypted headers, thanks to GitHub user amarendrabiorad.
Fixed an issue when appending to encrypted archives, using streams.
</releaseNotes>
<dependencies>
<group targetFramework="netstandard2.0">
Expand Down
10 changes: 8 additions & 2 deletions package.regular.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@
<projectUrl>https://github.com/squid-box/SevenZipSharp</projectUrl>
<tags>7z sevenzip sevenzipsharp 7-zip</tags>
<releaseNotes>
Fixed an issue when seeking in streams with SeekOrigin.End, thanks to GitHub user bneidhold.
Fixed an issue when checking multi-volume 7z archives, thanks to GitHub user panda73111.
Added option to leave streams open after extraction, thanks to GitHub user acrilly-msft.
Added support for GPT and NTFS formats, thanks to GitHub user acrilly-msft.
Added support for the CPIO format, thanks to GitHub user doug24.
Changed how the Unique ID's are generated, thanks to GitHub user RoadTrain.
Fixed an issue with multi-volume archives of more than 1000 parts, thanks to GitHub user in1gma.
Fixed an issue when extracting from a read-only file, thanks to GitHub user benjicoh.
Fixed an issue with appending to archives with encrypted headers, thanks to GitHub user amarendrabiorad.
Fixed an issue when appending to encrypted archives, using streams.
</releaseNotes>
<dependencies>
<group targetFramework="netstandard2.0">
Expand Down

0 comments on commit cdcae1e

Please sign in to comment.