Solved

Return ZipOutputStream using Servlet

Posted on 2016-08-28
2
48 Views
Last Modified: 2016-09-04
In my Servlet, I am creating a zip file using ZipoutputStream

public ZipOutputStream zipIt(String zipFile) {
	   byte[] buffer = new byte[1024];
	   String source = "";
	   FileOutputStream fos = null;
	   ZipOutputStream zos = null;
	   try {
	      try {
	         source = SOURCE_FOLDER.substring(SOURCE_FOLDER.lastIndexOf("\\") + 1, SOURCE_FOLDER.length());
	      }
	     catch (Exception e) {
	        source = SOURCE_FOLDER;
	        e.printStackTrace();
	     }
	     fos = new FileOutputStream(zipFile);
	     zos = new ZipOutputStream(fos);

	     System.out.println("Output to Zip : " + zipFile);
	     FileInputStream in = null;

	     for (String file : this.fileList) {
	        System.out.println("File Added : " + file);
	        ZipEntry ze = new ZipEntry(source + File.separator + file);
	        zos.putNextEntry(ze);
	        try {
	           in = new FileInputStream(SOURCE_FOLDER + File.separator + file);
	           int len;
	           while ((len = in.read(buffer)) > 0) {
	              zos.write(buffer, 0, len);
	           }
	        }
	        finally {
	           in.close();
	        }
	     }
	     zos.closeEntry();
	     System.out.println("Folder successfully compressed");
	  }

Open in new window


Once the zip is created, I would like to send it back to the client. However, to send it as a part of the response object, it needs to be a byte[]. How can I convert ZipOutputStream to byte[]?
0
Comment
Question by:ank5
  • 2
2 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 41774391
However, to send it as a part of the response object, it needs to be a byte[].
Hmm. I don't quite understand that. It actually needs to be written to the servlet's OutputStream, so just write it to that instead of to 'fos'

Your buffer is almost certainly too small btw
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 41783800
:)
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
allswap challenge 6 99
difference of if loops 23 49
configure dependency in POM for new database 3 25
JAVA API design with micro service cloud in mind 1 18
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question