"C #" zip compression and decompression

Source: Internet
Author: User
Tags md5 encryption

//Compression ClassclassZipclass { Public voidZipFile (stringFiletozip,stringZipedfile,intCompressionLevel,intBlockSize) {            if(!System.IO.File.Exists (Filetozip)) {                Throw NewSystem.IO.FileNotFoundException ("The specified file"+ Filetozip +"could not being found. Zipping Aborderd"); } System.IO.FileStream Streamtozip=NewSystem.IO.FileStream (Filetozip, System.IO.FileMode.Open, System.IO.FileAccess.Read); System.IO.FileStream ZipFile=System.IO.File.Create (Zipedfile); Zipoutputstream ZipStream=NewZipoutputstream (ZipFile); ZipEntry ZipEntry=NewZipEntry ("Zippedfile");            Zipstream.putnextentry (ZipEntry);            Zipstream.setlevel (CompressionLevel); byte[] buffer =New byte[BlockSize]; System.Int32 size= Streamtozip.read (buffer,0, buffer.            Length); Zipstream.write (Buffer,0, size); Try            {                 while(Size <streamtozip.length) {intSizeread = streamtozip.read (buffer,0, buffer.                    Length); Zipstream.write (Buffer,0, Sizeread); Size+=Sizeread; }            }            Catch(System.Exception ex) {Throwex;            } zipstream.finish ();            Zipstream.close ();        Streamtozip.close (); }         Public voidZipfilemain (string[] args,stringpassword) {            string[] filenames = Directory.GetFiles (args[0]); Zipoutputstream s=NewZipoutputstream (File.create (args[1])); S.setlevel (6);//0-store-9-means best CompressionS.password=md5.encrypt (password); foreach(stringFileinchfilenames) {                //Open Compressed FileFileStream fs =File.openread (File); byte[] buffer =New byte[FS.                Length]; Fs. Read (Buffer,0, buffer.                Length); Array arr= file. Split ('\\'); stringLe = arr. GetValue (arr. Length-1).                ToString (); ZipEntry entry=NewZipEntry (LE); Entry. DateTime=DateTime.Now; Entry. Size=FS.                Length; Fs.                Close ();                S.putnextentry (entry); S.write (Buffer,0, buffer.            Length);            } s.finish ();        S.close (); }    }
//Decompression ClassclassUnzipclass { Public voidUnZip (string[] args,stringpassword) {            stringDirectoryName = Path.getdirectoryname (args[1]); using(FileStream filestreamin =NewFileStream (args[0], FileMode.Open, FileAccess.Read) {using(Zipinputstream Zipinstream =NewZipinputstream (filestreamin)) {Zipinstream.password=md5.encrypt (password); ZipEntry entry=Zipinstream.getnextentry ();  Do                    {                        using(FileStream filestreamout =NewFileStream (DirectoryName +@"\"+entry. Name, FileMode.Create, FileAccess.Write)) {intSize =2048; byte[] buffer =New byte[2048];  Do{size= Zipinstream.read (buffer,0, buffer.                                Length); Filestreamout.write (Buffer,0, size); }  while(Size >0); }                    }  while((Entry = Zipinstream.getnextentry ())! =NULL); }            }        }    }
//MD5 Encryption Class classMD5 {/// <summary>        ///32-bit MD5 encryption/// </summary>        /// <param name= "str" >Encrypted characters</param>        /// <returns></returns>         Public Static stringEncryptstringstr) {            stringCL =str; stringPWD =""; MD5 MD5=MD5.            Create (); byte[] s =Md5.computehash (Encoding.UTF8.GetBytes (CL));  for(inti =0; i < s.length; i++) {pwd= pwd + s[i]. ToString ("X"); }            returnpwd; }    }

ICSharpCode.SharpZipLib.dll Download

"C #" zip compression and decompression

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.