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


上一篇:ORM(三)QuerySet查询字段操作


下一篇:AngularJS 关于ng-model和ng-bind还有{{}}