说明 1 : 在webUI 公共类,存放 ZipHelper 类

说明  2 :判断文件路径是否存在,不存在则创建文件夹

说明 3  : 引用类方法,判断压缩文件,返回的,是true/false

引用常用公共压缩方法类
public class CLeopardZip
{
/// <summary>
/// 适用与ZIP压缩
/// </summary>
public class ZipHelper
{
#region 压缩 /// <summary>
/// 递归压缩文件夹的内部方法
/// </summary>
/// <param name="folderToZip">要压缩的文件夹路径</param>
/// <param name="zipStream">压缩输出流</param>
/// <param name="parentFolderName">此文件夹的上级文件夹</param>
/// <returns></returns>
private static bool ZipDirectory(string folderToZip, ZipOutputStream zipStream, string parentFolderName)
{
bool result = true;
string[] folders, files;
ZipEntry ent = null;
FileStream fs = null;
Crc32 crc = new Crc32(); try
{
ent = new ZipEntry(Path.Combine(parentFolderName, Path.GetFileName(folderToZip) + "/"));
zipStream.PutNextEntry(ent);
zipStream.Flush();
//从复制后的路径,获取的当前文件夹的,所有源文件
files = Directory.GetFiles(folderToZip);
foreach (string file in files)
{
fs = System.IO.File.OpenRead(file);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, , buffer.Length);
ent = new ZipEntry(Path.Combine(parentFolderName, Path.GetFileName(folderToZip) + "//" + Path.GetFileName(file)));
ent.DateTime = DateTime.Now;
ent.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
ent.Crc = crc.Value;
zipStream.PutNextEntry(ent);
zipStream.Write(buffer, , buffer.Length); }
}
catch
{
result = false;
}
finally
{
if (fs != null)
{
fs.Close();
fs.Dispose();
}
if (ent != null)
{
ent = null;
}
GC.Collect();
GC.Collect();
} folders = Directory.GetDirectories(folderToZip);
//此,不需要遍历其他的文件
//遍历doc中的所有文件,
//foreach (string folder in folders)
// if (!ZipDirectory(folder, zipStream, folderToZip,HtmlFiles))
// return false; return result;
} /// <summary>
/// 压缩文件夹
/// </summary>
/// <param name="folderToZip">要压缩的文件夹路径</param>
/// <param name="zipedFile">压缩文件完整路径</param>
/// <param name="password">密码</param>
/// <returns>是否压缩成功</returns>
public static bool ZipDirectory(string folderToZip, string zipedFile, string password,string HtmlFiles)
{
bool result = false;
if (!Directory.Exists(folderToZip))
return result; ZipOutputStream zipStream = new ZipOutputStream(System.IO.File.Create(zipedFile));
zipStream.SetLevel();
if (!string.IsNullOrEmpty(password))
zipStream.Password = password; result = ZipDirectory(folderToZip, zipStream, ""); zipStream.Finish();
zipStream.Close(); return result;
} /// <summary>
/// 压缩文件夹
/// </summary>
/// <param name="folderToZip">要压缩的文件夹路径</param>
/// <param name="zipedFile">压缩文件完整路径</param>
/// <returns>是否压缩成功</returns>
public static bool ZipDirectory(string folderToZip, string zipedFile,string HtmlFiles)
{
bool result = ZipDirectory(folderToZip, zipedFile, null,HtmlFiles);
return result;
} /// <summary>
/// 压缩文件
/// </summary>
/// <param name="fileToZip">要压缩的文件全名</param>
/// <param name="zipedFile">压缩后的文件名</param>
/// <param name="password">密码</param>
/// <returns>压缩结果</returns>
public static bool ZipFile(string fileToZip, string zipedFile, string password)
{
bool result = true;
ZipOutputStream zipStream = null;
FileStream fs = null;
ZipEntry ent = null; if (!System.IO.File.Exists(fileToZip))
return false; try
{
fs = System.IO.File.OpenRead(fileToZip);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, , buffer.Length);
fs.Close(); fs = System.IO.File.Create(zipedFile);
zipStream = new ZipOutputStream(fs);
if (!string.IsNullOrEmpty(password)) zipStream.Password = password;
ent = new ZipEntry(Path.GetFileName(fileToZip));
zipStream.PutNextEntry(ent);
zipStream.SetLevel(); zipStream.Write(buffer, , buffer.Length); }
catch
{
result = false;
}
finally
{
if (zipStream != null)
{
zipStream.Finish();
zipStream.Close();
}
if (ent != null)
{
ent = null;
}
if (fs != null)
{
fs.Close();
fs.Dispose();
}
}
GC.Collect();
GC.Collect(); return result;
} /// <summary>
/// 压缩文件
/// </summary>
/// <param name="fileToZip">要压缩的文件全名</param>
/// <param name="zipedFile">压缩后的文件名</param>
/// <returns>压缩结果</returns>
public static bool ZipFile(string fileToZip, string zipedFile)
{
bool result = ZipFile(fileToZip, zipedFile, null);
return result;
} /// <summary>
/// 压缩文件或文件夹
/// </summary>
/// <param name="fileToZip">要压缩的路径</param>
/// <param name="zipedFile">压缩后的文件名</param>
/// <param name="password">密码</param>
/// <param name="HtmlFiles">比对文件</param>
/// <returns>压缩结果</returns>
public static bool Zip(string fileToZip, string zipedFile, string password)
{
bool result = false;
if (Directory.Exists(fileToZip))
result = ZipDirectory(fileToZip, zipedFile, password);
else if (System.IO.File.Exists(fileToZip))
result = ZipFile(fileToZip, zipedFile, password); return result;
} /// <summary>
/// 压缩文件或文件夹
/// </summary>
/// <param name="fileToZip">要压缩的路径</param>
/// <param name="zipedFile">压缩后的文件名</param>
/// <returns>压缩结果</returns>
public static bool Zip(string fileToZip, string zipedFile)
{
bool result = Zip(fileToZip, zipedFile, null);
return result; } #endregion }
}

