/**//*

* 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. 方法 :PHP开发环境搭建(phpstorm + xampp+mongodb)

    phpstorm 安装下载       百度网盘资源 phpstorm 9.0.1(有序列号)   http://pan.baidu.com/s/1kTvX0jl xampp 安装下载         ...

  2. Redis多机集群

    Redis集群.网上很多教程,只是按着它的步骤来做只能在单机上跑,而已不有点抗.也不用密码验证 开始: 1:redis集群最少需要要6个服务器端,因此先搞6台虚拟机 我用 centOS-7 mini ...

  3. static的应用以及静态与非静态的区别

    先前看到一个技术大牛写了一个关于静态成员与非静态成员,静态方法和非静态方法的各自区别,觉得挺好的,在这里写一个小程序来说明这些区别. package com.liaojianya.chapter5; ...

  4. hibernate的dao操作不能提交到数据库问题的解决

    刚学的时候总是各种错误,解决方法也无厘头的很 将UserDAO里面的的save方法修改try { getSession().save(transientInstance); log.debug(&qu ...

  5. C++对象模型与内存位对齐的简单分析(GNU GCC&VS2015编译器)

    以Fruit和Apple为例进行分析: Fruit和Apple的定义如下: 通过在两种编译环境下的测试(GNU GCC & VS2015),可以发现这两种编译器的对象模型是一样的,如下图所示: ...

  6. Skia

    1 What is SKIA. Skia is an open source 2D graphics library which provides common APIs that work acro ...

  7. 计算序列中第k小的数

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4046399.html 使用分治算法,首先选择随机选择轴值pivot,并使的序列中比pivot ...

  8. thinkphp分页实现

    以上为我对于thinkphp分页的实现效果,两种方法,一种调用公共函数中的函数方法(参考http://www.cnblogs.com/tianguook/p/4326613.html),一种是在模型中 ...

  9. 关于SQL优化的一个小试例子

    原SQL: select ta.serialno,       ta.accepttime,       ta.subsnumber,       ta.subsname,       ta.cont ...

  10. Vijos p1165 火烧赤壁 离散化+单调栈

    题目链接:https://vijos.org/p/1165 题意:输入n(n <= 20,000)段线段的端点,问所有线段的长度总和为多少? input: -1 1 5 11 2 9 outpu ...