Skip to content

Commit

Permalink
Added 7zip command line into the project so it's not dependent on 7zi…
Browse files Browse the repository at this point in the history
…p being installed

Modified Options to be registered as singleton
Modified extraction view model to resolve the extraction tool path from both root and relative directories
Fixed bug where completed extraction progress wouldn't display as 100% due to roundings
  • Loading branch information
laurencee committed Jul 6, 2013
1 parent 5e1ad38 commit 26c9298
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 25 deletions.
Binary file added SubFolderExtractor/7zip/7z.dll
Binary file not shown.
Binary file added SubFolderExtractor/7zip/7z.exe
Binary file not shown.
3 changes: 2 additions & 1 deletion SubFolderExtractor/AppBootstrapper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using Autofac;
using Caliburn.Micro;
Expand All @@ -24,7 +25,7 @@ protected override void ConfigureContainer(ContainerBuilder builder)
base.ConfigureContainer(builder);

builder.Register<IContextMenuRegistrator>(c => new ContextMenuRegistrator());
builder.Register<IOptions>(c => new Options());
builder.Register<IOptions>(c => new Options()).SingleInstance();
}

protected override void OnStartup(object sender, System.Windows.StartupEventArgs e)
Expand Down
7 changes: 5 additions & 2 deletions SubFolderExtractor/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ public static void IgnoreExceptions(this Task task)
if (task == null)
return;

task.ContinueWith(t => Logger.ErrorException(t.Exception.ExtractErrorMessage(), t.Exception),
TaskContinuationOptions.OnlyOnFaulted);
task.ContinueWith(t =>
{
if (t.Exception != null)
Logger.ErrorException(t.Exception.ExtractErrorMessage(), t.Exception);
});
}

public static bool IsEqualTo(this string original, string value, StringComparison stringComparison = StringComparison.OrdinalIgnoreCase)
Expand Down
2 changes: 1 addition & 1 deletion SubFolderExtractor/NLog.config
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</targets>

<rules>
<!--<logger name="*" minlevel="Trace" maxlevel="Warn" writeTo="infoLogFile" />-->
<logger name="*" minlevel="Trace" maxlevel="Warn" writeTo="infoLogFile" />
<logger name="*" minlevel="Error" writeTo="errorLogFile" />
</rules>
</nlog>
6 changes: 3 additions & 3 deletions SubFolderExtractor/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion SubFolderExtractor/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
&lt;/ArrayOfString&gt;</Value>
</Setting>
<Setting Name="ExtractionToolPath" Type="System.String" Scope="User">
<Value Profile="(Default)">C:\Program Files\7-Zip\7z.exe</Value>
<Value Profile="(Default)">7zip\7z.exe</Value>
</Setting>
<Setting Name="RenameToFolder" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
Expand Down
6 changes: 6 additions & 0 deletions SubFolderExtractor/SubFolderExtractor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@
</Compile>
<None Include="app.config" />
<Resource Include="extract-icon.ico" />
<Content Include="7zip\7z.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="7zip\7z.exe">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="NLog.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
Expand Down
24 changes: 22 additions & 2 deletions SubFolderExtractor/ViewModels/ExtractProgressViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class ExtractProgressViewModel : Screen
private string status;
private int totalDirectoriesCount;
private bool progressIsIndeterminate;
private string extractionToolFullPath;

public ExtractProgressViewModel(IOptions options,
IEventAggregator eventAggregator)
Expand Down Expand Up @@ -366,7 +367,9 @@ private string DiscoverExtractedFileName(Action action)
private Process CreateExtractionProcess(FileInfo compressedFile)
{
var extractor = new Process();
extractor.StartInfo.FileName = Settings.Default.ExtractionToolPath;

var extractionToolPath = GetExtractionToolPath();
extractor.StartInfo.FileName = extractionToolPath;
extractor.StartInfo.CreateNoWindow = true;
extractor.StartInfo.UseShellExecute = false;
extractor.StartInfo.RedirectStandardOutput = true;
Expand All @@ -378,6 +381,23 @@ private Process CreateExtractionProcess(FileInfo compressedFile)
return extractor;
}

