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 TestForm
13 
14 {
15     public class ZipHelper
16     {
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-9
28             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。

转载于:https://www.cnblogs.com/lovecsharp094/p/5509093.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值