Skip to content

Commit 193ecbc

Browse files
Added compression level options
1 parent 89f5d33 commit 193ecbc

File tree

7 files changed

+188
-83
lines changed

7 files changed

+188
-83
lines changed

FileHider.Lib/ArchiveOptions.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System.ComponentModel;
2+
3+
namespace FileHider.Lib
4+
{
5+
public class ArchiveOptions : INotifyPropertyChanged
6+
{
7+
private System.IO.Compression.CompressionLevel compressionLevel;
8+
public System.IO.Compression.CompressionLevel CompressionLevel
9+
{
10+
get { return compressionLevel; }
11+
set
12+
{
13+
compressionLevel = value;
14+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CompressionLevel)));
15+
}
16+
}
17+
18+
public event PropertyChangedEventHandler? PropertyChanged;
19+
20+
// To-do implement password protection.
21+
//private bool isPasswordProtected;
22+
//public bool IsPasswordProtected { get => isPasswordProtected;
23+
// set
24+
// {
25+
// isPasswordProtected = value;
26+
// PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsPasswordProtected)));
27+
// }
28+
//}
29+
30+
//string? password;
31+
//public string? Password
32+
//{
33+
// get { return password; }
34+
// set
35+
// {
36+
// password = value;
37+
// PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Password)));
38+
// }
39+
//}
40+
}
41+
}

FileHider.Lib/FilesList.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,25 +141,25 @@ public void GoToRoot()
141141
/// <summary>
142142
/// Adds all the files to the archive.
143143
/// </summary>
144-
public void AddItemsToArchive(DirectoryNode directory, ZipArchive archive, IProgress<float> progress)
144+
public void AddItemsToArchive(ArchiveOptions options, DirectoryNode directory, ZipArchive archive, IProgress<float> progress)
145145
{
146146
itemsProcessed = 0;
147-
_AddItemsToArchive(directory, archive, progress);
147+
_AddItemsToArchive(options, directory, archive, progress);
148148
}
149149

