添加引用ICSharpCode.SharpZipLib.dll
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Threading;
- using ICSharpCode.SharpZipLib.Zip;
- namespace Lib
- {
- /// <summary>
- /// 文件压缩、解压缩
- /// </summary>
- public class FileCompression
- {
- /// <summary>
- /// 构造函数
- /// </summary>
- public FileCompression()
- {
- }
- #region 加密、压缩文件
- /// <summary>
- /// 压缩文件
- /// </summary>
- /// <param name="fileNames">要打包的文件列表</param>
- /// <param name="GzipFileName">目标文件名</param>
- /// <param name="CompressionLevel">压缩品质级别(0~9)</param>
- /// <param name="SleepTimer">休眠时间(单位毫秒)</param>
- public static void Compress(List<FileInfo> fileNames, string GzipFileName, int CompressionLevel, int SleepTimer)
- {
- ZipOutputStream s = new ZipOutputStream(File.Create(GzipFileName));
- try
- {
- s.SetLevel(CompressionLevel); //0 - store only to 9 - means best compression
- foreach (FileInfo file in fileNames)
- {
- FileStream fs = null;
- try
- {
- fs = file.Open(FileMode.Open, FileAccess.ReadWrite);
- }
- catch
- { continue; }
- // 方法二,将文件分批读入缓冲区
- byte[] data = new byte[2048];
- int size = 2048;
- ZipEntry entry = new ZipEntry(Path.GetFileName(file.Name));
- entry.DateTime = (file.CreationTime > file.LastWriteTime ? file.LastWriteTime : file.CreationTime);
- s.PutNextEntry(entry);
- while (true)
- {
- size = fs.Read(data, 0, size);
- if (size <= 0) break;
- s.Write(data, 0, size);
- }
- fs.Close();
- file.Delete();
- Thread.Sleep(SleepTimer);
- }
- }
- finally
- {
- s.Finish();
- s.Close();
- }
- }
- #endregion
- #region 解密、解压缩文件
- /// <summary>
- /// 解压缩文件
- /// </summary>
- /// <param name="GzipFile">压缩包文件名</param>
- /// <param name="targetPath">解压缩目标路径</param>
- public static void Decompress(string GzipFile, string targetPath)
- {
- //string directoryName = Path.GetDirectoryName(targetPath + "\\") + "\\";
- string directoryName = targetPath;
- if (!Directory.Exists(directoryName)) Directory.CreateDirectory(directoryName);//生成解压目录
- string CurrentDirectory = directoryName;
- byte[] data = new byte[2048];
- int size = 2048;
- ZipEntry theEntry = null;
- using (ZipInputStream s = new ZipInputStream(File.OpenRead(GzipFile)))
- {
- while ((theEntry = s.GetNextEntry()) != null)
- {
- if (theEntry.IsDirectory)
- {// 该结点是目录
- if (!Directory.Exists(CurrentDirectory + theEntry.Name)) Directory.CreateDirectory(CurrentDirectory + theEntry.Name);
- }
- else
- {
- if (theEntry.Name != String.Empty)
- {
- //解压文件到指定的目录
- using (FileStream streamWriter = File.Create(CurrentDirectory + theEntry.Name))
- {
- while (true)
- {
- size = s.Read(data, 0, data.Length);
- if (size <= 0) break;
- streamWriter.Write(data, 0, size);
- }
- streamWriter.Close();
- }
- }
- }
- }
- s.Close();
- }
- }
- #endregion
- }
- }
前台调用:
/// <summary>
/// 压缩
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnCompress_Click(object sender, EventArgs e)
{
String path = Server.MapPath("~/Files/");
// 检查文件是否存在
if (File.Exists(path + "第2章 使用数据库.pdf"))
{
FileInfo fi1 = new FileInfo(path + "第2章 使用数据库.pdf");
FileInfo fi2 = new FileInfo(path + "第3章 使用数据绑定和DataSet.pdf");
FileInfo fi3 = new FileInfo(path + "第4章 SQL Server XML的功能.pdf");
FileInfo fi4 = new FileInfo(path + "第5章 XML编程.pdf");
List<FileInfo> fileList = new List<FileInfo>();
fileList.Add(fi1);
fileList.Add(fi2);
fileList.Add(fi3);
fileList.Add(fi4);
// 调用方法
string targetZipFilePath = Server.MapPath("~/ZipFile/") + "Book.zip";// 扩展名可随意
Lib.FileCompression.Compress(fileList, targetZipFilePath, 5, 5);
Response.Write("文件压缩成功,请在ZipFile文件夹查看。");
}
else
{
Response.Write("被压缩文件不存在,请先解压文件。");
}
}
/// <summary>
/// 解压缩
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnDecompress_Click(object sender, EventArgs e)
{
string zipFilePath = Server.MapPath("~/ZipFile/") + "Book.zip";//
if (File.Exists(zipFilePath))
{
string targetPath = Server.MapPath("~/Files/");
Lib.FileCompression.Decompress(zipFilePath, targetPath);
//// 解压后要删除zip文件
//File.Delete(zipFilePath);
Response.Write("文件解压成功,请在Files文件夹查看。");
}
else
{
Response.Write("压缩文件不存在,请先压缩文件。");
}
}
}