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;


  • 相关阅读:
    hdu 1348【凸包模板】
    hdu 3007【最小圆覆盖-随机增量法模板】
    poj 2187【旋转卡壳模板】
    bzoj 2618【半平面交模板】
    hdu1115【多边形求重心模板】
    bzoj 1006: [HNOI2008]神奇的国度【弦图+LesBFS】
    bzoj 3456: 城市规划【NTT+多项式求逆】
    bzoj 2194: 快速傅立叶之二【NTT】
    bzoj 4555: [Tjoi2016&Heoi2016]求和【NTT】
    bzoj 4842: [Neerc2016]Delight for a Cat【最小费用最大流】
  • 原文地址:https://www.cnblogs.com/yipeng-yu/p/5411589.html
Copyright © 2011-2022 走看看