代码如下:

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace SolutionZip
{
class SolutionZip
{
static void Main(string[] args)
{
string rootFolder = ".\\";
string archiveName = "Archive.zip"; if (args.Length > )
{
rootFolder = args[];
}
if (args.Length > )
{
archiveName = args[];
}
List<string> exceptions = new List<string>();
exceptions.Add(@".user");
exceptions.Add(@".suo");
exceptions.Add(@"\bin");
exceptions.Add(@"\obj");
exceptions.Add(@"\packages"); int filesAdded = CreateArchive(rootFolder,
exceptions, archiveName);
Console.WriteLine(String.Format(" {0} file(s) added ",
filesAdded));
Console.ReadLine();
} public static int CreateArchive(string folder,
IList<string> exceptions, string archiveName)
{
int filesCount = ;
string folderFullPath = Path.GetFullPath(folder);
string archivePath = Path.Combine(folderFullPath, archiveName);
if (File.Exists(archivePath))
{
Console.WriteLine(
string.Format(@"File '{0}' already exists. Overwrite (y/n): ",
archiveName));
string read = Console.ReadLine();
if (read.ToLower() == "y")
{
File.Delete(archivePath);
}
else
{
Console.WriteLine(string.Format(@"Archive {0} already exists.
Aborting!", archivePath));
return ;
}
}
IEnumerable<string> files = Directory.EnumerateFiles(folder,
"*.*", SearchOption.AllDirectories);
using (ZipArchive archive = ZipFile.Open(archivePath, ZipArchiveMode.Create))
{
foreach (string file in files)
{
if (!Excluded(file, exceptions))
{
try
{
var addFile = Path.GetFullPath(file);
if (addFile != archivePath)
{
addFile = addFile.Substring(folderFullPath.Length);
Console.WriteLine("Adding " + addFile);
archive.CreateEntryFromFile(file, addFile);
filesCount++;
}
}
catch (IOException ex)
{
Console.WriteLine(@"Failed to add {0} due to error :
{1} \n Ignoring it!", file, ex.Message);
}
}
}
}
return filesCount;
} private static bool Excluded(string file, IList<string> exceptions)
{
List<String> folderNames = (from folder in exceptions
where folder.StartsWith(@"\")
|| folder.StartsWith(@"/")
select folder).ToList<string>();
if (!exceptions.Contains(Path.GetExtension(file)))
{
foreach (string folderException in folderNames)
{
if(Path.GetDirectoryName(file).Contains(folderException))
{
return true;
}
}
return false;
}
return true;
}
}
}

图文解释地址:http://www.dotnetcurry.com/showarticle.aspx?ID=974

谢谢浏览!

代码演示用 .NET 4.5 (C# 5.0)自带的压缩类 ZipArchive 创建一个压缩文件的更多相关文章

  1. JUC 并发编程--10, 阻塞队列之--LinkedBlockingDeque 工作窃取, 代码演示

    直接上代码 class LinkedBlockingDequeDemo { // 循环是否结束的开关 private static volatile boolean flag1 = true; pri ...

  2. C++使用代码创建一个Windows桌面应用程序

    WinMain函数 Windows应用程序的唯一程序入口. 函数原型 int WINAPI WinMain { HINSTANCE hInstancem HINSTANCE hPreInstance, ...

  3. JavaScript: JavaScript的简介和入门代码演示

    1.Javascript的发展历史介绍: javascript是指的实在网页上编写的编程语言,其主要是控制器html的动态显示效果.HTMl能带来的只是一些基本的页面的风格,而要展示的漂亮使用CSS, ...

  4. 14种网页jQuery和css3特效插件代码演示

    1.网页table增删样式代码 演示和下载地址 2.jQuery左右滑动幻灯片插件 演示和下载地址 3.jQuery文字轮播焦点图 演示和下载地址 4.网页文字焦点图切换 演示和下载地址 5.jQue ...

  5. 9种jQuery和css3图片动画特效代码演示

    1.自由旋转的jQuery图片 演示和下载地址 2.css3阴影动画效果 演示和下载地址 3.拉窗帘特效图片 演示和下载地址 4.css3文字特效动画 演示和下载地址 5.css3时钟代码 演示和下载 ...

  6. java 覆盖hashCode()深入探讨 代码演示样例

    java 翻盖hashCode()深入探讨 代码演示样例 package org.rui.collection2.hashcode; /** * 覆盖hashcode * 设计HashCode时最重要 ...

  7. Web---session技术代码演示(request,session,servletContext)

    Session会话简介与基本知识点 当浏览器第一次访问服务器时,无论先访问哪一个页面,服务器就会给用户分配一个唯一的会话标识,即jsessionid然后以cookie的形式返回给用户. 会话是指在一段 ...

  8. javascript 压缩空格代码演示

          压缩空格代码演示 主要是讲解 压缩一个字符串两段空格          例如:javascript函数里的空格不论是这样     var s = "Hello World     ...

  9. 单元测试_JUnit常用单元测试注解介绍及代码演示

    JUnit常用单元测试注解介绍及代码演示   by:授客 QQ:1033553122 1. 测试环境 1 2. 基础概念 1 3. 常用Annotation 1 4. 运行环境配置 3 maven配置 ...

随机推荐

  1. 将外卖O2O广告一棍子打成竞价排名,秤把平了吗?

    近日,诸多媒体报道称美团外卖.饿了么等外卖O2O将竞价排名引入外卖平台当中进行广告运营一事闹得沸沸扬扬.那么,美团外卖.饿了么真的都是竞价排名吗? 其实,美团外卖的付费推广仅仅只是针对列表的固定位置, ...

  2. Python之Django--ORM连表操作

    一对多 class UserType(models.Model): caption = models.CharField(max_length=32) class UserInfo(models.Mo ...

  3. Django模板系统

    创建模板对象Template类在django.template模板中 // 用django-admin.py startproject 命令创建一个项目目录django-admin.py startp ...

  4. js高仿QQ消息列表左滑功能

    该组件,主要功能类似于QQ消息列表左滑出现删除.标为已读等按钮的功能:现在的版本用的是纯javaScript编写:后续会跟进 angularJs 开发的类似组件以及jquery的; 下面,就让我们来认 ...

  5. Advanced Puppet 系列的前言

    什么是Advanced 在网络上,你能找到大量关于Puppet的安装,配置以及基础用法的文章和博客.你在通过一段时间的实战后,熟练掌握了Puppet基础用法,随着你管理的集群日益扩大,你的部署逻辑越来 ...

  6. showmessage函数里

    首先说一下,漏洞是t00ls核心群传出去的,xhming先去读的,然后我后来读的,读出来的都是代码执行,1月5日夜里11点多钟,在核心群的黑客们的要求下,xhming给了个poc,我给了个exp,确实 ...

  7. TStringList中AddObject使用

    结构体定义 PYpType=^TYpType; TYpType=record    yfcode:string;    ypcode:string;    YpUnitPrice:Currency;  ...

  8. 突然顿悟的Javascript中的this

    一直对Javascript中的this都有一种似是而非的感觉,今天突然感觉豁然开朗,特此记录一下. 咱们先看个栗子: <!DOCTYPE html> <html> <he ...

  9. mac下忘记mysql密码, 重新设置mysql密码

    step1: 苹果->系统偏好设置->最下边点mysql 在弹出页面中 关闭mysql服务(点击stop mysql server)step2:进入终端输入:cd /usr/local/m ...

  10. 那些一目了然的3D地质模型 【转】

    http://www.360doc.com/content/16/0830/09/14719766_586950902.shtml