Zip a file in C# -


i trying zip files c# code showing zipfile class does't exist. please suggest how zip c# , using framework 4.5

my code written below

using system; using system.io; using system.io.compression;   namespace zipsample {     class program     {         static void main(string[] args)         {             string[] maindirs = directory.getdirectories("c:\\users\\public\reports");              (int = 0; < maindirs.length; i++)             {                 using (zipfile zip = new zipfile())                 {                     zip.useunicodeasnecessary = true;                     zip.adddirectory(maindirs[i]);                     zip.compressionlevel = ionic.zlib.compressionlevel.bestcompression;                     zip.comment = "this zip created @ " + system.datetime.now.tostring("g");                     zip.save(string.format("test{0}.zip", i));                 }             }         }     } } 

enter image description here

solution 1 :

it because trying instanciate static class.

using system.io.compression; ...  string startpath = @"c:\example\start"; string zippath = @"c:\example\result.zip"; string extractpath = @"c:\example\extract";  zipfile.createfromdirectory(startpath, zippath); zipfile.extracttodirectory(zippath, extractpath); 

after fixing seems work me code.

from documentation :

provides static methods creating, extracting, , opening zip archives.

more information here : https://msdn.microsoft.com/en-us/library/system.io.compression.zipfile%28v=vs.110%29.aspx


solution 2 :

it's because you've mixed ionic library code , and .net library reference. if want stick it, should download library , add reference instead of .net libraries.

the library , documentation can found here : http://dotnetzip.herobo.com/dnzhelp/index.html or http://dotnetzip.codeplex.com/

here quick example took documentation (found within library zip downloaded on links above) :

string zipfiletocreate = @"c:\temp\test.zip"; string directorytozip = @"c:\temp\foldertozip\"; try {   using (zipfile zip = new zipfile())   {     zip.statusmessagetextwriter = system.console.out;     zip.adddirectory(directorytozip); // recurses subdirectories     zip.save(zipfiletocreate);   } } catch (system.exception ex1) {   system.console.error.writeline("exception: " + ex1); } 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -