/**//*

* Gary Zhang -- cbcye@live.com

* www.cbcye.com

* www.quicklearn.cn

* cbcye.cnblogs.com

*/

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using ICSharpCode.SharpZipLib.Zip;

using System.Diagnostics;

using ICSharpCode.SharpZipLib.Core;

namespace TestConsole

{

class Program

{

static void Main()

{

//CreateZipFile(@"d:\", @"d:\a.zip");

UnZipFile(@"d:\a.zip");

Console.Read();

}

private static void CreateZipFile(string filesPath, string zipFilePath)

{

if (!Directory.Exists(filesPath))

{

Console.WriteLine("Cannot find directory '{0}'", filesPath);

return;

}

try

{

string[] filenames = Directory.GetFiles(filesPath);

using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFilePath)))

{

s.SetLevel(9); // 压缩级别 0-9

//s.Password = "123"; //Zip压缩文件密码

byte[] buffer = new byte[4096]; //缓冲区大小

foreach (string file in filenames)

{

ZipEntry entry = new ZipEntry(Path.GetFileName(file));

entry.DateTime = DateTime.Now;

s.PutNextEntry(entry);

using (FileStream fs = File.OpenRead(file))

{

int sourceBytes;

do

{

sourceBytes = fs.Read(buffer, 0, buffer.Length);

s.Write(buffer, 0, sourceBytes);

} while (sourceBytes > 0);

}

}

s.Finish();

s.Close();

}

}

catch (Exception ex)

{

Console.WriteLine("Exception during processing {0}", ex);

}

}

private static void UnZipFile( string zipFilePath)

{

if (!File.Exists(zipFilePath))

{

Console.WriteLine("Cannot find file '{0}'", zipFilePath);

return;

}

using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))

{

ZipEntry theEntry;

while ((theEntry = s.GetNextEntry()) != null)

{

Console.WriteLine(theEntry.Name);

string directoryName = Path.GetDirectoryName(theEntry.Name);

string fileName = Path.GetFileName(theEntry.Name);

// create directory

if (directoryName.Length > 0)

{

Directory.CreateDirectory(directoryName);

}

if (fileName != String.Empty)

{

using (FileStream streamWriter = File.Create(theEntry.Name))

{

int size = 2048;

byte[] data = new byte[2048];

while (true)

{

size = s.Read(data, 0, data.Length);

if (size > 0)

{

streamWriter.Write(data, 0, size);

}

else

{

break;

}

}

}

}

}

}

}

}

}

C# zip压缩的更多相关文章

  1. Android总结之Gzip/Zip压缩

    前言: 做过Android网络开发的都知道,在网络传输中我们一般都会开启GZIP压缩,但是出于刨根问底的天性仅仅知道如何开启就不能满足俺的好奇心的,所以想着写个demo测试一下比较常用的两个数据压缩方 ...

  2. C# 对多个文件进行zip压缩

    本文使用的ICSharpCode.SharpZipLib.dll类库来实现文件压缩,你可以通过Nuget来安装此类库,或者到搜索引擎去搜索一下遍地都是.类库下载下来之后,添加到项目引用就可以了.下面这 ...

  3. 【VC++技术杂谈008】使用zlib解压zip压缩文件

    最近因为项目的需要,要对zip压缩文件进行批量解压.在网上查阅了相关的资料后,最终使用zlib开源库实现了该功能.本文将对zlib开源库进行简单介绍,并给出一个使用zlib开源库对zip压缩文件进行解 ...

  4. zip压缩命令的使用

    file命令可以查看文件的类型 tar类型 .tar gzip类型   .gz  bzip2类型  .bz2 zip类型    .zip 如果一个压缩文件由tar命令解压的前提,2个条件 1.这个文件 ...

  5. Zip压缩和解压缩

    这个功能完全依靠一个第三方的类,ICSharpCode.SharpZipLib.dll,只是在网上搜了大半天,都没有关于这个类的详细解释,搜索的demo也是各种错误,感觉作者完全没有跑过,就那么贸贸然 ...

  6. zip压缩与解压缩示例

    范例: zip命令可以用来将文件压缩成为常用的zip格式.unzip命令则用来解压缩zip文件. 1. 我想把一个文件abc.txt和一个目录dir1压缩成为yasuo.zip: # zip -r y ...

  7. RAR和ZIP:压缩大战真相

    转:http://fqd2eh4y.blog.163.com/blog/static/69195855200801035015857 前言--王者归来? 等待足足两年之久,压缩霸主WinZip终于在万 ...

  8. zip压缩

    package com.green.project.compress; import java.io.File;import java.io.FileInputStream;import java.i ...

  9. Java Zip压缩实现

    最近在自学javaWeb,先复习一下java,把还给老师的东西再找回来(知识如果不用很快就会忘记啊).. 今天看到了zip压缩,决定要整理一下. java将有关zip压缩的内容都封装在java.uti ...

  10. java ZIP压缩文件

    问题描述:     使用java ZIP压缩文件和目录 问题解决:     (1)单个文件压缩 注:     以上是实现单个文件写入压缩包的代码,注意其中主要是在ZipOutStream流对象中创建Z ...

