1、首先下载:ICSharpCode.SharpZipLib.dll
2、代码:
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="fileName">要压缩的所有文件(完全路径)</param>
/// <param name="name">压缩后文件路径</param>
/// <param name="Level">压缩级别</param>
public void ZipFileMain(string filenames, string name)
{
ZipOutputStream s = new ZipOutputStream(File.Create(name));
Crc32 crc = new Crc32();
try
{
//打开压缩文件
FileStream fs = File.OpenRead(filenames);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
//建立压缩实体
ZipEntry entry = new ZipEntry(System.IO.Path.GetFileName(filenames));
//时间
entry.DateTime = DateTime.Now;
//空间大小
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
s.PutNextEntry(entry);
s.Write(buffer, 0, buffer.Length);
}
catch
{
throw;
}
finally
{
s.Finish();
s.Close();
}
}
}