Added support for extracting tar and tar.gz formats

This commit is contained in:
That_One_Nerd 2023-03-30 09:06:09 -04:00
parent dda3ccad4d
commit 8f7c2775d4

View File

@ -50,10 +50,59 @@ public static class ExtractionModule
writer.Close(); writer.Close();
Console.CursorLeft = 0; Console.CursorLeft = 0;
Console.CursorTop -= (message.Length / Console.BufferWidth) + 2; Console.CursorTop -= (message.Length / Console.BufferWidth) + 1;
if (Console.CursorTop >= Console.BufferHeight - 3) Console.CursorTop--;
Write(new string(' ', message.Length), newLine: false); Write(new string(' ', message.Length), newLine: false);
} }
[Command("tar")]
[Command("tarball")]
public static void ExtractTar(string source, string? destination = null)
{
if (!File.Exists(source)) throw new($"No file exists at \"{source}\".");
if (destination is null)
{
string full = Path.GetFullPath(source);
string folder = Program.Shell!.WorkingDirectory;
string name = Path.GetFileNameWithoutExtension(full);
destination = $"{folder}\\{name}";
}
if (!Directory.Exists(destination)) Directory.CreateDirectory(destination);
FileStream reader = new(source, FileMode.Open);
TarFile.ExtractToDirectory(reader, Path.GetFileName(destination), true);
reader.Dispose();
}
[Command("targz")]
[Command("tar.gz")]
[Command("tar-gz")]
public static void ExtractTarGz(string source, string? destination = null)
{
if (!File.Exists(source)) throw new($"No file exists at \"{source}\".");
if (destination is null)
{
string full = Path.GetFullPath(source);
string folder = Program.Shell!.WorkingDirectory;
string name = Path.GetFileNameWithoutExtension(Path.GetFileNameWithoutExtension(full));
destination = $"{folder}\\{name}";
}
string absSource = Path.GetFullPath(source),
temp = Path.Combine(Path.GetDirectoryName(absSource)!, Path.GetFileNameWithoutExtension(absSource));
ExtractGZip(source, temp);
ExtractTar(temp, destination);
File.Delete(temp);
}
[Command("zip")] [Command("zip")]
public static void ExtractZip(string source, string? destination = null) public static void ExtractZip(string source, string? destination = null)
{ {