在企业开发过程中经常会遇到文件的压缩与解压,虽然网上很多流行的压缩文件格式都是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. html初学(一)

    学习了一些html的基本用法,代码和效果如下: <!DOCTYPE html> <html> <head> <meta charset="utf-8 ...

  2. ORACLE 包[转]

    包的作用: 包可以将任何出现在块声明的语句 ( 过程 , 函数 , 游标 , 游标 , 类型 , 变量 ) 放于包中 , 相当于一个容器 .使用包的好处不仅仅是程序的分组管理,还可以使用包中的程序重载 ...

  3. Redis系统管理相关指令简介

    常用命令列表 DBSIZE                                            返回当前数据库 Key 的数量 INFO                       ...

  4. BOOST的AUTO link机制以及配置

    我们在使用BOOST的时候,如果需要链接一些库,是不用我们手动去链接的,归根结底还是boost的auto_link这个机制,在boost下的auto_link.hpp这个文件夹里面,基本可以看出要根据 ...

  5. BeX5平台简明部署过程

    http://wex5.com/cn/concise-deployment/ BeX5平台简明部署过程 该文章主要介绍BeX5平台开发完成后,资源部署至正式环境的过程. 一. 获取BeX5企业快速开发 ...

  6. NOR Flash擦写和原理分析 (二)

    Nor Flash上电后处于数据读取状态(Reading Array Data).此状态可以进行正常的读.这和读取SDRAM/SRAM/ROM一样.(要是不一样的话,芯片上电后如何从NorFlash中 ...

  7. linux截图工具scrot

    SCROT截图工具 安装命令 sudo apt-get install scrot 截图命令使用说明: 1.抓取整个桌面:    scrot   ~/Pictures/pic1.jpg2.抓取窗口:  ...

  8. 105. Construct Binary Tree from Preorder and Inorder Traversal

    Given preorder and inorder traversal of a tree, construct the binary tree. ============== 基本功: 利用前序和 ...

  9. FB Flash Builder 安装错误 ERROR: DW050: - Microsoft Visual C++ 2010 Redistributable Package (x86): Install failed

    这个问题很可能是你的 Microsoft Visual C++ 2010 Redistributable Package (x86) 太新的缘故,所以无法安装成功,导致最终的失败. 在控制面板-程序和 ...

  10. Blockchain概述--转

    编者按:著名投资人 Fred Wilson 的同事 Joel Monegro 近日参加了纽约比特币 workshop HackBit聚会,其间他们讨论了比特币式的思维方式对未来十年世界的影响,而这种影 ...