Skip to content

Commit 654b958

Browse files
committed
Added Unzip Functionality and guards for Execption errors
Added. * Added a reference to System.IO.Compression.FileSystem assembly to the solution * Installed the System.IO.Compression package via NuGet
1 parent f1a1fd7 commit 654b958

File tree

4 files changed

+68
-8
lines changed

4 files changed

+68
-8
lines changed

MainWindow.xaml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<RowDefinition Height="143*"/>
2727
<RowDefinition Height="14*"/>
2828
</Grid.RowDefinitions>
29-
<TextBlock Text="Drag and drop your logs folder:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="16,109,0,0" FontFamily="Segoe UI" FontWeight="Bold" FontSize="14" Foreground="#FF99BCD6" Width="324"/>
29+
<TextBlock Text="Drag and drop your logs folder or Zip File:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="16,109,0,0" FontFamily="Segoe UI" FontWeight="Bold" FontSize="14" Foreground="#FF99BCD6" Width="324"/>
3030

3131
<ListBox AllowDrop="True" Name="FolderListBox" HorizontalAlignment="Left" VerticalAlignment="Top" Width="407" Height="59" Margin="14,133,0,0" Drop="FolderListBox_Drop" Background="#FF203746" Foreground="#FF99BCD6" FontWeight="Bold" FontStyle="Italic" SelectionChanged="FolderListBox_SelectionChanged" RenderTransformOrigin="0.5,0.5">
3232
<ListBox.RenderTransform>
@@ -52,7 +52,7 @@
5252
<Button Content="Search" Click="Search_Click" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Height="24" Margin="620,218,0,0" Background="#FF3F82F1" BorderBrush="Transparent" FontSize="14" Foreground="White" />
5353
<Button Content="Clear" Click="Clear_Click" HorizontalAlignment="Left" VerticalAlignment="Top" Width="74" Height="25" Margin="700,218,0,0" Background="{DynamicResource {x:Static SystemColors.HotTrackBrushKey}}" BorderBrush="Transparent" FontSize="14" Foreground="White" />
5454
<ProgressBar Name="SearchProgressBar" IsIndeterminate="True" Visibility="Collapsed" HorizontalAlignment="Left" VerticalAlignment="Top" Width="100" Height="10" Margin="680,86,0,0"/>
55-
<TextBlock Name="ResultCountTextBlock" HorizontalAlignment="Left" Margin="10,133,0,0" VerticalAlignment="Top" FontFamily="Segoe UI" FontWeight="Bold" FontSize="14" Width="168" Grid.Row="2" Foreground="#FF99BCD6"/>
55+
<TextBlock Name="ResultCountTextBlock" HorizontalAlignment="Left" Margin="10,133,0,0" VerticalAlignment="Top" FontFamily="Segoe UI" FontWeight="Bold" FontSize="14" Width="289" Grid.Row="2" Foreground="#FF99BCD6"/>
5656

5757
<ListView x:Name="ResultListView" HorizontalAlignment="Left" VerticalAlignment="Top" Width="770" Height="264" Margin="10,247,0,0" Grid.RowSpan="3">
5858
<ListView.View>
@@ -69,12 +69,19 @@
6969
Margin="10,10,543,0"
7070
FontSize="28" Foreground="White"/>
7171

72-
<Button Content="" HorizontalAlignment="Right"
72+
<Button Content="!" HorizontalAlignment="Right"
7373
VerticalAlignment="Top"
7474
Margin="0,10,10,0"
7575
Background="Transparent" BorderBrush="White"
7676
Foreground="White" FontSize="16"
7777
Click="About_Click" />
78+
79+
<Button Content="?" HorizontalAlignment="Right"
80+
VerticalAlignment="Top"
81+
Margin="0,10,24,0"
82+
Background="Transparent" BorderBrush="White"
83+
Foreground="White" FontSize="16"
84+
Click="Help_Click" />
7885
</Grid>
7986
</Grid>
8087
</Window>

MainWindow.xaml.cs

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using System.Windows.Controls;
99
using System.Windows.Input;
1010
using evtxToXml;
11+
using System.IO.Compression;
12+
1113

