Skip to content

Commit

Permalink
Zip progress bar (#274)
Browse files Browse the repository at this point in the history
* new zip helper with progress

* missed one

* version bump

* hm, this isnt working right

* moved progress bar to new helper
fixed recursive logic for file copy
  • Loading branch information
mattpannella authored Mar 9, 2024
1 parent dfc30de commit dac8dcc
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 27 deletions.
2 changes: 1 addition & 1 deletion pupdate.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
<Version>3.8.1</Version>
<Version>3.9.0</Version>
<Description>Keep your Analogue Pocket up to date</Description>
<Copyright>2024 Matt Pannella</Copyright>
<Authors>Matt Pannella</Authors>
Expand Down
46 changes: 46 additions & 0 deletions src/helpers/ConsoleHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.IO.Compression;

namespace Pannella.Helpers;

public class ConsoleHelper
{
public static void ShowProgressBar(int current, int total)
{
var progress = (double)current / (double)total;

var progressWidth = Console.WindowWidth - 14;
var progressBarWidth = (int)(progress * progressWidth);
var progressBar = new string('=', progressBarWidth);
var emptyProgressBar = new string(' ', progressWidth - progressBarWidth);

Console.Write($"\r{progressBar}{emptyProgressBar}] {(progress * 100):0.00}%");

if (current == total)
{
Console.CursorLeft = 0;
Console.Write(new string(' ', Console.WindowWidth));
Console.CursorLeft = 0;
Console.Write("\r");
}
}

public static void ShowProgressBar(long current, long total)
{
var progress = (double)current / total;

var progressWidth = Console.WindowWidth - 14;
var progressBarWidth = (int)(progress * progressWidth);
var progressBar = new string('=', progressBarWidth);
var emptyProgressBar = new string(' ', progressWidth - progressBarWidth);

Console.Write($"\r{progressBar}{emptyProgressBar}] {(progress * 100):0.00}%");

if (current == total)
{
Console.CursorLeft = 0;
Console.Write(new string(' ', Console.WindowWidth));
Console.CursorLeft = 0;
Console.Write("\r");
}
}
}
14 changes: 1 addition & 13 deletions src/helpers/HttpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,7 @@ public void DownloadFile(string uri, string outputPath, int timeout = 100)

if (console)
{
var progressWidth = Console.WindowWidth - 14;
var progressBarWidth = (int)(progress * progressWidth);
var progressBar = new string('=', progressBarWidth);
var emptyProgressBar = new string(' ', progressWidth - progressBarWidth);

Console.Write($"\r{progressBar}{emptyProgressBar}] {(progress * 100):0.00}%");

if (readSoFar == totalSize)
{
Console.CursorLeft = 0;
Console.Write(new string(' ', Console.WindowWidth));
Console.CursorLeft = 0;
}
ConsoleHelper.ShowProgressBar(readSoFar, totalSize);
}

DownloadProgressEventArgs args = new()
Expand Down
15 changes: 14 additions & 1 deletion src/helpers/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public enum HashTypes
}

