-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
dfc30de
commit dac8dcc
Showing
12 changed files
with
151 additions
and
27 deletions.
There are no files selected for viewing
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
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
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"); | ||
} | ||
} | ||
} |
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
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
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
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); | ||
} | ||
} | ||
} | ||
} |
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
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
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
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
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
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
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