星空网 > 软件开发 > ASP.net

C# 实现压缩文件的方法以及故障排除

C#压缩文件可以使用第三方dll库:ICSharpCode.SharpZipLib.dll;

以下代码能实现文件夹与多个文件的同时压缩。(例:把三个文件夹和五个文件一起压缩成一个zip)

直接上代码,代码来自:http://blog.csdn.net/jk007/article/details/8115825

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.IO; 6 using System.Diagnostics; 7 using ICSharpCode.SharpZipLib; 8 using ICSharpCode.SharpZipLib.Zip; 9 using ICSharpCode.SharpZipLib.Checksums;10 using ICSharpCode.SharpZipLib.Core;11 12 namespace TestForm13 14 {15   public class ZipHelper16   {17     /// <summary>18     /// 压缩文件19     /// </summary>20     /// <param name="sourceFilePath"></param>21     /// <param name="destinationZipFilePath"></param>22     public static void CreateZip(string sourceFilePath, string destinationZipFilePath)23     {24       if (sourceFilePath[sourceFilePath.Length - 1] != System.IO.Path.DirectorySeparatorChar)25         sourceFilePath += System.IO.Path.DirectorySeparatorChar;26       ZipOutputStream zipStream = new ZipOutputStream(File.Create(destinationZipFilePath));27       zipStream.SetLevel(6); // 压缩级别 0-928       CreateZipFiles(sourceFilePath, zipStream);29       zipStream.Finish();30       zipStream.Close();31     }32     /// <summary>33     /// 递归压缩文件34     /// </summary>35     /// <param name="sourceFilePath">待压缩的文件或文件夹路径</param>36     /// <param name="zipStream">打包结果的zip文件路径(类似 D:\WorkSpace\a.zip),全路径包括文件名和.zip扩展名</param>37     /// <param name="staticFile"></param>38     private static void CreateZipFiles(string sourceFilePath, ZipOutputStream zipStream)39     {40       Crc32 crc = new Crc32();41       string[] filesArray = Directory.GetFileSystemEntries(sourceFilePath);42       foreach (string file in filesArray)43       {44         if (Directory.Exists(file))           //如果当前是文件夹,递归45         {46           CreateZipFiles(file, zipStream);47         }48         else                      //如果是文件,开始压缩49         {50           FileStream fileStream = File.OpenRead(file);51           byte[] buffer = new byte[fileStream.Length];52           fileStream.Read(buffer, 0, buffer.Length);53           string tempFile = file.Substring(sourceFilePath.LastIndexOf("\\") + 1);54           ZipEntry entry = new ZipEntry(tempFile);55           entry.DateTime = DateTime.Now;56           entry.Size = fileStream.Length;57           fileStream.Close();58           crc.Reset();59           crc.Update(buffer);60           entry.Crc = crc.Value;61           zipStream.PutNextEntry(entry);62           zipStream.Write(buffer, 0, buffer.Length);63         }64       }65     }66   }67 }

运行时可能发生报错,断点不能进入该类中的函数,故障信息为不能加载该程序集。

故障分析:
1. 在下载dll文件后切不可在工程外部直接引用dll,把其放在自己工程的bin目录下。

2. 注意该dll的版本,可能是32位的,可能是64位的,那么在VS的生成中就要设置相应的目标平台。32位对应于X86,64位对应X64。

C# 实现压缩文件的方法以及故障排除




原标题:C# 实现压缩文件的方法以及故障排除

关键词:C#

C#
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

出口到菲律宾:https://www.goluckyvip.com/tag/96086.html
菲律宾 快运:https://www.goluckyvip.com/tag/96087.html
宁波海运菲律宾:https://www.goluckyvip.com/tag/96088.html
货物出口菲律宾:https://www.goluckyvip.com/tag/96089.html
NEGG:https://www.goluckyvip.com/tag/9609.html
菲律宾的港口:https://www.goluckyvip.com/tag/96090.html
长治婚庆女司仪和主持人:https://www.vstour.cn/a/366176.html
北京丰台区水上乐园哪家好玩?:https://www.vstour.cn/a/366177.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流