wabbajack/Wabbajack.Common/FileExtractor.cs

150 lines
4.2 KiB
C#
Raw Normal View History

using Compression.BSA;
using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections.Generic;
2019-08-25 23:52:03 +00:00
using System.Diagnostics;
using System.Linq;
2019-08-25 23:52:03 +00:00
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
2019-08-28 03:22:57 +00:00
using Alphaleonis.Win32.Filesystem;
2019-08-25 23:52:03 +00:00
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
namespace Wabbajack.Common
{
public class FileExtractor
{
static FileExtractor()
{
2019-08-25 23:52:03 +00:00
ExtractResource("Wabbajack.Common.7z.dll.gz", "7z.dll");
ExtractResource("Wabbajack.Common.7z.exe.gz", "7z.exe");
}
private static void ExtractResource(string from, string to)
{
if (File.Exists(to))
File.Delete(to);
using (var ous = File.OpenWrite(to))
using (var ins = new GZipInputStream(Assembly.GetExecutingAssembly().GetManifestResourceStream(from)))
{
ins.CopyTo(ous);
}
}
public class Entry
{
public string Name;
public ulong Size;
}
2019-08-25 23:52:03 +00:00
public static void ExtractAll(string source, string dest)
{
2019-08-25 23:52:03 +00:00
if (source.EndsWith(".bsa")) {
ExtractAllWithBSA(source, dest);
}
else
{
2019-08-25 23:52:03 +00:00
ExtractAllWith7Zip(source, dest);
}
}
2019-08-25 23:52:03 +00:00
private static void ExtractAllWithBSA(string source, string dest)
{
2019-08-25 23:52:03 +00:00
using (var arch = new BSAReader(source))
{
2019-08-25 23:52:03 +00:00
arch.Files.PMap(f =>
{
2019-08-25 23:52:03 +00:00
var path = f.Path;
if (f.Path.StartsWith("\\"))
path = f.Path.Substring(1);
Utils.Status($"Extracting {path}");
var out_path = Path.Combine(dest, path);
var parent = Path.GetDirectoryName(out_path);
2019-08-25 23:52:03 +00:00
if (!Directory.Exists(parent))
Directory.CreateDirectory(parent);
2019-08-25 23:52:03 +00:00
using (var fs = File.OpenWrite(out_path))
2019-08-22 23:29:44 +00:00
{
2019-08-25 23:52:03 +00:00
f.CopyDataTo(fs);
2019-08-22 23:29:44 +00:00
}
2019-08-25 23:52:03 +00:00
});
}
}
2019-08-25 23:52:03 +00:00
private static void ExtractAllWith7Zip(string source, string dest)
{
2019-08-25 23:52:03 +00:00
Utils.Log($"Extracting {Path.GetFileName(source)}");
2019-08-25 23:52:03 +00:00
var info = new ProcessStartInfo
{
2019-08-25 23:52:03 +00:00
FileName = "7z.exe",
2019-08-29 22:49:48 +00:00
Arguments = $"x -bsp1 -y -o\"{dest}\" \"{source}\"",
2019-08-25 23:52:03 +00:00
RedirectStandardError = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
};
2019-08-25 23:52:03 +00:00
var p = new Process
{
StartInfo = info,
};
2019-08-25 23:52:03 +00:00
p.Start();
ChildProcessTracker.AddProcess(p);
2019-08-28 03:22:57 +00:00
try
{
p.PriorityClass = ProcessPriorityClass.BelowNormal;
}
catch (Exception) { }
2019-08-29 22:49:48 +00:00
var name = Path.GetFileName(source);
try
{
while (!p.HasExited)
{
var line = p.StandardOutput.ReadLine();
if (line == null)
break;
int percent = 0;
if (line.Length > 4 && line[3] == '%')
{
Int32.TryParse(line.Substring(0, 3), out percent);
Utils.Status($"Extracting {name} - {line.Trim()}", percent);
}
}
}
catch (Exception ex)
{
}
2019-08-25 23:52:03 +00:00
p.WaitForExit();
if (p.ExitCode != 0)
{
Utils.Log(p.StandardOutput.ReadToEnd());
Utils.Log($"Extraction error extracting {source}");
}
2019-08-25 23:52:03 +00:00
}
/// <summary>
/// Returns true if the given extension type can be extracted
/// </summary>
/// <param name="v"></param>
/// <returns></returns>
public static bool CanExtract(string v)
{
return Consts.SupportedArchives.Contains(v) || v == ".bsa";
}
}
}