需要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;
/// <summary>
/// 2005-7-9
/// 垃圾猪
/// ewebapp.net
/// </summary>
namespace eWebapp.Upload
{
/// <summary>
/// 压缩解压处理。
/// </summary>
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 属性
/// <summary>
/// 压缩文件名称
/// </summary>
public string Zipname
{
set{ zipname = value;}
}
/// <summary>
/// 解压文件夹
/// </summary>
public string Unzipdir
{
set { unzipdir = value;}
}
/// <summary>
/// 压缩文件密码
/// </summary>
public string Password
{
set {password = value;}
}
/// <summary>
/// 压缩级别
/// </summary>
public int Level
{
set {level = value;}
}
public eZip()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#endregion
/// <summary>
/// 创建压缩文件
/// </summary>
public void CreateZip()
{
crc = new Crc32();
s = new ZipOutputStream(File.Create(this.zipname));
s.SetLevel(level);
s.Password = password;
}
/// <summary>
/// 结束操作
/// </summary>
public void FinishZip()
{
s.Finish();
s.Close();
}
/// <summary>
/// 增加一个文件
/// </summary>
/// <param name="_crc"></param>
/// <param name="_s"></param>
/// <param name="_filename"></param>
/// <param name="_filesize"></param>
/// <param name="_buffer"></param>
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);
}
/// <summary>
/// 添加一个文件到压缩文件中
/// </summary>
/// <param name="_filename"></param>
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);
}
/// <summary>
/// 新建文本文件
/// </summary>
/// <param name="_crc"></param>
/// <param name="_s"></param>
/// <param name="_filename"></param>
/// <param name="_text"></param>
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);
}
/// <summary>
/// 添加一个指定内容的文本文件到压缩文件中
/// </summary>
/// <param name="_filename"></param>
/// <param name="_text"></param>
public void AddText(string _filename,string _text)
{
addText(this.crc,this.s,_filename,_text);
}
/// <summary>
/// 解压处理
/// </summary>
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();
}
}
}