Compressing files and downloading

Home > Basic examples > Example 12

Explanation

DEXTUploadX5 supports compressed downloads starting with version 1.3.0.0.

To receive multiple files simultaneously, a compressed download asks the server to pack the target files into a single compressed file, and then downloads the compressed file. Unlike the single/multiple file download examples in the other examples, compressed downloads are not affected by the attribute (downUrl) that specifies the download path to the target file because the compression process is handled by the server.

To perform a compressed download, you must specify the address that will handle the compression, and return the path value to download the compressed file using the setCompressURL method. Unlike single/multiple download, the downloadCompressed method is used when you start a compressed download.

var dx = dx5.get("component-id");

// The downUrl property is unnecessary.
// You must specify the value of the vindex property to distinguish targets on the server.
// vindex is a unique string value, which is the only way to distinguish between virtual files. 
dx.addVirtualFile({ vindex: "IDX0003", name: "bridge_509147.jpg", size: 509147 });
dx.addVirtualFile({ vindex: "IDX0004", name: "beach_239826.jpg", size: 239826 });
dx.addVirtualFile({ vindex: "IDX0005", name: "cosmos (empty) 195779.jpg", size: 195779 });

// Set the address to process the compression and return the compressed file download path. 
dx.setCompressURL("http://domain/path.../service/compress.do");

// Perform the download according to the flag value.
// AUTO: Download compressed virtual files.
// SELECTED: Download compressed selected virtual files.
// CHECKED: Download compressed checked virtual files. 
dx.downloadCompressed("SELECTED");

File compression and download are handled by the FileCompression servlet and are mapped to compress.do in web.xml. DEXTUploadX5 sends the form data with the name 'DEXTUploadX5_VIndexes' to the server in POST method when the downloadCompressed method is called, and the vindex property value of the virtual file to be downloaded is a list with a comma (,) as a separator.

If it is compressed, it must send the path to download the target compressed file to the client in the response.

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	List<File> files = new ArrayList<File>();
	String vindices = request.getParameter("DEXTUploadX5_VIndexes");
	String[] tokens = vindices.split(",");
	
	for (int i = 0; i < tokens.length; i++) {
		if (tokens[i].equals("IDX0003"))
	    	files.add(new File(fileRoot, "attach/bridge_509147.jpg"));
		if (tokens[i].equals("IDX0004"))
	    	files.add(new File(fileRoot, "attach/beach_239826.jpg"));
		if (tokens[i].equals("IDX0005"))
	    	files.add(new File(fileRoot, "attach/cosmos (empty) 195779.jpg"));
	}
	
	// Create a compressed file in a temporary location.
	CompressUtil cu = new CompressUtil();
	File zipped = cu.zip(files, new File(fileRoot, "/temp/"), "UTF-8", false, false);
	
	...
	
	String compresskey = FileRepository.addFileEntity(target);
	
	response.setContentType("text/plain");
	
	// In the example, you can download the compressed file by requesting the same servlet as GET.
	// The compresskey query string specifies what to download. 
	response.getWriter().write(request.getRequestURL().append("?compresskey=".concat(compresskey)).toString());
}
/**
 * Perform file (compressed) downloads.
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	String compresskey = request.getParameter("compresskey");
	
	// Get the compressed object.
	...
	
	if (target != null) {
		FileDownload dextnj = new FileDownload();
		
		// Do not use partial content download function for compressed download. (Default) 
		dextnj.setAllowingWeakRange(false);
		
		// Because the compressed file is a temporary file, delete it after the response data is written. 
		dextnj.setRemoveAfterDownloading(true);
		
		// Set parameters to prevent client cache from being used when downloading.
		dextnj.download(request, response, target.getFile(), target.getFilename(), null, false, false);
	} else {
	    response.sendError(404);
	}
}

Compressing files is a task that consumes a lot of server resources (CPU, I/O), so if you have a large number of files that are large files or compressed files, depending on the situation, it may take a long time to wait for the file compression process, which may lead to a problem of a session being disconnected or a serious problem that the service itself is stopped. Therefore, it is recommended to use the function after establishing policy restrictions for compressed download.

The DEXTUploadNJ product provides a class called CompressUtil for compression, which uses the Apache Commons Compress library. Refer to the DEXTUploadNJ product manual for details.

Example

Compress download

Compression proceeds on the server. The progress window is displayed while the compression is in progress, but when the compressed file is downloaded, a single file download is performed. Therefore, the progress window for the downloading process is not provided.