随机推荐

  1. 禁止button响应回车(.net页面)

    1. 深层次来说这不是 ASP.NET 的问题, 而是 html form 的 submit 按钮就是如何设计的. 当你的光标焦点进入某个表单元素的时候,会激活该表单中第一个(流布局顺从左到右,从上至 ...

  2. linux查看文件个数命令

    linux下查看当前目录下文件个数命令: 使用背景:有时想了解一个目录下具体有多少文件或者有多少文件夹. 1. 查看当前目录下文件个数 ls -l |grep "^-"|wc -l ...

  3. Lodop错误汇总

    代码方面 需要修改 LodopFuncs.js 里面的src地址,主要是端口号,端口号需要和服务器里面的程序的端口一样.如下:   调试错误 可以通过查看调用lodop的地方,查看lodop是否为空( ...

  4. 关于JSON对象,以及联合数组,eval函数的使用参考

    关于JSON对象,以及联合数组,eval函数的使用参考 var json="{persons:[{name:'Zhangsan',sex:'male'},{name:'Lisi',sex:' ...

  5. 九度OJ 1362 左旋转字符串(Move!Move!!Move!!!)【算法】

    题目地址:http://ac.jobdu.com/problem.php?pid=1362 题目描述: 汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运 ...

  6. [翻译][MVC 5 + EF 6] 5:Code First数据库迁移与程序部署

    原文:Code First Migrations and Deployment with the Entity Framework in an ASP.NET MVC Application 1.启用 ...

  7. mysql show processlist 显示mysql查询进程

    1.进入mysql/bin目录下输入mysqladmin processlist; 2.启动mysql,输入show processlist; 如果有 SUPER 权限,则可以看到全部的线程,否则,只 ...

  8. 转载:【高并发简单解决方案 | 靠谱崔小拽 】redis队列缓存 + mysql 批量入库 + php离线整合

    需求背景:有个调用统计日志存储和统计需求,要求存储到mysql中:存储数据高峰能达到日均千万,瓶颈在于直接入库并发太高,可能会把mysql干垮. 问题分析 思考:应用网站架构的衍化过程中,应用最新的框 ...

  9. YII2框架动态创建表模型

    YII2框架动态创建表模型 在YII2中,每个表对应一个model类 在开发过程中,我们在填写一个大型表单的时候,表单里有N个select下拉列表,每个下拉select来自于不同的表: 如果要在程序里 ...

  10. Distributed R

    R语言的分布式目前有这几个产品: (A)RHadoop:对hadoop族系的产品,其中提供了以下的组件 A.1 rhdfs  浏览读取增加修改hdfs上面的文件数据: A.2 rhbase 浏览读取增 ...