Downloading a compressed file
The DEXTUploadJK product provides the ability to compress and download files or directories.
The downloadZip method of the FileDownload class performs downloading immediately after creating a compressed file of files or directories, and the JKFilesToZipDownloadView and JKDirectoryToZipDownloadView classes perform download work after compressing objects in the Spring environment.
- Servet/JSP environment
-
public class DownloadServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("UTF-8"); List<File> files = new ArrayList<File>(); files.add(new File("/src/test/resources/compress/....txt")); files.add(new File("/src/test/resources/compress/....pdf")); files.add(new File("/src/test/resources/compress/subA/subB/....txt")); files.add(new File("/src/test/resources/compress/....jpg")); files.add(new File("/src/test/resources/compress/....docx")); FileDownload fileDownload = new FileDownload(); // Set the list of files to compress fileDownload.downloadZip(request, response, null, files, null, null, false); } }In a servlet environment, compression and downloading are performed simultaneously using the downloadZip method of the FileDownload class. Only zip files are supported for compression, and when processing a list of files, a compressed file is created by gathering them together, excluding the paths of the files. Compressed files are created in a temporary location. If a file name is not specified when downloading, a zip extension is added to the name of the first file in the target list. If the file name to be compressed is not in English, specify the character set name in the encoding parameter to support multilingual file names. Usually, "UTF-8" is used. If null is specified, the character set set in the current response object is used as the default value. If the target to be compressed is a directory rather than a file, set the directory path in the downloadZip method.
public class DownloadServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("UTF-8"); String targetDirPath = new File("/src/test/resources/compress/").getAbsolutePath(); FileDownload fileDownload = new FileDownload(); // Set the directory to compress fileDownload.downloadZip(request, response, null, targetDirPath, true, null, null, false); } }When compressing a directory, unlike when compressing with only a file list, subpaths are compressed while maintaining the tree form. And by using the includeTargetDirName parameter, you can decide whether to include the target directory or child files/directories.
- Spring MVC or Spring Boot environment
-
In the Spring environment, you can compress and download using the JKFilesToZipDownloadView view class.
@Controller public class DownloadController { @RequestMapping(value = "/zip-download.do", method = RequestMethod.GET) public ModelAndView compressAndDownload() { List<File> files = new ArrayList<File>(); files.add(new File("/src/test/resources/compress/....txt")); files.add(new File("/src/test/resources/compress/....pdf")); files.add(new File("/src/test/resources/compress/subA/subB/....txt")); files.add(new File("/src/test/resources/compress/....jpg")); files.add(new File("/src/test/resources/compress/....docx")); JKFilesToZipDownloadView view = new JKFilesToZipDownloadView(); view.setEntries(files); view.setCharsetName("UTF-8"); return new ModelAndView(view); } }Like the downloadZip method of the FileDownload class, the view class also appends zip to the first file name in the target list if the name of the file to be downloaded is not determined. The JKFilesToZipDownloadView class has a zipCharsetName property, which can be used to compress multilingual file names. Generally, it is set to "UTF-8", and if null, it follows the character set set in the response. If you want to change the character set of the response, you can do so using the setCharsetName method of the JKFilesToZipDownloadView class.
When compressing and downloading a directory, you must use the JKDirectoryToZipDownloadView class.
Files compressed using the methods or classes described above are set to be removed when the VM (Virtual machine) is terminated. If you want to keep compressed files, you must compress them directly using the CompressUtil class instead of using the methods and classes above. And when downloading a compressed file using the CompressUtil class, download it using the download method of the existing FileDownload class or the JKFileDownloadView class.
List<File> files = new ArrayList<File>();
files.add(new File("/src/test/resources/compress/....txt"));
files.add(new File("/src/test/resources/compress/....pdf"));
files.add(new File("/src/test/resources/compress/subA/subB/....txt"));
files.add(new File("/src/test/resources/compress/....jpg"));
files.add(new File("/src/test/resources/compress/....docx"));
CompressUtil zipper = new CompressUtil();
// When compressing the file
File zippedFiles = zipper.zip(files, new File("/tmp"), "UTF-8", false, true);
// When compressing a directory
File zippedDir = zipper.zip(new File("/src/test/resources/compress/"), true, new File("/tmp"), "UTF-8", false, true);
The CompressUtil class does not use the java.util.zip package to compress in zip format, but uses the Apache Commons Compress™ library. Therefore, to use the zip compression function, you must refer to the Apache Commons Compress™ library (commons-compress-{version}.jar).
In a Maven environment using a remote repository, you can reference the library by adding the following to the pom.xml file.
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.12</version>
</dependency>
For more details about the library, see https://commons.apache.org/proper/commons-compress/.
Compressed download restrictions are as follows:
- It can only be compressed into a zip file.
- It does not provide various options to reduce compression time or change compression ratio.
- Compression capabilities and limitations follow the Apache Commons Compress™ specification, and problems with the Apache Commons Compress™ library are not subject to technical support.
- Because the compression process consumes significant system resources, it can seriously affect server performance if there are a lot of objects to be compressed or if the compression process takes a long time due to the large capacity.
- Compression time varies greatly depending on the server environment, such as the objects to be compressed (number, size), server CPU and file system performance, OS, etc.