说明 2 判断文件路径是否存在,不存在则创建文件夹
1 

 if (!System.IO.Directory.Exists(newPath))
{
System.IO.Directory.CreateDirectory(newPath);
}
说明 3  : 引用类方法,判断返回的,是true/false

 1 #region FilesZIP
public string FilesZIP(List<Files> _list)
{
string password = "";
var Path = HttpContext.Current.Server.MapPath("~/doc/");
string newPath = Path + Guid.NewGuid().ToString();
if (!System.IO.Directory.Exists(newPath))
{
System.IO.Directory.CreateDirectory(newPath);
}
foreach (Exx.QMS.Model.File.FileModel item in _list)
{
var _files = Directory.GetFiles(Path);
if (_files.Contains(Path + item.FileContent))
{
var ext = item.FileContent.Split('.')[];
//第一个参数:要复制的路径 ; 第二个参数 给复制的路径文件,重新命名 eg:源文件名称:DHFDSJF.docx 命名后的:统计说明.docx
System.IO.File.Copy(Path + item.FileContent, newPath + "/" + item.FileName+"."+ ext, true);
}
}
string fileToZip = newPath;
var zipedFile = newPath + ".zip";
var fileZip = Common.CLeopardZip.ZipHelper.Zip(fileToZip, zipedFile, password);
var IsHtml = fileZip == true ? zipedFile : "Error";
return IsHtml;
}
#endregion

最新文章

  1. vert.x学习(六),动态模板与静态文件的结合
  2. C# 解析 Json
  3. REST API出错响应的设计
  4. [Android]Activity启动过程
  5. 全息眼镜HoloLens可快速捕捉真人3D图像
  6. Memory Allocation in the MySQL Server
  7. PLSQL_解析过程及硬解析和软解析的区别(案例)
  8. Minimum_Window_Substring两种方法求解
  9. Commons-logging + Log4j
  10. Burosuite抓包Sqlmap学习Sql注入
  11. 查看library_cache 库缓冲区的命中率
  12. Maven实战——生命周期和插件
  13. 简单的mvc之三:灵活的路由(上)
  14. 高质量PHP代码的50个实用技巧必备(上)
  15. elasticsearch安装ik分词器
  16. es6新增
  17. Python字符串 --Python3
  18. SQL行转列(PIVOT)与列转行(UNPIVOT)简明方法
  19. 视图模型-Lambda表达式
  20. psql常用命令

热门文章

  1. SEO(Search Engine Optimization)优化
  2. 朴素贝叶斯分类器基本代码 &amp;&amp; n折交叉优化 2
  3. 15、Java中级进阶 面向对象 继承
  4. C++游戏(大型PC端枪战游戏)服务器架构
  5. C#LeetCode刷题之#39-组合总和(Combination Sum)
  6. C#LeetCode刷题之#788-旋转数字(Rotated Digits)
  7. C#算法设计查找篇之05-二叉树查找
  8. JavaScript promise基础示例
  9. SpringBoot ---yml 整合 Druid(1.1.23) 数据源
  10. 获取到jqgrid发送的请求得到的数据