代码演示用 .NET 4.5 (C# 5.0)自带的压缩类 ZipArchive 创建一个压缩文件
代码如下:
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 创建一个压缩文件的更多相关文章
- JUC 并发编程--10, 阻塞队列之--LinkedBlockingDeque 工作窃取, 代码演示
直接上代码 class LinkedBlockingDequeDemo { // 循环是否结束的开关 private static volatile boolean flag1 = true; pri ...
- C++使用代码创建一个Windows桌面应用程序
WinMain函数 Windows应用程序的唯一程序入口. 函数原型 int WINAPI WinMain { HINSTANCE hInstancem HINSTANCE hPreInstance, ...
- JavaScript: JavaScript的简介和入门代码演示
1.Javascript的发展历史介绍: javascript是指的实在网页上编写的编程语言,其主要是控制器html的动态显示效果.HTMl能带来的只是一些基本的页面的风格,而要展示的漂亮使用CSS, ...
- 14种网页jQuery和css3特效插件代码演示
1.网页table增删样式代码 演示和下载地址 2.jQuery左右滑动幻灯片插件 演示和下载地址 3.jQuery文字轮播焦点图 演示和下载地址 4.网页文字焦点图切换 演示和下载地址 5.jQue ...
- 9种jQuery和css3图片动画特效代码演示
1.自由旋转的jQuery图片 演示和下载地址 2.css3阴影动画效果 演示和下载地址 3.拉窗帘特效图片 演示和下载地址 4.css3文字特效动画 演示和下载地址 5.css3时钟代码 演示和下载 ...
- java 覆盖hashCode()深入探讨 代码演示样例
java 翻盖hashCode()深入探讨 代码演示样例 package org.rui.collection2.hashcode; /** * 覆盖hashcode * 设计HashCode时最重要 ...
- Web---session技术代码演示(request,session,servletContext)
Session会话简介与基本知识点 当浏览器第一次访问服务器时,无论先访问哪一个页面,服务器就会给用户分配一个唯一的会话标识,即jsessionid然后以cookie的形式返回给用户. 会话是指在一段 ...
- javascript 压缩空格代码演示
压缩空格代码演示 主要是讲解 压缩一个字符串两段空格 例如:javascript函数里的空格不论是这样 var s = "Hello World ...
- 单元测试_JUnit常用单元测试注解介绍及代码演示
JUnit常用单元测试注解介绍及代码演示 by:授客 QQ:1033553122 1. 测试环境 1 2. 基础概念 1 3. 常用Annotation 1 4. 运行环境配置 3 maven配置 ...
随机推荐
- java异常处理——题
1.建立exception包,编写TestException.java程序,主方法中有以下代码,确定其中可能出现的异常,进行捕获处理. public class YiChang { public st ...
- Android模拟器配置
// 检查连接设备,为了保证运行正常,只能留一个连接设备 adb devices
- ooj 1066 青蛙过河DP
http://121.249.217.157/JudgeOnline/problem.php?id=1066 1066: 青蛙过河 时间限制: 1 Sec 内存限制: 64 MB提交: 58 解决 ...
- SVN仓库删除最近的提交,还原到某日期以前的版本(svn仓库 删除最近几次更改)
由于某日删除了SVN仓库的大量内容,现在突然想恢复,又要保留LOG的连贯性(恢复出来的已删除文件,会是新增,没有之前的历史Log了),所以才有了这需求. Dump版本库的版本(457以后版本不要) - ...
- Andorid--java0
java code: public class Hello{ public static void main(String argv[]) { System.out.printl ...
- CentOS下httpd下php 连接mysql 本机可以,127.0.0.1不能访问
你看到的这个文章来自于http://www.cnblogs.com/ayanmw php代码很简单: $server="127.0.0.1"; println("Begi ...
- Grunt 新手指南
导言 作为一个正在准备从java 后端转大前端,一直都有想着,在js 的世界里面有没有类似于maven或者gradle 的东西..然后,就找到了grunt 这玩意 Grunt是用来干什么的 诸如ant ...
- Word中怎么设置标题的自动编号
转自: http://jingyan.baidu.com/article/4f7d5712c39c0a1a201927ea.html
- 1、Orchard商城开发——开发需求
需要开发的功能: 1.商品详情,可添加商品属性,如颜色,尺寸等. 2.商品类别,可显示该类别下的所有商品,可按品牌.颜色.尺寸等检索,并可按价格.销量等排序游览. 3.商品游览记录,收藏商品,加入购物 ...
- Windows搭建SVN
1.服务器下载 VisualSVN 地址:http://subversion.apache.org/packages.html 2.然后下载TortoiseSVN客户端,如果要下中文语言包 也在这个页 ...