1214
namespace LogSearchApp
1315
{
@@ -49,20 +51,54 @@ private void CopySelectedItemToClipboard()
4951
}
5052
}
5153
}
52-
53-
private void FolderListBox_Drop(object sender, DragEventArgs e)
54+
private async void FolderListBox_Drop(object sender, DragEventArgs e)
5455
{
5556
if (e.Data.GetDataPresent(DataFormats.FileDrop))
5657
{
57-
string[] folders = (string[])e.Data.GetData(DataFormats.FileDrop);
58-
if (folders.Length > 0)
58+
string[] droppedItems = (string[])e.Data.GetData(DataFormats.FileDrop);
59+
if (droppedItems.Length > 0)
5960
{
61+
string path = droppedItems[0];
62+
63+
// Check if the dropped item is a zip file
64+
if (Path.GetExtension(path).Equals(".zip", StringComparison.OrdinalIgnoreCase))
65+
{
66+
// Display message and activate progress bar during uncompressing
67+
ResultCountTextBlock.Text = "Uncompressing...";
68+
SearchProgressBar.Visibility = Visibility.Visible;
69+
70+
try
71+
{
72+
// Extract the contents of the zip file to a folder with the same path
73+
string extractPath = Path.Combine(Path.GetDirectoryName(path), Path.GetFileNameWithoutExtension(path));
74+
await Task.Run(() => ZipFile.ExtractToDirectory(path, extractPath));
75+
76+
// Update the log path to the extracted folder
77+
path = extractPath;
78+
79+
// Hide progress bar/results after uncompressing
80+
SearchProgressBar.Visibility = Visibility.Collapsed;
81+
ResultCountTextBlock.Text = "File Uncompressed and Ready to Search";
82+
}
83+
catch (Exception ex)
84+
{
85+
// Handle the exception (display or log the error)
86+
MessageBox.Show($"Error opening file with Notepad: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
87+
88+
// Reset UI state
89+
ResultCountTextBlock.Text = "Error during extraction";
90+
SearchProgressBar.Visibility = Visibility.Collapsed;
91+
return;
92+
}
93+
}
94+
6095
FolderListBox.Items.Clear();
61-
FolderListBox.Items.Add(folders[0]);
96+
FolderListBox.Items.Add(path);
6297
}
6398
}
6499
}
65100

101+
66102
private async void Search_Click(object sender, RoutedEventArgs e)
67103
{
68104
string logPath = FolderListBox.Items.Count > 0 ? FolderListBox.Items[0] as string : null;
@@ -184,5 +220,11 @@ private void FolderListBox_SelectionChanged(object sender, SelectionChangedEvent
184220
{
185221

186222
}
223+
224+
private void Help_Click(object sender, RoutedEventArgs e)
225+
{
226+
MessageBox.Show("If extracting a Zip file, ensure that the extracted content does not reside in the same location with an identical name as the " +
227+
"original Zip file","Help", MessageBoxButton.OK, MessageBoxImage.Information);
228+
}
187229
}
188230
}

SearchFilesTool.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@
4040
<ItemGroup>
4141
<Reference Include="System" />
4242
<Reference Include="System.Data" />
43+
<Reference Include="System.IO.Compression, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
44+
<HintPath>packages\System.IO.Compression.4.3.0\lib\net46\System.IO.Compression.dll</HintPath>
45+
<Private>True</Private>
46+
<Private>True</Private>
47+
</Reference>
48+
<Reference Include="System.IO.Compression.FileSystem" />
4349
<Reference Include="System.Xml" />
4450
<Reference Include="Microsoft.CSharp" />
4551
<Reference Include="System.Core" />
@@ -90,6 +96,7 @@
9096
<Generator>ResXFileCodeGenerator</Generator>
9197
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
9298
</EmbeddedResource>
99+
<None Include="packages.config" />
93100
<None Include="Properties\Settings.settings">
94101
<Generator>SettingsSingleFileGenerator</Generator>
95102
<LastGenOutput>Settings.Designer.cs</LastGenOutput>

packages.config

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="System.IO.Compression" version="4.3.0" targetFramework="net472" />
4+
</packages>

0 commit comments

Comments
 (0)