1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.IO;6 usingIcsharpcode.sharpziplib;7 usingICSharpCode.SharpZipLib.Zip;8 usingICSharpCode.SharpZipLib.Checksums;9 Ten namespaceUnzip One { A classUnzip - { - /// <summary> the ///Unzip a file - /// </summary> - /// <param name= "Zipfilepath" >the path where the files need to be unzipped</param> - /// <param name= "Targetfilepath" >unzip the zip to this directory</param> + /// <returns></returns> - Public Static voidUnzipfile (stringZipfilepath,stringTargetfilepath) + { AZipinputstream Zipinputstream =NULL; atFileStream Streamwrite =NULL; -ZipEntry ent =NULL; - stringFileName;//variables used to read and write Zip files when extracting files - - //Remove read-only properties for a folder or file - if(true==directory.exists (zipfilepath)) in { -System.IO.DirectoryInfo DirectoryInfo =NewSystem.IO.DirectoryInfo (zipfilepath); toDirectoryinfo.attributes =Fileattributes.normal; + } - the //Create a directory * if(!directory.exists (targetfilepath)) $ directory.createdirectory (targetfilepath);Panax Notoginseng - //Traverse Folder theDirectoryInfo Thefolder =NewDirectoryInfo (zipfilepath); +directoryinfo[] Dirinfo =thefolder.getdirectories (); A the foreach(DirectoryInfo Nextfolderinchdirinfo) + { - //traversing sub-files $fileinfo[] FileInfo = Nextfolder.getfiles ("*.zip", searchoption.alldirectories); $ - foreach(FileInfo NextfileinchfileInfo) - { the Try - {Wuyi //Remove read-only properties the file.setattributes (Zipfilepath, fileattributes.normal); - WuZipinputstream =NewZipinputstream (File.openread (Nextfile.fullname)); - About while(ent = zipinputstream.getnextentry ())! =NULL) $ { - if(!string. IsNullOrEmpty (ent. Name)) - { -FileName = Path.Combine (Targetfilepath +@"\"+nextfolder.name, Ent. Name); AFileName = Filename.replace ('/','\\'); + the if(Filename.endswith ("\\")) - { $ directory.createdirectory (fileName); the Continue; the } the theStreamwrite =file.create (fileName); - intSize =2048; in byte[] data =New byte[size]; the the while(true) About { theSize = Zipinputstream.read (data,0, data. Length); the if(Size >0) theStreamwrite.write (data,0, size); + Else - Break; the }Bayi } the } the } - Catch(Exception ex) - { the Throwex; the } the finally the { - if(Streamwrite! =NULL) the { the streamwrite.close (); the streamwrite.dispose ();94 } the if(Zipinputstream! =NULL) the { the zipinputstream.close ();98 zipinputstream.dispose (); About } - if(ENT! =NULL)101 {102ENT =NULL;103 }104 GC. Collect (); theGc. Collect (1);106 }107 }108 }109 } the 111 the
C # Extract multiple folders under multiple zip to a directory