using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using ICSharpCode.SharpZipLib.Zip; namespace Unzip
{
      class UnzipCore
      {
       ///配置为 QueueUserWorkItem 或 Task.Factory.StartNew,两者速度差不多
            public static void UnZip(string zipFullName, string targetPath)
            {
                  if (!Directory.Exists(targetPath))
                        Directory.CreateDirectory(targetPath);
                  int size = 1024;
                  byte[] data = new byte[size];
                  string fileName = null;
                  FileStream fs = null;
                  ZipInputStream zs = null;
                  List<string> files = new List<string>();
                  try
                  {
                        fs = new FileStream(zipFullName, FileMode.Open, FileAccess.ReadWrite);
                        zs = new ZipInputStream(fs);
                        ZipEntry theEntry;                         while ((theEntry = zs.GetNextEntry()) != null)
                        {
                              fileName = theEntry.Name;                               if (fileName.IndexOf(":") > -1)
                              {
                                    fileName = fileName.Replace(":", "_");
                              }
                              if (fileName.IndexOf("/") > -1)
                              {
                                    fileName = fileName.Replace("/", "\\");
                              }
                              if (theEntry.IsDirectory)
                              {
                                    if (!Directory.Exists(Path.Combine(targetPath, fileName)))
                                          Directory.CreateDirectory(Path.Combine(targetPath, fileName));
                              }
                              else
                              {
                                    string fullname = Path.Combine(targetPath, fileName);
                                    if (files.Count(f => f.ToLower().StartsWith(Path.GetDirectoryName(fullname).ToLower())) == 0)
                                    {
                                          Directory.CreateDirectory(Path.GetDirectoryName(fullname));
                                    }
                                    files.Add(fullname);
                     //该方式降低解压速度6倍以上
                                    //using (var ws = File.Create(fullname))
                                    //{
                                    //      size = zs.Read(data, 0, data.Length);
                                    //      while (size > 0)
                                    //      {
                                    //            ws.Write(data, 0, size);
                                    //            size = zs.Read(data, 0, data.Length);
                                    //      }
                                    //}
                                    List<byte> bytes = new List<byte>();
                                    size = zs.Read(data, 0, data.Length);
                                    while (size > 0)
                                    {
                                          bytes.AddRange(data);
                                          size = zs.Read(data, 0, data.Length);
                                    }
                                    var ws = new FileStream(fullname, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite, 1024, true);
                                    ws.BeginWrite(bytes.ToArray(), 0, bytes.Count, new AsyncCallback(EndWriteCallback), ws);
                              }
                        }
                  }
                  catch
                  {
                        throw;
                  }
                  finally
                  {
                        if (zs != null) zs.Close();
                        if (fs != null) fs.Close();
                  }
            }
            static void EndWriteCallback(IAsyncResult result)
            {
                  FileStream stream = (FileStream)result.AsyncState;
                  stream.EndWrite(result);
                  stream.Flush();
                  stream.Close();
            }
      }
}

在没有异步的情况下,配置如下,均无法提高解压速度

System.Threading.ThreadPool.SetMinThreads(Environment.ProcessorCount*20, Environment.ProcessorCount*20);

System.Diagnostics.Process.GetCurrentProcess().PriorityBoostEnabled = true;

System.Diagnostics.Process.GetCurrentProcess().PriorityClass = System.Diagnostics.ProcessPriorityClass.High;


