1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using ICSharpCode.SharpZipLib.Zip;
  7.  
  8. namespace Unzip
  9. {
  10.       class UnzipCore
  11.       {
           ///配置为 QueueUserWorkItem 或 Task.Factory.StartNew,两者速度差不多
  12.             public static void UnZip(string zipFullName, string targetPath)
  13.             {
  14.                   if (!Directory.Exists(targetPath))
  15.                         Directory.CreateDirectory(targetPath);
  16.                   int size = 1024;
  17.                   byte[] data = new byte[size];
  18.                   string fileName = null;
  19.                   FileStream fs = null;
  20.                   ZipInputStream zs = null;
  21.                   List<string> files = new List<string>();
  22.                   try
  23.                   {
  24.                         fs = new FileStream(zipFullName, FileMode.Open, FileAccess.ReadWrite);
  25.                         zs = new ZipInputStream(fs);
  26.                         ZipEntry theEntry;
  27.  
  28.                         while ((theEntry = zs.GetNextEntry()) != null)
  29.                         {
  30.                               fileName = theEntry.Name;
  31.  
  32.                               if (fileName.IndexOf(":") > -1)
  33.                               {
  34.                                     fileName = fileName.Replace(":", "_");
  35.                               }
  36.                               if (fileName.IndexOf("/") > -1)
  37.                               {
  38.                                     fileName = fileName.Replace("/", "\\");
  39.                               }
  40.                               if (theEntry.IsDirectory)
  41.                               {
  42.                                     if (!Directory.Exists(Path.Combine(targetPath, fileName)))
  43.                                           Directory.CreateDirectory(Path.Combine(targetPath, fileName));
  44.                               }
  45.                               else
  46.                               {
  47.                                     string fullname = Path.Combine(targetPath, fileName);
  48.                                     if (files.Count(=> f.ToLower().StartsWith(Path.GetDirectoryName(fullname).ToLower())) == 0)
  49.                                     {
  50.                                           Directory.CreateDirectory(Path.GetDirectoryName(fullname));
  51.                                     }
  52.                                     files.Add(fullname);
  53.                      //该方式降低解压速度6倍以上
  54.                                     //using (var ws = File.Create(fullname))
  55.                                     //{
  56.                                     //      size = zs.Read(data, 0, data.Length);
  57.                                     //      while (size > 0)
  58.                                     //      {
  59.                                     //            ws.Write(data, 0, size);
  60.                                     //            size = zs.Read(data, 0, data.Length);
  61.                                     //      }
  62.                                     //}
  63.                                     List<byte> bytes = new List<byte>();
  64.                                     size = zs.Read(data, 0, data.Length);
  65.                                     while (size > 0)
  66.                                     {
  67.                                           bytes.AddRange(data);
  68.                                           size = zs.Read(data, 0, data.Length);
  69.                                     }
  70.                                     var ws = new FileStream(fullname, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite, 1024, true);
  71.                                     ws.BeginWrite(bytes.ToArray(), 0, bytes.Count, new AsyncCallback(EndWriteCallback), ws);
  72.                               }
  73.                         }
  74.                   }
  75.                   catch
  76.                   {
  77.                         throw;
  78.                   }
  79.                   finally
  80.                   {
  81.                         if (zs != null) zs.Close();
  82.                         if (fs != null) fs.Close();
  83.                   }
  84.             }
  85.             static void EndWriteCallback(IAsyncResult result)
  86.             {
  87.                   FileStream stream = (FileStream)result.AsyncState;
  88.                   stream.EndWrite(result);
  89.                   stream.Flush();
  90.                   stream.Close();
  91.             }
  92.       }
  93. }

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

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. mongodb 修改数据结构的一个例子以及小梳理

    mongodb的存储结构是灵活可变的,但是,并不意味着我们就肆意地使用不规则的文档结构.不规则的文档结构对于开发和后期的维护都是一个灾难.所以,还是要有一个约定的格式. 但是,由于前期设计的不周详和其 ...

  2. WordPress 主题开发 - (六) 创建主题函数 待翻译

    We’ve got a file structure in place, now let’s start adding things to them! First, we’re going to ad ...

  3. PHP mysql 事务处理实例

    事务是必须满足4个条件(ACID):原子性(Autmic).一致性(Consistency).隔离性(Isolation).持久性(Durability) 原子性(Autmic):事务在执行性,要做到 ...

  4. Lua 练习中的Bug 以及日志

    使用 Lua 中的table.getn获得数组的table的长度:运行失败-- > t ={1,2,3 } > print(table.getn(t)) stdin:1: attempt ...

  5. ubuntu下svn使用指南

    ubuntu下安装subversion客户端: sudo apt-get install subversion subversion-tools 详细请看 http://www.subversion. ...

  6. PHP判断用户所在国家并跳转对应的目录

    <?php // 淘宝API查询国家代码 $url = "http://ip.taobao.com/service/getIpInfo.php?ip=".get_client ...

  7. java初探native

    最近碰见一个java中一个native关键字,不知道是干什么的,如下: public native String FileName(String strURL);     static{        ...

  8. ASP.NET MVC4学习笔记路由系统实现

    一.路由实现 路由系统实际是一个实现了ASP.NET IHttpModule接口的模块,通过注册HttpApplication的PostResolveRequestCache 事件对Url路由处理.总 ...

  9. mysql 1093 错误

    1093错误: 要更新某表,同时该表有字段值又来自该表的查询语句. 例如: INSERT INTO m_bulletincategory ( OrganizationKey , CategoryNam ...

  10. JavaWeb之Servlet:Cookie 和 Session

    会话 现实生活中我们会用手机跟对方对话,拿起手机,拨号,然后对面接听,跟着互相通话,最后会话结束. 这个过程也可以用我们的B/S模式来描述: 打开浏览器—>输入地址->发出请求->服 ...