Last active
April 11, 2018 08:07
-
-
Save moritzuehling/fb371e20d93569271481a9a4f74a024b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// LICENSE: MIT | |
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace StatsHelix.Utils | |
{ | |
public static class MultiplatformUnrar | |
{ | |
static string UnrarBinary = "/usr/bin/env"; | |
static string UnrarArgsPrefix = "unrar e"; | |
static bool IsInitialized = false; | |
public static bool ShouldUseShellExecute { get; set; } = false; | |
public static bool ShouldRedirectStandardOutput { get; set; } = false; | |
public static void Initialize() | |
{ | |
if (IsInitialized) | |
return; | |
if (Environment.GetEnvironmentVariable("UNRAR_BINARY") != null) | |
{ | |
if (Environment.GetEnvironmentVariable("UNRAR_ARGS_PREFIX") == null) | |
throw new Exception("You need to both set UNRAR_BINARY and UNRAR_ARGS_PREFIX"); | |
UnrarBinary = Environment.GetEnvironmentVariable("UNRAR_BINARY"); | |
UnrarArgsPrefix = Environment.GetEnvironmentVariable("UNRAR_ARGS_PREFIX"); | |
} | |
else if (Environment.OSVersion.Platform == PlatformID.Win32NT) | |
{ | |
UnrarBinary = "C:\\Program Files\\7-Zip\\7z.exe"; | |
UnrarArgsPrefix = "e"; | |
} | |
if (!File.Exists(UnrarBinary)) | |
throw new Exception("You need to install " + Path.GetFileNameWithoutExtension(UnrarBinary) + " for this to work, or set the the UNRAR_BINARY and UNRAR_ARGS_PREFIX appropriately."); | |
IsInitialized = true; | |
} | |
public static void Unrar(string file, string toDirectory) | |
{ | |
string archiveName = GetRandomArchiveName(); | |
Directory.CreateDirectory(toDirectory); | |
File.Move(file, Path.Combine(toDirectory, archiveName)); | |
UnrarArchive(toDirectory, archiveName); | |
} | |
public static void Unrar(byte[] data, string toDirectory) | |
{ | |
string archiveName = GetRandomArchiveName(); | |
Directory.CreateDirectory(toDirectory); | |
File.WriteAllBytes(Path.Combine(toDirectory, archiveName), data); | |
UnrarArchive(toDirectory, archiveName); | |
} | |
public static void Unrar(Stream stream, string toDirectory) | |
{ | |
string archiveName = GetRandomArchiveName(); | |
Directory.CreateDirectory(toDirectory); | |
using (var fileStream = File.OpenWrite(Path.Combine(toDirectory, archiveName))) | |
stream.CopyTo(fileStream); | |
UnrarArchive(toDirectory, archiveName); | |
} | |
private static void UnrarArchive(string toDirectory, string archiveName) | |
{ | |
Initialize(); | |
var psi = new ProcessStartInfo(UnrarBinary, UnrarArgsPrefix + " " + archiveName); | |
psi.UseShellExecute = ShouldUseShellExecute; | |
psi.RedirectStandardOutput = ShouldRedirectStandardOutput; | |
psi.WorkingDirectory = toDirectory; | |
var process = Process.Start(psi); | |
process.WaitForExit(); | |
File.Delete(Path.Combine(toDirectory, archiveName)); | |
if (process.ExitCode != 0) | |
{ | |
throw new InvalidDataException("Something went wrong extracting the archive, unrar returned an error."); | |
} | |
} | |
private static string GetRandomArchiveName() | |
{ | |
return Path.GetRandomFileName() + ".rar"; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment