ZipClass zc=new ZipClass ();
zc.ZipDir(@"E:\1\新建文件夹", @"E:\1\新建文件夹.zip", 1);//压缩
zc.UnZip(@"E:\1\新建文件夹.zip",@"E:\1\2222");//解压
cs
1classZipClass2{3publicvoidUnZip(stringzipFilePath,stringunZipDir)4{5if(zipFilePath ==string.Empty)6{7thrownewException("压缩文件不能为空!");8}9if(!File.Exists(zipFilePath))10{11thrownewSystem.IO.FileNotFoundException("压缩文件不存在!");12}13//解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹14if(unZipDir ==string.Empty)15unZipDir =zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));16if(!unZipDir.EndsWith("//"))17unZipDir +="//";18if(!Directory.Exists(unZipDir))19Directory.CreateDirectory(unZipDir);2021using(ZipInputStream s =newZipInputStream(File.OpenRead(zipFilePath)))22{2324ZipEntry theEntry;25while((theEntry = s.GetNextEntry()) !=null)26{27stringdirectoryName =Path.GetDirectoryName(theEntry.Name);28stringfileName =Path.GetFileName(theEntry.Name);29if(directoryName.Length >0)30{31Directory.CreateDirectory(unZipDir +directoryName);32}33if(!directoryName.EndsWith("//"))34directoryName +="//";35if(fileName !=String.Empty)36{37using(FileStream streamWriter = File.Create(unZipDir +theEntry.Name))38{3940intsize =2048;41byte[] data =newbyte[2048];42while(true)43{44size = s.Read(data,0, data.Length);45if(size >0)46{47streamWriter.Write(data,0, size);48}49else50{51
break;52}53}54}55}56}57}58}59606162publicstaticvoidZipDir(stringsDir,stringsZip, ZipOutputStream s)63{64string[] filenames =Directory.GetFiles(sDir);65string[] dirnames =Directory.GetDirectories(sDir);66Crc32 crc =newCrc32();67if(s ==null)68{69s =newZipOutputStream(File.Create(sZip));70s.SetLevel(6);//0 - store only to 9 - means best comPRession71}727374foreach(stringfileinfilenames)75fileZipInStream(file, s);7677foreach(stringdirindirnames)78ZipDir(dir,"", s);79if(sZip !="")80{81s.Finish();82s.Close();8384FileInfo fInfo =newFileInfo(sZip);85longsize =fInfo.Length;86//Log.WriteLogD("----------------" + size.ToString());87if(size <100)88File.Delete(sZip);89}90}9192}
View Code
ICSharpCode.SharpZipLib.dll下载地址:http://i.cnblogs.com/Files.aspx
感谢提供此类的朋友