返回首页
当前位置: 主页 > 编程语言 > C#教程 >

C# 解压zip Deflate64,BZip2,WinZipAES压缩文件

时间:2016-04-18 15:12来源:电脑教程学习网 www.etwiki.cn 编辑:admin

C# 解压zip文件,大家经常用的是ICSharpCode.SharpZipLib.dll,ZipInputStream,去操作zip文件,今天博主也遇到了这个问题,这边先和大家交流我是怎么解决的

首先如果是普通的Stored和Deflated,这边采用ZipInputStream解决
using (var s = new ZipInputStream(File.OpenRead("")))
{
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
if (theEntry.IsDirectory)
{
continue;
}
string directorName = Path.Combine("", Path.GetDirectoryName(theEntry.Name));
string fileName = Path.Combine(directorName, Path.GetFileName(theEntry.Name));
if (!Directory.Exists(directorName))
{
Directory.CreateDirectory(directorName);
}
if (!String.IsNullOrEmpty(fileName))
{
using (FileStream streamWriter = File.Create(fileName))
{
int size = 4096;
byte[] data = new byte[size];
while (size > 0)
{
streamWriter.Write(data, 0, size);
size = s.Read(data, 0, data.Length);
}
}
}
}
}
Deflate64,BZip2,WinZipAES等压缩方式
如果出现Deflate64,BZip2,WinZipAES等压缩方式,就非常麻烦,ZipInputStream解决不了,博主的解决方案是调用本地的winRar处理这个问题

public class WinRARHelper
{

/// <summary>
/// 是否安装了Winrar
/// </summary>
/// <returns></returns>
static public bool Exists()
{
RegistryKey the_Reg = Registry.LocalMachine.
OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\AppPaths\WinRAR.exe");
return !string.IsNullOrEmpty(the_Reg.GetValue("").ToString());
}

/// 解压
/// </summary>
/// <param name="unRarPatch"></param>
/// <param name="rarPatch"></param>
/// <param name="rarName"></param>
/// <returns></returns>
public bool unCompressRAR(string unRarPatch, string rarPatch, string rarName)
{
string the_rar;
RegistryKey the_Reg;
object the_Obj;
string the_Info;
try
{
the_Reg = Registry.LocalMachine.
OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
the_Obj = the_Reg.GetValue("");
the_rar = the_Obj.ToString();
the_Reg.Close();

if (Directory.Exists(unRarPatch) == false)
{
Directory.CreateDirectory(unRarPatch);
}
the_Info = "x " + rarName + " " + unRarPatch + " -y";

ProcessStartInfo the_StartInfo = new ProcessStartInfo();
the_StartInfo.FileName = the_rar;
the_StartInfo.Arguments = the_Info;
the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
the_StartInfo.WorkingDirectory = rarPatch;//获取压缩包路径

Process the_Process = new Process();
the_Process.StartInfo = the_StartInfo;
the_Process.Start();
the_Process.WaitForExit();
the_Process.Close();
}
catch (Exception ex)
{
throw ex;
}
return true;
}

}
完美解决问题。

 

 

 

 

------分隔线----------------------------
标签(Tag):c# c#技巧 c#实例教程 c#源代码 c#基础教程
------分隔线----------------------------
推荐内容
猜你感兴趣