在企业开发过程中经常会遇到文件的压缩与解压,虽然网上很多流行的压缩文件格式都是RAR的,但是由于RAR不是一个开放的标准,因此ZIP成了更多人的选择。如果你不想自己开发的话可以选择开源的项目,比如SharpZipLib就是一个不错的选择。

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(); // 压缩级别 0-9
//s.Password = "123"; //Zip压缩文件密码
byte[] buffer = new byte[]; //缓冲区大小
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, , buffer.Length);
s.Write(buffer, , sourceBytes);
} while (sourceBytes > );
}
}
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 > )
{
Directory.CreateDirectory(directoryName);
} if (fileName != String.Empty)
{
using (FileStream streamWriter = File.Create(theEntry.Name))
{ int size = ;
byte[] data = new byte[];
while (true)
{
size = s.Read(data, , data.Length);
if (size > )
{
streamWriter.Write(data, , size);
}
else
{
break;
}
}
}
}
}
}
}
}
}

C# 文件压缩与解压(ZIP格式)的更多相关文章

  1. Java实现文件压缩与解压[zip格式,gzip格式]

    Java实现ZIP的解压与压缩功能基本都是使用了Java的多肽和递归技术,可以对单个文件和任意级联文件夹进行压缩和解压,对于一些初学者来说是个很不错的实例. zip扮演着归档和压缩两个角色:gzip并 ...

  2. 文件压缩、解压工具类。文件压缩格式为zip

    package com.JUtils.file; import java.io.BufferedOutputStream; import java.io.File; import java.io.Fi ...

  3. 使用SharpZipLib实现文件压缩、解压

    接口 public interface IUnZip { /// <summary> /// 功能:解压zip格式的文件. /// </summary> /// <par ...

  4. linux下压缩与解压(zip、unzip、tar)详解

    linux下压缩与解压(zip.unzip.tar)详解 2012-05-09 13:58:39| 分类: linux | 标签:linux zip unzip tar linux命令详解 |举报|字 ...

  5. CSharp tar类型文件压缩与解压

    最近闲暇时间开始写点通用基础类在写到tar类型文件压缩与解压时遇到点问题 压缩用的类库我是下载的 SharpZipLib_0860版本 先上代码 加压核心 /// <summary> // ...

  6. Linux之文件压缩与解压

    文件压缩与解压 1.tar命令 tar命令可以为Linux的文件和目录创建档案. 利用tar,可以为某一特定文件创建档案(备份文件),也可以在档案中改变文件,或者向档案中加入新的文件.tar最初被用来 ...

  7. I/O操作之文件压缩与解压

    与文件压缩与解压相关的类在java.util.zip包下 实例 //文件压缩 import java.io.File; import java.io.FileInputStream; import j ...

  8. 文件压缩跟解压(本地&Linux服务器)

    远程解压需要的jar包: <dependency> <groupId>commons-net</groupId> <artifactId>commons ...

  9. Java实现文件压缩与解压

    Java实现ZIP的解压与压缩功能基本都是使用了Java的多肽和递归技术,可以对单个文件和任意级联文件夹进行压缩和解压,对于一些初学者来说是个很不错的实例.(转载自http://www.puiedu. ...

随机推荐

  1. JS刷新页面的几种方法(转)

    Javascript刷新页面的几种方法: 1 history.go(0) 2 location.reload() 3 location=location 4 location.assign(locat ...

  2. python之chardet库

    chardet库是python的字符编码检测器,能够检测出各种编码的类型,例如: import chardet import urllib.request testdata = urllib.requ ...

  3. 1 TKinter小窗口及标题

    说明 :本博客户关于tkinter的知识参考:2014 年辛星 Python 界面编程教程第二版 创建一个GUI程序的步骤: 创建一个GUI程序 1.导入Tkinter模块 2.创建控件 3.指定这个 ...

  4. dubbo 解决Multicast java.net.SocketException: No such device

    log4j:WARN No appenders could be found for logger (com.alibaba.dubbo.common.logger.LoggerFactory). l ...

  5. EventBus学习

    Git位置https://github.com/greenrobot/EventBus 使用起来很方便:1. Implement any number of event handling method ...

  6. 程序员书单_HeadFirst系列

    Head First Jquery(中文版),完整扫描版 http://download.csdn.net/detail/shenzhq1980/9103615 Head First Python(完 ...

  7. gcc杂谈

    1. -l选项自动给库文件名增加lib前缀和.a/.so后缀.所以如果你有一个lib叫做libusb.a,那么编译选项是-lusb.另一方面,如果你有一个文件叫做libusb.o(是目标文件而不是库文 ...

  8. 如何把python最小化安装在客户机上面

    因为想尝试在我们的桌面软件中加入python支持,所以想简化python的库,到时候直接放到客户机上面,并且放到我们的目录下,尽量免去不必要的东西,也不要影响机子,不过当写好的程序放到测试机子上后,老 ...

  9. item30,最小的k个数

    剑指offer给出两类方法: 1,借助快排的思想,需要修改输入数组的元素,时间复杂度O(n) 2,借助STL中set或者multiset,因为它们的底层数据结构是红黑树实现的,插入数据时间复杂度为O( ...

  10. 回到顶部缓动效果代码 --- tween动画函数库

    function animateGoTop() { var top = $(document).scrollTop(); var end = 0; var dur = 500; var t = 0; ...