广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >C#通过cmd调用7z软件实现压缩和解压文件
  • 257
分享到

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

2024-04-02 19:04:59 257人浏览 独家记忆
摘要

压缩文件: public object CompressZipFile(string sourceFile, string destinationFile) {

压缩文件:

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;

        }

到此这篇关于C#通过cmd调用7z软件实现压缩和解压文件的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持编程网。

--结束END--

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

本文链接: https://www.lsjlt.com/news/145996.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作