/**//*

 * 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. 使用JS计算前一天和后一天

    使用JS实现前一天和后一天 首先,我们先在html中写两个按钮来进行简单操作 <button onclick="Before()">前一天</button> ...

  2. Pycharm使用git版本控制

    一.使用Pycharm进行版本控制 01 从远程仓库克隆项目 从远程仓库将一个已存在的项目克隆到本地 打开pycharm, VCS --> Checkout from Version Contr ...

  3. [RoarCTF]Easy Calc

    目录 [RoarCTF]Easy Calc 知识点 1.http走私绕过WAF 2.php字符串解析特性绕过WAF 3.绕过过滤写shell [RoarCTF]Easy Calc 题目复现链接:htt ...

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

    队名:拾光组 组长博客链接 作业博客链接 团队项目情况 燃尽图(组内共享) 组长:宋奕 过去两天完成了哪些任务 主要完成了个人中心模块的接口设计 完善后端的信息处理 GitHub签入记录 接下来的计划 ...

  5. B2C电子商务系统研发——商品SKU分析和设计(一)

    一.SKU及相关概念定义 在设计商品SKU之前,首先让我们熟悉一下SKU和相关的一些概念. # 什么是SKU: SKU=Stock Keeping Unit(库存量单位) 同一型号的商品,或者说是同一 ...

  6. 为什么用ls和du显示出来的文件大小有差别?【转】

    在使用Linux ls命令查看文件大小时,发现文件很大,足有100个G,而使用du命令查看则不超过10个G. [root@shanghai devicemapper]# ls -l 总用量 -rwxr ...

  7. qemu-img convert -c disk /var/lib/nova/instances/_base/94a107318b54108fc8e76fff21a86d7c390a20bf -O qcow2 hebin.qcow2

  8. 记某app内购破解 – 安卓逆向菜鸟的初体验

    前言 因为某个机缘,我拿到一个赛车app,玩了一会想买个装备,居然要我掏钱包,作为一名cracker,我觉得我的尊严受到了严重的蔑视(无奈钱包空空),我觉得要捍卫我那脆弱的玻璃心(钱包),所以,开干吧 ...

  9. 推荐:使用 Anaconda 工具搭建并管理 Python 环境

    https://www.anaconda.com/distribution/#download-section  官网下载地址 https://mirror.tuna.tsinghua.edu.cn/ ...

  10. Android 解读Event和Main Log

    1 Android P EventLogTags文件 Android P 9.0.0 所有EventLogTags文件List: system/bt/EventLogTags.logtags syst ...