-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
62 lines (54 loc) · 1.66 KB
/
Program.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
using Ionic.Zip;
const string zippedFilePath = @"C:\Users\salmam\Downloads\MyFile.Zip";
const string filePathToBeZipped = @"C:\Users\salmam\Downloads\static-web-blazor-func-2020-11-20.pdf";
const string directoryInZippedFolder = "Files";
const string extractedZippedFolderPath = @"C:\Users\salmam\Downloads\MyFile";
CreateProtectedZip();
ExtractProtectedZip();
void CreateProtectedZip()
{
try
{
if (File.Exists(zippedFilePath))
{
File.Delete(zippedFilePath);
}
var zip = new ZipFile(zippedFilePath);
var entry = zip.AddFile(filePathToBeZipped, directoryInZippedFolder);
entry.Password = "123456!";
zip.Save(zippedFilePath);
Console.WriteLine("Zip file has been successfully Created.");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
void ExtractProtectedZip()
{
try
{
using (var zip = ZipFile.Read(zippedFilePath))
{
const string password = "123456!";
var zipPassword= ZipFile.CheckZipPassword(zippedFilePath, password);
if (!zipPassword)
{
Console.WriteLine("Password in incorrect");
throw new BadPasswordException("Password is Incorrect");
}
zip.Password = password;
zip.ExtractAll(extractedZippedFolderPath, ExtractExistingFileAction.OverwriteSilently);
}
Console.WriteLine("Zip file has been successfully extracted.");
Console.Read();
}
catch (BadPasswordException ep)
{
Console.Write(ep.Message);
}
catch (Exception ep)
{
Console.Write(ep.Message);
}
}