异步解压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;
异步解压ZIP文件的更多相关文章
- 通过javascript在网页端解压zip文件并查看压缩包内容
WEB前端解压ZIP压缩包 web前端解压zip文件有什么用: 只考虑标准浏览器的话, 服务器只要传输压缩包到客户端, 节约了带宽, 而且节约了传输时间, 听起来好像很厉害的说: 如果前端的代 ...
- Android 解压zip文件(支持中文)
过了n多天后,当再次使用原先博客上写的那篇: Android 压缩解压zip文件 去做zip包的解压的时候,出现了原来没有发现的很多问题.首先是中文汉字问题,使用java的zip包不能很好的解决解压问 ...
- (转载)C#压缩解压zip 文件
转载之: C#压缩解压zip 文件 - 大气象 - 博客园http://www.cnblogs.com/greatverve/archive/2011/12/27/csharp-zip.html C# ...
- Android 解压zip文件你知道多少?
对于Android常用的压缩格式ZIP,你了解多少? Android的有两种解压ZIP的方法,你知道吗? ZipFile和ZipInputStream的解压效率,你对比过吗? 带着以上问题,现在就开始 ...
- java实现解压zip文件,(亲测可用)!!!!!!
项目结构: Util.java内容: package com.cfets.demo; import java.io.File; import java.io.FileOutputStream; imp ...
- Android 解压zip文件
过了n多天后,当再次使用原先博客上写的那篇: Android 压缩解压zip文件 去做zip包的解压的时候,出现了原来没有发现的很多问题.首先是中文汉字问题,使用java的zip包不能很好的解决解压问 ...
- python用zipfile模块打包文件或是目录、解压zip文件实例
#!/usr/bin/env python # -*- coding: utf-8 -*- from zipfile import * import zipfile #解压zip文件 def unzi ...
- AIX解压ZIP文件
AIX系统自身是没有解压ZIP文件的,但在AIX安装oracle数据库服务器的话,在$ORACLE_HOME/bin路径下方却有unzip命令,可以解压ZIP文件. 一.shell脚本 之前的版本 ...
- linux 解压zip文件
linux 解压zip文件 学习了:https://blog.csdn.net/hbcui1984/article/details/1583796 unzip xx.zip
随机推荐
- LevelDb系列之简介
说起LevelDb也许您不清楚,但是如果作为IT工程师,不知道下面两位大神级别的工程师,那您的领导估计会Hold不住了:Jeff Dean和Sanjay Ghemawat.这两位是Google公司重量 ...
- PHP代码优化的53个细节
PHP代码优化的53个细节,常见而重要的php优化策略. 用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量,单引号则不会,注意:只有echo能这么做,它是一 ...
- Yii中使用PHPExcel导出Excel
最近在研究PHP的Yii框架,很喜欢,碰到导出Excel的问题,研究了一下,就有了下面的方法: 1.首先在config/main.php中添加对PHPExcel的引用,我的方式是这样: // auto ...
- C# A窗口内容显示在B窗口中的方法
HeScripts script = new HeScripts(); //A窗口中实例化B窗口 string okscripts = "test"; //设置字段内容 scrip ...
- SQL Server 基础:Join用法
Join(麻蛋 废话不多说 有图有真相) 测试数据脚本 CREATE TABLE Atable ( S# INT, Sname ), Sage INT, Sfrom ) ) insert into ...
- python杂记-4(迭代器&生成器)
#!/usr/bin/env python# -*- coding: utf-8 -*-#1.迭代器&生成器#生成器#正确的方法是使用for循环,因为generator也是可迭代对象:g = ...
- bat隐藏文件夹
在windows中隐藏文件是可以隐藏,但是下面红色区域一旦选中,那也就显示出来了 现在有如下代码,可以实现个性化隐藏 ========================================= ...
- SQL Server基本操作积累
一.基本操作 1.将数据绑定到DataGridVirw控件上显示的数据列标题将会是数据库中的字段名称,可以在使用select语句时使用AS关键字将转化为列名的别名 select name AS 姓名 ...
- iOS中远程推送实现—在Apple的生产环境上测试Push Notifications功能
1.在“Provisioning Profiles”中点击“Add”按钮. 2.在“What type of provisioning profile do you need?”页面中选择“Distr ...
- 在 App 扩展和主 App 间共享数据
tags: iOS 8,Swift,App Groups 随着 iOS 8 的发布,苹果为广大开发者很多新的 API,其中最突出显著的就非 App Extension 莫属了.这为开发者们又带来了很多 ...