asp.net SharpZipLib的压缩与解压问题
2009-11-11 00:00
676 查看
我使用SharpZipLib.dll中遇到的问题是:利用SharpZipLib压缩后生成的*.rar文件,利用其可以正常解压,但如果使用文件右击压缩生成的*.RAR文件,在解压过程中出错,具体报错信息:Wrong Local header signature: 0x21726152 ;但*.zip文件可正常解压。
具体压缩、解压代码实现参照网络上的代码,贴出概要代码:
如果需解压*.rar的压缩文件在网络也可以找到相关的实现代码,概要代码:
我本想利用FileUpload控件将上传的压缩文件解压后保存至相对应的目录并更新数据库文件目录,后发现一些较好的用于上传的开源软件:如NeatUpload,SWFUpload可以较方便的实现我的需求,遂没有过多纠缠于SharpZipLib,可能关于SharpZipLib的压缩与解压有其它用法,不能被我误导,以上代码是从网络上整合出来的,因为它太过于重复和散乱。
具体压缩、解压代码实现参照网络上的代码,贴出概要代码:
/// <summary> /// 压缩文件 /// </summary> /// <param name="sourceFilePath">源文件路径</param> /// <param name="destinationPath">压缩文件后的保存路径</param> /// <returns>压缩是否成功</returns> public bool Compress(string sourceFilePath, string destinationPath) { try { string[] filenames = Directory.GetFiles(sourceFilePath); using (ZipOutputStream zs = new ZipOutputStream(File.Create(destinationPath))) { zs.SetLevel(9); byte[] buffer = new byte[4096]; foreach (string file in filenames) { ZipEntry entry = new ZipEntry(Path.GetFileName(file)); entry.DateTime = DateTime.Now; zs.PutNextEntry(entry); using (FileStream fs = File.OpenRead(file)) { int sourceBytes; do { sourceBytes = fs.Read(buffer, 0, buffer.Length); zs.Write(buffer, 0, sourceBytes); } while (sourceBytes > 0); } } zs.Finish(); zs.Flush(); zs.Close(); } } catch (Exception) { return false; } return true; } public bool DeCompress(string sourceFilePath, string destinationPath) { try { using (ZipInputStream zs = new ZipInputStream(File.OpenRead(sourceFilePath))) { ZipEntry entry = null; //解压缩*.rar文件运行至此处出错:Wrong Local header signature: 0x21726152,解压*.zip文件不出错 while ((entry = zs.GetNextEntry()) != null) { string directoryName = Path.GetDirectoryName(entry.Name); string fileName = Path.GetFileName(entry.Name); if (!string.IsNullOrEmpty(fileName)) { using (FileStream streamWriter = File.Create(destinationPath + entry.Name)) { int size = 2048; byte[] data = new byte[size]; while (true) { size = zs.Read(data, 0, data.Length); if (size > 0) { streamWriter.Write(data, 0, size); } else { break; } } } } } } } catch (System.Exception) { return false; } return true; }
如果需解压*.rar的压缩文件在网络也可以找到相关的实现代码,概要代码:
public bool DeCompressRAR(string sourceFilePath, string destinationPath) { try { string SeverDir = @"D:\Program Files\WinRAR";//rar.exe的要目录 Process ProcessDecompression = new Process(); ProcessDecompression.StartInfo.FileName = SeverDir + "\\rar.exe"; Directory.CreateDirectory(sourceFilePath); ProcessDecompression.StartInfo.Arguments = " X " + sourceFilePath + " " + destinationPath; ProcessDecompression.Start(); while (!ProcessDecompression.HasExited) { //nothing to do here. } return true; } catch (System.Exception) { return false; } }
我本想利用FileUpload控件将上传的压缩文件解压后保存至相对应的目录并更新数据库文件目录,后发现一些较好的用于上传的开源软件:如NeatUpload,SWFUpload可以较方便的实现我的需求,遂没有过多纠缠于SharpZipLib,可能关于SharpZipLib的压缩与解压有其它用法,不能被我误导,以上代码是从网络上整合出来的,因为它太过于重复和散乱。
相关文章推荐
- [Asp.net]ZipHelper 在线压缩解压帮助类(SharpZipLib组件实现)
- Asp.Net[C#]在线压缩解压
- Asp.net调用RAR压缩文件与解压文件源码
- Asp.net 2.0 C#实现压缩/解压功能 (示例代码下载)
- asp.net实现GZip压缩和GZip解压
- asp.NET中实现文件的压缩和解压(3种方式)
- ASP.NET MVC 静态资源打包和压缩问题小记
- ASP.NET上传文件或文件夹并进行压缩及解压
- Asp.net 2.0 C#实现压缩/解压功能 (示例代码下载)
- iis 发布asp.net mvc 网站时候js css 压缩问题,图片不加载问题
- 如何解决Asp.Net中不能上传压缩文件的问题
- Asp.net 2.0 C#实现压缩/解压功能
- 实现asp.net的文件压缩、解压、下载
- Asp.net中文件的压缩与解压
- Asp.net 2.0 C#实现压缩/解压功能 (示例代码下载)
- Asp.net调用RAR压缩 解压文件
- Asp.net 2.0 C#实现压缩/解压功能
- asp.net 在线压缩不能执执行,或压缩包为空的问题解决,好气又好笑
- 如何解决Asp.Net中不能上传压缩文件的问题