/**//*

 * 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. QML学习(三)——<QML命名规范>

    QML对象声明 QML对象特性一般使用下面的顺序进行构造: id 属性声明 信号声明 JavaScript函数 对象属性 子对象 状态 状态切换 为了获取更好的可读性,建议在不同部分之间添加一个空行. ...

  2. 卸载nginx之后重新安装

    Ubuntu 14.04上卸载nginx之后重新安装没有重新生成配置文件的解决方法 在配置nginx做实验时配置错了,导致访问不了虚拟主机.一狠心把nginx的配置文件目录(/etc/nginx)都删 ...

  3. Linux下WebLogic的启动、停止和后台运行的方法

    Linux下WebLogic的启动.停止和后台运行的方法 进入目录:/home/weblogic/user_projects/domains/base_domain/bin 查看目录下的命令,如图: ...

  4. TCP的连接如何知道对方已经异常断开

    断电的话,对方不会发送任何数据包过来,包括RST.主机无法得知.如果是TCP已经连接,有个定时器,会发送空包,sequence number不变.如果一直收不到ack,会断定对方已经无法通信,而释放系 ...

  5. uniapp - 键盘弹起背景图片不会被挤压

    [释义] uni.getSystemInfoSync()获取屏幕可用高度windowScreen做为背景图高度即可(非虚拟DOM也可以使用本思路). [源码] <template> < ...

  6. hdu5387 钟表指针之间夹角(分数计算,模拟)

    题意: 给你一个24格式的数字时间,(字符串),问你这个时刻时针与分针 时针与秒针 分针与秒针 之间的夹角, 我们发现 秒针每秒转6度,分针每秒转1/10度,每分转6度,时针每小时转30度,每分转1/ ...

  7. MYSQL事务的开启与提交

    MYSQL 事务处理主要有两种方法: 1.用 BEGIN, ROLLBACK, COMMIT来实现 BEGIN 开始一个事务 ROLLBACK 事务回滚 COMMIT 事务确认 2.直接用 SET 来 ...

  8. EOS测试链智能合约部署调用

    ETH与EOS两者智能合约进行简单的对比. 1.编译智能合约(合约编译成.wasm与.abi格式后即可部署到区块链) [root@C03-12U-26 testcontract]# cat testc ...

  9. ubuntu18.04 无法连接有线

    突然发现Ubuntu无法连接有线,插上网线后还是显示 Cable unplugged. 参考这篇文章:https://zhuanlan.zhihu.com/p/32924819 因为我无线网卡正常工作 ...

  10. [LeetCode] 312. Burst Balloons 爆气球

    Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...