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. Spark自定义分区(Partitioner)

    我们都知道Spark内部提供了HashPartitioner和RangePartitioner两种分区策略,这两种分区策略在很多情况下都适合我们的场景.但是有些情况下,Spark内部不能符合咱们的需求 ...

  2. 登录成功返回登录前页面js代码

    /*------ setCookie(name,value) -----------*/ function setCookie(name,value) { var Days = 30; //此 coo ...

  3. object在ie8与ie9中与下文多出几像素问题

    今天发现一个很古怪的问题,object与下面文字部分的间隔超过了30个像素,关系是不管用padding还是margin都是一样的效果: 给其设置overflow:hidden属性依然没有任何效果,再设 ...

  4. 关于linux上pdf阅读器

    今天也是倒腾linux 上pdf阅读器好久. 1.okular是挺好的,但是却太大了,好多功能,我没有细看.我简单的打开了几个pdf文件,发现加载速度还是太慢了.所以基于种种,我给卸载掉了. 安装直接 ...

  5. spring debug

    DispatcherServlet{ getHandler()}handlerMappings{ RequestMappingHandlerMapping BeanNameUrlHandlerMapp ...

  6. [div+css布局]命名规则

    //首页可能碰到的 页头:header登录条:loginBar标志:logo侧栏:sideBar广告:banner导航:nav子导航:subNav菜单:menu子菜单:subMenu搜索:search ...

  7. eclipse集成maven

    1.工具下载: Eclipse4.2 jee版本(这里使用最新的Eclipse版本,3.7以上版本按照以下步骤都可以) 下载地址:http://www.eclipse.org/downloads/do ...

  8. C 实现一个跨平台的定时器 论述

    引言 今天我们要讲述和构建的是一个跨平台多线程C的定时器对象,粒度是毫秒级别.可以称之为简易的timer, sctimer.h 库. 首先看总的接口,看门见客. sctimer.h #ifndef _ ...

  9. 023使用typeof关键字获取类内部结构

    private void button1_Click(object sender, EventArgs e) { Focus(); string a=txtType.Text; // Type typ ...

  10. Android之完美退出方法

    为什么要写这篇文章? 网上有很多种退出方法,可实际上很多方法都不通用(在某个版本下可用,到了另一个版本就不行),或者方法的实际效果根本就和其描述不符(也不知道那些发帖的人测没测试过). 但我们的需求又 ...