Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Return ZipOutputStream using Servlet

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
expectj telnet failing 5 45
null output 3 35
running on tomcat not jboss eap 7.0 3 18
jsp error 6 25
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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:

808 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