-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
208 lines (194 loc) · 10.1 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using Microsoft.Win32;
using System;
using System.Windows;
using System.Windows.Shapes;
using System.IO;
using CompressionApp.ViewModel;
using CompressionApp.Models;
using System.Text;
using System.Collections.Generic;
using System.Drawing;
using Ookii.Dialogs.Wpf;
using System.Drawing.Imaging;
using System.Security.Policy;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Threading.Tasks;
namespace CompressionApp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void CompressTextFile(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = true;
openFileDialog.Filter = "Text files (*.txt)|*.txt";
openFileDialog.InitialDirectory = @"C:\Users\hp\Desktop";
if (openFileDialog.ShowDialog() == true)
{
var fileInfo = new FileInfo(openFileDialog.FileName);
sizeBefore.Content = fileInfo.Length + " Byte";
string content = File.ReadAllText(openFileDialog.FileName);
textBox.Text = content;
TextFile compressedFile = CompressionTextFile.CompressTextFile(content);
sizeAfter.Content = compressedFile.Size + " Byte";
string directoryName = fileInfo.DirectoryName.ToString();
string fileName = fileInfo.Name.Substring(0, fileInfo.Name.Length - 4);
filePathlbl.Content = "file path : " + FileService.WriteFile(directoryName, fileName, compressedFile.Content);
textBox.Visibility = Visibility.Visible;
imageSelected.Visibility = Visibility.Hidden;
}
}
private void CompressImageFile(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Image Files(*.png, *.jpg,*.bmp)|*.png;*.bmp;*.jpg;";
openFileDialog.InitialDirectory = @"C:\Users\hp\Desktop";
if (openFileDialog.ShowDialog() == true)
{
FileStream selectedFile = new FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.Read);
var fileInfo = new FileInfo(openFileDialog.FileName);
sizeBefore.Content = fileInfo.Length + " Byte";
string path = CompressionImageFile.Compress(selectedFile, openFileDialog.FileName, openFileDialog.FileName);
fileInfo = new FileInfo(path);
sizeAfter.Content = fileInfo.Length + " Byte";
imageSelected.Source = new BitmapImage(new Uri(openFileDialog.FileName));
imageSelected.Visibility = Visibility.Visible;
textBox.Visibility = Visibility.Hidden;
filePathlbl.Content = "file path : " + path;
}
}
private void CompressFolder(object sender, RoutedEventArgs e)
{
VistaFolderBrowserDialog FolderDialog = new VistaFolderBrowserDialog();
if ((bool)FolderDialog.ShowDialog(this))
{
string[] files = Directory.GetFiles(FolderDialog.SelectedPath);
string newDirectory = FolderDialog.SelectedPath + "-compressed";
for (int i = 0; i < files.Length; i++)
{
if (files[i].Contains(".png") || files[i].Contains(".jpg") || files[i].Contains(".bmp"))
{
Directory.CreateDirectory(newDirectory);
FileStream selectedFile = new FileStream(files[i], FileMode.Open, FileAccess.Read);
int index = files[i].LastIndexOf("\\");
string fileName = files[i].Substring(index);
CompressionImageFile.Compress(selectedFile, newDirectory + fileName, files[i]);
}
else if (files[i].Contains(".txt"))
{
string content = File.ReadAllText(files[i]);
TextFile compressedFile = CompressionTextFile.CompressTextFile(content);
var fileInfo = new FileInfo(files[i]);
string fileName = fileInfo.Name.Substring(0, fileInfo.Name Length - 4);
FileService.WriteFile(newDirectory, fileName, compressedFile.Content);
}
}
filePathlbl.Content = "file path : " + newDirectory;
}
}
private void DecompressTextFile(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Text files (*.fano)|*.fano";
openFileDialog.InitialDirectory = @"C:\Users\hp\Desktop";
if (openFileDialog.ShowDialog() == true)
{
byte[] content = File.ReadAllBytes(openFileDialog.FileName);
KeyValuePair<int, TextFile> decompressTextFile = CompressionTextFile.DecompressTextFile(content);
TextFile compressedFile = decompressTextFile.Value;
sizeBefore.Content = compressedFile.Size + " Byte";
string _content = Encoding.UTF8.GetString(compressedFile.Content, 0, compressedFile.Content.Length);
textBox.Text = _content;
File.WriteAllText(openFileDialog.FileName.Substring(0, openFileDialog.FileName.Length - 15)+".txt", _content);
sizeAfter.Content = decompressTextFile.Key + " Byte";
filePathlbl.Content = "file path : " + openFileDialog.FileName.Substring(0, openFileDialog.FileName.Length - 15) + ".txt";
textBox.Visibility = Visibility.Visible;
imageSelected.Visibility = Visibility.Hidden;
}
}
private void DeCompressImageFile(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Compressed Files(.lzw)|*.lzw;";
openFileDialog.InitialDirectory = @"C:\Users\hp\Desktop";
if (openFileDialog.ShowDialog() == true)
{
FileStream selectedFile = new FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.Read);
var fileInfo = new FileInfo(openFileDialog.FileName);
sizeBefore.Content = fileInfo.Length + " Byte";
string path = CompressionImageFile.Decompress(selectedFile, openFileDialog.FileName);
filePathlbl.Content = "file path : " + path;
fileInfo = new FileInfo(path);
sizeAfter.Content = fileInfo.Length + " Byte";
imageSelected.Source = new BitmapImage(new Uri(path));
textBox.Visibility = Visibility.Hidden;
imageSelected.Visibility = Visibility.Visible;
}
}
private void DeCompressFolder(object sender, RoutedEventArgs e)
{
VistaFolderBrowserDialog FolderDialog = new VistaFolderBrowserDialog();
if ((bool)FolderDialog.ShowDialog(this))
{
string[] files = Directory.GetFiles(FolderDialog.SelectedPath);
string newDirectory = FolderDialog.SelectedPath.Substring(0, FolderDialog.SelectedPath.Length - 11) + "-decompressed";
for (int i = 0; i < files.Length; i++)
{
if (files[i].Contains(".lzw"))
{
Directory.CreateDirectory(newDirectory);
FileStream selectedFile = new FileStream(files[i], FileMode.Open, FileAccess.Read);
int index = files[i].LastIndexOf("\\");
string fileName = files[i].Substring(index);
CompressionImageFile.Decompress(selectedFile, newDirectory + fileName);
}
else if (files[i].Contains(".fano"))
{
byte[] content = File.ReadAllBytes(files[i]);
KeyValuePair<int, TextFile> decompressTextFile = CompressionTextFile.DecompressTextFile(content);
int index = files[i].LastIndexOf("\\");
string fileName = files[i].Substring(index, files[i].Length - index - 5);
string _content = Encoding.UTF8.GetString(decompressTextFile.Value.Content, 0, decompressTextFile.Value.Content.Length);
File.WriteAllText(newDirectory + fileName + ".txt", _content);
}
}
filePathlbl.Content = "file path : " + newDirectory;
}
}
private void CompressImageUsingJpegLossy(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Image Files(*.png, *.jpg,*.bmp)|*.png;*.bmp;*.jpg;";
openFileDialog.InitialDirectory = @"C:\Users\hp\Desktop";
if (openFileDialog.ShowDialog() == true)
{
var fileInfo = new FileInfo(openFileDialog.FileName);
sizeBefore.Content = fileInfo.Length + " Byte";
string newPath;
using (var image = Image.FromFile(openFileDialog.FileName))
{
using (MemoryStream ms = new MemoryStream())
{
image.Save(ms, ImageFormat.Jpeg);
}
newPath = @"F:\Descover_Your_self_" + Guid.NewGuid() + ".jpeg";
image.Save(newPath);
}
textBox.Visibility = Visibility.Hidden;
imageSelected.Visibility = Visibility.Visible;
imageSelected.Source = new BitmapImage(new Uri(newPath));
fileInfo = new FileInfo(newPath);
sizeAfter.Content = fileInfo.Length + " Byte";
filePathlbl.Content = "file path : " + newPath;
}
}
}
}