//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