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语言编译出来可以拿编译后的东西去运行,所以就出现了这种需求. ...
随机推荐
- 秦九韶算法 & 三分法
前言 今天考试出了一个题 郭郭模拟退火骗了75分 于是再次把咕咕了好久的模退提上日程 如果进展顺利 明后天应该会开爬山算法和模退的博客笔记 今天先把今天考试的正解学习一下--三分法 引入 老规矩上板子 ...
- Hive操作——删除表(drop、truncate)
Hive删除操作主要分为几大类:删除数据(保留表).删除库表.删除分区. 一.仅删除表中数据,保留表结构 hive> truncate table 表名; truncate操作用于删除指定表中的 ...
- redis(二)redis的主从模式和集群模式
redis(二)redis的主从模式和集群模式 主从模式 集群模式 主从模式 redis的主从模式,指的是针对多台redis实例时候,只存在一台主服务器master,提供读写的功能,同时存在依附在这台 ...
- Python实现迪杰斯特拉算法
首先我采用邻接矩阵法来表示图(有向图无向图皆可) 图的定义如下: class Graph: def __init__(self, arcs=[]): self.vexs = [] self.arcs ...
- C#LeetCode刷题之#88-合并两个有序数组(Merge Sorted Array)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3686 访问. 给定两个有序整数数组 nums1 和 nums2, ...
- ElasticSearch 7.X版本19个常用的查询语句
整理一篇常用的CRUD查询语句,之前这篇文件是在17年左右发表的,从英文翻译过来,现在采用7.x 版本进行实验,弃用的功能或者参数,我这边会进行更新,一起来学习吧. 为了演示不同类型的 Elastic ...
- golang中type关键字使用
type关键字使用 type是go语法里的重要而且常用的关键字,type绝不只是对应于C/C++中的typedef.搞清楚type的使用,就容易理解go语言中的核心概念struct.interface ...
- name 'xrange' is not defined
出现这个错误是因为examples使用的是Python2 在Python3中,移除了在Python2中的range, 并将 xrange 命名为 range 将代码中的xrange改为range就可以 ...
- Maven工程 install和run包配置
1.New一个Environment变量: Name:global.config.path Value:D:\490993\config;
- hiho一下第零周(题目1 : A + B)
#include<iostream> using namespace std; int main() { int a,b; while(cin>>a>>b) cou ...