用SharpZipLib可以解压.zip文件,但是.rar文件出错

.Net技术 针尖舞 9年前 (2015-08-26) 3117次浏览 0个评论

SharpZipLib可以解压.zip文件,但是.rar文件出错using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

using DeCompression;

namespace zip

{

/// <summary>

/// index 的摘要说明。

/// </summary>

public class index : System.Web.UI.Page

{

protected System.Web.UI.WebControls.Button Button1;

private void Page_Load(object sender, System.EventArgs e)

{

// 在此处放置用户代码以初始化页面

}

#region Web 窗体设计器生成的代码

override protected void OnInit(EventArgs e)

{

//

// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。

//

InitializeComponent();

base.OnInit(e);

}

/// <summary>

/// 设计器支持所需的方法 – 不要使用代码编辑器修改

/// 此方法的内容。

/// </summary>

private void InitializeComponent()

{    

this.Button1.Click += new System.EventHandler(this.Button1_Click);

this.Load += new System.EventHandler(this.Page_Load);

}

#endregion

private void Button1_Click(object sender, System.EventArgs e)

{

string []FileProperties=new string[2];

FileProperties[0]=”C:\zip\1test.rar”;//待解压的文件

FileProperties[1]=”C:\unzipped\”;//解压后放置的目标目录

UnZipClass UnZc=new UnZipClass();

UnZc.UnZip(FileProperties);

}

}

}

文件二:icsharpcode.cs

using System;

using System.Text;

using System.Collections;

using System.IO;

using System.Diagnostics;

using System.Runtime.Serialization.Formatters.Binary;

using System.Data;

using ICSharpCode.SharpZipLib.BZip2;

using ICSharpCode.SharpZipLib.Zip;

using ICSharpCode.SharpZipLib.Zip.Compression;

using ICSharpCode.SharpZipLib.Zip.Compression.Streams;

using ICSharpCode.SharpZipLib.GZip;

namespace DeCompression

{

public class UnZipClass

{   

public void UnZip(string[] args)

{

ZipInputStream s = new ZipInputStream(File.OpenRead(args[0]));  

ZipEntry theEntry;

while ((theEntry = s.GetNextEntry()) != null) 

{

   

string directoryName = Path.GetDirectoryName(args[1]);

string fileName      = Path.GetFileName(theEntry.Name);

   

//生成解压目录

Directory.CreateDirectory(directoryName);

   

if (fileName != String.Empty) 

{   

//解压文件到指定的目录

FileStream streamWriter = File.Create(args[1]+theEntry.Name);

    

int size = 2048;

byte[] data = new byte[2048];

while (true) 

{

size = s.Read(data, 0, data.Length);

if (size > 0) 

{

streamWriter.Write(data, 0, size);

else 

{

break;

}

}

    

streamWriter.Close();

}

}

s.Close();

}

}

}

错误提示:

“/zip”应用程序中的服务器错误。

—————–

Wrong Local header signature: 0x21726152 

说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 

异常详细信息: ICSharpCode.SharpZipLib.Zip.ZipException: Wrong Local header signature: 0x21726152

源错误: 

行 21:  ZipInputStream s = new ZipInputStream(File.OpenRead(args[0]));  

行 22:  ZipEntry theEntry;

行 23:  while ((theEntry = s.GetNextEntry()) != null) 

行 24:  {

行 25:    

 

源文件: d:\asp.net\zip\icsharpcode.cs    行: 23


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明用SharpZipLib可以解压.zip文件,但是.rar文件出错
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!