/**//*

 * 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;

                                }

                            }

                        }

                    }

                }

            }

        }

    }

}

  点击下载项目源码

ZIP压缩与解压的更多相关文章

  1. 正确的 zip 压缩与解压代码

    网上流传的zip压缩与解压 的代码有非常大的问题 尽管使用了ant进行压缩与解压,可是任务的流程还是用的java.util.zip 的方式写的,我在使用的过程中遇到了压缩的文件夹结构有误,甚至出现不同 ...

  2. java zip 压缩与解压

    java zip 压缩与解压 import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java. ...

  3. 文件操作工具类: 文件/目录的创建、删除、移动、复制、zip压缩与解压.

    FileOperationUtils.java package com.xnl.utils; import java.io.BufferedInputStream; import java.io.Bu ...

  4. Zip 压缩、解压技术在 HTML5 浏览器中的应用

    JSZip 是一款可以创建.读取.修改 .zip 文件的 javaScript 工具.在 web 应用中,免不了需要从 web 服务器中获取资源,如果可以将所有的资源都合并到一个 .zip 文件中,这 ...

  5. golang zip 压缩,解压(含目录文件)

    每天学习一点go src. 今天学习了zip包的简单使用,实现了含目录的压缩与解压. 写了两个方法,实现了压缩.解压. package ziptest import ( "archive/z ...

  6. zip压缩,解压

    //引用 System.IO.Compression.FileSystem.dll var basePath = AppDomain.CurrentDomain.BaseDirectory; Syst ...

  7. zip压缩与解压文件夹或文件

    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...

  8. C# Zip压缩、解压

    /* *引用 NuGet包 ICSharpCode.SharpZipLib.dll */ public class ZipUtility { /// <summary> /// 所有文件缓 ...

  9. JAVA实现实用的ZIP压缩与解压

    http://blog.csdn.net/z69183787/article/details/38555913

随机推荐

  1. spring boot 防止重复提交

    服务器端实现方案:同一客户端在2秒内对同一URL的提交视为重复提交 上代码吧 pom.xml <?xml version="1.0" encoding="UTF-8 ...

  2. 作业:分布式文件系统HDFS 练习

    这个作业的要求来自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/3292. 利用Shell命令与HDFS进行交互 1.目录操作 H ...

  3. 【大数据作业九】安装关系型数据库MySQL 安装大数据处理框架Hadoop

    作业要求:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/3161 4.简述Hadoop平台的起源.发展历史与应用现状. 列举发展过程中 ...

  4. 第10组 Alpha冲刺(5/6)

    链接部分 队名:女生都队 组长博客: 博客链接 作业博客:博客链接 小组内容 恩泽(组长) 过去两天完成了哪些任务 描述 学习调用中国天气网API,接近实现天气推送功能 对天气推送的形式进行讨论及重确 ...

  5. 解决Bootstrap标签页(Tab)插件切换echarts不显示问题

    1.参考连接:https://blog.csdn.net/qq_24313955/article/details/78363981 问题描述:在echarts跟bootstrap选项卡整合的时候,默认 ...

  6. jmeter BeanShell断言(三)数据处理

    在做接口测试时,对响应数据的校验是非常重要的部分:在使用Jmeter进行接口测试时,有多种respone校验方式,比如响应断言.BeanShell断言等等,BeanShell断言可以自定义断言,自由灵 ...

  7. JQuery selector form input

    var inputPhoneInFormActivity = $("form#formtab input[name='phone']"); if(inputPhoneInFormA ...

  8. Docker打包部署前端项目与负载均衡

    设置淘宝镜像 npm install -g cnpm --registry=https://registry.npm.taobao.org //在home/fn1 home/fn2放入项目和nginx ...

  9. web服务器请求代理方式

    1 通信数据转发程序:代理.网关.隧道 代理:是一种有转发功能的应用程序,他扮演了位于服务器和客户端“中间人”的角色,接收客户端发送的请求并转发给服务器:同时也接收服务器返回的响应并转发给客户端. 使 ...

  10. python开发笔记-字典按值排序取前n个key值

    场景举例: 假如我们有某个班级的语文成绩数据,格式为字典,其中字典key为学生姓名,value为学生成绩: 那么,如何获得单科成绩排名前3的学生姓名? 代码如下:--数据样例,方便测试 def dic ...