Solved

Return ZipOutputStream using Servlet

Posted on 2016-08-28
2
32 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
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…
This video teaches viewers about errors in exception handling.

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now