150-
private void _AddItemsToArchive(DirectoryNode directory, ZipArchive archive, IProgress<float> progress)
150+
private void _AddItemsToArchive(ArchiveOptions options, DirectoryNode directory, ZipArchive archive, IProgress<float> progress)
151151
{
152152
foreach (var item in directory.Children)
153153
{
154154
if (item == null) continue;
155155
else if (item is DirectoryNode)
156156
{
157-
_AddItemsToArchive((DirectoryNode)item, archive, progress);
157+
_AddItemsToArchive(options, (DirectoryNode)item, archive, progress);
158158
}
159159
else
160160
{
161161
var fileItem = (FileNode)item;
162-
archive.CreateEntryFromFile(fileItem.RealPath, fileItem.GetVirtualPath());
162+
archive.CreateEntryFromFile(fileItem.RealPath, fileItem.GetVirtualPath(), options.CompressionLevel);
163163
itemsProcessed += 1;
164164
progress.Report((float)itemsProcessed * 100.0f / FileCount);
165165
}

FileHider.Tests/FileHiderLibTests.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,6 @@ public void DirectoryCountTest3()
5656
Assert.IsTrue(dirInDir.Children.Count == 1);
5757
}
5858

59-
//[TestMethod]
60-
//public void DirectoryClearTest()
61-
//{
62-
// dirInDir.Delete();
63-
// Assert.IsTrue(dirInDir.Children.FileCount == 0);
64-
//}
65-
6659
void TestPath(string path1, string path2)
6760
{
6861
if (System.OperatingSystem.IsWindows())

FileHider/App.axaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->
66

77
<Application.Styles>
8-
<FluentTheme />
8+
<FluentTheme DensityStyle="Compact"/>
99
</Application.Styles>
1010
</Application>

FileHider/FileHider.csproj

Lines changed: 0 additions & 26 deletions
This file was deleted.

FileHider/ViewModels/MainViewModel.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@
1111
using System;
1212
using CommunityToolkit.Mvvm.Input;
1313
using FileHider.Services;
14+
using FileHider.Lib;
15+
using System.Linq;
16+
using System.Runtime.InteropServices.Marshalling;
1417

1518
namespace FileHider.ViewModels;
1619

20+
using IOCompression = System.IO.Compression;
1721

1822
public partial class MainViewModel : ObservableObject
1923
{
@@ -26,6 +30,7 @@ public partial class MainViewModel : ObservableObject
2630
[ObservableProperty]
2731
public FilesList files;
2832

33+
2934
public IStorageProvider? StorageProvider { get; set; }
3035

3136
[ObservableProperty]
@@ -36,6 +41,29 @@ public partial class MainViewModel : ObservableObject
3641

3742
[ObservableProperty]
3843
private float percentDone;
44+
[ObservableProperty]
45+
private ArchiveOptions options;
46+
47+
private string[] compressionLevels;
48+
public string[] CompressionLevels { get => compressionLevels; }
49+
50+
private string selectedCompressionLevel;
51+
public string SelectedCompressionLevel
52+
{
53+
get => selectedCompressionLevel;
54+
set
55+
{
56+
selectedCompressionLevel = value;
57+
Options.CompressionLevel = selectedCompressionLevel switch
58+
{
59+
"No Compression" => IOCompression.CompressionLevel.NoCompression,
60+
"Fastest" => IOCompression.CompressionLevel.Fastest,
61+
"Optimal" => IOCompression.CompressionLevel.Optimal,
62+
"High Compression" => IOCompression.CompressionLevel.SmallestSize,
63+
_ => throw new Exception(message: $"Invalid value for {nameof(SelectedCompressionLevel)} '{value}'")
64+
};
65+
}
66+
}
3967

4068
public MainViewModel()
4169
{
@@ -46,6 +74,10 @@ public MainViewModel()
4674
percentDone = new();
4775
SelectedItem = null;
4876
StorageProvider = null;
77+
options = new();
78+
compressionLevels = ["No Compression", "Fastest", "Optimal", "High Compression"];
79+
selectedCompressionLevel = "Optimal";
80+
Options.CompressionLevel = IOCompression.CompressionLevel.Optimal;
4981
}
5082

5183
[RelayCommand]
@@ -222,7 +254,7 @@ public void ProcessFiles(IProgress<float> progress)
222254
outfile.Write(buffer);
223255
using (ZipArchive archive = new(outfile, ZipArchiveMode.Create))
224256
{
225-
Files.AddItemsToArchive(Files.RootDirectory, archive, progress);
257+
Files.AddItemsToArchive(Options, Files.RootDirectory, archive, progress);
226258
}
227259
outfile.Close();
228260
}

FileHider/Views/MainView.axaml

Lines changed: 108 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,62 +11,127 @@
1111
<Design.DataContext>
1212
<vm:MainViewModel />
1313
</Design.DataContext>
14-
14+
<UserControl.Styles>
15+
<Style Selector="TabControl WrapPanel">
16+
<Setter Property="MaxHeight" Value="32"/>
17+
<Setter Property="Margin" Value="0, 0, 0, 5"/>
18+
</Style>
19+
<Style Selector="TabControl > TextBlock">
20+
<Setter Property="FontSize" Value="12"/>
21+
</Style>
22+
<Style Selector="Label">
23+
<Setter Property="VerticalAlignment" Value="Center"/>
24+
</Style>
25+
</UserControl.Styles>
26+
1527
<Grid Margin="5">
1628
<Grid.RowDefinitions>
17-
<RowDefinition Height="Auto"/>
1829
<RowDefinition Height="Auto"/>
1930
<RowDefinition Height="10"/>
2031
<RowDefinition Height="*"/>
2132
</Grid.RowDefinitions>
22-
<Grid Grid.Row="0" Margin="0, 5, 0, 0">
23-
<Grid.ColumnDefinitions>
24-
<ColumnDefinition Width="120"/>
25-
<ColumnDefinition/>
26-
<ColumnDefinition Width="110"/>
27-
</Grid.ColumnDefinitions>
28-
<Label Grid.Column="0" >Input file: </Label>
29-
<TextBox Grid.Column="1" Height="24" Text="{Binding InputFile, Mode=TwoWay}"></TextBox>
30-
<Button Grid.Column="2" Margin="5, 0, 0, 0" Command="{Binding BrowseInputFileCommand}" HorizontalAlignment="Right">Browse</Button>
31-
</Grid>
32-
<Grid Grid.Row="1">
33+
<Grid Grid.Row="0"
34+
Margin="0, 5, 0, 0">
3335
<Grid.ColumnDefinitions>
36+
<ColumnDefinition Width="Auto"/>
3437
<ColumnDefinition/>
35-
<ColumnDefinition/>
38+
<ColumnDefinition Width="Auto"/>
39+
<ColumnDefinition Width="Auto"/>
3640
</Grid.ColumnDefinitions>
37-
<StackPanel Grid.Column="0" Orientation="Horizontal" Margin="0, 5">
38-
<Button Command="{Binding AddFilesCommand}"
39-
Margin="5, 0">Add files</Button>
40-
<Button Command="{Binding AddDirCommand}"
41-
Margin="5, 0">Add folder</Button>
42-
<Button Command="{Binding RemoveCommand}"
43-
Margin="5, 0">Remove</Button>
44-
<Button Command="{Binding RemoveAllCommand}"
45-
Margin="5, 0">Remove All</Button>
46-
<Button Command="{Binding BackCommand}"
47-
Margin="5, 0">Back</Button>
48-
</StackPanel>
49-
<StackPanel Grid.Column="1" Orientation="Horizontal" FlowDirection="RightToLeft">
50-
<Button Command="{Binding HideFilesCommand}"
51-
Margin="5, 0">Hide Files</Button>
52-
</StackPanel>
41+
<Label Grid.Column="0"
42+
Margin="2, 2, 5, 2">Input file: </Label>
43+
<TextBox Grid.Column="1"
44+
Height="24"
45+
Text="{Binding InputFile, Mode=TwoWay}"
46+
Margin="2, 2, 2, 2"></TextBox>
47+
<Button Grid.Column="2"
48+
Command="{Binding BrowseInputFileCommand}"
49+
Margin="10, 0, 5, 0"
50+
HorizontalAlignment="Right">Browse</Button>
51+
<Button Grid.Column="3"
52+
Command="{Binding HideFilesCommand}"
53+
Margin="5, 0">Hide Files</Button>
5354
</Grid>
54-
55-
<ProgressBar Grid.Row="2"
55+
<ProgressBar Grid.Row="1"
5656
Margin="5"
5757
Value="{Binding PercentDone}"
5858
IsVisible="{Binding IsRunning}"/>
59+
<TabControl Grid.Row="2">
60+
<TabItem Name="FilesList" Header="File View">
61+
<Grid>
62+
<Grid.RowDefinitions>
63+
<RowDefinition Height="48"/>
64+
<RowDefinition Height="Auto"/>
65+
</Grid.RowDefinitions>
66+
<Grid Grid.Row="0">
67+
<Grid.ColumnDefinitions>
68+
<ColumnDefinition/>
69+
<ColumnDefinition/>
70+
</Grid.ColumnDefinitions>
71+
<StackPanel Grid.Column="0"
72+
Orientation="Horizontal"
73+
Margin="0, 5">
74+
<Button Command="{Binding AddFilesCommand}"
75+
Margin="5, 0">Add files</Button>
76+
<Button Command="{Binding AddDirCommand}"
77+
Margin="5, 0">Add folder</Button>
78+
<Button Command="{Binding RemoveCommand}"
79+
Margin="5, 0">Remove</Button>
80+
<Button Command="{Binding RemoveAllCommand}"
81+
Margin="5, 0">Remove All</Button>
82+
<Button Command="{Binding BackCommand}"
83+
Margin="5, 0">Back</Button>
84+
</StackPanel>
85+
<StackPanel Grid.Column="1"
86+
Orientation="Horizontal"
87+
FlowDirection="RightToLeft">
88+
</StackPanel>
89+
</Grid>
90+
<ListBox Grid.Row="1"
91+
Name="FilesListBox"
92+
ItemsSource="{Binding Files.CurrentDirectory.Children}"
93+
SelectedItem="{Binding SelectedItem}"
94+
DoubleTapped="FilesList_DoubleTap">
95+
<ListBox.ItemTemplate>
96+
<DataTemplate>
97+
<TextBlock Text="{Binding Name}"/>
98+
</DataTemplate>
99+
</ListBox.ItemTemplate>
100+
</ListBox>
101+
</Grid>
102+
</TabItem>
103+
<TabItem Name="Options" Header="Options">
104+
<StackPanel Margin="5, 15, 5, 5" HorizontalAlignment="Center" VerticalAlignment="Center">
105+
<!--To-do: implement password protection-->
106+
<!--<Grid>
107+
<Grid.ColumnDefinitions>
108+
<ColumnDefinition Width="48"/>
109+
<ColumnDefinition Width="120"/>
110+
<ColumnDefinition/>
111+
</Grid.ColumnDefinitions>
112+
<CheckBox Grid.Column="0"
113+
Name="IsEnabledCheckBox"
114+
IsChecked="{Binding Options.IsPasswordProtected}"/>
115+
<Label Grid.Column="1">Password: </Label>
116+
<TextBox Grid.Column="2"
117+
Text="{Binding Options.Password}"
118+
IsEnabled="{Binding Options.IsPasswordProtected}"/>
119+
</Grid>-->
120+
<Grid>
121+
<Grid.ColumnDefinitions>
122+
<ColumnDefinition/>
123+
<ColumnDefinition/>
124+
</Grid.ColumnDefinitions>
125+
<Label Grid.Column="0"
126+
Margin="0, 0, 5, 0">Compression Level: </Label>
127+
<ComboBox Grid.Column="1"
128+
Name="CompressionLevelComboBox"
129+
ItemsSource="{Binding CompressionLevels}"
130+
SelectedItem="{Binding SelectedCompressionLevel}"/>
131+
</Grid>
132+
</StackPanel>
133+
</TabItem>
134+
</TabControl>
59135

60-
<ListBox Grid.Row="3"
61-
Name="FilesListBox"
62-
ItemsSource="{Binding Files.CurrentDirectory.Children}"
63-
SelectedItem="{Binding SelectedItem}"
64-
DoubleTapped="FilesList_DoubleTap">
65-
<ListBox.ItemTemplate>
66-
<DataTemplate>
67-
<TextBlock Text="{Binding Name}"/>
68-
</DataTemplate>
69-
</ListBox.ItemTemplate>
70-
</ListBox>
71136
</Grid>
72137
</UserControl>

0 commit comments

Comments
 (0)