From 9d24c53cfd49850c8b312706d3aefbf07017f38f Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Mon, 29 May 2017 10:19:09 +0100 Subject: [PATCH] Intermediate commit. Was trying out a progress bar --- src/dotnet-sharpcompress/BaseOptions.cs | 32 ++++++++++++ src/dotnet-sharpcompress/ConsoleHelper.cs | 32 ++++++++++++ src/dotnet-sharpcompress/ExtractOptions.cs | 29 ++++++++++- src/dotnet-sharpcompress/InfoOptions.cs | 49 +++++++++---------- src/dotnet-sharpcompress/Program.cs | 7 +-- .../dotnet-sharpcompress.csproj | 1 + 6 files changed, 118 insertions(+), 32 deletions(-) create mode 100644 src/dotnet-sharpcompress/BaseOptions.cs create mode 100644 src/dotnet-sharpcompress/ConsoleHelper.cs diff --git a/src/dotnet-sharpcompress/BaseOptions.cs b/src/dotnet-sharpcompress/BaseOptions.cs new file mode 100644 index 00000000..d959746d --- /dev/null +++ b/src/dotnet-sharpcompress/BaseOptions.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.IO; +using CommandLine; + +namespace SharpCompress +{ + public class BaseOptions + { + [Value(0, Min = 1)] + public IEnumerable Path { get; set; } + + protected IEnumerable GetFilesFromPath() + { + foreach (var s in Path) + { + var fileInfo = new FileInfo(s); + if (fileInfo.Exists) + { + yield return fileInfo; + } + else + { + using (ConsoleHelper.PushError()) + { + Console.WriteLine($"{s} does not exist"); + } + } + } + } + } +} \ No newline at end of file diff --git a/src/dotnet-sharpcompress/ConsoleHelper.cs b/src/dotnet-sharpcompress/ConsoleHelper.cs new file mode 100644 index 00000000..542b10e1 --- /dev/null +++ b/src/dotnet-sharpcompress/ConsoleHelper.cs @@ -0,0 +1,32 @@ +using System; + +namespace SharpCompress +{ + public static class ConsoleHelper + { + private class ConsoleTextPush : IDisposable + { + private readonly ConsoleColor _restoreColor; + + public ConsoleTextPush(ConsoleColor displayColor) + { + _restoreColor = Console.ForegroundColor; + Console.ForegroundColor = displayColor; + } + + public void Dispose() + { + Console.ForegroundColor = _restoreColor; + } + } + + public static IDisposable PushForeground(ConsoleColor color) + { + return new ConsoleTextPush(color); + } + public static IDisposable PushError() + { + return PushForeground(ConsoleColor.Red); + } + } +} \ No newline at end of file diff --git a/src/dotnet-sharpcompress/ExtractOptions.cs b/src/dotnet-sharpcompress/ExtractOptions.cs index 4d0369b3..d1da688b 100644 --- a/src/dotnet-sharpcompress/ExtractOptions.cs +++ b/src/dotnet-sharpcompress/ExtractOptions.cs @@ -1,10 +1,37 @@ +using System; using CommandLine; +using SharpCompress.Readers; namespace SharpCompress { [Verb("x", HelpText = "Extract an archive")] - public class ExtractOptions + public class ExtractOptions : BaseOptions { + + [Option('p', HelpText = "Path to extract to")] + public string ExtractionPath { get; set; } = AppContext.BaseDirectory; + public int Process() + { + foreach (var fileInfo in GetFilesFromPath()) + { + Console.WriteLine($"Extracting archive {fileInfo.FullName} to path: {ExtractionPath}"); + using (var reader = ReaderFactory.Open(fileInfo.OpenRead())) + { + while (reader.MoveToNextEntry()) + { + var progress = new ProgressBar(); + reader.EntryExtractionProgress += (sender, args) => + { + progress.Report(args.ReaderProgress.PercentageReadExact); + }; + Console.Write($"Extracting entry {reader.Entry.Key}: "); + reader.WriteEntryToDirectory(ExtractionPath); + Console.WriteLine(); + } + } + } + return 1; + } } } \ No newline at end of file diff --git a/src/dotnet-sharpcompress/InfoOptions.cs b/src/dotnet-sharpcompress/InfoOptions.cs index b3a3d5f9..271db931 100644 --- a/src/dotnet-sharpcompress/InfoOptions.cs +++ b/src/dotnet-sharpcompress/InfoOptions.cs @@ -1,54 +1,53 @@ using System; -using System.Collections.Generic; -using System.IO; using CommandLine; using SharpCompress.Archives; +using SharpCompress.Common; namespace SharpCompress { [Verb("i", HelpText = "Information about an archive")] - public class InfoOptions + public class InfoOptions : BaseOptions { - [Value(0, Min = 1)] - public IEnumerable Path { get; set; } - [Option('e', HelpText = "Show Archive Entry Information")] public bool ShowEntries { get; set; } public int Process() { - foreach (var s in Path) + foreach (var fileInfo in GetFilesFromPath()) { - if (File.Exists(s)) + Console.WriteLine($"=== Archive: {fileInfo}"); + try { - Console.WriteLine($"=== Archive: {s}"); - try + using (var archive = ArchiveFactory.Open(fileInfo.OpenRead())) { - using (var archive = ArchiveFactory.Open(File.OpenRead(s))) + Console.WriteLine($"Archive Type: {archive.Type}"); + + Console.WriteLine($"Size: {archive.TotalSize}"); + Console.WriteLine($"Uncompressed Size: {archive.TotalUncompressSize}"); + + if (ShowEntries) { - Console.WriteLine($"Archive Type: {archive.Type}"); - - Console.WriteLine($"Size: {archive.TotalSize}"); - Console.WriteLine($"Uncompressed Size: {archive.TotalUncompressSize}"); - - if (ShowEntries) + foreach (var archiveEntry in archive.Entries) { - foreach (var archiveEntry in archive.Entries) - { - Console.WriteLine($"\tEntry: {archiveEntry.Key}"); - } + Console.WriteLine($"\tEntry: {archiveEntry.Key}"); } } } - catch (Exception) + } + catch (InvalidFormatException) + { + using (ConsoleHelper.PushError()) { Console.WriteLine("Archive Type is unknown."); } } - else + catch (Exception e) { - Console.WriteLine($"{s} does not exist"); - return 1; + using (ConsoleHelper.PushError()) + { + Console.WriteLine($"Unhandled Error: {e}"); + return 1; + } } } return 0; diff --git a/src/dotnet-sharpcompress/Program.cs b/src/dotnet-sharpcompress/Program.cs index d2c94c38..39e1bf81 100644 --- a/src/dotnet-sharpcompress/Program.cs +++ b/src/dotnet-sharpcompress/Program.cs @@ -9,13 +9,8 @@ namespace SharpCompress return Parser.Default.ParseArguments(args) .MapResult( (InfoOptions opts) => opts.Process(), - (ExtractOptions opts) => Extract(opts), + (ExtractOptions opts) => opts.Process(), errs => 1); } - - public static int Extract(ExtractOptions options) - { - return 0; - } } } \ No newline at end of file diff --git a/src/dotnet-sharpcompress/dotnet-sharpcompress.csproj b/src/dotnet-sharpcompress/dotnet-sharpcompress.csproj index 24d19747..7305995e 100644 --- a/src/dotnet-sharpcompress/dotnet-sharpcompress.csproj +++ b/src/dotnet-sharpcompress/dotnet-sharpcompress.csproj @@ -14,5 +14,6 @@ 2.1.1-beta + \ No newline at end of file