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#将多个文件放入文件夹中,并压缩下载的更多相关文章

  1. 【转】【Android测试技巧】01. root后adb shell默认不是root用户时,如何将文件放入手机系统中

    http://blog.csdn.net/wirelessqa/article/details/8624208 有些机器root后通过adb shell 后,默认不是root用户,需要输入 su才能切 ...

  2. 字体文件放入CDN服务器中,跨域问题(IIS版)

    Font from origin 'http:/XXXX' has been blocked from loading by Cross-Origin Resource Sharing policy: ...

  3. 用MT.exe将exe中的manifest文件提取出来和将manifest文件放入exe中

     前一种方法是将manifest文件放入exe中,但是要记得需要在工程中设置 这样的话exe中就不存在manifest了,在debug目录下就会看到相应的manifest文件.后者是将exe中的man ...

  4. .Net中把图片等文件放入DLL中,并在程序中引用

    原文:.Net中把图片等文件放入DLL中,并在程序中引用 [摘要] 有时我们需要隐藏程序中的一些资源,比如游戏,过关后才能看到图片,那么图片就必须隐藏起来,否则不用玩这个游戏就可以看到你的图片了,呵呵 ...

  5. target存放的是编译后的.class文件地方 默认情况下不会讲非class文件放入进入 如果要使用非.class文件 需要通过增加配置方式自动加入文件

    target存放的是编译后的.class文件地方 默认情况下不会讲非class文件放入进入 如果要使用非.class文件 需要通过增加配置方式自动加入文件

  6. 如何将Linux rm命令删除的文件放入垃圾箱

    因为rm命令删除的文件是不会放入垃圾箱的,所以无法恢复,下面小编就给大家介绍一种方法,通过替换Linux rm命令的方法,从而将rm命令删除的文件放入垃圾箱. 方法: 1. 在/home/userna ...

  7. 通过itunes把文件放入app的document目录

    通过itunes把文件放入app的document目录 反向也是可以的. 仅仅需要添加plist中一项:Application supports iTunes file sharing,value Y ...

  8. 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目录运 ...

  9. C语言:将3*4矩阵中找出行最大,列最小的那个元素。-将低于平均值的人数作为函数返回值,将低于平均分的分数放入below数组中。

    //将3*4矩阵中找出行最大,列最小的那个元素. #include <stdio.h> #define M 3 #define N 4 void fun(int (*a)[N]) { ,j ...

随机推荐

  1. 关于c#中的Timer控件的简单用法

    Timer控件主要会用到2个属性一个是Enabled和IntervalEnabled主要是控制当前Timer控件是否可用timer1.Enabled=false;不可用timer1.Enabled=t ...

  2. AIM Tech Round (Div. 2) D. Array GCD dp

    D. Array GCD 题目连接: http://codeforces.com/contest/624/problem/D Description You are given array ai of ...

  3. 最长公共子序列(LCS问题)

    先简单介绍下什么是最长公共子序列问题,其实问题很直白,假设两个序列X,Y,X的值是ACBDDCB,Y的值是BBDC,那么XY的最长公共子序列就是BDC.这里解决的问题就是需要一种算法可以快速的计算出这 ...

  4. 【JavaScript】 2013年度最强AngularJS资源合集

    http://www.iteye.com/news/28651-AngularJS-Google-resource AngularJS是Google开源的一款JavaScript MVC框架,弥补了H ...

  5. MySQL Cluster 4个数据节点压力测试--mysqlslap工具压400W写

    锅巴哥的个人建议:cluster叫电信运营商版本,所以基本上在很大的用户并发量的情况下才会用到,对连接数的线性增长要求高的场景,千兆就不用想了, 没万兆就不用玩了. 很不幸,我的就是千兆网络,我的数据 ...

  6. [ES6] 12. Shorthand Properties in ES6

    Where destructuring in ES6 allows you to easily get properties out of an object, this shorthand prop ...

  7. [Node.js] Broswerify -- 1

    Browserify is a tool that brings node.js style development to the browser. The thing you can see on ...

  8. Swift用UIBezierPath来画圆角矩形、自定义多路径图形

    最好的特点就是可以自定义路径,设置圆角和描边都很方便,以下为代码和效果,均在playground中实现 1.首先实现一个圆角矩形,并对此路径描边,为其绘制一个轮廓. 1 2 3 4 5 6 7 8 9 ...

  9. h5-3

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. this class is not key value coding-compliant for the key XXX错误的解决方法

    转自:http://www.cnblogs.com/zhangronghua/archive/2012/03/16/iOSError1.html 今天在听iOS开发讲座时,照着讲座的demo输入代码, ...