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. ASP.NET中DesignMode属性

    参考:http://blog.sina.com.cn/s/blog_4c9da9b50100r4u7.html http://book.51cto.com/art/200902/108836.htm ...

  2. EXTJS AJAX解析XML数据

    public String getAllAreaInfos() { try { List<Areainfo> list = null; if(areaName!=null&& ...

  3. intval()和(int)转换使用与区别

    <?php echo "<br/>数值强制转换:"; $string="2a"; $string1=intval($string); echo ...

  4. NFS错误Starting NFS quotas: Cannot register service: RPC: Unable to receive; errno=Connection refused

    NFS报错一例 [root@bjs0- ~]# /etc/init.d/portreserve start Starting portreserve:                          ...

  5. Codeforces Round #311 (Div. 2) A. Ilya and Diplomas 水题

    A. Ilya and Diplomas Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/557/ ...

  6. GLSL实现HDR Rendering 【转】

    http://blog.csdn.net/a3070173/archive/2008/11/29/3408573.aspx HDR - 全称High dynamic rang,是目前流行的3D特效技术 ...

  7. 《计算机问题求解》总结——2014年CCF计算机课程改革导教班(2014.07.11)

    一:引言     "心想事成".这是自己获得导教班学习机会的最佳概括.2013年年末学习李晓明老师的<人群与网络>课程:随后网络认识烟台大学贺利坚老师,了解到2013年 ...

  8. [原创]SSIS-执行包任务调用子包且子包读取父包变量

    背景:       有时候需要将一个个开发好的独立的ETL包串接起来形成一个独立而庞大的包,如:每家分公司都开发不同的ETL包,最后使用执行包任务来将这些分公司的包给串联起来形成一个独立而完整运行的E ...

  9. ubuntu 上使用apt-get安装oracle-jdk

    Installing default JRE/JDK sudo apt-get updatesudo apt-get install default-jresudo apt-get install d ...

  10. Xcode 8 用处不大的新特性:CLANG_WARN_INFINITE_RECURSION

    来源:酷酷的哀殿 链接:http://www.jianshu.com/p/067f4674f75f Xcode 8 的 clang 新的警告控制,当所有路径都通过一个函数都调用自身时,会产生一个警告 ...