-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
184 lines (149 loc) · 4.69 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
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
// See https://aka.ms/new-console-template for more information
using System.IO.Compression;
Console.WriteLine("Enter path of the source folder:");
string? sourceFolder = Console.ReadLine();
Console.WriteLine("Enter path of the destination folder:");
string? destinationFolder = Console.ReadLine();
if (sourceFolder == destinationFolder)
{
Exit("Source and destination folder cannot point to the same location.");
}
string[]? filesInSource = Directory.GetFiles(sourceFolder, "*.*", SearchOption.AllDirectories);
if (filesInSource.Length == 0)
{
Exit("No files found in the source folder.");
return;
}
string[]? filesInDestination = Directory.GetFiles(destinationFolder, "*.*", SearchOption.AllDirectories);
if (filesInDestination.Length == 0)
{
Exit("No files found in the destination folder.");
return;
}
Console.WriteLine("Listing files only in the source folder...");
List<string> uniqueFiles = CompareFolders(sourceFolder, destinationFolder);
string temporaryFolder = Path.GetTempPath();
string uniqueFilesFile = Path.Combine(temporaryFolder, "uniqueFiles.txt");
if (File.Exists(uniqueFilesFile))
{
File.Delete(uniqueFilesFile);
}
File.AppendAllText(uniqueFilesFile, "Orphaned files only in source folder:" + Environment.NewLine);
foreach (string file in uniqueFiles.Where(file => !IsFileExcluded(file)))
{
File.AppendAllText(uniqueFilesFile, file + Environment.NewLine);
}
Console.WriteLine("Comparing contents of the source folder with contents of the destination folder...");
List<string> changedOrNewFiles = new()
{
Capacity = 100
};
foreach (string file in filesInDestination)
{
string relativePath = file[(destinationFolder.Length + 1)..];
string correspondingFile = Path.Combine(sourceFolder, relativePath);
if (!File.Exists(correspondingFile) || !AreFilesContentEqual(file, correspondingFile))
{
if (!IsFileExcluded(file))
{
changedOrNewFiles.Add(file);
}
}
}
Console.WriteLine("Please enter the full name for the output archive (blank for ChangedFiles.zip in system's temporary folder)");
string? zipFilePath = Console.ReadLine() ?? Path.Combine(temporaryFolder, "ChangedFiles.zip");
if (File.Exists(zipFilePath))
{
ConsoleKeyInfo choice = Choice("ChangedFiles.zip already exists. Do you want to overwrite file or cancel? (o/esc)?");
if (choice.KeyChar == 'O' || choice.KeyChar == 'o')
{
File.Delete(zipFilePath);
}
else
{
Exit();
}
}
if (IsFileInUse(zipFilePath))
{
ConsoleKeyInfo choice = Choice("ChangedFiles.zip seems to be in use. Do you want to continue or cancel? (c/esc)");
if (choice.Key == ConsoleKey.Escape)
{
Exit();
}
}
using (ZipArchive zip = ZipFile.Open(zipFilePath, ZipArchiveMode.Create))
{
zip.CreateEntryFromFile(uniqueFilesFile, uniqueFilesFile[(temporaryFolder.Length + 1)..]);
File.Delete(uniqueFilesFile);
foreach (string file in changedOrNewFiles)
{
string relativePath = file[(destinationFolder.Length + 1)..];
zip.CreateEntryFromFile(file, relativePath);
}
}
Exit($"Zip file created at: {zipFilePath}");
return;
static bool IsFileInUse(string file)
{
if (!File.Exists(file))
{
return false;
}
try
{
using FileStream stream = new(file, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
stream.Close();
return false;
}
catch (IOException)
{
return true;
}
}
static List<string> CompareFolders(string? source, string destination)
{
HashSet<string?> sourceFiles = Directory.GetFiles(source).Select(Path.GetFileName).ToHashSet();
HashSet<string?> destinationFiles = Directory.GetFiles(destination).Select(Path.GetFileName).ToHashSet();
sourceFiles.ExceptWith(destinationFiles);
return sourceFiles.ToList();
}
static bool AreFilesContentEqual(string filePath1, string filePath2)
{
byte[] file1 = File.ReadAllBytes(filePath1);
byte[] file2 = File.ReadAllBytes(filePath2);
// compare contents
if (file1.Length != file2.Length)
{
return false;
}
// compare bytes
for (int i = 0; i < file1.Length; i++)
{
if (file1[i] != file2[i])
{
return false;
}
}
return true;
}
static bool IsFileExcluded(string filePath)
{
return filePath.Contains(".git") || filePath.Contains(".vs");
}
static ConsoleKeyInfo Choice(string message)
{
Console.WriteLine(message);
ConsoleKeyInfo choice = Console.ReadKey();
Console.WriteLine();
return choice;
}
static void Exit(string message = "")
{
if (!string.IsNullOrEmpty(message))
{
Console.WriteLine(message);
}
Console.WriteLine("Press any key to exit.");
Console.ReadLine();
}