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. nginx 配置虚拟主机(支持php)

    配置步骤: 1.在nginx安装目录下,找到nginx.conf所在文件夹,新建vhost文件夹 2.在nginx.conf http{} 末端加入 include vhost/*.conf; 3.进 ...

  2. eclipse界面语言的切换方法

    很久以前在我一个朋友的blog上看到过关于eclipse中英文语言界面切换的方法,觉得挺不错,后来自己也用过几次 现在想想,这个方法真的很不错,刚才又在自己机器上做了几次试验,发现eclipse事实上 ...

  3. Android中string.xml文件中设置部分字体颜色大小

    1.在string.xml文件中: <string name="tips_all"><Data><![CDATA[清理进程:<font colo ...

  4. java连接mysql批量写入数据

    1.采用公认的MYSQL最快批量提交办法 public void index() throws UnsupportedEncodingException, Exception { //1000个一提交 ...

  5. vimrc常用配置项

    设置行号 set nu 设置自动缩进 set autoindent 设置tab占n个字符 set tabstop=n 设置以空格代替tab(因为有部分场合不允许使用tab) set expandtab ...

  6. 离线安装Android开发环境的方法

    对于大家从官网上下载下来的SDK其实是一个安装工具,里面啥都没有,如果在线安装的话会需要很长时间.我们同样可以从网络上用下载工具将所需要安装的东西下载下来,(同样有劳大家自己动手找找了)然后直接放入相 ...

  7. java 哈夫曼编码

    //哈夫曼树类 public class HaffmanTree { //最大权值 ; int nodeNum ; //叶子结点个数 public HaffmanTree(int n) { this. ...

  8. python脚本初探---新手如何直接编写一个hello world模块即可执行的.py文件

    废话不多说,就讲一下这个背景吧: 事情是这个样子的~ 本着好学的精神,咱就买了本书,学习python结果呢,发现python的教程都是一个样子滴,上来的第一个hello world 都是通过IDLE来 ...

  9. 关于Android WindowManager显示悬浮窗的动画效果

    要实现WindowManager添加的窗口,实现动画显示,就需要添加如下红色的属性,其他的添加View只要设置其Animations属性也会实现动画,当然自己实现也可,但是能直接用系统的已经实现好的, ...

  10. 关于报错The specified child already has a parent的解决办法

    报错信息为:java.lang.IllegalStateException: The specified child already has a parent. You must call remov ...