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. Java是如何管理内存的?

    本文转自CSDN用户Kevin涂腾飞的文章java内存管理机制:http://blog.csdn.net/tutngfei1129287460/article/details/7383480 JAVA ...

  2. redis-在乌班图下设置自动启动

    一.修改redis.conf 1.打开后台运行选项,默认情况下,Redis不在后台运行: daemonize yes 2.配置log文件地址,默认使用标准输入,即打印在命令行终端 的窗口上 logfi ...

  3. The IAR Archive Tool—iarchive

    The IAR Archive Tool—iarchive—creates and manipulates a library (anarchive) of several ELF object fi ...

  4. On-board diagnostics connector SAE J1962

    http://en.wikipedia.org/wiki/On-board_diagnostics#Standard_interfaces OBD-II diagnostic connector Th ...

  5. 怎样基于android4.4.2的源代码和android-4.3.1_r1的驱动编译I9250的ROM

    怎样基于android4.4.2的源代码和android-4.3.1_r1的驱动编译I9250的ROM 作者:雨水  2014-05-04 联系方式:dennis.hu.cd at gmail.com ...

  6. [AngularJS] TweenList 3D + AngularJS Animate

    AngularJS animations and TweenLite make it really easy to create cool 3d effects in your application ...

  7. Dapper链接查询扩展

    一对多映射关系 /// <summary> /// 一对多连接查询 /// </summary> /// <typeparam name="FirstT&quo ...

  8. Golang学习 - bufio 包

    ------------------------------------------------------------ // bufio 包实现了带缓存的 I/O 操作 -------------- ...

  9. [Buzz.Today]2013.07.26

    # OpenFab 3D printing hardware is rapidly scaling up to output continuous mixtures of multiple mater ...

  10. shell 学习笔记

    <Linux命令行与shell脚本编程大全>笔记   wkss 其他:http://www.cnblogs.com/pengdonglin137/p/3528303.html 一.基本命令 ...