Compare commits

...

3 Commits

Author SHA1 Message Date
Adam Hathcock
9d24c53cfd Intermediate commit. Was trying out a progress bar 2017-06-09 08:25:28 +01:00
Adam Hathcock
e7720ccc4e small refactor 2017-06-09 08:25:28 +01:00
Adam Hathcock
723f4dc83f Start making a dotnet tool 2017-06-09 08:25:28 +01:00
7 changed files with 199 additions and 0 deletions

View File

@@ -13,6 +13,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SharpCompress", "src\SharpC
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SharpCompress.Test", "tests\SharpCompress.Test\SharpCompress.Test.csproj", "{F2B1A1EB-0FA6-40D0-8908-E13247C7226F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "dotnet-sharpcompress", "src\dotnet-sharpcompress\dotnet-sharpcompress.csproj", "{CC08976E-8E3B-44EE-BDA7-6A9D2FDDDB02}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -27,6 +29,10 @@ Global
{F2B1A1EB-0FA6-40D0-8908-E13247C7226F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F2B1A1EB-0FA6-40D0-8908-E13247C7226F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F2B1A1EB-0FA6-40D0-8908-E13247C7226F}.Release|Any CPU.Build.0 = Release|Any CPU
{CC08976E-8E3B-44EE-BDA7-6A9D2FDDDB02}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CC08976E-8E3B-44EE-BDA7-6A9D2FDDDB02}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CC08976E-8E3B-44EE-BDA7-6A9D2FDDDB02}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CC08976E-8E3B-44EE-BDA7-6A9D2FDDDB02}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -34,5 +40,6 @@ Global
GlobalSection(NestedProjects) = preSolution
{FD19DDD8-72B2-4024-8665-0D1F7A2AA998} = {3C5BE746-03E5-4895-9988-0B57F162F86C}
{F2B1A1EB-0FA6-40D0-8908-E13247C7226F} = {0F0901FF-E8D9-426A-B5A2-17C7F47C1529}
{CC08976E-8E3B-44EE-BDA7-6A9D2FDDDB02} = {3C5BE746-03E5-4895-9988-0B57F162F86C}
EndGlobalSection
EndGlobal

View File

@@ -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<string> Path { get; set; }
protected IEnumerable<FileInfo> 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");
}
}
}
}
}
}

View File

@@ -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);
}
}
}

View File

@@ -0,0 +1,37 @@
using System;
using CommandLine;
using SharpCompress.Readers;
namespace SharpCompress
{
[Verb("x", HelpText = "Extract an archive")]
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;
}
}
}

View File

@@ -0,0 +1,56 @@
using System;
using CommandLine;
using SharpCompress.Archives;
using SharpCompress.Common;
namespace SharpCompress
{
[Verb("i", HelpText = "Information about an archive")]
public class InfoOptions : BaseOptions
{
[Option('e', HelpText = "Show Archive Entry Information")]
public bool ShowEntries { get; set; }
public int Process()
{
foreach (var fileInfo in GetFilesFromPath())
{
Console.WriteLine($"=== Archive: {fileInfo}");
try
{
using (var archive = ArchiveFactory.Open(fileInfo.OpenRead()))
{
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)
{
Console.WriteLine($"\tEntry: {archiveEntry.Key}");
}
}
}
}
catch (InvalidFormatException)
{
using (ConsoleHelper.PushError())
{
Console.WriteLine("Archive Type is unknown.");
}
}
catch (Exception e)
{
using (ConsoleHelper.PushError())
{
Console.WriteLine($"Unhandled Error: {e}");
return 1;
}
}
}
return 0;
}
}
}

View File

@@ -0,0 +1,16 @@
using CommandLine;
namespace SharpCompress
{
public class Program
{
public static int Main(string[] args)
{
return Parser.Default.ParseArguments<InfoOptions, ExtractOptions>(args)
.MapResult(
(InfoOptions opts) => opts.Process(),
(ExtractOptions opts) => opts.Process(),
errs => 1);
}
}
}

View File

@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.0</TargetFramework>
<RootNamespace>SharpCompress</RootNamespace>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\SharpCompress\SharpCompress.csproj">
<Project>{fd19ddd8-72b2-4024-8665-0d1f7a2aa998}</Project>
<Name>SharpCompress</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="CommandLineParser">
<Version>2.1.1-beta</Version>
</PackageReference>
<PackageReference Include="goblinfactory.konsole" Version="2.0.2" />
</ItemGroup>
</Project>