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

Fix exctract from stream #1

Merged
merged 10 commits into from
May 20, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion SevenZip.Tests/SevenZip.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net45</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net45;net462</TargetFrameworks>
<AssemblyTitle>SevenZip.Tests</AssemblyTitle>
<Product>SevenZipzTests</Product>
<Copyright>Copyright © 2018</Copyright>
Expand All @@ -15,6 +15,11 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NUnit.ConsoleRunner" Version="3.10.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0"/>
<PackageReference Include="OpenCover" Version="4.7.922" />
<PackageReference Include="System.Data.DataSetExtensions" Version="4.*" />
<PackageReference Include="Microsoft.CSharp" Version="4.*" />
Expand Down
7 changes: 6 additions & 1 deletion SevenZip.Tests/SevenZipExtractorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,13 @@ public void ThreadedExtractionTest()
public void ExtractArchiveWithLongPath()
{
using (var extractor = new SevenZipExtractor(@"TestData\long_path.7z"))
{
{
#if NET462
var uncOutputDirectory = @"\\?\"+ Path.GetFullPath(OutputDirectory);
Assert.DoesNotThrow(() => extractor.ExtractArchive(uncOutputDirectory));
#else
Assert.Throws<PathTooLongException>(() => extractor.ExtractArchive(OutputDirectory));
#endif
}
}

Expand Down
11 changes: 8 additions & 3 deletions SevenZip/ArchiveEmulationStreamProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@ internal class ArchiveEmulationStreamProxy : Stream, IDisposable
/// </summary>
public Stream Source { get; }

readonly bool _leaveOpen = false;
/// <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">true to leave the wraped stream open after the ArchiveEmulationStreamProxy object is disposed; otherwise, false.</param>
public ArchiveEmulationStreamProxy(Stream stream, int offset, bool leaveOpen = false)
{
Source = stream;
Offset = offset;
Source.Position = offset;
_leaveOpen = leaveOpen;
}

public override bool CanRead => Source.CanRead;
Expand Down Expand Up @@ -72,12 +75,14 @@ public override void Write(byte[] buffer, int offset, int count)

public new void Dispose()
{
Source.Dispose();
if(!_leaveOpen)
Source.Dispose();
}

public override void Close()
{
Source.Close();
if(!_leaveOpen)
Source.Close();
}
}
}
13 changes: 11 additions & 2 deletions SevenZip/ArchiveExtractCallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,11 @@ private static void ValidateFileNameAndCreateDirectory(string fileName)
/// <returns></returns>
private static string RemoveIllegalCharacters(string str, bool isDirectory = false)
{
#if NET462
var isUncPath = str.StartsWith(@"\\?\");
if(isUncPath)
str = str.Substring(@"\\?\".Length);
#endif
var splittedFileName = new List<string>(str.Split(Path.DirectorySeparatorChar));

foreach (char chr in Path.GetInvalidFileNameChars())
Expand Down Expand Up @@ -592,8 +597,12 @@ private static string RemoveIllegalCharacters(string str, bool isDirectory = fal
splittedFileName.RemoveAt(0);
splittedFileName[0] = new string(Path.DirectorySeparatorChar, 2) + splittedFileName[0];
}

return String.Join(new string(Path.DirectorySeparatorChar, 1), splittedFileName.ToArray());
var result = String.Join(new string(Path.DirectorySeparatorChar, 1), splittedFileName.ToArray());
#if NET462
if (isUncPath)
result = @"\\?\" + result;
#endif
return result;
}
}
#endif
Expand Down
3 changes: 2 additions & 1 deletion SevenZip/SevenZip.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<ProductVersion>9.0.30729</ProductVersion>
<AssemblyName>SevenZipSharp</AssemblyName>
<TargetFrameworks>netstandard2.0;net45</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net45;net462</TargetFrameworks>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>SevenZip.snk</AssemblyOriginatorKeyFile>
<FileUpgradeFlags />
Expand Down Expand Up @@ -94,5 +94,6 @@
<Reference Include="System.Configuration" Condition="'$(TargetFramework)' == 'net45'">
<HintPath>C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll</HintPath>
</Reference>
<Reference Include="System.Configuration" Condition="'$(TargetFramework)' == 'net462'" />
</ItemGroup>
</Project>
31 changes: 4 additions & 27 deletions SevenZip/SevenZipBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public abstract class SevenZipBase : MarshalByRefObject
{
private readonly bool _reportErrors;
private readonly int _uniqueID;
private static readonly List<int> Identifiers = new List<int>();
private static readonly object _syncUniqueIds = new object();
private static int _lastUniqueId = int.MinValue;

/// <summary>
/// True if the instance of the class needs to be recreated in new thread context; otherwise, false.
Expand Down Expand Up @@ -103,21 +104,9 @@ internal virtual void ReleaseContext()

private static int GetUniqueID()
{
lock (Identifiers)
lock (_syncUniqueIds)
{
int id;

var rnd = new Random(DateTime.Now.Millisecond);

do
{
id = rnd.Next(int.MaxValue);
}
while (Identifiers.Contains(id));

Identifiers.Add(id);

return id;
return _lastUniqueId++;
}
}

Expand All @@ -132,18 +121,6 @@ protected SevenZipBase(string password = "")
_uniqueID = GetUniqueID();
}

/// <summary>
/// Removes the UniqueID from the list.
/// </summary>
~SevenZipBase()
{
// This lock probably isn't necessary but just in case...
lock (Identifiers)
{
Identifiers.Remove(_uniqueID);
}
}

/// <summary>
/// Gets or sets the archive password
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions SevenZip/SevenZipExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private void Init(Stream stream)
SevenZipLibraryManager.LoadLibrary(this, _format);
try
{
_inStream = new ArchiveEmulationStreamProxy(stream, _offset);
_inStream = new ArchiveEmulationStreamProxy(stream, _offset, leaveOpen: true);
_packedSize = stream.Length;
_archive = SevenZipLibraryManager.InArchive(_format, this);
}
Expand All @@ -124,7 +124,7 @@ private void Init(Stream stream)
_format = InArchiveFormat.PE;
try
{
_inStream = new ArchiveEmulationStreamProxy(stream, _offset);
_inStream = new ArchiveEmulationStreamProxy(stream, _offset, leaveOpen: true);
_packedSize = stream.Length;
_archive = SevenZipLibraryManager.InArchive(_format, this);
}
Expand Down
2 changes: 2 additions & 0 deletions package.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
</releaseNotes>
<dependencies>
<group targetFramework="net45" />
<group targetFramework="net462" />
<group targetFramework="netstandard2.0">
<dependency id="System.Configuration.ConfigurationManager" version="4.5.0" />
<dependency id="System.Security.Permissions" version="4.5.0" />
Expand All @@ -25,6 +26,7 @@
<files>
<file src="Stage\Release\netstandard2.0\SevenZipSharp.dll" target="lib\netstandard2.0" />
<file src="Stage\Release\net45\SevenZipSharp.dll" target="lib\net45" />
<file src="Stage\Release\net462\SevenZipSharp.dll" target="lib\net462" />
<file src="license" />
</files>
</package>