• 大小: 9.22M
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2020-12-26
  • 语言: C#
  • 标签: 解压缩  IP  C#  压缩  解压  

资源简介

DotnetZip是一个开源类库,支持.NET的任何语言,可很方便的创建,读取,和更新zip文件。而且还可以使用在.NETCompact Framework中。

下载地址在这里:

http://dotnetzip.codeplex.com/

下载到的包里有很多个dll文件,一般引用Ionic.Zip.dll就可以:

然后引用这个命名空间:

using Ionic.Zip;

以下是我自己封装的一个类:

/// <summary>
    /// Zip操作基于 DotNetZip 的封装
    /// </summary>
    public static class ZipUtils
    {
        /// <summary>
        /// 得到指定的输入流的ZIP压缩流对象【原有流对象不会改变】
        /// </summary>
        /// <param name="sourceStream"></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())
                    {
                        zip.AddEntry(entryName, sourceStream);
                        zip.Save(compressedStream);
                        compressedStream.Position = 0;
                    }
                }
                catch
                {
                }
                finally
                {
                    try
                    {
                        sourceStream.Position = sourceOldPosition;
                    }
                    catch
                    {
                    }
                }
            }
            return compressedStream;
        }
 
 
        /// <summary>
        /// 得到指定的字节数组的ZIP解压流对象
        /// 当前方法仅适用于只有一个压缩文件的压缩包,即方法内只取压缩包中的第一个压缩文件
        /// </summary>
        /// <param name="sourceStream"></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.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>成功:true/失败:false</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 (Exception)
            {
                return false;
            }
        }
 
        /// <summary>
        /// 解压ZIP文件
        /// </summary>
        /// <param name="strZipPath">待解压的ZIP文件</param>
        /// <param name="strUnZipPath">解压的目录</param>
        /// <param name="overWrite">是否覆盖</param>
        /// <returns>成功:true/失败:false</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;
            }
        }
 
 
    }

使用方法:

1.压缩文件

  List<string> list = new List<string>(); 
  list.Add(@"D:\Test\ss"); 
  list.Add(@"D:\Test\test1.jpg"); 
  list.Add(@"D:\公司文件.txt"); 
  list.Add(@"D:\Test\ss.xml");  
bool isSuc =ZipUtils. CompressMulti(list, "D:\\Test2.zip",true);

2.解压文件

  bool isSuc = ZipUtils.Decompression("D:\\Test\\Test1.zip", "D:\\Teest", true);

更详细的例子在这里:

http://dotnetzip.codeplex.com/wikipage?title=Examples&referringTitle=Home

资源截图

代码片段和文件信息

using System.Reflection;
using System.Security;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[assembly: AssemblyCompany(“Dino Chiesa“)]
[assembly: AssemblyProduct(“DotNetZip Library examples“)]
[assembly: AssemblyCopyright(“Copyright © Dino Chiesa 2006 - 2011“)]
[assembly: AssemblyTrademark(““)]
[assembly: AssemblyCulture(““)]


[assembly: AssemblyVersion(“1.9.1.8“)]

#if !NETCF
[assembly: AssemblyFileVersion(“1.9.1.8“)]
// workitem 4698
[assembly: AllowPartiallyTrustedCallers]
#endif

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件         834  2011-08-07 10:16  Contents.txt
     文件         154  2011-08-07 10:16  PleaseDonate.txt
     文件       40478  2011-07-31 03:36  Readme.txt
     文件        2705  2011-05-25 02:25  License.txt
     文件        3205  2011-06-13 21:32  License.zlib.txt
     文件        1200  2011-07-27 22:31  License.bzip2.txt
     文件         296  2011-08-07 10:16  zip-v1.9\Readme.txt
     文件      492032  2011-08-07 09:59  zip-v1.9\Debug\Ionic.Zip.dll
     文件      909019  2011-08-07 09:59  zip-v1.9\Debug\Ionic.Zip.xml
     文件      765440  2011-08-07 09:59  zip-v1.9\Debug\Ionic.Zip.pdb
     文件      462336  2011-08-07 10:01  zip-v1.9\Release\Ionic.Zip.dll
     文件         422  2011-08-07 10:16  zip-v1.9-Reduced\Readme.txt
     文件      283136  2011-08-07 10:00  zip-v1.9-Reduced\Debug\Ionic.Zip.Reduced.dll
     文件      691712  2011-08-07 10:00  zip-v1.9-Reduced\Debug\Ionic.Zip.Reduced.pdb
     文件      909027  2011-08-07 10:00  zip-v1.9-Reduced\Debug\Ionic.Zip.Reduced.xml
     文件      253440  2011-08-07 10:01  zip-v1.9-Reduced\Release\Ionic.Zip.Reduced.dll
     文件         468  2011-08-07 10:16  zip-v1.9-Compactframework\Readme.txt
     文件      230912  2011-08-07 10:00  zip-v1.9-Compactframework\Debug\Ionic.Zip.CF.dll
     文件      579072  2011-08-07 10:00  zip-v1.9-Compactframework\Debug\Ionic.Zip.CF.pdb
     文件      206336  2011-08-07 10:01  zip-v1.9-Compactframework\Release\Ionic.Zip.CF.dll
     文件         220  2011-08-07 10:16  zip-v1.9-Silverlight\Readme.txt
     文件      255488  2011-08-07 10:00  zip-v1.9-Silverlight\Debug\Ionic.Zip.dll
     文件      648704  2011-08-07 10:00  zip-v1.9-Silverlight\Debug\Ionic.Zip.pdb
     文件      866016  2011-08-07 10:00  zip-v1.9-Silverlight\Debug\Ionic.Zip.xml
     文件      228352  2011-08-07 10:01  zip-v1.9-Silverlight\Release\Ionic.Zip.dll
     文件         317  2011-08-07 10:17  zlib-v1.9\Readme.txt
     文件      102400  2011-08-07 10:00  zlib-v1.9\Debug\Ionic.Zlib.dll
     文件      206336  2011-08-07 10:00  zlib-v1.9\Debug\Ionic.Zlib.pdb
     文件      160967  2011-08-07 10:00  zlib-v1.9\Debug\Ionic.Zlib.xml
     文件      102400  2011-08-07 10:01  zlib-v1.9\Release\Ionic.Zlib.dll
     文件         337  2011-08-07 10:17  zlib-v1.9-Compactframework\Readme.txt
............此处省略144个文件信息

评论

共有 条评论