使用c#将多个文件放入文件夹中,并压缩下载
ZipClass.cs 这个是一个压缩文件的类,可直接复制使用,使用到的命名空间是
using System.IO;
using ICSharpCode.SharpZipLib;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
请自行网上查找此压缩程序集下载使用
public class ZipClass
{ #region ZipFileDictory
/// <summary>
/// 递归压缩文件夹方法
/// </summary>
/// <param name="FolderToZip"></param>
/// <param name="s"></param>
/// <param name="ParentFolderName"></param>
private bool ZipFileDictory(string FolderToZip, ZipOutputStream s, string ParentFolderName)
{
bool res = true;
string[] folders, filenames;
ZipEntry entry = null;
FileStream fs = null;
Crc32 crc = new Crc32();
try
{
//创建当前文件夹
entry = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/")); //加上 “/” 才会当成是文件夹创建
s.PutNextEntry(entry);
s.Flush();
//先压缩文件,再递归压缩文件夹
filenames = Directory.GetFiles(FolderToZip);
foreach (string file in filenames)
{
//打开压缩文件
fs = File.OpenRead(file); byte[] buffer = new byte[fs.Length];
fs.Read(buffer, , buffer.Length);
entry = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/" + Path.GetFileName(file)));
entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
s.PutNextEntry(entry);
s.Write(buffer, , buffer.Length);
}
}
catch
{
res = false;
}
finally
{
if (fs != null)
{
fs.Close();
fs = null;
}
if (entry != null)
entry = null;
GC.Collect();
GC.Collect();
}
folders = Directory.GetDirectories(FolderToZip);
foreach (string folder in folders)
{
if (!ZipFileDictory(folder, s, Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip))))
return false;
}
return res;
} #endregion #region ZipFileDictory /// <summary>
/// 压缩目录
/// </summary>
/// <param name="FolderToZip">待压缩的文件夹,全路径格式</param>
/// <param name="ZipedFile">压缩后的文件名,全路径格式</param>
/// <returns></returns>
private string ZipFileDictory(string FolderToZip, string ZipedFile, string Password)
{
bool res;
if (!Directory.Exists(FolderToZip))
return "错误";
ZipOutputStream s = new ZipOutputStream(File.Create(@""+FolderToZip+".zip"));
s.SetLevel();
if (!string.IsNullOrEmpty(Password.Trim()))
s.Password = Password.Trim();
res = ZipFileDictory(FolderToZip, s, "");
s.Finish();
s.Close();
if (res)
{
return ZipedFile;
}
else
{
return null;
} } #endregion #region ZipFile /// <summary>
/// 压缩文件
/// </summary>
/// <param name="FileToZip">要进行压缩的文件名</param>
/// <param name="ZipedFile">压缩后生成的压缩文件名</param>
/// <returns></returns>
private string ZipFile(string FileToZip, string ZipedFile, string Password)
{
//如果文件没有找到,则报错
if (!File.Exists(FileToZip))
throw new System.IO.FileNotFoundException("指定要压缩的文件: " + FileToZip + " 不存在!");
//FileStream fs = null;
FileStream ZipFile = null;
ZipOutputStream ZipStream = null;
ZipEntry ZipEntry = null;
string res;
try
{
ZipFile = File.OpenRead(FileToZip);
byte[] buffer = new byte[ZipFile.Length];
ZipFile.Read(buffer, , buffer.Length);
ZipFile.Close();
ZipFile = File.Create(ZipedFile);
ZipStream = new ZipOutputStream(ZipFile);
if (!string.IsNullOrEmpty(Password.Trim()))
ZipStream.Password = Password.Trim();
ZipEntry = new ZipEntry(Path.GetFileName(FileToZip));
ZipStream.PutNextEntry(ZipEntry);
ZipStream.SetLevel();
ZipStream.Write(buffer, , buffer.Length);
}
catch
{
res = "错误";
}
finally
{
if (ZipEntry != null)
{
ZipEntry = null;
}
if (ZipStream != null)
{
ZipStream.Finish();
ZipStream.Close();
}
if (ZipFile != null)
{
ZipFile.Close();
ZipFile = null;
}
GC.Collect();
GC.Collect();
} return "成功";
} #endregion #region Zip /// <summary>
/// 压缩文件 和 文件夹
/// </summary>
/// <param name="FileToZip">待压缩的文件或文件夹,全路径格式</param>
/// <param name="ZipedFile">压缩后生,全路成的压缩文件名径格式</param>
/// <param name="Password">压缩密码</param>
/// <returns></returns>
public string Zip(string FileToZip, string ZipedFile, string Password)
{
if (Directory.Exists(FileToZip))
{
return ZipFileDictory(FileToZip, ZipedFile, Password);
}
else if (File.Exists(FileToZip))
{
return ZipFile(FileToZip, ZipedFile, Password);
}
else
{
return "失败";
}
} /// <summary>
/// 压缩文件 和 文件夹
/// </summary>
/// <param name="FileToZip">待压缩的文件或文件夹,全路径格式</param>
/// <param name="ZipedFile">压缩后生成的压缩文件名,全路径格式</param>
/// <returns></returns>
public string Zip(string FileToZip, string ZipedFile)
{
return Zip(FileToZip, ZipedFile, "");
} #endregion }
类使用方法:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Firstload(); ResponseFile(); }
}
/// <summary>
/// 输出文件流
/// </summary>
protected void ResponseFile()
{
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("Content-Disposition", "attachment;filename=CurriculumDocument.zip");
string filename = Webpath+".zip";
Response.TransmitFile(filename); //输出文件流之后删除服务器中文件夹
if (Directory.Exists(Webpath))
{
Directory.Delete(Webpath, true); //true为递归删除子文件内容
}
} public string FileName = "";
public string Webpath = "";
/// <summary>
/// 页面第一次加载时开始获取文件压缩
/// </summary>
/// <returns></returns>
private string Firstload()
{
//当前目录下新建文件夹
Webpath = Server.MapPath("./CurriculumFiles/下载文件/" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Second + DateTime.Now.Millisecond); //文件名称
FileName = DateTime.Now.Year.ToString() + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Second + DateTime.Now.Millisecond; //判断文件夹是否存在
if (Directory.Exists(Webpath))
{
//true为递归删除子文件内容
Directory.Delete(Webpath, true);
Directory.CreateDirectory(Webpath);
}
else if (!Directory.Exists(Webpath))
{
Directory.CreateDirectory(Webpath);
} //将要下载的文件copy到新的文件夹中
List<string> DocumentNames_List = GetDocumentNames(ClassID);
for (int i = ; i < DocumentNames_List.Count;i++)
{
//获取服务器中CurriculumFiles目录下的文件及其路径
string DocumentFileName=Server.MapPath("./CurriculumFiles/" + DocumentNames_List[i] + "");
//判断文件是否存在
if (File.Exists(DocumentFileName))
{
File.Copy(DocumentFileName, Webpath + "\\" + "" + DocumentNames_List[i] + "", true);
}
}
//压缩文件
ZipClass zipClass = new ZipClass();
return zipClass.Zip(Webpath, FileName);
}
使用c#将多个文件放入文件夹中,并压缩下载的更多相关文章
- 【转】【Android测试技巧】01. root后adb shell默认不是root用户时,如何将文件放入手机系统中
http://blog.csdn.net/wirelessqa/article/details/8624208 有些机器root后通过adb shell 后,默认不是root用户,需要输入 su才能切 ...
- 字体文件放入CDN服务器中,跨域问题(IIS版)
Font from origin 'http:/XXXX' has been blocked from loading by Cross-Origin Resource Sharing policy: ...
- 用MT.exe将exe中的manifest文件提取出来和将manifest文件放入exe中
前一种方法是将manifest文件放入exe中,但是要记得需要在工程中设置 这样的话exe中就不存在manifest了,在debug目录下就会看到相应的manifest文件.后者是将exe中的man ...
- .Net中把图片等文件放入DLL中,并在程序中引用
原文:.Net中把图片等文件放入DLL中,并在程序中引用 [摘要] 有时我们需要隐藏程序中的一些资源,比如游戏,过关后才能看到图片,那么图片就必须隐藏起来,否则不用玩这个游戏就可以看到你的图片了,呵呵 ...
- target存放的是编译后的.class文件地方 默认情况下不会讲非class文件放入进入 如果要使用非.class文件 需要通过增加配置方式自动加入文件
target存放的是编译后的.class文件地方 默认情况下不会讲非class文件放入进入 如果要使用非.class文件 需要通过增加配置方式自动加入文件
- 如何将Linux rm命令删除的文件放入垃圾箱
因为rm命令删除的文件是不会放入垃圾箱的,所以无法恢复,下面小编就给大家介绍一种方法,通过替换Linux rm命令的方法,从而将rm命令删除的文件放入垃圾箱. 方法: 1. 在/home/userna ...
- 通过itunes把文件放入app的document目录
通过itunes把文件放入app的document目录 反向也是可以的. 仅仅需要添加plist中一项:Application supports iTunes file sharing,value Y ...
- tomcat 对 vue的history默认支持 tomcat 开启步骤 1.build文件放入webapps目录 2.进入conf目录修改server.xml端口号改成8088 3.进入bin目录运行startup.bat 4.浏览器 localhost:8088/workName 访问即可
tomcat 对 vue的history默认支持 tomcat 开启步骤 1.build文件放入webapps目录 2.进入conf目录修改server.xml端口号改成8088 3.进入bin目录运 ...
- C语言:将3*4矩阵中找出行最大,列最小的那个元素。-将低于平均值的人数作为函数返回值,将低于平均分的分数放入below数组中。
//将3*4矩阵中找出行最大,列最小的那个元素. #include <stdio.h> #define M 3 #define N 4 void fun(int (*a)[N]) { ,j ...
随机推荐
- Java异常处理中finally中的return会覆盖catch语句中的return语句
Java异常处理中finally中的return会覆盖catch语句中的return语句和throw语句,所以Java不建议在finally中使用return语句 此外 finally中的throw语 ...
- Windows转到linux中,文件乱码,文件编码转换
最近,学习又重新开始Linux学习,所以一直在Centos中,昨天一朋友把他在Windows下写的C程序发给我,我欣然答应,本以为很快就能在我的Linux系统中运行起来.没想到出现了乱码,结果想把这个 ...
- 数据对象ajax学习篇9
题记:写这篇博客要主是加深自己对数据对象的认识和总结实现算法时的一些验经和训教,如果有错误请指出,万分感谢. 对ajax工作道理: 下面这段是来自一个网友的blog: Ajax的道理简单来说通过Xml ...
- 微信公共服务平台开发(.Net 的实现)13-------网页授权(下 :C#代码的实现 )
接着上次的理论,我们这次来研究用代码实现“网页授权获取用户基本信息”,首先我们需要一个链接指向微信的授权页面,在微信开发平台中已经说了,这个链接必须在微信客户端中打开,那么我们就干脆使用文本消息来完成 ...
- JavaScript Design Patterns: Mediator
The Mediator Design Pattern The Mediator is a behavioral design pattern in which objects, instead of ...
- 商户怎样选择商业wifi进行移动营销
互联网移动营销的大潮席卷而来,带给很多其它的商户营销理念上的升级和更新.商业wifi营销成为很多其它的商户选择.以往,人们在咖啡店不过聊聊天喝喝咖啡,如今,人们能够更悠闲的歇息娱乐享受带有wifi的咖 ...
- ci框架学习中注意的事项
视图: 加载视图:$this->load->view('name'); 一次可以加载多个视图,如: public function index() { $data['page_title' ...
- 关于Android WindowManager显示悬浮窗的动画效果
要实现WindowManager添加的窗口,实现动画显示,就需要添加如下红色的属性,其他的添加View只要设置其Animations属性也会实现动画,当然自己实现也可,但是能直接用系统的已经实现好的, ...
- velocity 随笔
资源网站: http://wiki.apache.org/velocity/ http://velocity.apache.org/engine/releases/velocity-1.7/user- ...
- 【阿里云产品公测】PTS测试 SLB+ECS+RDS组合的DZ论坛负载极限压力,100并发2000页
作者:阿里云用户woaj01 环境介绍: 1.ECS:1核 1G 5M 杭州 2.RDS:240M 5G 杭州内网 3.SLB:私网实例 配置测试环境: 测试脚本: 1.生成参数文件,我的方 ...