-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathArchive.cs
101 lines (81 loc) · 3.91 KB
/
Archive.cs
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using Gibbed.IO;
using System;
using System.IO;
using UnPSARC.Helpers;
namespace UnPSARC
{
public class Archive
{
public static string FNameFileHash = BitConverter.ToString(new byte[16]);
public static void Unpack(Stream ArchiveRaw, string Folder)
{
Stream Reader = ArchiveRaw;
PSARC Psarc = new PSARC(Reader);
Console.WriteLine("Files count: " + Psarc.FilesCount);
Psarc.Read();
int FailedFiles = 0;
for (int i = 0; i < Psarc.FilesCount; i++)
{
TEntry ThisEntry = Psarc.Entries[i];
string filenameHash = BitConverter.ToString(ThisEntry.HashNames);
if (ThisEntry.Offset == 0 || filenameHash == FNameFileHash)
continue;
string FileName;
if (!Psarc.FileNames.ContainsKey(filenameHash))
{
Console.WriteLine("Archive Contains a hash which is not in filenames table: " + filenameHash);
FileName = "_Unknowns\\" + filenameHash.Replace("-", "") + ".bin";
}
else
{
FileName = Psarc.FileNames[filenameHash].Replace("/", "\\");
}
if (FileName.StartsWith("\\"))
FileName = FileName.Remove(0, 1);
try
{
TryUnpack(Psarc.Reader, out HugeMemoryStream FileWriter, Psarc.Entries[i], Psarc.ZSizes, Psarc.BlockSize, Psarc.CompressionType);
IOHelper.CheckFolderExists(FileName, Folder);
Stream fileHandle = File.Open(@"\\?\" + Path.Combine(Folder, FileName), FileMode.Create, FileAccess.Write);
FileWriter.CopyTo(fileHandle);
fileHandle.Close();
Console.WriteLine("[" + i + "] " + FileName + " Exported!");
}
catch (Exception ex)
{
var _baseforgroud = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("[" + i + "] " + FileName + " Cannot Exported! Error:" + ex.Message);
Console.ForegroundColor = _baseforgroud;
Console.WriteLine("Press any key to continue unpacking");
Console.ReadKey();
FailedFiles++;
}
}
Console.WriteLine($"Unpacking done! | {Psarc.FilesCount - FailedFiles} of {Psarc.FilesCount} Files Exported");
}
public static void TryUnpack(Stream Reader, out HugeMemoryStream Writer, TEntry ThisEntry, TZSize[] ZSizes, int BlockSize, string CompressionType)
{
long RemainingSize = ThisEntry.UncompressedSize;
int ZSizeIndex = ThisEntry.ZSizeIndex;
long BlockOffset = ThisEntry.Offset;
Stream MEMORY_FILE = new HugeMemoryStream();
while (MEMORY_FILE.Length < ThisEntry.UncompressedSize)
{
int CompressedSize = ZSizes[ZSizeIndex++].ZSize;
if (CompressedSize == 0)
CompressedSize = BlockSize;
if (CompressedSize == ThisEntry.UncompressedSize)
MEMORY_FILE.WriteBytes(Reader.ReadAtOffset(BlockOffset, ThisEntry.UncompressedSize));
else if (RemainingSize < BlockSize || CompressedSize == BlockSize)
MEMORY_FILE.WriteBytes(Reader.ReadAtOffset(BlockOffset, RemainingSize, CompressedSize, CompressionType));
else
MEMORY_FILE.WriteBytes(Reader.ReadAtOffset(BlockOffset, BlockSize, CompressedSize, CompressionType));
BlockOffset += (uint)CompressedSize;
RemainingSize -= BlockSize;
}
MEMORY_FILE.Position = 0;
Writer = (HugeMemoryStream)MEMORY_FILE;
}
}
}