zoukankan      html  css  js  c++  java
  • C#中用SharpZipLib.dll实现压缩解压2

    /*******************************************************
    版权所有:
    用    途:文件压缩类

    结构组成:

    说    明:靠调用CSharpCode.SharpZipLib.dll类库来实现对文件的压缩和解压。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using ICSharpCode.SharpZipLib.Zip;
    using ICSharpCode.SharpZipLib.Checksums;
    using System.Threading;

    namespace Framework.Utility.FileHelper
    {
        class SharpZipHelper
        {
            #region 压缩指定文件生成ZIP文件
            /// <summary>
            /// 压缩指定文件生成ZIP文件
            /// </summary>
            /// <param name="fileNamesToZip">待压缩文件列表(绝对路径)</param>
            /// <param name="ZipedFileName">ZIP文件(绝对路径)</param>
            /// <param name="CompressionLevel">压缩比</param>
            /// <param name="password">密码</param>
            /// <param name="comment">压缩文件注释文字</param>
            /// <returns>返回错误信息</returns>
            public static string ZipFile(string[] fileNamesToZip, string ZipedFileName, int CompressionLevel, string password, string comment)
            {
                try
                {
                    System.Text.RegularExpressions.Regex regPath = new System.Text.RegularExpressions.Regex(@"^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w   ]*.*))");
                    if (!regPath.Match(ZipedFileName).Success)
                    {
                        File.Delete(ZipedFileName);
                        return "压缩文件的路径不正确!";
                    }
                    ZipOutputStream s = new ZipOutputStream(File.Open(ZipedFileName, FileMode.OpenOrCreate));
                    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)
                    {
                        FileStream fs = File.OpenRead(file);    //打开待压缩文件   
                        if (!regPath.Match(ZipedFileName).Success)
                        {
                            File.Delete(ZipedFileName);
                            return "待压缩的文件路径不正确!";
                        }
                        byte[] buffer = new byte[fs.Length];
                        fs.Read(buffer, 0, buffer.Length);      //读取文件流   
                        ZipEntry entry = new ZipEntry(Path.GetFileName(file));    //新建实例   

                        entry.DateTime = DateTime.Now;

                        entry.Size = fs.Length;
                        fs.Close();

                        s.PutNextEntry(entry);
                        s.Write(buffer, 0, buffer.Length);
                    }
                    s.Finish();
                    s.Close();
                }
                catch(Exception err)
                {
                    File.Delete(ZipedFileName);
                    return err.Message;
                }
                return "";
            }
            public static void ZipFile(string[] fileNamesToZip, string ZipedFileName, int CompressionLevel, string password)
            {
                ZipFile(fileNamesToZip, ZipedFileName, CompressionLevel, password, null);
            }
            public static void ZipFile(string[] fileNamesToZip, string ZipedFileName, int CompressionLevel)
            {
                ZipFile(fileNamesToZip, ZipedFileName, CompressionLevel, null);
            }
            #endregion

            #region UnZipFile:解压缩ZIP文件到指定文件夹
            /// <summary>   
            /// 解压缩ZIP文件到指定文件夹   
            /// </summary>   
            /// <param name="zipfileName">ZIP文件(物理路径)</param>   
            /// <param name="UnZipDir">解压文件夹(物理路径)</param>   
            /// <param name="password">压缩文件密码</param> 
            /// <returns>返回错误信息</returns>
            public static string UnZipFile(string zipfileName, string UnZipDir, string password)
            {
                if (!File.Exists(zipfileName))
                    return "待解压的文件路径不存在!";
                ZipInputStream s = new ZipInputStream(File.OpenRead(zipfileName));
                if (password != null && password.Length > 0)
                    s.Password = password;
                try
                {
                    ZipEntry theEntry;
                    while ((theEntry = s.GetNextEntry()) != null)
                    {
                        if (Directory.Exists(UnZipDir))
                        {
                            Directory.CreateDirectory(UnZipDir);
                        }
                        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)
                        {
                            //解压文件到指定的目录   
                            FileStream 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.Close();
                        }
                    }
                    s.Close();
                }
                catch (Exception eu)
                {
                    return eu.Message;
                }
                finally
                {
                    s.Close();
                }
                return "";
            }

            public static void UnZipFile(string zipfileName, string UnZipDir)
            {
                UnZipFile(zipfileName, UnZipDir, null);
            }
            #endregion
        }
    }
  • 相关阅读:
    ICONS-图标库
    图形资源
    vue项目中,如果修改了组件名称,vscode编辑器会在引入修改组件的名字处提示红色波浪线 The file is in the program because:Imported via xxx Root file specified for compilation .
    接口在dev环境报跨域问题(has been blocked by CORS policy:Response to preflight request doesn't pass access control check:No 'Access-Control-Allow-Origin' header ispresent on the requested resource.),qa环境正常
    阿里云occ的图片文件URL用浏览器直接打开无法访问,提示This XML file does noe appear to have any style information associated with it. The document tree is shown below.
    vue 项目使用element ui 中tree组件 check-strictly 用法(父子不互相关联的反显情况)
    高德地图进行线路规划绘制标记点操作(vue)
    vue中实现拖拽调整顺序功能
    2021-01-22 浏览器相关知识
    2021-01-22 js 相关知识点
  • 原文地址:https://www.cnblogs.com/aaa6818162/p/1509478.html
Copyright © 2011-2022 走看看