异步解压ZIP文件的更多相关文章

  1. 通过javascript在网页端解压zip文件并查看压缩包内容

    WEB前端解压ZIP压缩包 web前端解压zip文件有什么用: 只考虑标准浏览器的话, 服务器只要传输压缩包到客户端, 节约了带宽, 而且节约了传输时间, 听起来好像很厉害的说:     如果前端的代 ...

  2. Android 解压zip文件(支持中文)

    过了n多天后,当再次使用原先博客上写的那篇: Android 压缩解压zip文件 去做zip包的解压的时候,出现了原来没有发现的很多问题.首先是中文汉字问题,使用java的zip包不能很好的解决解压问 ...

  3. (转载)C#压缩解压zip 文件

    转载之: C#压缩解压zip 文件 - 大气象 - 博客园http://www.cnblogs.com/greatverve/archive/2011/12/27/csharp-zip.html C# ...

  4. Android 解压zip文件你知道多少?

    对于Android常用的压缩格式ZIP,你了解多少? Android的有两种解压ZIP的方法,你知道吗? ZipFile和ZipInputStream的解压效率,你对比过吗? 带着以上问题,现在就开始 ...

  5. java实现解压zip文件,(亲测可用)!!!!!!

    项目结构: Util.java内容: package com.cfets.demo; import java.io.File; import java.io.FileOutputStream; imp ...

  6. Android 解压zip文件

    过了n多天后,当再次使用原先博客上写的那篇: Android 压缩解压zip文件 去做zip包的解压的时候,出现了原来没有发现的很多问题.首先是中文汉字问题,使用java的zip包不能很好的解决解压问 ...

  7. python用zipfile模块打包文件或是目录、解压zip文件实例

    #!/usr/bin/env python # -*- coding: utf-8 -*- from zipfile import * import zipfile #解压zip文件 def unzi ...

  8. AIX解压ZIP文件

    AIX系统自身是没有解压ZIP文件的,但在AIX安装oracle数据库服务器的话,在$ORACLE_HOME/bin路径下方却有unzip命令,可以解压ZIP文件. 一.shell脚本   之前的版本 ...

  9. linux 解压zip文件

    linux 解压zip文件 学习了:https://blog.csdn.net/hbcui1984/article/details/1583796 unzip xx.zip

随机推荐

  1. mariadb主从复制架构学习笔记

    复制功用: 数据分布 负载均衡:读操作,适用于读密集型的应用 备份 高可用和故障切换 MySQL升级测试 在从服务器上有两个线程: I/O线程:从master请求二进制日志信息,并保存至中继日志 SQ ...

  2. excute和query

    query(update goods set is_delete=1 where goods_id=13)总是出错??为什么, excute(update goods set is_delete=1 ...

  3. 【Apache】2.4.6版本的安装和配置

  4. windows下编译lua源码

    所有的lua源码都放在了 src 文件夹下,当然,不同的lua版本目录结构有可能不一样,但是都会有 src 这个文件夹,只有这个才是我们编译必须的.而且lua越做越精简了,5.1的版本里面还有一些te ...

  5. Dll学习二_Dll 窗体中动态创建数据并使用Demo

    沿用上一篇Demo 环境:DelphiXE,XP,SQL2005 贴出改动过的单元代码: dbGrid控件版: unit SubMain_Unit; interface uses Windows, M ...

  6. Python学习教程(learning Python)--3.3.1 Python下的布尔表达式

    简单的说就是if要判断condition是真是假,Python和C语言一样非0即真,所以如果if的condition是布尔表达式我们可以用True或者非0数(不可是浮点数)表示真,用False或者0表 ...

  7. pyenv

    export PYTHON_BUILD_MIRROR_URL="http://pyenv.qiniudn.com/pythons/"

  8. IOS之表视图单元格删除、移动及插入

    1.实现单元格的删除,实现效果如下 - (void)viewDidLoad { [super viewDidLoad]; //设置导航栏 self.editButtonItem.title = @&q ...

  9. 008--VS2013 C++ 位图半透明化(另一种显示)

    注:主要变化是在下面这张位图上的操作 //全局变量HBITMAP bg, girl;HDC mdc;//起始坐标const int xstart = 50;const int ystart = 20; ...

  10. VS2010出现灾难性错误的解决办法

    VS2010出现灾难性错误的解决办法   之前本人利用VS2010 在编写一个基于对话框的程序的时候,要在对话框类C-.DLG中添加函数,右键点击类向导,此时界面上弹出一个消息框,告知出现灾难性事故, ...