www.dextsolution.com
DEXTUPLOAD
NJ
menu toggleProduct description > Compressed download

Compressed download

Since the DEXTUploadNJ product version 1.3.0, it provides a function to compress a file or directory and download it.

The downloadZip method of the FileDownload class downloads immediately after creating a file compressed with files or a directory, and the DEXTUploadNJFilesToZipDownloadView class downloads immediately after compressing the object in the Spring environment.

DEXTUploadNJ + Servlet
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 dextnj = new FileDownload();
        dextnj.downloadZip(request, response, null, files, null, null, false);
    }
}

In the servlet environment, use the downloadZip method of the FileDownload class to compress and download simultaneously. Compression supports only zip files. If processing is carried out with a list of files, the files are collected except for the path of the file and a compressed file is generated. A compressed file is created in a temporary location, and if you do not specify a file name when downloading it, concate the zip extension to the name of the first file in the target list. If the file name to be compressed is not English, specify the character set name in the encoding parameter to support multilingual file names. Uses regular "UTF-8", but when it is specified as null, it uses the character set for the current response object as the default. If it is not a file to be compressed, but a directory, set the directory path to 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 dextnj = new FileDownload();
        dextnj.downloadZip(request, response, null, targetDirPath, true, null, null, false);
    }
}

Unlike compressing directories with only a list of files, subpaths are kept in a tree format and compressed. And you can use includeTargetDirName parameter to decide whether to include the base name of the target directory.

DEXTUploadNJ + Spring Web Framework

In the Spring environment, you can download it after compressing using the DEXTUploadNJFilesToZipDownloadView 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"));
		
        DEXTUploadNJFilesToZipDownloadView view = new DEXTUploadNJFilesToZipDownloadView();
			
        view.setEntries(files);
        view.setCharsetName("UTF-8");
			
        return new ModelAndView(view);
    }
}

Like the downloadZip method of the FileDownload class, if the name of the file to be downloaded cannot be determined also for the view class, zip is added to the first file name of the target list. The DEXTUploadNJFilesToZipDownloadView class has a zipCharsetName property, which can be used to compress multilingual file names. Generally set to "UTF-8", if null follows the character set to response. If you want to change the character set of response you can change it using the setCharsetName method of the DEXTUploadNJFilesToZipDownloadView class.

When compressing and downloading a directory, use the DEXTUploadNJDirectoryToZipDownloadView view class.

A file compressed with the method and class described above is set to be deleted when the VM (virtual machine) ends(terminates). If you keep compressed files, you need to compress directly using the CompressUtil class without using the above methods and classes. And when downloading the compressed file using the CompressUtil class, download it using the download method of the existing the FileDownload class or the DEXTUploadNJFileDownloadView 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 a 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 uses the Apache Commons Compress™ library, instead of using the java.util.zip package to compress in zip format. Although the DEXTUploadNJ runs under the JRE 1.6 environment, the java.util.zip has a problem that it cannot handle file names with multilingual 1.6-based names. Therefore, to use the zip compression function, refer to the Apache Commons Compress™ library (commons-compress-{version}.jar) and do it. (Apache Commons Compress™ supports up to version 1.12 in 1.6 environment.)

In the Maven environment using the remote repository, you can refer to the library by adding the following to the POM file.

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-compress</artifactId>
    <version>1.12</version>
</dependency>

For details on the library, please refer to https://commons.apache.org/proper/commons-compress/.

Compression download restrictions are as follows.

  • Only zip files can be compressed.
  • It does not provide various options for reducing the compression time or changing the compression ratio.
  • Compression capabilities and restrictions conform to the Apache Commons Compress™ specification, and problems with the Apache Commons Compress™ library are not subject to technical support.
  • Since the compression process consumes considerable system resources, it can have a serious impact on the performance of the server if there are too many objects to compress, capacity is large, compression work takes time.
  • There are many differences in compression time depending on the object(number, size) to be compressed, the performance of the CPU and file system of the server, and the server environment such as OS.