欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

C#通过cmd调用7z软件实现压缩和解压文件

时间:2023-03-05 12:21:20|栏目:.NET代码|点击:

压缩文件:

public object CompressZipFile(string sourceFile, string destinationFile)
        {
            object resObj;
            Process process = new Process();
            string tempDestinationFile = "";

            try
            {
                if (process == null)
                {
                    process = new Process();
                }
                tempDestinationFile = destinationFile.ToLower().EndsWith(".zip") ? destinationFile : destinationFile + ".zip";
                process.StartInfo.FileName = "cmd.exe";
                string command1 = @"cd c:\progra~1\7-zip";
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.RedirectStandardInput = true;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError = true;
                process.StartInfo.CreateNoWindow = true;
                process.Start();
                process.StandardInput.WriteLine(@"c:");
                process.StandardInput.WriteLine(@"cd\");
                process.StandardInput.WriteLine(command1);
                process.StandardInput.WriteLine(@"7Z a -tzip " + destinationFile + " " + sourceFile);
                process.StandardInput.WriteLine("exit");

                string output = process.StandardOutput.ReadToEnd();
                if (output.ToUpper().IndexOf("EVERYTHING IS OK") > -1)
                {
                    resObj = new object[] { 0, "Compress file[" + destinationFile + "] OK" };
                }
                else
                {
                    resObj = new object[] { 1, "Compress File[" + destinationFile + "] Error!" };
                }

            }
            catch (Exception ex)
            {
                resObj = new object[] { 1, "Compress File[" + destinationFile + "] exception:" + ex.Message };

            }
            return resObj;
        }

解压文件:

public object decompressFile(string zipFilepath, string FileDir)
        {
            object resObj;
            try
            {
                string batfiledata = @"c:\progra~1\7-zip\7z.exe x " + zipFilepath + " -o" + FileDir + " -y";

                Process process = new Process();
                process.StartInfo.FileName = "cmd.exe";
                process.StartInfo.Arguments = "/c " + batfiledata;
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.RedirectStandardInput = true;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError = true;
                process.StartInfo.CreateNoWindow = true;
                process.Start();

                string output = process.StandardOutput.ReadToEnd();
                if (output.ToUpper().IndexOf("EVERYTHING IS OK") > -1)
                {
                    resObj = new object[] { 0, "解压完成" };
                }
                else
                {
                    resObj = new object[] { 1, "解压出错!" };
                }

            }
            catch (Exception ex)
            {
                resObj = new object[] { 1, ex.Message };
            }

            return resObj;

        }

上一篇:为WPF框架Prism注册Nlog日志服务

栏    目:.NET代码

下一篇:WPF路由事件中的三种策略介绍

本文标题:C#通过cmd调用7z软件实现压缩和解压文件

本文地址:http://www.codeinn.net/misctech/226896.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有