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

multi-extract variation #24

Open
wants to merge 1 commit 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
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,36 @@ using (ArchiveFile archiveFile = new ArchiveFile(@"Archive.ARJ"))

```

#### Alternative multi-extraction pattern:
```cs
using (ArchiveFile archiveFile = new ArchiveFile(@"Archive.ARJ"))
{
byte[] backingArray = null;
MemoryStream outStr = null;
Entry entry = null;
Stream before(Entry _entry)
{
if (_entry.IsFolder || _entry.Size < 1)
return null;
entry = _entry;
backingArray = new byte[_entry.Size];
outStr = new MemoryStream(backingArray);
return outStr;
}
void after(OperationResult opRes)
{
if (opRes == OperationResult.kOK)
{
// do whatever with Entry and extracted data
}
outStr.Dispose();
backingArray = null;
}
archiveFile.Extract(before, after);
}

```

#### Guess archive format from files without extensions
```cs
using (ArchiveFile archiveFile = new ArchiveFile(@"c:\random-archive"))
Expand Down
58 changes: 58 additions & 0 deletions SevenZipExtractor/ArchiveFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using static SevenZipExtractor.ArchiveStreamsCallbackEx;

namespace SevenZipExtractor
{
Expand Down Expand Up @@ -95,6 +96,63 @@ public void Extract(string outputFolder, bool overwrite = false)
});
}

public void Extract(Func<Entry, Stream> getOutputStream)
{
IList<Stream> fileStreams = new List<Stream>();

try
{
foreach (Entry entry in Entries)
{
var outputStream = getOutputStream(entry);

if (outputStream == null)
{
fileStreams.Add(null);
continue;
}
if (entry.IsFolder)
{
fileStreams.Add(null);
continue;
}
fileStreams.Add(outputStream);
}

archive.Extract(null, 0xFFFFFFFF, 0, new ArchiveStreamsCallback(fileStreams));
}
finally
{
}
}

public void Extract(Func<Entry, Stream> before, Action<OperationResult> after)
{
IList<Operation> operations = new List<Operation>();
try
{
foreach (Entry entry in Entries)
{
var op = new Operation()
{
Entry = entry,
Before = before,
After = after
};
if (entry.IsFolder)
{
operations.Add(null);
continue;
}
operations.Add(op);
}
archive.Extract(null, 0xFFFFFFFF, 0, new ArchiveStreamsCallbackEx(operations));
}
finally
{
}
}

public void Extract(Func<Entry, string> getOutputPath)
{
IList<Stream> fileStreams = new List<Stream>();
Expand Down
77 changes: 77 additions & 0 deletions SevenZipExtractor/ArchiveStreamsCallbackEx.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using System.Collections.Generic;
using System.IO;

namespace SevenZipExtractor
{
internal class ArchiveStreamsCallbackEx : IArchiveExtractCallback
{
private readonly IList<Operation> operations;

public class Operation
{
public Entry Entry { get; set; }
public Func<Entry, Stream> Before { get; set; }
public Action<OperationResult> After { get; set; }
}

public ArchiveStreamsCallbackEx(IList<Operation> operations)
{
this.operations = operations;
}

public void SetTotal(ulong total)
{
}

public void SetCompleted(ref ulong completeValue)
{
}

public int GetStream(uint index, out ISequentialOutStream outStream, AskMode askExtractMode)
{
if (askExtractMode != AskMode.kExtract)
{
outStream = null;
return 0;
}

if (operations == null)
{
outStream = null;
return 0;
}

Operation op = operations[(int)index];
if (op == null)
{
outStream = null;
return 0;
}
var stream = op.Before(op.Entry);
if (stream == null)
{
outStream = null;
return 0;
}

currentOperation = op;

outStream = new OutStreamWrapper(stream);

return 0;
}

public void PrepareOperation(AskMode askExtractMode)
{
}

private Operation currentOperation;

public void SetOperationResult(OperationResult resultEOperationResult)
{
currentOperation?.After(resultEOperationResult);
currentOperation = null;
}
}
}
1 change: 1 addition & 0 deletions SevenZipExtractor/SevenZipExtractor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ArchiveFileCallback.cs" />
<Compile Include="ArchiveStreamsCallbackEx.cs" />
<Compile Include="ArchiveStreamsCallback.cs" />
<Compile Include="ArchiveStreamCallback.cs" />
<Compile Include="Entry.cs" />
Expand Down
2 changes: 1 addition & 1 deletion SevenZipExtractor/SevenZipInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ internal enum AskMode : int
kSkip
}

internal enum OperationResult : int
public enum OperationResult : int
{
kOK = 0,
kUnSupportedMethod,
Expand Down