序言:

    在我接触Git和SVN之前,我最常用的保存数据的办法就是把文件夹压缩成一个zip文件,添加上时间戳。下面是我在学习C#的文件操作之后做的一个练习,使用开源的ICSharpZipLib来压缩文件夹并保存到指定的目录,还附带简单的日志文件。

 

简介:

    ICSharpZipLib是一个开源的应用于.NET平台的开源类库。运用C#语言和这个类库,我们可以很轻松地创建和操作压缩文件,例如:Zip,GZip, Tar and BZip2

 

源代码:

    废话不多说,咱直接上源代码。说实话,使用ICSharpZipLib的博客应该也有很多,但是试了一下好多不能用。下面的这个类是我根据一个印度程序员(英文网站上的,不知道到底是啥名字,但是长得很三哥!)的博客改写的。测试过的哦,亲!

  1: class CompressFolderIntoZip
  2: {
  3:     /// <summary>
  4:     /// 用来进行文件夹的压缩,输入参数源文件夹路径和目标文件夹路径即可。
  5:     /// </summary>
  6:     /// <param name="sourceFolderPath"></param>
  7:     /// <param name="destFolderPath"></param>
  8:     /// <returns></returns>
  9:     public static bool CompressFolder(string sourceFolderPath, string destFolderPath)
 10:     {
 11:         try
 12:         {
 13:             //文件名=文件夹名+时间戳+.zip;
 14:             string zipFileName = destFolderPath + "\\" +
 15:                 sourceFolderPath.Substring(sourceFolderPath.LastIndexOf('\\') + 1)
 16:                 + "(" + DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss") + ").zip";
 17:             ZipFile zipFile = ZipFile.Create(zipFileName);
 18:             zipFile.BeginUpdate();
 19:             //压缩文件夹时默认采用源文件夹的上一级作为根目录;
 20:             AddFolderToZip(zipFile,
 21:                 sourceFolderPath.Substring(0, sourceFolderPath.LastIndexOf('\\')),
 22:                 sourceFolderPath);
 23:             zipFile.CommitUpdate();
 24:             zipFile.Close();
 25:         }
 26:         catch (Exception exception)
 27:         {
 28:             Console.WriteLine(exception.ToString());
 29:             return false;
 30:         }
 31:         return true;
 32:     }
 33:
 34:     /// <summary>
 35:     /// 该方法用来实现文件夹及其中文件和子文件夹的递归压缩
 36:     /// ZipFile zipFile=ZipFile.Create(string <zipfilename>)
 37:     /// folderPath是需要压缩的文件夹
 38:     /// root是压缩文件中的根目录,如果folderPath=@"C:\Test"而root=@"C:\"那么打开压缩文件会先显示一个名为Test的文件夹。如果folderPath=@"C:\Test"而root=@"C:\Test"那么压缩文件打开会直接显示Test文件夹下所有文件盒子文件夹。
 39:     /// </summary>
 40:     /// <param name="zipFile"></param>
 41:     /// <param name="root"></param>
 42:     /// <param name="folderPath"></param>
 43:     private static void AddFolderToZip(ZipFile zipFile, string root, string folderPath)
 44:     {
 45:         string relativePath = folderPath.Substring(root.Length);
 46:         if (relativePath.Length > 0)
 47:         {
 48:             zipFile.AddDirectory(relativePath);
 49:         }
 50:
 51:         foreach (string file in Directory.GetFiles(folderPath))
 52:         {
 53:             relativePath = file.Substring(root.Length);
 54:             zipFile.Add(file, relativePath);
 55:         }
 56:
 57:         foreach (string subFolder in Directory.GetDirectories(folderPath))
 58:         {
 59:             AddFolderToZip(zipFile, root, subFolder);
 60:         }
 61:     }
 62: }

 

