Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.IO.Compression;
- namespace ConsoleApplication {
- class Program
- {
- static void Main(string[] args)
- {
- string target_subdir = "forzipping";
- string archive_dest_dir = "destination";
- long cum_bytes_files_targeted_for_archiving = 0;
- string[] files = Directory.GetFiles(@"c:\csdev\examples\CompressFilesToZipArchive", "*", SearchOption.AllDirectories);
- Console.Write("\n Top-level and subdirectory files discovered: {0}", files.Length.ToString());
- if(Directory.Exists(target_subdir)){
- files = Directory.GetFiles(target_subdir, "*", SearchOption.TopDirectoryOnly);
- if(files.Length > 0){
- long tot_bytes = CountBytesAcrossAllFiles(files);
- cum_bytes_files_targeted_for_archiving = tot_bytes;
- long cum_bytes = 0;
- 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));
- Console.Write("\n {0}:", target_subdir);
- for(int i=0; i < files.Length; i++){
- Console.Write("\n {0}", Path.GetFileName(files[i]));
- FileInfo fi = new FileInfo(files[i]);
- cum_bytes += fi.Length;
- Console.Write(" ({0} bytes)", String.Format("{0:n0}", fi.Length));
- Console.Write("; cumulative {0} bytes...", String.Format("{0:n0}", cum_bytes));
- }
- }else{
- Console.Write("\n No files were found in target subdirectory \"{0}\"", target_subdir);
- }
- if(!Directory.Exists("destination")){ Directory.CreateDirectory(archive_dest_dir);}
- string[] dest_files = Directory.GetFiles(archive_dest_dir, "*.zip", SearchOption.TopDirectoryOnly);
- if(dest_files.Length > 0){
- for(int i=0; i < dest_files.Length; i++){
- try{
- File.Delete(dest_files[i]);
- }catch{}
- }
- }
- if(CompressDirectoryToZipArchive(target_subdir, archive_dest_dir, false, cum_bytes_files_targeted_for_archiving)){
- //archiving of files into a single *.zip file was successful
- }
- }else{
- Directory.CreateDirectory(target_subdir);
- Console.Write("\n Targeted subdirectory for archiving, \"{0}\", contains zero files.", target_subdir);
- }
- }
- public static bool CompressDirectoryToZipArchive(string targetDirectory, string outputDirectory, bool silent, long cumulative_files_bytes)
- {
- bool success = false;
- if(!Directory.Exists(targetDirectory)){
- Console.Write("\n The target directory does not exist.");
- return success;
- }
- if(Directory.Exists(targetDirectory)){
- int subdirs = CountDirectoriesRecursively(targetDirectory);
- int files = GetDirectoryFileCount(targetDirectory);
- if((subdirs >= 0) && (files > 0)){
- try{
- string dirName = new DirectoryInfo(targetDirectory).Name;
- try{
- if(Directory.Exists(outputDirectory)){
- dirName = outputDirectory + "\\" + dirName;
- }
- }catch{}
- string fileOut = dirName + ".zip";
- ZipFile.CreateFromDirectory(targetDirectory, fileOut);
- if(!silent){
- FileInfo fi = new FileInfo(fileOut);
- Console.Write("\n Archive was created: {0} bytes", String.Format("{0:n0}", fi.Length));
- decimal percentOfOriginalSize = (100 * ((decimal)fi.Length / (decimal)cumulative_files_bytes));
- //Console.Write("\n Size of created archive zip file: {0}", String.Format("{0:n0}", fi.Length));
- 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));
- Console.WriteLine();
- }
- success = true;
- return success;
- }catch(Exception exWhileTryingToCompressToZip){
- Console.WriteLine(" Error in CompressDirectoryToZipArchive() method:");
- Console.WriteLine(" " + exWhileTryingToCompressToZip.Message);
- }
- }else{
- if(files < 1){
- Console.Write("\n There were no files in the target directory; archive creation aborted.\n");
- success = false;
- }
- }
- }else{
- success = false;
- }
- return success;
- }
- public static long CountBytesAcrossAllFiles(string[] _files)
- {
- long result = 0;
- for(int i=0; i < _files.Length; i++){
- FileInfo fi = new FileInfo(_files[i]);
- result += fi.Length;
- }
- return result;
- }
- public static int CountDirectoriesRecursively(string targetDirectory)
- {
- int iDirs = 0; //if targetDirectory exists, returns total # of all subdirectories, even nested ones
- if(Directory.Exists(targetDirectory)){
- iDirs = Directory.GetDirectories(targetDirectory, "*", SearchOption.AllDirectories).Length;
- }
- return iDirs;
- }
- public static int GetDirectoryFileCount(string path)
- {
- int fileCount = 0;
- if(Directory.Exists(path))
- {
- fileCount = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).Length;
- }
- return fileCount;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement