Zipclass zc=new Zipclass (); Zc. Zipdir (@ "E:\1\ new Folder", @ "E:\1\ new folder. zip", 1);//Compression ZC. UnZip (@ "E:\1\ new folder. zip", @ "E:\1\2222");//Unzip
Cs
1 classZipclass2 {3 Public voidUnZip (stringZipfilepath,stringUnzipdir)4 {5 if(Zipfilepath = =string. Empty)6 {7 Throw NewException ("The compressed file cannot be empty! ");8 }9 if(!file.exists (zipfilepath))Ten { One Throw NewSystem.IO.FileNotFoundException ("The compressed file does not exist! "); A } - //The folder with the same name as the compressed file is in the same directory as the compressed file when the Unpacked folder is empty - if(Unzipdir = =string. Empty) theUnzipdir =Zipfilepath.replace (Path.getfilename (Zipfilepath), Path.getfilenamewithoutextension (ZipFilePath)); - if(!unzipdir.endswith ("//")) -Unzipdir + ="//"; - if(!directory.exists (unzipdir)) + directory.createdirectory (unzipdir); - + using(Zipinputstream s =NewZipinputstream (File.openread (Zipfilepath))) A { at - ZipEntry theentry; - while((Theentry = S.getnextentry ())! =NULL) - { - stringDirectoryName =Path.getdirectoryname (theentry.name); - stringFileName =Path.getfilename (theentry.name); in if(Directoryname.length >0) - { toDirectory.CreateDirectory (Unzipdir +directoryname); + } - if(!directoryname.endswith ("//")) theDirectoryName + ="//"; * if(FileName! =String.Empty) $ {Panax Notoginseng using(FileStream streamWriter = file.create (Unzipdir +theentry.name)) - { the + intSize =2048; A byte[] data =New byte[2048]; the while(true) + { -Size = S.read (data,0, data. Length); $ if(Size >0) $ { -StreamWriter.Write (data,0, size); - } the Else - {Wuyi Break; the } - } Wu } - } About } $ } - } - - A + Public Static voidZipdir (stringSDir,stringSzip, Zipoutputstream s) the { - string[] filenames =Directory.GetFiles (sDir); $ string[] Dirnames =directory.getdirectories (sDir); theCrc32 CRC =NewCrc32 (); the if(s = =NULL) the { thes =NewZipoutputstream (File.create (Szip)); -S.setlevel (6);//0-store-9-means best Compression in } the the About foreach(stringFileinchfilenames) the Filezipinstream (file, s); the the foreach(stringDirinchdirnames) +Zipdir (dir,"", s); - if(Szip! ="") the {Bayi s.finish (); the s.close (); the -FileInfo Finfo =NewFileInfo (szip); - LongSize =finfo.length; the //log.writelogd ("----------------" + size. ToString ()); the if(Size < -) the File.delete (szip); the } - } the the}
View Code
Zip (ICSharpCode.SharpZipLib.dll file needs to be downloaded)