Added GZip compatibility

This commit is contained in:
That-One-Nerd 2023-03-29 19:02:55 -04:00
parent 9738599088
commit 87166acda3
2 changed files with 64 additions and 14 deletions

View File

@ -5,14 +5,14 @@ public static class CompressionModule
{ {
[Command("gz")] [Command("gz")]
[Command("gzip")] [Command("gzip")]
public static void CompressGzip(string source, string? destination = null, public static void CompressGZip(string source, string? destination = null,
CompressionLevel level = CompressionLevel.Optimal) CompressionLevel level = CompressionLevel.Optimal)
{ {
if (destination is null) if (destination is null)
{ {
string full = Path.GetFullPath(source); string full = Path.GetFullPath(source),
string name = Path.GetFileNameWithoutExtension(full); name = Path.GetFileName(full),
string folder = Program.Shell!.WorkingDirectory; folder = Program.Shell!.WorkingDirectory;
destination ??= $"{folder}\\{name}.gz"; destination ??= $"{folder}\\{name}.gz";
} }
@ -25,21 +25,21 @@ public static class CompressionModule
string message = $"Compressing file at \"{source}\" into \"{localDest}\"..."; string message = $"Compressing file at \"{source}\" into \"{localDest}\"...";
Write(message); Write(message);
FileStream writer = new(localDest, FileMode.CreateNew); FileStream writer = new(localDest, FileMode.CreateNew),
FileStream reader = new(absSource, FileMode.Open); reader = new(absSource, FileMode.Open);
GZipStream gzip = new(writer, level); GZipStream gzip = new(writer, level);
LoadingBarStart(); LoadingBarStart();
const int bufferSize = 1024; int bufferSize = Mathf.Clamp((int)reader.Length / Console.BufferWidth, 1024 * 1024, 128 * 1024 * 1024);
byte[] buffer = new byte[bufferSize]; byte[] buffer = new byte[bufferSize];
for (int i = 0; i < reader.Length; i += bufferSize) for (long i = 0; i < reader.Length; i += bufferSize)
{ {
int size = reader.Read(buffer, 0, bufferSize); int size = reader.Read(buffer, 0, bufferSize);
gzip.Write(buffer, 0, size); gzip.Write(buffer, 0, size);
gzip.Flush(); gzip.Flush();
LoadingBarSet((float)i / gzip.Length, ConsoleColor.Magenta); LoadingBarSet((float)i / reader.Length, ConsoleColor.DarkGreen);
} }
LoadingBarEnd(); LoadingBarEnd();
@ -49,7 +49,7 @@ public static class CompressionModule
writer.Close(); writer.Close();
Console.CursorLeft = 0; Console.CursorLeft = 0;
Console.CursorTop -= (message.Length / Console.BufferWidth) + 1; Console.CursorTop -= (message.Length / Console.BufferWidth) + 2;
Write(new string(' ', message.Length), newLine: false); Write(new string(' ', message.Length), newLine: false);
} }
else if (Directory.Exists(source)) throw new("The GZip format can only compress 1 file."); else if (Directory.Exists(source)) throw new("The GZip format can only compress 1 file.");
@ -62,9 +62,9 @@ public static class CompressionModule
{ {
if (destination is null) if (destination is null)
{ {
string full = Path.GetFullPath(source); string full = Path.GetFullPath(source),
string name = Path.GetFileNameWithoutExtension(full); name = Path.GetFileNameWithoutExtension(full),
string folder = Program.Shell!.WorkingDirectory; folder = Program.Shell!.WorkingDirectory;
destination ??= $"{folder}\\{name}.zip"; destination ??= $"{folder}\\{name}.zip";
} }
@ -152,7 +152,6 @@ public static class CompressionModule
if (failed > 0) if (failed > 0)
{ {
Console.CursorLeft = 0; Console.CursorLeft = 0;
Console.CursorTop--;
Write($"{failed} file{(failed == 1 ? " has" : "s have")} been ignored due to an error.", Write($"{failed} file{(failed == 1 ? " has" : "s have")} been ignored due to an error.",
ConsoleColor.DarkYellow); ConsoleColor.DarkYellow);
} }

View File

@ -3,6 +3,57 @@
[Module("extract")] [Module("extract")]
public static class ExtractionModule public static class ExtractionModule
{ {
[Command("gz")]
[Command("gzip")]
public static void ExtractGZip(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}";
}
string absSource = Path.GetFullPath(source),
localDest = Path.GetRelativePath(Program.Shell!.WorkingDirectory, destination);
if (File.Exists(destination)) throw new($"File already exists at \"{destination}\".");
string message = $"Extracting file at \"{source}\" into \"{localDest}\"...";
Write(message);
FileStream writer = new(destination, FileMode.CreateNew),
reader = new(absSource, FileMode.Open);
GZipStream gzip = new(reader, CompressionMode.Decompress);
LoadingBarStart();
int bufferSize = Mathf.Clamp((int)reader.Length / Console.BufferWidth, 1024 * 1024, 128 * 1024 * 1024);
byte[] buffer = new byte[bufferSize];
int i = 0;
int size;
while ((size = gzip.Read(buffer, i, bufferSize)) > 0)
{
writer.Write(buffer, 0, size);
writer.Flush();
LoadingBarSet((float)i / reader.Length, ConsoleColor.DarkGreen);
}
LoadingBarEnd();
gzip.Close();
reader.Close();
writer.Close();
Console.CursorLeft = 0;
Console.CursorTop -= (message.Length / Console.BufferWidth) + 2;
Write(new string(' ', message.Length), newLine: false);
}
[Command("zip")] [Command("zip")]
public static void ExtractZip(string source, string? destination = null) public static void ExtractZip(string source, string? destination = null)
{ {