需要SharpZipLib引用,ASP.NET 在线压缩与在线解压缩源码:
using System; using System.IO; using ICSharpCode.SharpZipLib.Checksums; using ICSharpCode.SharpZipLib.Zip; using ICSharpCode.SharpZipLib.Zip.Compression; using ICSharpCode.SharpZipLib.Zip.Compression.Streams; ////// 2005-7-9 /// 垃圾猪 /// ewebapp.net /// namespace eWebapp.Upload { ////// 压缩解压处理。 /// public class eZip { #region 私有域 private string zipname = string.Empty; private string password = string.Empty; private int level = 9; private string unzipdir = string.Empty; private Crc32 crc; ZipOutputStream s; #endregion #region 属性 ////// 压缩文件名称 /// public string Zipname { set{ zipname = value;} } ////// 解压文件夹 /// public string Unzipdir { set { unzipdir = value;} } ////// 压缩文件密码 /// public string Password { set {password = value;} } ////// 压缩级别 /// public int Level { set {level = value;} } public eZip() { // // TODO: 在此处添加构造函数逻辑 // } #endregion ////// 创建压缩文件 /// public void CreateZip() { crc = new Crc32(); s = new ZipOutputStream(File.Create(this.zipname)); s.SetLevel(level); s.Password = password; } ////// 结束操作 /// public void FinishZip() { s.Finish(); s.Close(); } ////// 增加一个文件 /// /// /// /// /// /// private void addFile(Crc32 _crc,ZipOutputStream _s,string _filename,long _filesize,byte[] _buffer) { // 添加一个文件到压缩文件中 ZipEntry entry = new ZipEntry(_filename); entry.DateTime = DateTime.Now; entry.Size = _filesize; _crc.Reset(); _crc.Update(_buffer); entry.Crc = _crc.Value; _s.PutNextEntry(entry); _s.Write(_buffer, 0, _buffer.Length); } ////// 添加一个文件到压缩文件中 /// /// public void AddFile(string _filename) { FileStream fs = File.OpenRead(_filename); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); string filename = System.IO.Path.GetFileName(_filename); long m_fLength = fs.Length; fs.Close(); addFile(this.crc,this.s,filename,m_fLength,buffer); } ////// 新建文本文件 /// /// /// /// /// private void addText(Crc32 _crc,ZipOutputStream _s,string _filename,string _text) { byte[] text = System.Text.Encoding.Default.GetBytes(_text); addFile(_crc,_s,_filename,text.Length,text); } ////// 添加一个指定内容的文本文件到压缩文件中 /// /// /// public void AddText(string _filename,string _text) { addText(this.crc,this.s,_filename,_text); } ////// 解压处理 /// public void Unzip() { string unzipdirectoryroot = this.unzipdir; // 调用组件中读取压缩文件的对象 ZipInputStream s = new ZipInputStream(File.OpenRead(this.zipname)); s.Password = this.password; ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { // 当前解压到的目录,Path.GetDirectoryName(theEntry.Name)为提取压缩文件目录 string directoryName = Path.Combine(unzipdirectoryroot,Path.GetDirectoryName(theEntry.Name)); // 当前正在解压的文件 string fileName = Path.Combine(directoryName,Path.GetFileName(theEntry.Name)); // 创建目录 Directory.CreateDirectory(directoryName); if (fileName != String.Empty) { try { // 创建解压文件 FileStream streamWriter = File.Create(fileName); // 将数据写入文件 int size = 2048; byte[] data = new byte[2048]; while (true) { size = s.Read(data, 0, data.Length); if (size > 0) { streamWriter.Write(data, 0, size); } else { break; } } streamWriter.Close(); } catch { } } } s.Close(); } } }