Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dotnet tool #234

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions SharpCompress.sln
Expand Up @@ -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
Expand All @@ -27,12 +29,17 @@ 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
EndGlobalSection
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
32 changes: 32 additions & 0 deletions 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<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");
}
}
}
}
}
}
32 changes: 32 additions & 0 deletions 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);
}
}
}
37 changes: 37 additions & 0 deletions src/dotnet-sharpcompress/ExtractOptions.cs
@@ -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;
}
}
}
56 changes: 56 additions & 0 deletions src/dotnet-sharpcompress/InfoOptions.cs
@@ -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;
}
}
}
16 changes: 16 additions & 0 deletions src/dotnet-sharpcompress/Program.cs
@@ -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);
}
}
}
19 changes: 19 additions & 0 deletions src/dotnet-sharpcompress/dotnet-sharpcompress.csproj
@@ -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>