zoukankan      html  css  js  c++  java
  • 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:WorkSpacea.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。

  • 相关阅读:
    luogu P3128 [USACO15DEC]最大流Max Flow (树上差分)
    codeforces 600E . Lomsat gelral (线段树合并)
    bzoj 1483: [HNOI2009]梦幻布丁 (链表启发式合并)
    bzoj 1257: [CQOI2007]余数之和 (数学+分块)
    codevs 2606 约数和问题 (数学+分块)
    bzoj 2038: [2009国家集训队]小Z的袜子(hose) (莫队)
    bzoj 1086: [SCOI2005]王室联邦 (分块+dfs)
    bzoj 4542: [Hnoi2016]大数 (莫队)
    【NOIp模拟赛】Tourist Attractions
    【NOIp模拟赛】String Master
  • 原文地址:https://www.cnblogs.com/lovecsharp094/p/5509093.html
Copyright © 2011-2022 走看看