Zip (ICSharpCode.SharpZipLib.dll file needs to be downloaded)

Source: Internet
Author: User
Tags crc32

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)

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.