Zip (the ICSharpCode. SharpZipLib. dll file needs to be downloaded ),
ZipClass zc = new ZipClass (); zc. zipDir (@ "E: \ 1 \ new folder", @ "E: \ 1 \ new folder .zip", 1); // compress zc. unZip (@ "E: \ 1 \ new folder. Zip", @ "E: \ 1 \ 2222"); // decompress
Cs
1 class ZipClass 2 {3 public void UnZip (string zipFilePath, string unZipDir) 4 {5 if (zipFilePath = string. empty) 6 {7 throw new Exception ("the compressed file cannot be Empty! "); 8} 9 if (! File. Exists (zipFilePath) 10 {11 throw new System. IO. FileNotFoundException ("the compressed File does not exist! "); 12} 13 // when the decompressed folder is empty, the folder 14 if (unZipDir = string. empty) 15 unZipDir = zipFilePath. replace (Path. getFileName (zipFilePath), Path. getFileNameWithoutExtension (zipFilePath); 16 if (! UnZipDir. EndsWith ("//") 17 unZipDir + = "//"; 18 if (! Directory. exists (unZipDir) 19 Directory. createDirectory (unZipDir); 20 21 using (ZipInputStream s = new ZipInputStream (File. openRead (zipFilePath) 22 {23 24 ZipEntry theEntry; 25 while (theEntry = s. getNextEntry ())! = Null) 26 {27 string directoryName = Path. getDirectoryName (theEntry. name); 28 string fileName = Path. getFileName (theEntry. name); 29 if (directoryName. length> 0) 30 {31 Directory. createDirectory (unZipDir + directoryName); 32} 33 if (! DirectoryName. EndsWith ("//") 34 directoryName + = "//"; 35 if (fileName! = String. empty) 36 {37 using (FileStream streamWriter = File. create (unZipDir + theEntry. name) 38 {39 40 int size = 2048; 41 byte [] data = new byte [2048]; 42 while (true) 43 {44 size = s. read (data, 0, data. length); 45 if (size> 0) 46 {47 streamWriter. write (data, 0, size); 48} 49 else50 {51 break; 52} 53} 54} 55} 56} 57} 58} 59 60 61 62 public static void ZipDir (string sDir, string sZip, ZipOutputStream s) 6 3 {64 string [] filenames = Directory. getFiles (sDir); 65 string [] dirnames = Directory. getDirectories (sDir); 66 Crc32 crc = new Crc32 (); 67 if (s = null) 68 {69 s = new ZipOutputStream (File. create (sZip); 70 s. setLevel (6); // 0-store only to 9-means best compression71} 72 73 74 foreach (string file in filenames) 75 fileZipInStream (file, s ); 76 77 foreach (string dir in dirnames) 78 ZipDir (dir, "", s ); 79 if (sZip! = "") 80 {81 s. finish (); 82 s. close (); 83 84 FileInfo fInfo = new FileInfo (sZip); 85 long size = fInfo. length; 86 // Log. writeLogD ("----------------" + size. toString (); 87 if (size <100) 88 File. delete (sZip); 89} 90} 91 92}View Code
ICSharpCode. SharpZipLib. dll: http:// I .cnblogs.com/Files.aspx
Thank you for providing such information.