mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-02-04 13:34:59 +00:00
Compare commits
3 Commits
adam/memor
...
dotnet-too
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9d24c53cfd | ||
|
|
e7720ccc4e | ||
|
|
723f4dc83f |
@@ -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
|
||||
|
||||
32
src/dotnet-sharpcompress/BaseOptions.cs
Normal file
32
src/dotnet-sharpcompress/BaseOptions.cs
Normal 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
32
src/dotnet-sharpcompress/ConsoleHelper.cs
Normal file
32
src/dotnet-sharpcompress/ConsoleHelper.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
37
src/dotnet-sharpcompress/ExtractOptions.cs
Normal file
37
src/dotnet-sharpcompress/ExtractOptions.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
56
src/dotnet-sharpcompress/InfoOptions.cs
Normal file
56
src/dotnet-sharpcompress/InfoOptions.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
16
src/dotnet-sharpcompress/Program.cs
Normal file
16
src/dotnet-sharpcompress/Program.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
19
src/dotnet-sharpcompress/dotnet-sharpcompress.csproj
Normal file
19
src/dotnet-sharpcompress/dotnet-sharpcompress.csproj
Normal 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>
|
||||
Reference in New Issue
Block a user