栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

c#解压文件的实例方法

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c#解压文件的实例方法

复制代码 代码如下:
#region 解压 文件 zip 格式 rar 格式
        ///
        ///解压文件
        ///

        /// 解压前的文件路径(绝对路径)
        /// 解压后的文件目录(绝对路径)
        public static void UnpackFile(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;
            }
        }
        //解压rar格式的文件
        private static void UnRar(string fileFromUnZip, string fileToUnZip)
        {
            using (Process Process1 = new Process())// 开启一个进程 执行解压工作
            {
          string ServerDir = ConfigurationManager.AppSettings["UnpackFile"].ToString();//rar工具的安装路径   必须要安装 WinRAR     //例于:C:Program Files (x86)WinRARRAR.exe
                Process1.StartInfo.UseShellExecute = false;
                Process1.StartInfo.RedirectStandardInput = true;
                Process1.StartInfo.RedirectStandardOutput = true;
                Process1.StartInfo.RedirectStandardError = true;
                Process1.StartInfo.CreateNoWindow = true;
                Process1.StartInfo.FileName = ServerDir;
                Process1.StartInfo.Arguments = " x -inul -y " + fileFromUnZip + " " + fileToUnZip;
                Process1.Start();//解压开始 
                Process1.WaitForExit();
                Process1.Close();
            }
        }
        // 解压zip 文件
        public static void UnZip(string fileFromUnZip, string fileToUnZip)
        {
            ZipInputStream inputStream = new ZipInputStream(File.OpenRead(fileFromUnZip));
            ZipEntry theEntry;
            while ((theEntry = inputStream.GetNextEntry()) != null)
            {
                fileToUnZip += "/";
                string fileName = Path.GetFileName(theEntry.Name);
                string path = Path.GetDirectoryName(fileToUnZip) + "/";
                // Directory.CreateDirectory(path);//生成解压目录
                if (fileName != String.Empty)
                {
                    FileStream streamWriter = File.Create(path + fileName);//解压文件到指定的目录
                    int size = 2048;
                    byte[] data = new byte[2048];
                    while (true)
                    {
                        size = inputStream.Read(data, 0, data.Length);
                        if (size > 0)
                        {
                            streamWriter.Write(data, 0, size);
                        }
                        else
                        {
                            break;
                        }
                    }
                    streamWriter.Close();
                }
            }
            inputStream.Close();
        }
        #endregion
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/127333.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号