使用ICSharpZipLib将文件夹压缩为zip文件的更多相关文章

  1. Java—将文件夹压缩为zip文件

    import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java ...

  2. 基于Python——实现解压文件夹中的.zip文件

    [背景]当一个文件夹里存好好多.zip文件需要解压时,手动一个个解压再给文件重命名是一件很麻烦的事情,基于此,今天介绍一种使用python实现批量解压文件夹中的压缩文件并给文件重命名的方法—— [代码 ...

  3. Java实现将文件或者文件夹压缩成zip

            最近碰到个需要下载zip压缩包的需求,于是我在网上找了下别人写好的zip工具类.但找了好多篇博客,总是发现有bug.因此就自己来写了个工具类.         这个工具类的功能为: ( ...

  4. 【转】Java实现将文件或者文件夹压缩成zip

    转自:https://www.cnblogs.com/zeng1994/p/7862288.html package com.guo.utils; import java.io.*; import j ...

  5. php将文件夹打包成zip文件

    function addFileToZip($path,$zip){    $handler=opendir($path); //打开当前文件夹由$path指定.    while(($filenam ...

  6. .net 生成html文件后压缩成zip文件并下载

    这里只做一个简单的实例 public ActionResult Index() { string path = Server.MapPath("/test/");//文件输出目录 ...

  7. SharpCompress的压缩文件解压和文件夹压缩

    1.前言 最近做一个功能需要用到对压缩文件的解压,就找到了这个SharpCompress不错,还能解压rar的文件.但是网上的资料和我拿到的SharpCompress.dll的方法有些出入,所以我就自 ...

  8. 【C#公共帮助类】WinRarHelper帮助类,实现文件或文件夹压缩和解压,实战干货

    关于本文档的说明 本文档使用WinRAR方式来进行简单的压缩和解压动作,纯干货,实际项目这种压缩方式用的少一点,一般我会使用第三方的压缩dll来实现,就如同我上一个压缩类博客,压缩的是zip文件htt ...

  9. SharpZipLib 文件/文件夹压缩

    一.ZipFile ZipFile类用于选择文件或文件夹进行压缩生成压缩包. 常用属性: 属性 说明 Count 文件数目(注意是在ComitUpdat之后才有) Password 压缩包密码 Siz ...

随机推荐

  1. Yet Another Scheme Introduction学习

    Chapter 2 Function exact->inexact is to convert from fractional numbers to floating point numbers ...

  2. java.lang.IllegalStateException: Required view 'text1' with ID 2131492943 for field 'mText' was not found. If this view is optional add '@Nullable' annotation

    使用ButterKnife 8.2的时候遇到这个问题 很明显空指针问题 按照提示添加 import android.support.annotation.Nullable;@Nullable  造成原 ...

  3. POJ 2249

    #include<stdio.h> longlong sum; int main() { int k,n,m; while(~scanf("%d%d",&n,& ...

  4. 安装Python及工具

    在Windows上安装Python 第一步:下载安装包 根据Windows版本(64或32)从Python官方网站下载对应的Python版本,此次使用python V3.5. 下载路径:https:/ ...

  5. 为EF DbContext生成的实体添加注释(T5模板应用)[转]

    1 先加上类注释 找到这行代码WriteHeader(codeStringGenerator, fileManager): 在它下面加上我们的代码: string summary=string.Emp ...

  6. NOIP2001 数的划分

    题二 数的划分(20分) 问题描述 将整数n分成k份,且每份不能为空,任意两份不能相同(不考虑顺序). 例如:n=7,k=3,下面三种分法被认为是相同的. 1,1,5; 1,5,1; 5,1,1; 问 ...

  7. 新花生壳内网版2.3 + Tomcat7 搭建自己的网站(2015.01.21)

    网上很多资料,问题主要是出在 tomcat 的访问上而已: 如下总结一下: 首先在 花生壳 官网(http://hsk.oray.com/)注册一个帐号,每个帐号可以领取一个免费域名 然后下载安装新版 ...

  8. <转>Windows 各种计时函数总结

    本文转自MoreWindows 特此标识感谢 http://blog.csdn.net/morewindows/article/details/6854764 本文对Windows平台下常用的计时函数 ...

  9. web api中post参数时只能一个

    在WebAPI中,请求主体(HttpContent)只能被读取一次,不被缓存,只能向前读取的流. 举例子说明: /?id=123&name=bob void Action(int id, st ...

  10. poj 3469 Dual Core CPU【求最小割容量】

    Dual Core CPU Time Limit: 15000MS   Memory Limit: 131072K Total Submissions: 21453   Accepted: 9297 ...