压缩多个文件
来源:互联网 发布:自控力 知乎 编辑:程序博客网 时间:2024/05/28 22:12
首先需要ICSharpCode.SharpZipLib.dll
新建FileCompression.cs文件
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(); 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 }}
使用:
protected void butDerive_Click(object sender, EventArgs e) { //先压缩文件 string checkboxNameValues = Request.Form["checkboxImg"]; if (checkboxNameValues != "" && checkboxNameValues != null) { string[] values = checkboxNameValues.Split(','); List<FileInfo> fileList = new List<FileInfo>(); for (int i = 0; i < values.Length; i++) { string path = Server.MapPath(values[i]); FileInfo fi = new FileInfo(path); fileList.Add(fi); } if (!Directory.Exists(Server.MapPath("~/ZipFile/"))) { Directory.CreateDirectory(Server.MapPath("~/ZipFile/")); } //调用方法 string zipFilePath = "ZipFile/" + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".zip"; string zipServerFilepath = Server.MapPath("~/" + zipFilePath); Lib.FileCompression.Compress(fileList, zipServerFilepath, 5, 5); } }
- 压缩多个文件
- 压缩多个CSS文件
- JAVA多个文件压缩
- 多个文件压缩下载
- java 压缩单个文件,多个文件,或者压缩文件夹
- java实现多个文件压缩
- java实现多个文件压缩
- java zip压缩多个文件
- php压缩多个CSS/JS文件
- c# zip压缩多个文件
- java实现多个文件压缩
- 把多个文件打包压缩成ZIP文件
- java实现单个文件多个文件的zip压缩
- 利用SharpZipLib实现压缩多个文件或整个目录
- 使用GruntJS链接与压缩多个JavaScript文件
- 使用SharpZipLib压缩打包多个内存中的文件
- SharpZipLib实现压缩多个文件或整个目录
- 如何压缩多个文件/文件夹(GZipStream and C#)
- java 常用设计模式------单例模式
- windows环境下VS2010编译python
- String 类型与 Date 的转换
- 安装keepalived时遇到错误解决办法记录
- 请尽快报名参加Imagine Cup 微软“创新杯”全球学生大赛
- 压缩多个文件
- 应用python的性能测量工具cProfile
- ARM9_S3C2440学习(七)SDRAM学习总结
- 天水市的介绍
- oracle PATCH下载地址汇总
- Pureftpd问题集之防火墙篇
- Android软件开发之获取通讯录联系人信息
- list_for_each_entry宏
- wmsys.wm_concat函数用法