private string GetExtractionToolPath()
{
if (!string.IsNullOrEmpty(extractionToolFullPath))
return extractionToolFullPath;

string extractionToolPath = Settings.Default.ExtractionToolPath;

if(!Path.IsPathRooted(extractionToolPath))
{
var executingAssemblyLocation = AppDomain.CurrentDomain.BaseDirectory;
extractionToolPath = Path.Combine(executingAssemblyLocation, Settings.Default.ExtractionToolPath);
}

extractionToolFullPath = Path.GetFullPath(extractionToolPath);
return extractionToolFullPath;
}

private void WaitOnCompletion(Process extractionProcess)
{
var cancellationRegistration = cancellationTokenSource.Token.Register(() =>
Expand Down Expand Up @@ -417,7 +437,7 @@ private void LogExtractionOutput(Process extractionProcess)

private int GetCurrentProgress()
{
if (totalDirectoriesCount == 0)
if (totalDirectoriesCount == 0 || totalDirectoriesCount == currentDirectoryCount)
return 100;

int currentProgress = (100 / totalDirectoriesCount) * currentDirectoryCount;
Expand Down
6 changes: 2 additions & 4 deletions SubFolderExtractor/ViewModels/OptionsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ public class OptionsViewModel : Screen

public OptionsViewModel(IOptions options, IContextMenuRegistrator contextMenuRegistrator)
{
if (options == null)
throw new ArgumentNullException("options");
if (contextMenuRegistrator == null)
throw new ArgumentNullException("contextMenuRegistrator");
if (options == null) throw new ArgumentNullException("options");
if (contextMenuRegistrator == null) throw new ArgumentNullException("contextMenuRegistrator");

CanModifyContextMenuRegistration = false;
this.options = options;
Expand Down
7 changes: 2 additions & 5 deletions SubFolderExtractor/ViewModels/ShellViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ namespace SubFolderExtractor.ViewModels
public class ShellViewModel : Conductor<IScreen>.Collection.OneActive
{
private readonly MainViewModel mainViewModel;
private readonly OptionsViewModel optionsViewModel;

public ShellViewModel(MainViewModel mainViewModel, OptionsViewModel optionsViewModel)
public ShellViewModel(MainViewModel mainViewModel)
{
this.mainViewModel = mainViewModel;
this.optionsViewModel = optionsViewModel;
if (mainViewModel == null) throw new ArgumentNullException("mainViewModel");
this.mainViewModel = mainViewModel;

Items.Add(mainViewModel);
Items.Add(optionsViewModel);
}

protected override void OnActivate()
Expand Down
14 changes: 8 additions & 6 deletions SubFolderExtractor/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
<SubFolderExtractor.Properties.Settings>
<setting name="CompressionExtensions" serializeAs="Xml">
<value>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>rar</string>
<string>r00</string>
<string>zip</string>
Expand All @@ -18,24 +19,25 @@
</value>
</setting>
<setting name="ExtractionToolPath" serializeAs="String">
<value>C:\Program Files\7-Zip\7z.exe</value>
</setting>
<setting name="ExtractionCommand" serializeAs="String">
<value>x "[CompressedFile]" -o"[Destination]" -y</value>
<value>7zip\7z.exe</value>
</setting>
<setting name="RenameToFolder" serializeAs="String">
<value>True</value>
</setting>
<setting name="ChainedFileRegularExpressions" serializeAs="Xml">
<value>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>.*part\d+.rar</string>
</ArrayOfString>
</value>
</setting>
<setting name="DeleteAfterExtract" serializeAs="String">
<value>False</value>
</setting>
<setting name="ExtractionCommand" serializeAs="String">
<value>x "[CompressedFile]" -o"[Destination]" -y</value>
</setting>
</SubFolderExtractor.Properties.Settings>
</userSettings>
<runtime>
Expand Down

0 comments on commit 26c9298

Please sign in to comment.