|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.IO.Compression; |
| 4 | +using System.Windows; |
| 5 | + |
| 6 | +namespace Hider |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Main logic for MainWindow |
| 10 | + /// </summary> |
| 11 | + public partial class MainWindow : Window |
| 12 | + { |
| 13 | + string inputFileExt; |
| 14 | + |
| 15 | + public MainWindow() |
| 16 | + { |
| 17 | + inputFileExt = ""; |
| 18 | + } |
| 19 | + |
| 20 | + private void OnQuitApp(object sender, RoutedEventArgs e) |
| 21 | + { |
| 22 | + Application.Current.Shutdown(); |
| 23 | + } |
| 24 | + |
| 25 | + private void OnHideFiles(object sender, RoutedEventArgs e) |
| 26 | + { |
| 27 | + // Get the path values from the input boxes. |
| 28 | + String inputFilePath = inputFileBox.Text.Trim(); |
| 29 | + String inputDirPath = inputDirBox.Text.Trim(); |
| 30 | + String outputFilePath = outputFileBox.Text.Trim(); |
| 31 | + |
| 32 | + // Remove the temporary file if it exists. |
| 33 | + if (File.Exists("./temp.zip")) |
| 34 | + { |
| 35 | + File.Delete("./temp.zip"); |
| 36 | + } |
| 37 | + |
| 38 | + byte[] inputFileBuffer; |
| 39 | + if (File.Exists(inputFilePath)) |
| 40 | + { |
| 41 | + inputFileBuffer = File.ReadAllBytes(inputFilePath); |
| 42 | + } |
| 43 | + else |
| 44 | + { |
| 45 | + ShowError("Cannot open input file to read!"); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + FileStream outputFile; |
| 50 | + if(File.Exists(outputFilePath)) |
| 51 | + { |
| 52 | + ShowError("Please select a different output file!"); |
| 53 | + return; |
| 54 | + } |
| 55 | + else |
| 56 | + { |
| 57 | + outputFile = File.Create(outputFilePath); |
| 58 | + } |
| 59 | + |
| 60 | + if (!Directory.Exists(inputDirPath)) |
| 61 | + { |
| 62 | + ShowError("The given input directory does not exist!"); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + ZipFile.CreateFromDirectory(inputDirPath, "./temp.zip"); |
| 67 | + byte[] zipBuffer = File.ReadAllBytes("./temp.zip"); |
| 68 | + byte[] buffer = new byte[zipBuffer.Length+inputFileBuffer.Length]; |
| 69 | + inputFileBuffer.CopyTo(buffer, 0); |
| 70 | + zipBuffer.CopyTo(buffer, inputFileBuffer.Length); |
| 71 | + outputFile.Write(buffer); |
| 72 | + outputFile.Close(); |
| 73 | + File.Delete("./temp.zip"); |
| 74 | + |
| 75 | + ShowInformation("The file has been saved successfully! Open the output file using an archive manager(WinRAR, 7-Zip, WinZIP etc.) to view the hidden files!"); |
| 76 | + } |
| 77 | + |
| 78 | + private void ShowError(string errMsg) |
| 79 | + { |
| 80 | + MessageBox.Show(errMsg, "Error", MessageBoxButton.OK, MessageBoxImage.Error); |
| 81 | + } |
| 82 | + |
| 83 | + private void ShowInformation(string msg) |
| 84 | + { |
| 85 | + MessageBox.Show(msg, "Information", MessageBoxButton.OK, MessageBoxImage.Information); |
| 86 | + } |
| 87 | + |
| 88 | + private void OnBrowseInputFile(object sender, RoutedEventArgs e) |
| 89 | + { |
| 90 | + System.Windows.Forms.OpenFileDialog dialog = new(); |
| 91 | + dialog.Title = "Please select an input file"; |
| 92 | + dialog.Filter = "Supported Files(*.jpg, *.mp3, *.mp4)|*.jpg;*.mp3;*.mp4"; |
| 93 | + dialog.ShowDialog(); |
| 94 | + inputFileBox.Text = dialog.FileName; |
| 95 | + inputFileExt = Path.GetExtension(inputFileBox.Text); // Get the extension of the input file. |
| 96 | + } |
| 97 | + |
| 98 | + private void OnBrowseInputDir(object sender, RoutedEventArgs e) |
| 99 | + { |
| 100 | + System.Windows.Forms.FolderBrowserDialog dialog = new(); |
| 101 | + dialog.ShowNewFolderButton = false; |
| 102 | + dialog.ShowDialog(); |
| 103 | + inputDirBox.Text = dialog.SelectedPath; |
| 104 | + } |
| 105 | + |
| 106 | + private void OnBrowseOutputFile(object sender, RoutedEventArgs e) |
| 107 | + { |
| 108 | + System.Windows.Forms.SaveFileDialog dialog = new(); |
| 109 | + dialog.Title = "Please select an output file"; |
| 110 | + dialog.Filter = $"Input file type (*{inputFileExt})|*{inputFileExt}"; // Remove the . from the input file extension and set is as filter. |
| 111 | + dialog.ShowDialog(); |
| 112 | + outputFileBox.Text = dialog.FileName; |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments