SoFunction
Updated on 2025-03-07

C# calls 7z to implement file compression and decompression

1. About 7z

First of all, let me introduce 7z compression software here. 7z is a mainstream compression format and has an extremely high compression ratio. In computer science, 7z is an archive format that can use multiple compression algorithms for data compression. It has the following main characteristics:

  • Source and modular component structure
  • The highest compression ratio
  • Powerful AES-256 encryption
  • The compression algorithm that can be changed
  • Support large files
  • Supports multi-threaded compression
  • With multiple compressed file formats

2. Decompress the implementation code

The method to decompress the file is to decompress and compress the file through the cmd command by calling the 7z program through the cmd command. The specific implementation code is as follows:

  • Compressed code

Compressed cmd command: "7Z a -tzip " + zipPath + "  " + filePath;

public ExecutionResult CompressFile(string filePath, string zipPath)//Run DOS command    {
      ExecutionResult exeRes = new ExecutionResult();
       = true;
      try
      {
        Process process = new Process();
         = "";
        string message = "";
        string command1 = "c:";
        string command2 = @"cd\";
        string command3 = @"cd C:\Progra~1\7-Zip";
        string command4 = "";


        command4 = "7Z a -tzip " + zipPath + " " + filePath;

         = false;
         = true;
         = true;
         = true;
         = true;
        ();
        (command1);
        (command2);
        (command3);
        (command4);
        ("exit");
        message = (); //You can only grab this compressed file after the compression is completed
        ();
        if (!("Everything is Ok"))
        {
           = false;
           = message;
        }
        else
        {
           = zipPath;
        }
      }
      catch (Exception ex)
      {
         = ;
      }

      return exeRes;
    }
  • Unzip the code

Unzipped cmd command: "7Z x -tzip " + zipPath + " -o" + filePath + " -y";

public ExecutionResult DeCompressFile( string zipPath, string filePath)//Run DOS command    {
      ExecutionResult exeRes = new ExecutionResult();
       = true;
      try
      {
        Process process = new Process();
         = "";
        string message = "";
        string command1 = "c:";
        string command2 = @"cd\";
        string command3 = @"cd C:\Progra~1\7-Zip";
        string command4 = "";


        command4 = "7Z x -tzip " + zipPath + " -o" + filePath + " -y";

         = false;
         = true;
         = true;
         = true;
         = true;
        ();
        (command1);
        (command2);
        (command3);
        (command4);
        ("exit");
        //();
        message = ();//You can only grab this compressed file after the compression is completed
        ();
        if (!("Everything is Ok"))
        {
           = false;
           = message;
        }
        else
        {
           = filePath;
        }
      }
      catch (Exception ex)
      {
         = ;
      }

      return exeRes;
    }

The above is the detailed content of C# calling 7z to implement file compression and decompression. For more information about C# file compression and decompression, please pay attention to my other related articles!