C # Extract multiple folders under multiple zip to a directory

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.