Java calls 7zip to decompress an instance of the compressed package

preface

Recent projects need to use the function of decompressing the package. The format of the compressed package given by the customer is mainly RAR and zip. Therefore, we intend to use java to call the command line of 7zip to decompress the file. This paper mainly records the implementation process and the problems encountered.

7zip command line

The commands of 7z include several commands, such as adding files to the compressed package, deleting files from the compressed package, extracting files, etc. I won't elaborate here. If you are interested, you can use CMD to run 7z in the installation directory of 7zip Exe to view specific commands.

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:

• X: indicates that the file is extracted from the compressed package

• - AOA: Ao means overwrite mode, and 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

-In fact, I don't quite understand the BS command, which literally means to set the output stream for the output / error / process line. If the - bse1 attribute is not set, when an error occurs during decompression, such as the file cannot be found, the error prompt will appear in the CMD window instead of the generated log file, so the - bse1 command must be added

Java calls 7zip to decompress through CMD

CMD command interpretation

• cmd /c start 7z. Exe indicates that 7z will be started to execute the following commands. After 7z is started, the original CMD window will be closed

• cmd /k start 7z. Exe indicates that 7z will be started to execute the following commands. After 7z is started, the original CMD window will not be closed

• / b means that the application is started, but a new window is not created

Through the above code, you can decompress the compressed package file. 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 not within the scope of this article, and will be discussed later.

Problems encountered

7z. Exe file path not found

This problem occurs because there are spaces in the installation path of 7zip

Solution:

• select the 7zip installation path and do not install it in the path with spaces

• enclose the spaces in the path in double quotation marks, such as C: "program files" \ 7-Zip or "C: \ program files \ 7-Zip" or C: \ program "" files \ 7-Zip "

The file cannot be found because there are consecutive spaces in the file path

The problem is due to

During the execution of the method, multiple consecutive spaces in the file path will be converted into a single space. In this case, it is useless to add double quotes outside the full file path

Solution:

• replace the spaces in the file path. Replace each space with a space with double quotation marks

The above example of Java calling 7zip to decompress the compressed package is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>