Fixed an issue with automatic destinations (#29)

This commit is contained in:
That_One_Nerd 2023-03-29 13:48:34 -04:00
parent 506892b6e0
commit 9928f7d62f
2 changed files with 47 additions and 7 deletions

View File

@ -7,8 +7,13 @@ public static class CompressionModule
public static void CompressZip(string source, string? destination = null, public static void CompressZip(string source, string? destination = null,
CompressionLevel level = CompressionLevel.Optimal, string comment = "") CompressionLevel level = CompressionLevel.Optimal, string comment = "")
{ {
destination ??= Path.Combine(Path.GetDirectoryName(Path.GetFullPath(source))!, if (destination is null)
$"{Path.GetFileNameWithoutExtension(source)}.zip"); {
string full = Path.GetFullPath(source);
string name = Path.GetFileNameWithoutExtension(full);
string folder = Program.Shell!.WorkingDirectory;
destination ??= $"{folder}\\{name}.zip";
}
string absSource = Path.GetFullPath(source), string absSource = Path.GetFullPath(source),
localDest = Path.GetRelativePath(Program.Shell!.WorkingDirectory, destination); localDest = Path.GetRelativePath(Program.Shell!.WorkingDirectory, destination);
@ -44,13 +49,34 @@ public static class CompressionModule
List<string> files = new(GetAllFiles(absSource)), List<string> files = new(GetAllFiles(absSource)),
relative = new(); relative = new();
foreach (string f in files) relative.Add(Path.GetRelativePath(absSource, f)); for (int i = 0; i < files.Count; i++)
{
string f = files[i];
if (f.Trim().ToLower() == destination.Trim().ToLower())
{
files.RemoveAt(i);
i--;
continue;
}
relative.Add(Path.GetRelativePath(absSource, f));
}
int failed = 0;
LoadingBarStart(); LoadingBarStart();
for (int i = 0; i < files.Count; i++) for (int i = 0; i < files.Count; i++)
{
bool failedThisTime = false;
try
{ {
archive.CreateEntryFromFile(files[i], relative[i], level); archive.CreateEntryFromFile(files[i], relative[i], level);
LoadingBarSet((i + 1) / (float)files.Count, ConsoleColor.DarkGreen); }
catch
{
failedThisTime = true;
failed++;
}
LoadingBarSet((i + 1) / (float)files.Count, failedThisTime ? ConsoleColor.Red : ConsoleColor.DarkGreen); ;
Console.CursorLeft = 0; Console.CursorLeft = 0;
string message = $"{relative[i]}"; string message = $"{relative[i]}";
int remainder = Console.BufferWidth - message.Length; int remainder = Console.BufferWidth - message.Length;
@ -69,6 +95,14 @@ public static class CompressionModule
Write(new string(' ', Console.BufferWidth), newLine: false); Write(new string(' ', Console.BufferWidth), newLine: false);
Console.SetCursorPosition(0, Console.CursorTop - 2); Console.SetCursorPosition(0, Console.CursorTop - 2);
Write(new string(' ', Console.BufferWidth), newLine: false); Write(new string(' ', Console.BufferWidth), newLine: false);
if (failed > 0)
{
Console.CursorLeft = 0;
Console.CursorTop--;
Write($"{failed} file{(failed == 1 ? " has" : "s have")} been ignored due to an error.",
ConsoleColor.DarkYellow);
}
} }
else throw new("No file or directory located at \"source\""); else throw new("No file or directory located at \"source\"");

View File

@ -8,8 +8,14 @@ public static class ExtractionModule
{ {
if (!File.Exists(source)) throw new($"No file exists at \"{source}\"."); if (!File.Exists(source)) throw new($"No file exists at \"{source}\".");
destination ??= Path.GetDirectoryName(Path.GetFullPath(source)); if (destination is null)
if (destination is null) throw new("Error detecting destination path."); {
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); if (!Directory.Exists(destination)) Directory.CreateDirectory(destination);