C#操作ZIP的方法

本文转自   http://www.111cn.net/net/net/95306.htm

由于本文引用了其他的dll文件,需要下载的请自行前往原博客下载



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ionic.Zip ;
using System.IO;

namespace ZIPUtils_Test
{
   public static  class ZipUtils
    {
       /// <summary>
       /// 得到指定的输入流的ZIP压缩流对象【原有的文件不受影响】
       /// </summary>
       /// <param name="sourceStream">需要压缩的文件的目录</param>
       /// <param name="entryName">默认文件名</param>
       /// <returns></returns>
       public static Stream ZipCompress(Stream sourceStream, string entryName = "zip")
       {
           
           MemoryStream compressedStream = new MemoryStream();
           if (sourceStream != null)
           {
               long sourceOldPosition = 0;
               try
               {
                   sourceOldPosition = sourceStream.Position;
                   sourceStream.Position = 0;
                   using (ZipFile zip = new ZipFile())
                   {
                       //创建一个entryname的在指定目录中的ZIP文件
                       zip.AddEntry(entryName, sourceStream);
                       //保存文件
                       zip.Save(compressedStream);
                       //设置文件位置
                       compressedStream.Position = 0;
                   }
               }
               catch
               {

               }
               finally
               {
                   //如果没有成功,就把文件保存在旧有的位置
                   try { sourceStream.Position = sourceOldPosition; }
                   catch { }
               }
           }
           return compressedStream;
       }
       /// <summary>
       /// 得到指定的字节数组的ZIP解压流对象
       /// 当前方法仅适用只有一个压缩文件的压缩吧,即方法内只取压缩包中的第一个压缩文件
       /// </summary>
       /// <param name="data"></param>
       /// <returns></returns>
       public static Stream ZipDecompress(byte[] data)
       {
           Stream decompressedStream = new MemoryStream();
           if (data != null)
           {
               try
               {
                   MemoryStream dataStream = new MemoryStream(data);
                   using (ZipFile zip = ZipFile.Read(dataStream))
                   {
                       if (zip.Entries.Count > 0)
                       {
                           //解压ZIP文件
                           zip.Entries.First().Extract(decompressedStream);
                           // Extract方法中会操作ms,后续使用时必须先将Stream位置归零,否则会导致后续读取不到任何数据
                           // 返回该Stream对象之前进行一次位置归零动作
                           decompressedStream.Position = 0;
                       }
                   }
               }
               catch { }
           }
           return decompressedStream;
       }
       /// <summary>
       /// 压缩ZIP文件
       /// 支持多文件和多目录,或是多文件和多目录一起压缩
       /// </summary>
       /// <param name="list">待压缩的文件或者目录集合</param>
       /// <param name="strZipName">压缩后的文件名</param>
       /// <param name="isDirStruct">是否按目录结构压缩</param>
       /// <returns></returns>
       public static bool CompressMulti(List<string> list, string strZipName, bool isDirStruct)
       {
           try
           {
               using (ZipFile zip = new ZipFile(Encoding.Default))//设置编码,解决压缩文件时中文乱码的情况
               {
                   foreach (string path in list)
                   {
                       string fileName = Path.GetFileName(path);
                       if (Directory.Exists(path))
                       {
                           if (isDirStruct)//按目录结构压缩
                           {
                               zip.AddDirectory(path, fileName);
                           }
                           else//目录下的文件都压缩到ZIP的根目录
                           {
                               zip.AddDirectory(path);
                           }
                       }
                       if (File.Exists(path))
                       {
                           zip.AddFile(path);
                       }
                   }
                   zip.Save(strZipName);//开始压缩
                   return true;
               }
           }
           catch { return false; }
       }

       /// <summary>
       /// 解压ZIP文件
       /// </summary>
       /// <param name="strZipPath">等待解压的文件</param>
       /// <param name="strUnZipPath">解压的目录</param>
       /// <param name="overWrite">是否覆盖</param>
       /// <returns></returns>
       public static bool Decompression(string strZipPath, string strUnZipPath, bool overWrite)
       {
           try
           {
               ReadOptions options = new ReadOptions();
               options.Encoding = Encoding.Default;//设置编码,解决压缩文件时中文乱码的情况
               using (ZipFile zip = ZipFile.Read(strZipPath, options))
               {
                   foreach (ZipEntry entry in zip)
                   {
                       if (string.IsNullOrEmpty(strUnZipPath))
                       {
                           strUnZipPath = strZipPath.Split('.').First();
                       }
                       //判断是否覆盖
                       if (overWrite)
                       {
                           entry.Extract(strUnZipPath, ExtractExistingFileAction.OverwriteSilently);//解压文件,如果已存在就覆盖
                       }
                       else
                       {
                           entry.Extract(strUnZipPath, ExtractExistingFileAction.DoNotOverwrite);//解压文件,如果已存在不覆盖
                       }
                   }
                   return true;
               }
           }
           catch (Exception)
           {
               return false;
           }
       }

    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值