-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSevenZipProvider.cs
More file actions
76 lines (68 loc) · 2.63 KB
/
SevenZipProvider.cs
File metadata and controls
76 lines (68 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using System.IO;
using System.Runtime.InteropServices;
using SevenZip;
namespace WinAVFS.Core
{
public class SevenZipProvider : IArchiveProvider
{
private readonly ConcurrentObjectPool<SevenZipExtractor> extractorPool;
public SevenZipProvider(string path)
{
Console.WriteLine($"Loading archive {path} with 7z.dll");
this.extractorPool = new ConcurrentObjectPool<SevenZipExtractor>(() => new SevenZipExtractor(path));
}
public void Dispose()
{
foreach (var archive in this.extractorPool.GetAll())
{
archive.Dispose();
}
}
public FSTree ReadFSTree()
{
var extractor = this.extractorPool.Get();
var root = new FSTreeNode(true);
foreach (var entry in extractor.ArchiveFileData)
{
// Console.WriteLine($"Loading {entry.FileName} into FS tree");
var paths = entry.FileName.Split('/', '\\');
var node = root;
for (var i = 0; i < paths.Length - 1; i++)
{
node = node.GetOrAddChild(true, paths[i]);
}
if (!string.IsNullOrEmpty(paths[paths.Length - 1]))
{
node = node.GetOrAddChild(entry.IsDirectory, paths[paths.Length - 1], (long) entry.Size,
(long) entry.Size, entry.Index);
node.CreationTime = entry.CreationTime;
node.LastAccessTime = entry.LastAccessTime;
node.LastWriteTime = entry.LastWriteTime;
// if (!node.IsDirectory && node.Buffer == IntPtr.Zero)
// {
// node.Buffer = Marshal.AllocHGlobal((IntPtr) node.Length);
// }
}
}
Console.WriteLine($"Loaded {extractor.FilesCount} entries from archive");
this.extractorPool.Put(extractor);
return new FSTree {Root = root};
}
public void ExtractFileUnmanaged(FSTreeNode node, IntPtr buffer)
{
if (!(node.Context is int index))
{
throw new ArgumentException();
}
unsafe
{
using var target = new UnmanagedMemoryStream((byte*) buffer.ToPointer(), node.Length, node.Length,
FileAccess.Write);
var extractor = this.extractorPool.Get();
extractor.ExtractFile(index, target);
this.extractorPool.Put(extractor);
}
}
}
}