This repository was archived by the owner on Apr 2, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
73 lines (62 loc) · 2.73 KB
/
Copy pathProgram.cs
File metadata and controls
73 lines (62 loc) · 2.73 KB
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
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Threading;
namespace HuffmanCompression
{
public static class Program
{
private static readonly string NoArgumentErrorText = "You have not specified any files to compress/decompress.";
private static readonly string TooManyCharactersExceptionText = "The specified file has too many characters.";
private static readonly string UnexpectedErrorText = "There was an unexpected error. Skipping.";
private static readonly string WaitForKeypressText = "\nPress any button to close...";
private static void Main(string[] args)
{
if (!args.Any())
Console.WriteLine(NoArgumentErrorText);
else
DoActionsOnFiles(args);
WaitForKeyPress();
}
private static void WaitForKeyPress()
{
Console.WriteLine(WaitForKeypressText);
Console.ReadKey();
}
private static void DoActionsOnFiles(string[] paths)
{
var runningThreads = new List<Thread>();
foreach (string path in paths)
{
PerformFileAction fileAction = new PerformFileAction
(path.EndsWith(Compressor.CompressedFileExtension)
? (PerformFileAction)DecompressFile
: (PerformFileAction)CompressFile);
//TODO: Limit number of threads.
var thread = new Thread(() => fileAction.Invoke(path));
thread.Start();
runningThreads.Add(thread);
}
foreach (Thread thread in runningThreads)
if (thread.IsAlive)
thread.Join();
}
private delegate void PerformFileAction(string path);
private static void CompressFile(string path)
{
string filename = Path.GetFileName(path);
Console.WriteLine($"Compressing {filename}");
try { Console.WriteLine($"Done compressing {filename}. Summary:\n{Compressor.CompressFile(path)}"); ; }
catch (TooManyCharactersException) { Console.WriteLine(TooManyCharactersExceptionText); }
catch (Exception e) { Console.WriteLine($"{UnexpectedErrorText} (exception: {e.GetType()})"); }
}
private static void DecompressFile(string path)
{
string filename = Path.GetFileName(path);
Console.WriteLine($"Decompressing {filename}");
try { Decompressor.DecompressFile(path); Console.WriteLine($"Done decompressing {filename}"); ; }
catch (Exception e) { Console.WriteLine($"{UnexpectedErrorText} (exception: {e.GetType()})"); }
}
}
}