winforms - SevenZipSharp - how to compress multiple directories into a single file using c#? -


i want compress 3 folders single file using sevenzipcompressor . know how compress single folder . such thing possible ??

thank !

the sevenzipcompressor class provides method called compressfiledictionary(). 1 of method overloads expects file dictionary , file stream. file dictionary ordinary .net dictionary<string,string>. key of dictionary name (or relative path) of file in archive, value of dictionary path file in file system.

the key of dictionary allows control structure in 7z archive. example if want compress 3 folders

c:\temp\testdir1              |- file1.txt              |- file2.txt c:\temp\testdir2              |- file1.txt c:\temp2\test              |- file3.txt 

and resulting structure in archive should be

testdir1        |- file1.txt        |- file2.txt testdir2        |- file1.txt     test        |-file3.txt 

then add files dictonary in following way:

dictionary<string, string> filesdic = new dictionary<string, string>();  filesdic.add(@"testdir1\file1.txt", @"c:\temp\testdir1\files1.txt"); filesdic.add(@"testdir1\file2.txt", @"c:\temp\testdir1\files2.txt"); filesdic.add(@"testdir2\file1.txt", @"c:\temp\testdir2\files1.txt"); filesdic.add(@"test\file3.txt", @"c:\temp2\test\files3.txt"); 

the example below shows how automate process of creating such dictionary folders , compress single 7z archive file.

private static void addfilesfromdirectorytodictionary(dictionary<string, string> filesdictionary,   string pathtodirectory) {         directoryinfo dirinfo = new directoryinfo(pathtodirectory);          fileinfo[] fileinfos = dirinfo.getfiles("*.*", searchoption.alldirectories);    foreach (fileinfo fi in fileinfos)   {             filesdictionary.add(fi.fullname.replace(dirinfo.parent.fullname + "\\", "").tolower(),       fi.fullname);   }         }  static void main(string[] args) {   // set path 7z library.   sevenzipcompressor.setlibrarypath("7z.dll");    using (filestream fs = new filestream("c:\\temp\\test.7z", filemode.create))   {             sevenzipcompressor szc = new sevenzipcompressor                                  {                                    compressionmethod = compressionmethod.lzma,                                    compressionlevel = compressionlevel.normal,                                    compressionmode = compressionmode.create,                                                                          directorystructure = true,                                    preservedirectoryroot = false,                                    archiveformat = outarchiveformat.sevenzip                                  };              dictionary<string, string> filesdictionary = new dictionary<string, string>();      addfilesfromdirectorytodictionary(filesdictionary, @"c:\temp\testdir1");     addfilesfromdirectorytodictionary(filesdictionary, @"c:\temp\testdir2");     addfilesfromdirectorytodictionary(filesdictionary, @"c:\temp2\test");      szc.compressfiledictionary(filesdictionary, fs);                                  }       } 

you can create zip-archive using following code changes:

using (filestream fs = new filestream("c:\\temp\\test.zip", filemode.create)) {           sevenzipcompressor szc = new sevenzipcompressor           {             compressionmethod = compressionmethod.deflate,             compressionlevel = compressionlevel.normal,             compressionmode = compressionmode.create,                                                   directorystructure = true,             preservedirectoryroot = false,             archiveformat = outarchiveformat.zip           };             dictionary<string, string> filesdictionary = new dictionary<string, string>();     addfilesfromdirectorytodictionary(filesdictionary, @"c:\temp\testdir1");    addfilesfromdirectorytodictionary(filesdictionary, @"c:\temp\testdir2");    addfilesfromdirectorytodictionary(filesdictionary, @"c:\temp2\test");     szc.compressfiledictionary(filesdictionary, fs);                                 } 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -