zoukankan      html  css  js  c++  java
  • 异步解压ZIP文件

    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;


  • 相关阅读:
    div 圆角
    CSS定义鼠标经过时鼠标图型样式
    如何判断浏览器类型然后让它读取指定的CSS
    如何分别指定ie6及ie7浏览器的css
    用CSS控制DIV居中失效的解决方法
    css如何控制文字多行显示,溢出截断后末尾出现省略...
    样式命名规则
    type="file" 谁用过这个属性给定义样式
    有利于SEO的DIV+CSS的命名规矩小结
    左右两个div高度自动一致,自适应高度
  • 原文地址:https://www.cnblogs.com/yipeng-yu/p/5411589.html
Copyright © 2011-2022 走看看