Last active
June 6, 2019 21:53
-
-
Save kyrathasoft/bdebc9ad97941e35abdde146bbf2d73b to your computer and use it in GitHub Desktop.
example of zipping files to zip archive or extracting zipped files to a subdirectory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.IO; | |
| using System.IO.Compression; | |
| namespace ConsoleApplication { | |
| /* | |
| links to online source code listing: last updated 6/4/2019 | |
| + https://gist.github.com/kyrathasoft/bdebc9ad97941e35abdde146bbf2d73b | |
| + https://pastebin.com/QYFZXEkq | |
| */ | |
| class Program{ | |
| static bool blnExit = false; | |
| static string startPath = @".\start"; | |
| static string zipPath = @".\result.zip"; | |
| static string extractPath = @".\extract"; | |
| static void Main(string[] args){ | |
| ShowTitle(); | |
| if(Init()){ | |
| Console.WriteLine("\n Program initialization succeeded."); | |
| /* Because Init() succeeded, we know that: | |
| (1) there's a /start subdir with at least one file | |
| (2) there's NOT a result.zip file in the EXE's dir | |
| (3) there's an /extract subdir that is empty | |
| */ | |
| while(!blnExit){ | |
| BuildMenu(); | |
| } | |
| Console.WriteLine("\n Program exits...\n"); | |
| } | |
| } | |
| public static void BuildMenu(){ | |
| Console.WriteLine("\n\n Menu:"); | |
| if(StartSubdirExistsWithOneOrMoreFiles() && ZipArchiveResultFileDoesntExist()){ | |
| Console.WriteLine(" (Z)ip start subdir files to result.zip"); | |
| } | |
| if(File.Exists("result.zip")){ | |
| Console.WriteLine(" (E)xtract result.zip to .\\extract subdirectory"); | |
| Console.WriteLine(" Extract (O)nly 'Program.cs to .\\extract sudirectory"); | |
| Console.WriteLine(" (D)oes file 'MainForm.cs' exist in archive?"); | |
| if(!ZipArchiveContainsThisFile("result.zip", "MyNameIs.txt")){ | |
| Console.WriteLine(" D(y)namically add name/b-day to archive as entry 'MyNameIs.txt'"); | |
| } | |
| } | |
| Console.WriteLine(" E(x)it Program"); | |
| Console.Write(" Your selection > "); | |
| ConsoleKeyInfo cki = Console.ReadKey(); | |
| switch(cki.Key){ | |
| case ConsoleKey.Z: | |
| ZipFile.CreateFromDirectory(startPath, zipPath); | |
| SeeIfExtractDirExistsAndDeleteItsFiles(); | |
| break; | |
| case ConsoleKey.D: | |
| if(ZipArchiveContainsThisFile("result.zip", "MainForm.cs")){ | |
| Console.WriteLine("\n Yes, 'MainForm.cs' is contained in result.zip."); | |
| }else{ | |
| Console.WriteLine("\n No, the program didn't find 'MainForm.cs' in result.zip."); | |
| } | |
| break; | |
| case ConsoleKey.E: | |
| extractPath = SanitizeExtractPath(extractPath); | |
| ZipFile.ExtractToDirectory(zipPath, extractPath); | |
| SeeIfZippedArchiveExistsIfSoDelete(); | |
| break; | |
| case ConsoleKey.O: | |
| extractPath = SanitizeExtractPath(extractPath); | |
| using (ZipArchive archive = ZipFile.OpenRead(zipPath)){ | |
| foreach (ZipArchiveEntry entry in archive.Entries){ | |
| string fname = entry.FullName; | |
| if(fname.Contains("Program.cs")){ | |
| //Console.WriteLine("\n Found 'Program.cs'..."); | |
| string destinationPath = Path.GetFullPath(Path.Combine(extractPath, entry.FullName)); | |
| Console.WriteLine("\n Dest. path: {0}", destinationPath); | |
| entry.ExtractToFile(destinationPath); | |
| } | |
| } | |
| } | |
| break; | |
| case ConsoleKey.Y: | |
| using (FileStream zipToOpen = new FileStream(@".\result.zip", FileMode.Open)) | |
| { | |
| using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update)) | |
| { | |
| ZipArchiveEntry readmeEntry = archive.CreateEntry("MyNameIs.txt"); | |
| using (StreamWriter writer = new StreamWriter(readmeEntry.Open())) | |
| { | |
| writer.WriteLine("My name is Bryan Miller."); | |
| writer.WriteLine("The date: {0}", DateTime.Now.ToString()); | |
| } | |
| Console.WriteLine("\n Successfully added 'MyNameIs.txt' to 'result.zip'"); | |
| } | |
| } | |
| break; | |
| default: | |
| blnExit = true; | |
| break; | |
| } | |
| } | |
| public static bool Init(){ | |
| bool success = false; | |
| if(SeeIfZippedArchiveExistsIfSoDelete()){ | |
| if(SeeIfExtractDirExistsAndDeleteItsFiles()){ | |
| if(SeeIfStartDirExistsAndContainsFilesToBeZipped()){ | |
| success = true; | |
| } | |
| } | |
| } | |
| return success; | |
| } | |
| public static string SanitizeExtractPath(string p){ | |
| string result = p; | |
| result = Path.GetFullPath(result); | |
| if (!result.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal)){ | |
| result += Path.DirectorySeparatorChar; | |
| } | |
| return p; | |
| } | |
| public static bool ZipArchiveResultFileDoesntExist(){ | |
| bool result = false; | |
| if(!File.Exists("result.zip")){ result = true;} | |
| return result; | |
| } | |
| public static bool ZipArchiveContainsThisFile(string _archive, string _filename_in_question){ | |
| bool result = false; | |
| if(!File.Exists(_archive)){ | |
| //control falls through; false is returned... | |
| }else{ | |
| using(ZipArchive archive = ZipFile.OpenRead(_archive)){ | |
| foreach(ZipArchiveEntry entry in archive.Entries){ | |
| string filename = Path.GetFileName(entry.FullName); | |
| if(filename == _filename_in_question){ | |
| result = true; | |
| break; | |
| } | |
| } | |
| } | |
| } | |
| return result; | |
| } | |
| public static bool StartSubdirExistsWithOneOrMoreFiles(){ | |
| bool result = false; | |
| if(Directory.Exists(@".\start")){ | |
| string[] files = Directory.GetFiles(@".\start", "*.*", SearchOption.TopDirectoryOnly); | |
| if(files.Length > 0){ | |
| result = true; | |
| } | |
| } | |
| return result; | |
| } | |
| public static bool SeeIfZippedArchiveExistsIfSoDelete(){ | |
| bool success = false; | |
| if(File.Exists("result.zip")){ | |
| try{ | |
| File.Delete("result.zip"); | |
| success = true; | |
| }catch{ | |
| Console.WriteLine("\n Unable to delete result.zip during initialization."); | |
| } | |
| }else{ success = true; } | |
| return success; | |
| } | |
| public static bool SeeIfExtractDirExistsAndDeleteItsFiles(){ | |
| bool success = false; | |
| if(Directory.Exists(@".\extract")){ | |
| string[] files = Directory.GetFiles(@".\extract", "*.*", SearchOption.TopDirectoryOnly); | |
| if(files.Length > 0){ | |
| try{ | |
| for(int i=0; i < files.Length; i++){ | |
| File.Delete(files[i]); | |
| } | |
| success = true; | |
| }catch{ | |
| Console.WriteLine("\n Unable to delete files in .\\extract during initialization."); | |
| } | |
| }else{ success = true; } | |
| }else{ | |
| Directory.CreateDirectory(@".\extract"); | |
| success = true; | |
| } | |
| return success; | |
| } | |
| public static bool SeeIfStartDirExistsAndContainsFilesToBeZipped(){ | |
| bool success = false; | |
| if(!Directory.Exists(@".\start")){ | |
| Console.WriteLine("\n Missing .\\start subdirectory with files to be zipped."); | |
| Console.WriteLine(" Program will terminate.\n"); | |
| }else{ | |
| //directory exists, but does it contain one or more files to be zipped? | |
| string[] files = Directory.GetFiles(@".\start", "*.*", SearchOption.TopDirectoryOnly); | |
| if(files.Length < 1){ | |
| Console.WriteLine("\n Subdirectory .\\start doesn't have any files to be zipped."); | |
| Console.WriteLine(" Program will terminate.\n"); | |
| }else{ | |
| success = true; | |
| } | |
| } | |
| return success; | |
| } | |
| public static void ShowTitle(){ | |
| Console.Clear(); | |
| Console.ForegroundColor = ConsoleColor.Yellow; | |
| Console.WriteLine("\n running 'simple.exe'..."); | |
| Console.ForegroundColor = ConsoleColor.Gray; | |
| Console.WriteLine(" kyrathasoft@gmail.com, 6/02/2019"); | |
| Console.ForegroundColor = ConsoleColor.White; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment