Java implements a tool class for compressing files into zip

Recently, I came across a need to download the zip package, so I found the zip tool class written by others on the Internet. But I found many blogs and always found bugs. So I wrote a tool class myself.

The functions of this tool class are:

(1) Files and folders can be compressed (2) multi-level folders can be compressed at the same time. Recursive processing is done inside the tool (3) empty folders can also be compressed (4) You can choose whether to keep the original directory structure. If not, all files will go to the root directory of the compression package, and the empty folder will be discarded directly. Note: if you do not keep the original directory structure of the file, the compression will fail when you encounter a file with the same file name. (5) The code provides two methods to compress files. One is the folder path and the other is the file list. You can select the method according to the actual needs.

Here's the code

1、 Code

2、 Precautions

When writing this tool class, some precautions are as follows:

(1) Support the selection of whether to keep the original file directory structure. If not, the empty folder will not be processed directly. (2) when encountering an empty folder, if you need to keep the directory structure, you can directly add a zipentry, but the name of the entry needs to be followed by a slash (/) to indicate that it is a directory. (3) During recursion, it is not necessary to close the zip output stream. The zip output stream should be closed after calling the recursion method. (4) during recursion, if it is a folder and the directory structure needs to be preserved, when calling the method to compress its sub files, it is necessary to add a slash to the name of the folder in front of the name of the sub files, so that there will be multi-level directories after compression.

3、 How to use this tool class in Java Web projects

The usage scenario of this tool class in web projects is multi file download. Let me simply say a case of downloading multiple excel tables.

The steps in the code are:

(1) Create a temporary folder

(2) Generate the files to be downloaded into the temporary folder

(3) After all files are generated, get the httpservletresponse and set the header for download

(4) Call the method of the tool class, pass in the temporary folder path generated above and the output stream obtained by the response; in this way, the zip package will be downloaded

(5) Recursively delete the temporary folders and files generated above

The following is a code fragment of the sample code, not the complete code. Take a brief look at the steps in the code

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support 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
分享
二维码
< <上一篇
下一篇>>