• 文件解压缩公用类


    using System;
    using System.Collections.Generic;
    using System.Text;
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Configuration;
    using System.IO;
    using System.IO.Compression;
    using ICSharpCode.SharpZipLib.Zip;
    using Microsoft.Win32;


    namespace RST_ReadSDData.APP_Code
    {
       public class UnPackFile
        {
            public UnPackFile()
            {
                //
                //TODO: 在此处添加构造函数逻辑
                //
            }


            #region  解压文件 包括.rar 和zip

            /// <summary>
            ///解压文件
            /// </summary>
            /// <param name="fileFromUnZip">解压前的文件路径(绝对路径)</param>
            /// <param name="fileToUnZip">解压后的文件目录(绝对路径)</param>
            public static void UnpackFileRarOrZip(string fileFromUnZip, string fileToUnZip)
            {
                //获取压缩类型
                string unType = fileFromUnZip.Substring(fileFromUnZip.LastIndexOf(".") + 1, 3).ToLower();
                switch (unType)
                {
                    case "rar":
                        UnRar(fileFromUnZip, fileToUnZip);
                        break;
                    case "zip":
                        UnZip(fileFromUnZip, fileToUnZip);
                        break;
                }
            }


            #endregion



            #region  解压文件 .rar文件

            /// <summary>
            /// 解压
            /// </summary>
            /// <param name="unRarPatch"></param>
            /// <param name="rarPatch"></param>
            /// <param name="rarName"></param>
            /// <returns></returns>
            public static void UnRar(string fileFromUnZip, string fileToUnZip)
            {
                string the_rar;
                RegistryKey the_Reg;
                object the_Obj;
                string the_Info;

                try
                {
                    the_Reg = Registry.LocalMachine.OpenSubKey(
                             @"SOFTWAREMicrosoftWindowsCurrentVersionApp PathsWinRAR.exe");
                    the_Obj = the_Reg.GetValue("");
                    the_rar = the_Obj.ToString();
                    the_Reg.Close();
                    //the_rar = the_rar.Substring(1, the_rar.Length - 7);

                    if (Directory.Exists(fileToUnZip) == false)
                    {
                        Directory.CreateDirectory(fileToUnZip);
                    }
                    the_Info = "x " + Path.GetFileName(fileFromUnZip) + " " + fileToUnZip + " -y";

                    ProcessStartInfo the_StartInfo = new ProcessStartInfo();
                    the_StartInfo.FileName = the_rar;
                    the_StartInfo.Arguments = the_Info;
                    the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    the_StartInfo.WorkingDirectory = Path.GetDirectoryName(fileFromUnZip);//获取压缩包路径

                    Process the_Process = new Process();
                    the_Process.StartInfo = the_StartInfo;
                    the_Process.Start();
                    the_Process.WaitForExit();
                    the_Process.Close();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                //return unRarPatch;
            }

            #endregion



            #region  解压文件 .zip文件

            /// <summary>
            /// 解压功能(解压压缩文件到指定目录)
            /// </summary>
            /// <param name="FileToUpZip">待解压的文件</param>
            /// <param name="ZipedFolder">指定解压目标目录</param>
            public static void UnZip(string FileToUpZip, string ZipedFolder)
            {
                if (!File.Exists(FileToUpZip))
                {
                    return;
                }

                if (!Directory.Exists(ZipedFolder))
                {
                    Directory.CreateDirectory(ZipedFolder);
                }

                ZipInputStream s = null;
                ZipEntry theEntry = null;

                string fileName;
                FileStream streamWriter = null;
                try
                {
                    s = new ZipInputStream(File.OpenRead(FileToUpZip));
                    while ((theEntry = s.GetNextEntry()) != null)
                    {

                        if (theEntry.Name != String.Empty)
                        {
                            fileName = Path.Combine(ZipedFolder, theEntry.Name);
                            ///判断文件路径是否是文件夹

                            if (fileName.EndsWith("/") || fileName.EndsWith("\"))
                            {
                                Directory.CreateDirectory(fileName);
                                continue;
                            }

                            streamWriter = File.Create(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;
                                }
                            }
                        }
                    }
                }
                finally
                {
                    if (streamWriter != null)
                    {
                        streamWriter.Close();
                        streamWriter = null;
                    }
                    if (theEntry != null)
                    {
                        theEntry = null;
                    }
                    if (s != null)
                    {
                        s.Close();
                        s = null;
                    }
                    GC.Collect();
                    GC.Collect(1);
                }
            }


            #endregion
        }
    }

  • 相关阅读:
    Docker 常用命令及操作
    Docker 启动 tomcat 成功,但是访问为 404
    Linux 使用 yum install 安装程序时,提示另外一个程序锁定了 yum 等待它退出...
    Centos 6.8 安装 Docker 及 Docker 入门介绍
    iptables常见的模块拓展-m iprange 、-m multiport、
    iptables -m state模块 注意与-m conntrack的区别
    ros single network adapter的nat
    trap 信号捕捉
    ifconfig命令
    使用pipework给docker配置物理机器网卡上的地址
  • 原文地址:https://www.cnblogs.com/haofaner/p/3767788.html
Copyright © 2020-2023  润新知