C# 压缩源文件(导出源文件word文件)
说明 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
C# 压缩源文件(导出源文件word文件)的更多相关文章
- PHP获取网址详情页的内容导出到WORD文件
亲自测试效果一般, css的样式文件获取不到 如果没有特殊的样式 或者是内容里面包括样式的 直接输出有样式的内容 然后导出 这样还是可以的 class word { function start ...
- asp.net教程:GridView导出到Excel或Word文件
asp.net教程:GridView导出到Excel或Word文件</ br> 在项目中我们经常会遇到要求将一些数据导出成Excel或者Word表格的情况,比如中国移动(我是中国移动用户) ...
- freemarker根据模板生成word文件实现导出功能
一.准备工作 1.创建一个03的word文档,动态的数据用占位符标志占位(如testname).然后另存为word2003的xml文件. 2.格式化xml文件,占位符的位置用${testname}代替 ...
- PowerDesigner逆向操作(从mysql5.0生成数据库的物理模型),把Comment写到name中,pdm文件导出为word
PowerDesigner逆向操作(从mysql5.0生成数据库的物理模型) 环境:powderdesigner12.5:mysql5.0步骤:1. 为指定的数据库配置mysql的ODBC数据源先下载 ...
- 报表开发导出各种格式文件的API
文件输出的多样性,准确性和稳定性对于我们常用的报表软件来说很重要.报表的输入是指从报表的模板文件(XML格式的)创建WorkBook对象,输出则指将报表保存为各种格式文件,比如Pdf.Excel.Wo ...
- Weblogic读不到Word文件
之前遇到一导出word文件的需求,我的做法是把对应导出内容放到一个word文件中,把其中变化的内容作为变量,然后把该word文件放在WEB-INF目录下用来作为模板.在导出时通过ServletCont ...
- POI生成Web版Word文件
POI生成Web版Word文件 1 通过URL的输入流实现 2 直接把Html文本写入到Word文件 所谓的使用POI生成Web版Word文件是指利用POI将Html代码插入到 ...
- idea src下源文件和class编译文件不一致
今天遇到一个神奇BUG,一个和elasticsearch没有任何关系的项目,报错ES某个包找不到,刚开始以为是依赖了父项目的某个包,并且本项目主启动类ComponentScan扫描了相关的类进入Spr ...
- 利用模板导出文件(二)之jacob利用word模板导出word文件(Java2word)
https://blog.csdn.net/Fishroad/article/details/47951061?locationNum=2&fps=1 先下载jacob.jar包.解压后将ja ...
- Mac上把python源文件编译成so文件
把python源文件编译成so文件 前言 实际上属于一种代码混淆/加密的技术,大家知道python的源文件放在那里,大家是都可以看的,不像C语言编译出来可以拿编译后的东西去运行,所以就出现了这种需求. ...
随机推荐
- Python监控你的女朋友/男朋友每天都在看哪些网站
需求: 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却不知道如何去学习更加高深的知识.那么针对这三类人,我给大家提 ...
- Python预测2020高考分数和录取情况
“迟到”了一个月的高考终于要来了. 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却不知道如何去学习更加高深的知识. ...
- 6、Java 运算符
Java运算符按功能可分为:算数运算符.关系运算符.逻辑运算符.位运算符.赋值运算符和条件运算符. 1.算数运算符 算术运算符包括通常的加(+).减(-).乘(*).除(/).取模(%),完成整数型和 ...
- C#设计模式之14-命令模式
命令模式(Command Pattern) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/413 访问. 命令模式属于行 ...
- Python 3.x pip安装报错ERROR: No matching distribution found for PIL
安装完成即可解决无法引入PIL的问题.
- 关于 JavaScript 字符串的一个小知识
说起字符串,我们再熟悉不过了.接触编程的第一个经典任务就是输出字符串:Hello, world.但是你知道 JavaScript 字符串在计算机里是怎么表示的吗? 最简单直观但不太准确的的理解就是,字 ...
- mysql-5.7.xx在lcentos7下的安装以及mysql在windows以及linux上的性能差异
前言: 在centos上安装mysql,整整折腾了将近一天,因为是第一次安装,的确是踩了不少坑,这里详细记录下来,方便各位有同样需求的小伙伴参考. 该选择什么版本? mysql5.7有很多小版本,但是 ...
- Istio Routing 实践掌握virtualservice/gateway/destinationrule/AB版本发布/金丝雀发布
原文 在学习像 Istio 这样的新技术时,看一下示例应用程序总是一个好主意. Istio repo 有一些示例应用程序,但它们似乎有各种不足. 文档中的 BookInfo 是一个很好的示例. 但是, ...
- 聊一聊mycat数据库集群系列之双主双重实现
最近在梳理数据库集群的相关操作,现在花点时间整理一下关于mysql数据库集群的操作总结,恰好你又在看这一块,供一份参考.本次系列终结大概包括以下内容:多数据库安装.mycat部署安装.数据库之读写分离 ...
- 关于Spark RDD 的认识
一.基本认识 RDD 是Spark大数据计算引擎中,抽象的一种数据结构. RDD(Resilient Distributed Dataset),中文意思是弹性分布式数据集,它是Spark中的基本抽象. ...