ASP.NET生成压缩文件(rar打包)
首先引用ICSharpCode.SharpZipLib.dll,没有在这里下载:http://files.cnblogs.com/files/cang12138/ICSharpCode.SharpZipLib.rar
压缩打包代码:
/// <summary>
/// 生成压缩文件
/// </summary>
/// <param name="strZipPath">生成的zip文件的路径</param>
/// <param name="strZipTopDirectoryPath">源文件的上级目录</param>
/// <param name="intZipLevel">T压缩等级</param>
/// <param name="strPassword">压缩包解压密码</param>
/// <param name="filesOrDirectoriesPaths">源文件路径</param>
/// <returns></returns>
private bool Zip(string strZipPath, string strZipTopDirectoryPath, int intZipLevel, string strPassword, string[] filesOrDirectoriesPaths)
{
try
{
List<string> AllFilesPath = new List<string>();
if (filesOrDirectoriesPaths.Length > ) // get all files path
{
for (int i = ; i < filesOrDirectoriesPaths.Length; i++)
{
if (File.Exists(filesOrDirectoriesPaths[i]))
{
AllFilesPath.Add(filesOrDirectoriesPaths[i]);
}
else if (Directory.Exists(filesOrDirectoriesPaths[i]))
{
GetDirectoryFiles(filesOrDirectoriesPaths[i], AllFilesPath);
}
}
} if (AllFilesPath.Count > )
{ ZipOutputStream zipOutputStream = new ZipOutputStream(File.Create(strZipPath));
zipOutputStream.SetLevel(intZipLevel);
zipOutputStream.Password = strPassword; for (int i = ; i < AllFilesPath.Count; i++)
{
string strFile = AllFilesPath[i].ToString();
try
{
if (strFile.Substring(strFile.Length - ) == "") //folder
{
string strFileName = strFile.Replace(strZipTopDirectoryPath, "");
if (strFileName.StartsWith(""))
{
strFileName = strFileName.Substring();
}
ZipEntry entry = new ZipEntry(strFileName);
entry.DateTime = DateTime.Now;
zipOutputStream.PutNextEntry(entry);
}
else //file
{
FileStream fs = File.OpenRead(strFile); byte[] buffer = new byte[fs.Length];
fs.Read(buffer, , buffer.Length); string strFileName = strFile.Replace(strZipTopDirectoryPath, "");
if (strFileName.StartsWith(""))
{
strFileName = strFileName.Substring();
}
ZipEntry entry = new ZipEntry(strFileName);
entry.DateTime = DateTime.Now;
zipOutputStream.PutNextEntry(entry);
zipOutputStream.Write(buffer, , buffer.Length); fs.Close();
fs.Dispose();
}
}
catch
{
continue;
}
} zipOutputStream.Finish();
zipOutputStream.Close(); return true;
}
else
{
return false;
}
}
catch
{
return false;
}
} /// <summary>
/// Gets the directory files.
/// </summary>
/// <param name="strParentDirectoryPath">源文件路径</param>
/// <param name="AllFilesPath">所有文件路径</param>
private void GetDirectoryFiles(string strParentDirectoryPath, List<string> AllFilesPath)
{
string[] files = Directory.GetFiles(strParentDirectoryPath);
for (int i = ; i < files.Length; i++)
{
AllFilesPath.Add(files[i]);
}
string[] directorys = Directory.GetDirectories(strParentDirectoryPath);
for (int i = ; i < directorys.Length; i++)
{
GetDirectoryFiles(directorys[i], AllFilesPath);
}
if (files.Length == && directorys.Length == ) //empty folder
{
AllFilesPath.Add(strParentDirectoryPath);
}
} //调用 string strZipPath = @"D:\ConsultSystem\mgr\user.zip";
string strZipTopDirectoryPath = @"D:\ConsultSystem\mgr\";
int intZipLevel = ;
string strPassword = "";
string[] filesOrDirectoriesPaths = new string[] { @"D:\ConsultSystem\mgr\user.txt" };
Zip(strZipPath, strZipTopDirectoryPath, intZipLevel, strPassword, filesOrDirectoriesPaths);
出自:http://www.cnblogs.com/qiuweiguo/archive/2011/08/09/2132061.html
ASP.NET生成压缩文件(rar打包)的更多相关文章
- Java生成压缩文件(zip、rar 格式)
jar坐标: <dependency> <groupId>org.apache.ant</groupId> <artifactId>ant</ar ...
- java生成压缩文件
在工作过程中,需要将一个文件夹生成压缩文件,然后提供给用户下载.所以自己写了一个压缩文件的工具类.该工具类支持单个文件和文件夹压缩.放代码: import java.io.BufferedOutput ...
- PHP生成压缩文件开发实例
大概需求: 每一个订单都有多个文件附件,在下载的时候希望对当前订单的文件自动打包成一个压缩包下载 细节需求:当前订单号_年月日+时间.zip 例如: 1.生成压缩文件,压缩文件名格式: 2.压缩文件 ...
- asp.net生成PDF文件 (1)
asp.net生成PDF文件 (1) 这个是例子是网上淘来的,哈哈,很有用的! 首先要到网上下载itextsharp.dll,然后添加引用,主程序如下: 1 2 3 4 5 6 7 8 9 10 11 ...
- asp.net 生成xml文件 与 asp生成xml文件
一.asp.net 生成xml文件 webservice方式,调用接口: public XmlDocument List() { XmlDocument doc = new XmlDocument() ...
- css javascript 自动化压缩(保存后即自动生成压缩文件)
先上图:
- 一文教您如何通过 Java 压缩文件,打包一个 tar.gz Filebeat 采集器包
欢迎关注笔者的公众号: 小哈学Java, 专注于推送 Java 领域优质干货文章!! 个人网站: https://www.exception.site/essay/create-tar-gz-by-j ...
- asp.net生成PDF文件参考 .
TextSharp 是用来生成 PDF 的一个组件,在 1998 年夏天的时候,Bruno Lowagie ,iText 的创作者,参与了学校的一个项目,当时使用 HTML 来生成报告,但是,使用 ...
- asp.net生成PDF文件(一)
这个是例子是网上淘来的,哈哈,很有用的! 首先要到网上下载itextsharp.dll,然后添加引用,主程序如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 ...
随机推荐
- oracle timestamp的转换
select to_char(stime,'yyyy-mm-dd HH24:MI:ss.ff3') from e_bmp_log_operation t where t.sdetail like '% ...
- C#程序设计基础——类、对象、方法
类与对象 类 类是一种构造,通过使用该构造,用户可以将其他类型的变量.方法和事件组合在一起,从而创建自定义类型.类就像一个蓝图,它定义类型的数据和行为. 对象 定义类之后,便可通过将类加载到内存中来使 ...
- Java实现Qt的SIGNAL-SLOT机制
SIGNAL-SLOT是Qt的一大特色,使用起来十分方便.在传统的AWT和Swing编程中,我们都是为要在 监听的对象上添加Listener监听器.被监听对象中保存有Listener的列表,当相关事件 ...
- 【HDOJ】2717 Catch That Cow
bfs. /* 2717 */ #include <iostream> #include <cstdio> #include <cstring> #include ...
- 使用ICSharpCode.SharpZipLib.Zip实现压缩与解压缩
使用开源类库ICSharpCode.SharpZipLib.Zip可以实现压缩与解压缩功能,源代码和DLL可以从http://www.icsharpcode.net/OpenSource/SharpZ ...
- 网络流(最大密集度子图,分数规划):UvaLive 3709 Hard Life
John is a Chief Executive Officer at a privately owned medium size company. The owner of the company ...
- Rectangle Area——LeetCode
Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...
- 开源库CImg 数据格式存储之二(RGB 顺序)
在上一篇博客中已经初步说明了GDI和CImg数据的存储格式感谢博友 Imageshop 评论说明 CImg的说明文档中已有详细说明(详见上篇博客说明) CImg的数据格式确实是RRRGGGBBB顺序存 ...
- 什么是mata标签
<meta> 元素可提供有关页面的元信息(meta-information),比如针对搜索引擎和更新频度的描述和关键词. <meta> 标签位于文档的头部,不包含任何内容.&l ...
- media screen 响应式布局(知识点)
一.什么是响应式布局? 响应式布局是Ethan Marcotte在2010年5月份提出的一个概念,简而言之,就是一个网站能够兼容多个终端--而不是为每个终端做一个特定的版本.这个概念是为解决移动互联网 ...