异步解压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;


转载于:https://www.cnblogs.com/yipeng-yu/p/5411589.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值