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

压缩文件:

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软件实现压缩和解压文件的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持编程宝库

最近做了一个项目,使用不干胶标签贴在RFID抗金属标签上,那么就会出现标签打印的问题,该如何打印呢?后来经过网上冲浪发现,其实打印标签和打印A4纸的方法一样,只不过就是布局、设置纸张大小 ...