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. iostat命令简单说说

    tps: 每秒钟发送到的I/O请求数. Blk_read /s: 每秒读取的block数 Blk_wrtn/s: 每秒写入的block数 Blk_read: 读入的block总数 Blk_wrtn: ...

  2. LVS+keepalived实现负载均衡

    背景:         随着你的网站业务量的增长你网站的服务器压力越来越大?需要负载均衡方案!商业的硬件如F5又太贵,你们又是创业型互联公司如何有效节约成本,节省不必要 的浪费?同时实现商业硬件一样的 ...

  3. 自己一晚上总结的php基础知识!好累。好充实。

    为了巩固自己的基础提升自己的技术.花了一晚上的时间结合w3c上的非常基础的东西,和自己的部分见解,写了不少,望大神们指正,指导.. <?php /* 这段话必须要写在开篇啊!死老猫,你又刺激我! ...

  4. DOS下无法调出中文输入法-Solved

    From:http://www.cnblogs.com/killerlegend/p/3750542.html Author:KillerLegend Date:2014.5.24 DOS下无法打开中 ...

  5. mysql 汉字乱码

    原因:mysql server character设置问题 一.检查mysql server 安装目录下my.ini文件 找到如下设置 [mysql] default-character-set = ...

  6. programming ruby

    ri #rdoc reader attr_reader attr_writer @@xx 类变量都是私有的 def 类名.xx end 类方法 [1,3,5,7].inject(0){|sum,e| ...

  7. Ruby Code Style

    这篇博文逐渐写一点关于Ruby的编码风格,主要参考ruby-stytle-guide. 一些命名准则 函数与变量 使用蛇形小写比较好:some_var, some_function 目录和文件也使用s ...

  8. linux 病毒 sfewfesfs

    由于昨天在内网服务器A不小心rm -fr / ,导致服务器A完蛋,重装系统后,不知道啥原因,局域网瘫痪不能上网,最后发现内网服务器A的一个进程sfewfesfs cpu 300%.路由器被网络阻塞啦. ...

  9. StyleCop学习笔记——初识StyleCop

    一.定义 StyleCop是微软的一个开源的静态代码分析工具,检查c#代码一致性和编码风格. 二.支持的环境. JetBrains R# 5.1.3 ( 5.1.3000.12) JetBrains ...

  10. Android:简单实现ViewPager+TabHost+TabWidget实现导航栏导航和滑动切换

    viewPager是v4包里的一个组件,可以实现滑动显示多个界面. android也为viewPager提供了一个adapter,此adapter最少要重写4个方法: public int getCo ...