Advertisement
kyrathasoft

CompressFilesToZipArchive

May 27th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.IO.Compression;
  4.  
  5. namespace ConsoleApplication {
  6.  
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. string target_subdir = "forzipping";
  12. string archive_dest_dir = "destination";
  13. long cum_bytes_files_targeted_for_archiving = 0;
  14.  
  15. string[] files = Directory.GetFiles(@"c:\csdev\examples\CompressFilesToZipArchive", "*", SearchOption.AllDirectories);
  16.  
  17. Console.Write("\n Top-level and subdirectory files discovered: {0}", files.Length.ToString());
  18.  
  19. if(Directory.Exists(target_subdir)){
  20. files = Directory.GetFiles(target_subdir, "*", SearchOption.TopDirectoryOnly);
  21. if(files.Length > 0){
  22. long tot_bytes = CountBytesAcrossAllFiles(files);
  23. cum_bytes_files_targeted_for_archiving = tot_bytes;
  24. long cum_bytes = 0;
  25. Console.Write("\n Files to be zipped are in subdir \"{0}\": {1} file(s) totaling {2} bytes.", target_subdir, files.Length, String.Format("{0:n0}", tot_bytes));
  26. Console.Write("\n {0}:", target_subdir);
  27. for(int i=0; i < files.Length; i++){
  28. Console.Write("\n {0}", Path.GetFileName(files[i]));
  29. FileInfo fi = new FileInfo(files[i]);
  30. cum_bytes += fi.Length;
  31. Console.Write(" ({0} bytes)", String.Format("{0:n0}", fi.Length));
  32. Console.Write("; cumulative {0} bytes...", String.Format("{0:n0}", cum_bytes));
  33. }
  34. }else{
  35. Console.Write("\n No files were found in target subdirectory \"{0}\"", target_subdir);
  36. }
  37.  
  38. if(!Directory.Exists("destination")){ Directory.CreateDirectory(archive_dest_dir);}
  39. string[] dest_files = Directory.GetFiles(archive_dest_dir, "*.zip", SearchOption.TopDirectoryOnly);
  40. if(dest_files.Length > 0){
  41. for(int i=0; i < dest_files.Length; i++){
  42. try{
  43. File.Delete(dest_files[i]);
  44. }catch{}
  45. }
  46. }
  47.  
  48. if(CompressDirectoryToZipArchive(target_subdir, archive_dest_dir, false, cum_bytes_files_targeted_for_archiving)){
  49. //archiving of files into a single *.zip file was successful
  50. }
  51. }else{
  52. Directory.CreateDirectory(target_subdir);
  53. Console.Write("\n Targeted subdirectory for archiving, \"{0}\", contains zero files.", target_subdir);
  54. }
  55.  
  56.  
  57. }
  58.  
  59. public static bool CompressDirectoryToZipArchive(string targetDirectory, string outputDirectory, bool silent, long cumulative_files_bytes)
  60. {
  61. bool success = false;
  62.  
  63. if(!Directory.Exists(targetDirectory)){
  64. Console.Write("\n The target directory does not exist.");
  65. return success;
  66. }
  67.  
  68. if(Directory.Exists(targetDirectory)){
  69. int subdirs = CountDirectoriesRecursively(targetDirectory);
  70. int files = GetDirectoryFileCount(targetDirectory);
  71. if((subdirs >= 0) && (files > 0)){
  72. try{
  73. string dirName = new DirectoryInfo(targetDirectory).Name;
  74. try{
  75. if(Directory.Exists(outputDirectory)){
  76. dirName = outputDirectory + "\\" + dirName;
  77. }
  78. }catch{}
  79. string fileOut = dirName + ".zip";
  80. ZipFile.CreateFromDirectory(targetDirectory, fileOut);
  81. if(!silent){
  82. FileInfo fi = new FileInfo(fileOut);
  83. Console.Write("\n Archive was created: {0} bytes", String.Format("{0:n0}", fi.Length));
  84.  
  85. decimal percentOfOriginalSize = (100 * ((decimal)fi.Length / (decimal)cumulative_files_bytes));
  86. //Console.Write("\n Size of created archive zip file: {0}", String.Format("{0:n0}", fi.Length));
  87. Console.Write("\n The zipped archive of targeted files is {0}% the size of the sum of the targeted files.", String.Format("{0:0.00}", percentOfOriginalSize));
  88.  
  89. Console.WriteLine();
  90. }
  91. success = true;
  92. return success;
  93. }catch(Exception exWhileTryingToCompressToZip){
  94. Console.WriteLine(" Error in CompressDirectoryToZipArchive() method:");
  95. Console.WriteLine(" " + exWhileTryingToCompressToZip.Message);
  96. }
  97. }else{
  98. if(files < 1){
  99. Console.Write("\n There were no files in the target directory; archive creation aborted.\n");
  100. success = false;
  101. }
  102. }
  103. }else{
  104. success = false;
  105. }
  106.  
  107. return success;
  108. }
  109.  
  110. public static long CountBytesAcrossAllFiles(string[] _files)
  111. {
  112. long result = 0;
  113.  
  114. for(int i=0; i < _files.Length; i++){
  115. FileInfo fi = new FileInfo(_files[i]);
  116. result += fi.Length;
  117. }
  118.  
  119. return result;
  120. }
  121.  
  122. public static int CountDirectoriesRecursively(string targetDirectory)
  123. {
  124. int iDirs = 0; //if targetDirectory exists, returns total # of all subdirectories, even nested ones
  125.  
  126. if(Directory.Exists(targetDirectory)){
  127. iDirs = Directory.GetDirectories(targetDirectory, "*", SearchOption.AllDirectories).Length;
  128. }
  129.  
  130. return iDirs;
  131. }
  132.  
  133. public static int GetDirectoryFileCount(string path)
  134. {
  135. int fileCount = 0;
  136.  
  137. if(Directory.Exists(path))
  138. {
  139. fileCount = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).Length;
  140. }
  141.  
  142. return fileCount;
  143. }
  144.  
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement