代码如下:

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. paip.刮刮卡砸金蛋抽奖概率算法跟核心流程.

    paip.刮刮卡砸金蛋抽奖概率算法跟核心流程. #---抽奖算法需要满足的需求如下: 1 #---抽奖核心流程 1 #---问题???更好的算法 2 #---实际使用的扩展抽奖算法(带奖品送完判断和每 ...

  2. iOS开发-NSURLSession详解

    Core Foundation中NSURLConnection在2003年伴随着Safari浏览器的发行,诞生的时间比较久远,iOS升级比较快,AFNetWorking在3.0版本删除了所有基于NSU ...

  3. curl_setopt用此函数设置上传文件请求的兼容性调整

    在用curl_setopt($curl, CURLOPT_POSTFIELDS, $fileData);这个函数设置时会报错如下 curl_setopt(): The usage of the @fi ...

  4. 家中Win7 安装 Maven的步骤及参考文章

    Maven 实战系列之在Windows上安装Maven cy163注:Path中的值: %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32 ...

  5. Java反序列化漏洞执行命令回显实现及Exploit下载

    原文地址:http://www.freebuf.com/tools/88908.html 本文原创作者:rebeyond 文中提及的部分技术.工具可能带有一定攻击性,仅供安全学习和教学用途,禁止非法使 ...

  6. fresco Bitmap too large to be uploaded into a texture

    fresco加载图片方法 布局文件引入 xmlns:fresco="http://schemas.android.com/apk/res-auto" <com.faceboo ...

  7. Windows 2008 利用Filezilla server搭建FTP

    Windows 2008 利用Filezilla server搭建FTP, 安装后总是提示Error Connection To Server Lost , 后来,无意中先安装了IIS,再安装file ...

  8. [转载] A set of top Computer Science blogs

    This started out as a list of top Computer Science blogs, but it more closely resembles a set: the o ...

  9. SqlServer查看各个表所占空间大小的sql

    CREATE TABLE [dbo].#tableinfo( 表名 [varchar](50) COLLATE Chinese_PRC_CI_AS NULL, 记录数 [int] NULL, 预留空间 ...

  10. 1.C#中几个简单的内置Attribute

    阅读目录 一:Obsolete 二:Conditional 一:Obsolete      这个内置属性是说这个方法废弃了不可用,它有两个参数,第一个参数message是说废弃的原因,第二个参数err ...