代码如下:

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. A new start

    学习前端已经有一年多,也自己写过不少博客.笔记,但是比较不开心的是有时候自己写的博客自己都看不懂,所以最后决定重新开一个博客,目标是写能让别人看懂也能让自己看懂的东西,那才算真成功真的懂了.最后希望的 ...

  2. javaweb回顾第三篇数据库访问

    前言:本篇主要针对数据库的操作,在这里不适用hibernate或者mybatis,用最原始的JDBC进行讲解,通过了解这些原理以后更容易理解和学习hibernate或mybatis. 1:jdbc的简 ...

  3. windows xp/7命令提示符强制结束指定进程

    开始----“运行 ”输入cmd ,然后在命令提示符下输入tasklist,出现如下列表: Image Name                     PID Session Name        ...

  4. linux平台下防火墙iptables原理(转)

    原文地址:http://www.cnblogs.com/ggjucheng/archive/2012/08/19/2646466.html iptables简介 netfilter/iptables( ...

  5. PoolMon 使用

    PoolMon 显示   PoolMon 在命令窗口中显示有关池内存分配的数据列.使用箭头键.PAGE UP 和 PAGE DOWN 键在数据间滚动. 注意   若要查看全部 PoolMon 显示,则 ...

  6. 查看Oracle数据库的用户名和密码

    运行 cmd 按如下输入命令 sqlplus / as sysdba ---------以sys登陆          超级用户(sysdba) alter user 用户名 account unlo ...

  7. Transaction recovery: lock conflict caught and ignored

    Transaction recovery: lock conflict caught and ignored环境:RAC 4节点.oracle 11.2.0.4.redhat 5.9 64bit 问题 ...

  8. 在Oracle Linux Server release 6.4下配置ocfs2文件系统

    ① 安装ocfs-tools-1.8 如果是使用RedHat Enterprise Linux 6.4,也可以安装ocfs-tools-1.8的,只是要插入Oracle Linux Server re ...

  9. Python Base HTTP Server

    import BaseHTTPServer import cgi, random, sys MESSAGES = [ "That's as maybe, it's still a frog. ...

  10. Cubieboard2裸机开发之(二)板载LED交替闪烁

    前言 电路原理在文章http://www.cnblogs.com/lknlfy/p/3583806.html中已经说明,两个LED的原理图是一样的.要使两个LED交替闪烁,只需要在点亮蓝色LED,熄灭 ...