public static void CopyDirectory(string sourceDir, string destinationDir, bool recursive, bool overwrite)
{
_copyDirectory(sourceDir, destinationDir, recursive, overwrite);
}
private static int _copyDirectory(string sourceDir, string destinationDir, bool recursive, bool overwrite, int currentFileCount = 0, int? totalFiles = null)
{
// Get information about the source directory
var dir = new DirectoryInfo(sourceDir);
Expand All @@ -33,12 +37,19 @@ public static void CopyDirectory(string sourceDir, string destinationDir, bool r
// Create the destination directory
Directory.CreateDirectory(destinationDir);

List<string> allfiles = Directory.GetFiles(sourceDir, "*",SearchOption.AllDirectories).ToList();

int total = (totalFiles == null) ? allfiles.Count() : (int)totalFiles;
int count = currentFileCount;
// Get the files in the source directory and copy to the destination directory
foreach (FileInfo file in dir.GetFiles())
{
string targetFilePath = Path.Combine(destinationDir, file.Name);

file.CopyTo(targetFilePath, overwrite);

count++;
ConsoleHelper.ShowProgressBar(count, total);
}

// If recursive and copying subdirectories, recursively call this method
Expand All @@ -48,9 +59,11 @@ public static void CopyDirectory(string sourceDir, string destinationDir, bool r
{
string newDestinationDir = Path.Combine(destinationDir, subDir.Name);

CopyDirectory(subDir.FullName, newDestinationDir, true, overwrite);
count = _copyDirectory(subDir.FullName, newDestinationDir, true, overwrite, count, total);
}
}

return count;
}

public static void CleanDir(string source, string path = "", bool preservePlatformsFolder = false, string platform = "")
Expand Down
78 changes: 78 additions & 0 deletions src/helpers/ZipHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System.IO.Compression;

namespace Pannella.Helpers;

public class ZipHelper
{
private static void UpdateProgress(object sender, ZipProgress zipProgress)
{
ConsoleHelper.ShowProgressBar(zipProgress.Processed, zipProgress.Total);
}

public static void ExtractToDirectory(string zipFile, string destination, bool overwrite = false)
{
Progress<ZipProgress> _progress = new Progress<ZipProgress>();
_progress.ProgressChanged += UpdateProgress;
var stream = new FileStream(zipFile, FileMode.Open);
var zip = new ZipArchive(stream);
zip.ExtractToDirectory(destination, _progress, overwrite);
stream.Close();
}
}

public class ZipProgress
{
public ZipProgress(int total, int processed, string currentItem)
{
Total = total;
Processed = processed;
CurrentItem = currentItem;
}
public int Total { get; }
public int Processed { get; }
public string CurrentItem { get; }
}

public static class ZipExtension
{
public static void ExtractToDirectory(this ZipArchive zipFile, string target, IProgress<ZipProgress> progress)
{
ExtractToDirectory(zipFile, target, progress, overwrite: false);
}

public static void ExtractToDirectory(this ZipArchive zipFile, string target, IProgress<ZipProgress> progress, bool overwrite)
{
DirectoryInfo info = Directory.CreateDirectory(target);
string targetPath = info.FullName;

int count = 0;
foreach (ZipArchiveEntry entry in zipFile.Entries)
{
count++;
string fileDestinationPath = Path.GetFullPath(Path.Combine(targetPath, entry.FullName));

if (!fileDestinationPath.StartsWith(targetPath, StringComparison.OrdinalIgnoreCase))
{
throw new IOException("File is extracting to outside of the folder specified.");
}

var zipProgress = new ZipProgress(zipFile.Entries.Count, count, entry.FullName);
progress.Report(zipProgress);

if (Path.GetFileName(fileDestinationPath).Length == 0)
{
if (entry.Length != 0)
{
throw new IOException("Directory entry with data.");
}

Directory.CreateDirectory(fileDestinationPath);
}
else
{
Directory.CreateDirectory(Path.GetDirectoryName(fileDestinationPath));
entry.ExtractToFile(fileDestinationPath, overwrite: overwrite);
}
}
}
}
2 changes: 1 addition & 1 deletion src/partials/Program.GameAndWatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private static void BuildGameAndWatchRoms()
{
Directory.CreateDirectory(downloadPath);
HttpHelper.Instance.DownloadFile(asset.browser_download_url, filename);
ZipFile.ExtractToDirectory(filename, downloadPath, true);
ZipHelper.ExtractToDirectory(filename, downloadPath, true);
}

break;
Expand Down
2 changes: 1 addition & 1 deletion src/partials/Program.PocketLibraryImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private static void DownloadPockLibraryImages()
if (Directory.Exists(extractPath))
Directory.Delete(extractPath, true);

ZipFile.ExtractToDirectory(localFile, extractPath);
ZipHelper.ExtractToDirectory(localFile, extractPath);
File.Delete(localFile);
Util.CopyDirectory(extractPath, ServiceHelper.UpdateDirectory, true, true);

Expand Down
4 changes: 2 additions & 2 deletions src/partials/Program.UpdateSelfAndRun.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Diagnostics;
using System.IO.Compression;
using System.Reflection;
using System.Text;
using Pannella.Helpers;

namespace Pannella;

Expand Down Expand Up @@ -40,7 +40,7 @@ private static int UpdateSelfAndRun(string directory, string[] updaterArgs)

// Extract update
Console.WriteLine($"Extracting {updateLocation} to {directory}");
ZipFile.ExtractToDirectory(updateLocation, directory, true);
ZipHelper.ExtractToDirectory(updateLocation, directory, true);

// Execute
Console.WriteLine($"Executing {execLocation}");
Expand Down
4 changes: 2 additions & 2 deletions src/services/CoresService.Extras.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private void DownloadPocketExtrasPlatform(PocketExtra pocketExtra, string path,
if (Directory.Exists(extractPath))
Directory.Delete(extractPath, true);

ZipFile.ExtractToDirectory(localFile, extractPath);
ZipHelper.ExtractToDirectory(localFile, extractPath);
File.Delete(localFile);

if (pocketExtra.has_placeholders)
Expand Down Expand Up @@ -247,7 +247,7 @@ private void DownloadPocketExtras(PocketExtra pocketExtra, string path, bool dow
if (Directory.Exists(extractPath))
Directory.Delete(extractPath, true);

ZipFile.ExtractToDirectory(localFile, extractPath);
ZipHelper.ExtractToDirectory(localFile, extractPath);
File.Delete(localFile);
Util.CopyDirectory(sourceAssetsCore, destinationAssetsCore, true, true);

Expand Down
2 changes: 1 addition & 1 deletion src/services/CoresService.Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private bool InstallGithubAsset(string identifier, string platformId, string dow

string tempDir = Path.Combine(this.installPath, "temp", identifier);

ZipFile.ExtractToDirectory(zipPath, tempDir, true);
ZipHelper.ExtractToDirectory(zipPath, tempDir, true);

// Clean problematic directories and files.
Util.CleanDir(tempDir, this.installPath, this.settingsService.GetConfig().preserve_platforms_folder, platformId);
Expand Down
2 changes: 1 addition & 1 deletion src/services/CoresService.Jotego.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public bool ExtractBetaKey()
if (File.Exists(zipFile))
{
WriteMessage("JT beta key detected. Extracting...");
ZipFile.ExtractToDirectory(zipFile, keyPath, true);
ZipHelper.ExtractToDirectory(zipFile, keyPath, true);

return true;
}
Expand Down
7 changes: 3 additions & 4 deletions src/services/PlatformImagePacksService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ public void Install(string owner, string repository, string variant)
WriteMessage("Installing...");

string extractPath = Path.Combine(this.installPath, "temp");

ZipFile.ExtractToDirectory(localFile, extractPath, true);
ZipHelper.ExtractToDirectory(localFile, extractPath, true);

string imagePack = FindPlatformImagePack(extractPath);
string target = Path.Combine(this.installPath, "Platforms", "_images");
Expand All @@ -80,7 +79,7 @@ public void Install(string owner, string repository, string variant)
Directory.Delete(extractPath, true);
File.Delete(localFile);

WriteMessage("All Done");
WriteMessage("Installation complete.");
}

private static string FindPlatformImagePack(string temp)
Expand All @@ -104,4 +103,4 @@ private static string FindPlatformImagePack(string temp)

throw new Exception("Can't find image pack");
}
}
}

0 comments on commit dac8dcc

Please sign in to comment.