java calls 7zip to decompress the compressed package

foreword

  In recent projects, the function of decompressing the package needs to be used. The format of the compressed package given by the customer is mainly rar and zip. Therefore, we plan to use java to call the command line of 7zip to decompress the file. This article mainly records the implementation process and which problems encountered.

7zip command line

7z <command> [<switches>...] <archive_name> [<file_names>...][<@listfiles...>]

  7z's commands include multiple commands such as adding files to the compressed package, deleting files from the compressed package, and extracting files, etc. I will not repeat them here. If you are interested, you can use cmd to run 7z.exe in the 7zip installation directory to view specific command.

  What we need to do today is to use the password to extract the files in the compressed package. The specific commands are as follows:

7z x 压缩文件源路径 -o要解压的路径  -aoa -bse1  -p密码
  • x : means to extract the file from the archive
  • -aoa : ao means overwrite mode, the last a means to directly overwrite the existing file without any prompt
  • -bs < o|e|p > < 0|1|2 >:set output stream for output/error/progress line

I don't really understand the -bs command. The literal meaning is probably to set the output stream for the output/error/process line. If the -bse1 attribute is not set, when there is an error in decompression, such as when the file cannot be found, the error message will be displayed. Appears in the cmd window instead of the generated log file, so the -bse1 command must be added

java calls 7zip decompression through cmd

    String filePath = "F:\\test\\新建 文本 文档.rar";
    //将文件名称中的所有空格都替换为带有双引号的空格
    filePath = filePath.replaceAll(" ", "\" \"");  
    
    String command = "cmd /c start /B D:/工具/7zip/7-Zip/7z.exe x "+filePath+" -oF:/test/ -aoa -bse1 -p123 >F:/test/1.txt";
    System.out.println(command);
    Runtime.getRuntime().exec(command);

cmd command explanation

  • cmd /c start 7z.exe means that 7z will be started to execute the following commands, and the original cmd window will be closed after 7z is started
  • cmd /k start 7z.exe means that 7z will be started to execute the following commands, and the original cmd window will not be closed after 7z is started
  • /B starts the application, but does not create a new window

  Through the above code, the compressed package file can be decompressed. If you want to know whether the decompressed password is correct or whether the compressed package is damaged, you can know by obtaining the content of the generated txt log file. This is beyond the scope of this article and will be discussed later.

problems encountered

7z.exe file path not found

出现该问题是由于7zip的安装路径中存在空格
解决方案:

  • 选择7zip安装路径,不要安装在有空格的路径中
  • 将路径中的空格使用双引号引起来,如C:“Program Files”\7-Zip或者“C:\Program Files\7-Zip”或者C:\Program“ ”Files\7-Zip

文件路径中存在连续多个空格导致文件找不到

出现该问题是由于Runtime.getRuntime().exec(command)方法在执行过程中,会将文件路径中的多个连续空格转换为单个空格,这种情况在文件全路径外面加上双引号也是不管用的
解决方案:

  • 替换文件路径中的空格,将每一个空格都替换为带有双引号的空格即可
原文地址

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324602555&siteId=291194637