/**//*

 * 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. 深度学习图像配准 Image Registration: From SIFT to Deep Learning

    Image Registration is a fundamental step in Computer Vision. In this article, we present OpenCV feat ...

  2. OpenFOAM——梯形腔双边驱流

    本算例来自<ANSYS Fluid Dynamics Verification Manual>中的VMFL054: Laminar flow in a Trapezoidal Cavity ...

  3. Tcl数学运算

    expr 数学表达式 Tcl支持的数学操作符(优先级按照从高到低): -一元负号 +一元正号 ~按位取反 !逻辑非 *乘 /除 %取余 +加号 -减号 <<左移位 >>右移位 ...

  4. 坑爹微信之读取PKCS12流时出现的java.io.IOException: DerInputStream.getLength

    背景 微信退款接口需要使用到证书,我参考微信的官方Demo进行,部分代码如下: char[] password = config.getMchID().toCharArray(); InputStre ...

  5. CSS背景和精灵图

    如何设置背景图片? 1.在CSS中有个叫做background-image:url():的属性,就是专门用于设置背景图片的. 2.注意点: 1)图片的地址必须放在url()中,图片的地址可以是本地的地 ...

  6. 在python中使用elasticsearch 需要注意的一些问题

    1, py es client 使用是 http ,java  api 使用是 tcp 2, es.scroll() 方法 在查询多个索引的时候会报 : elasticsearch.exception ...

  7. CMU Database Systems - MVCC

    MVCC是一种用空间来换取更高的并发度的技术 对同一个对象不去update,而且记录下每一次的不同版本的值 存在不会消失,新值并不能抹杀原先的存在 所以update操作并不是对世界的真实反映,这是一种 ...

  8. 日期正则表达式yyyyMMdd

    日期校验yyyyMMdd, 包括闰月等校验. package com.xgcd; import java.util.regex.Matcher; import java.util.regex.Patt ...

  9. 【mybatis源码学习】ResultMap查询结果映射

    一.ResultMap包含的元素 constructor - 用于在实例化类时,注入结果到构造方法中 idArg - ID 参数:标记出作为 ID 的结果可以帮助提高整体性能 arg - 将被注入到构 ...

  10. ingress nginx https配置

    3.https配置第一步:制作自签证书 [root@master demo]# openssl genrsa -out tls.key 2048 [root@master demo]# openssl ...