日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

C#通過cmd調(diào)用7z軟件實(shí)現(xiàn)壓縮和解壓文件_C#教程

作者:農(nóng)碼一生 ? 更新時(shí)間: 2022-06-14 編程語言

壓縮文件:

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, "解壓出錯(cuò)!" };
                }

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

            return resObj;

        }

原文鏈接:https://www.cnblogs.com/wml-it/p/14729429.html

欄目分類
最近更新