zoukankan      html  css  js  c++  java
  • C# 利用ICSharpCode.SharpZipLib.dll 实现压缩和解压缩文件

    我们 开发时经常会遇到需要压缩文件的需求,利用C#的开源组件ICSharpCode.SharpZipLib,

    就可以很容易的实现压缩和解压缩功能.

    压缩文件:

     /// <summary>  
            /// 压缩指定文件生成ZIP文件  
            /// </summary>  
            /// <param name="topDirName">顶层文件夹名称</param>  
            /// <param name="fileNamesToZip">待压缩文件列表</param>  
            /// <param name="ZipedFileName">ZIP文件</param>  
            /// <param name="CompressionLevel">压缩比</param>  
            /// <param name="password">密码</param>  
            /// <param name="comment">压缩文件注释文字</param>  
            public static void ZipFile
                (
                string topDirName,   
                string fileNameToZip,   
                string ZipedFileName,    
                int CompressionLevel,   
                string password,     
                string comment    
                )
    
            {
                var ls = new List<string> { fileNameToZip };
                ZipFile(topDirName, ls.ToArray(), ZipedFileName, CompressionLevel, password, comment);
            }
            /// <summary>  
            /// 压缩指定文件生成ZIP文件  
            /// </summary>  
            /// <param name="topDirName">顶层文件夹名称</param>  
            /// <param name="fileNamesToZip">待压缩文件列表</param>  
            /// <param name="ZipedFileName">ZIP文件</param>  
            /// <param name="CompressionLevel">压缩比</param>  
            /// <param name="password">密码</param>  
            /// <param name="comment">压缩文件注释文字</param>  
            public static void ZipFile
                (
                string topDirName,   
                string[] fileNamesToZip,   
                string ZipedFileName,    
                int CompressionLevel,    
                string password,     
                string comment    
                )
            {
                using (var s = new ZipOutputStream(File.Open(ZipedFileName, FileMode.Create)))
                {
                    if (password != null && password.Length > 0)
                        s.Password = password;
    
                    if (comment != null && comment.Length > 0)
                        s.SetComment(comment);
    
                    s.SetLevel(CompressionLevel); // 0 - means store only to 9 - means best compression  
    
                    foreach (string file in fileNamesToZip)
                    {
                        using (FileStream fs = File.OpenRead(topDirName + file))
                        {   //打开待压缩文件  
                            byte[] buffer = new byte[fs.Length];
                            fs.Read(buffer, 0, buffer.Length);      //读取文件流  
                            ZipEntry entry = new ZipEntry(file);    //新建实例   
                            entry.DateTime = DateTime.Now;
                            entry.Size = fs.Length;
                            s.PutNextEntry(entry);
                            s.Write(buffer, 0, buffer.Length);
                        }
                    }
                    s.Finish();
                }
    
    
            }
    

    解压文件:

     /// <summary>  
            /// 解压缩ZIP文件到指定文件夹  
            /// </summary>  
            /// <param name="zipfileName">ZIP文件</param>  
            /// <param name="UnZipDir">解压文件夹</param>  
            /// <param name="password">压缩文件密码</param>  
            public static void UnZipFile(string zipfileName, string UnZipDir, string password)
            {
                using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipfileName)))
                {
                    if (!string.IsNullOrWhiteSpace(password))
                        s.Password = password;
                    try
                    {
                        ZipEntry theEntry;
                        while ((theEntry = s.GetNextEntry()) != null)
                        {
                            string directoryName = Path.GetDirectoryName(UnZipDir);
                            string pathname = Path.GetDirectoryName(theEntry.Name);
                            string fileName = Path.GetFileName(theEntry.Name);
    
                            //生成解压目录   
                            pathname = pathname.Replace(":", "$");//处理压缩时带有盘符的问题  
                            directoryName = directoryName + "\" + pathname;
                            Directory.CreateDirectory(directoryName);
    
                            if (fileName != String.Empty)
                            {
                                //解压文件到指定的目录  
                                using (var streamWriter = File.Create(directoryName + "\" + fileName))
                                {
                                    int size = 2048;
                                    byte[] data = new byte[2048];
                                    while (true)
                                    {
                                        size = s.Read(data, 0, data.Length);
                                        if (size > 0)
                                        {
                                            streamWriter.Write(data, 0, size);
                                        }
                                        else
                                        {
                                            break;
                                        }
                                    }
                                    streamWriter.Flush();
                                }
                            }
                        }
                    }
                    catch (Exception eu)
                    {
                        throw eu;
                    }
                }
            }
    

      

    本文引用:http://xqblog.top/Article.aspx?id=ART2018030900002

  • 相关阅读:
    vmware:使用.zip文件在vmware中安装操作系统
    Nodejs:Node.js模块机制小结
    vue:vue router学习小结
    axios使用思路总结
    vuex:使用思路总结
    React的keepAlive路由缓存的一种实现思路
    Echarts的一些用法
    gojs去水印的方法
    平面坐标与经纬度坐标的相互转换
    HTML5 添加水印
  • 原文地址:https://www.cnblogs.com/xqaizx/p/8533324.html
Copyright © 2011-2022 走看看