Jadson José dos Santos – email@example.com
The Java API has the java.util.zip package that allows compact several files in a unique zip file.
This functionality is very useful when you need to send to the user several files in a unique web application request. Since is only possible send one file by request and there is not a way to create several directories on the client side to organized this information.
The first thing is creating a temporary file output, since we don’t need to save a file in the server side. You can do this on Java using the ByteArrayOutputScream class. So you can instantiate a ZipOutputStream class passing this output stream.
ByteArrayOutputStream outputStreamZipFile = new ByteArrayOutputStream(); ZipOutputStream zout = new ZipOutputStream(outputStreamZipFile);
To organize many files, can be interesting create some directories in the zip file. To do this it is necessary just added “ZipEntries” with the “/” character. Everything that ends up with “/” is considered a directory to the Java Zip API.
String directoryName = directory+"/"; zout.putNextEntry(new ZipEntry(directoryName)); zout.closeEntry();
The below code create a temp text file. You can use any Java method that you know to create files, for example the File class.
The only thing you should know is that you will need to generate a byte array in the end. So, use the ByteArrayOutputScream class again is a good choose.
ByteArrayOutputStream tempFileOutput = new ByteArrayOutputStream(); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(tempFileOutput)); writer.write("Text"); writer.close();
Create a new “ZipEntry” object starting with the directory name and write the file output stream. It will do the file be created inside of the directory.
ZipEntry zipEntryFile = new ZipEntry(directoryName +"file.txt"); zout.putNextEntry(zipEntryFile ); zout.write(tempFileOutput.toByteArray()); // write the data into ZipEntry zout.closeEntry(); // create a new file inside zip file tempFileOutput.close();
You can create several “ZipEntry”, each “ZipEntry” will be a file inside the zip file.
To finish, close the ZipOutputStream and return the stream object that was used to create the zip file.
zout.close(); return outputStreamZipFile;
Send the file to the user by web browser:
It is possible to send the zip file to the web browser using the HttpServletResponse object and the ByteArrayOutputStream object that were used to create the ZipOutputStream object.
HttpServletResponse response = getCurrentResponse(); ByteArrayOutputStream outputStreamZipFile = genereteZipFile(); response.setContentType("Content-Type: application/zip;"); response.addHeader("Content-Disposition", "attachment; filename=output.zip"); response.getOutputStream().write(outputStreamZipFile.toByteArray());
For more details about how choose the zip compression model, API methods, among other things, consult:
http://www.devmedia.com.br/criando-um-arquivo-compactado-zip/2293 (in Portuguese)
http://www.guj.com.br/articles/181 (in Portuguese)