Solved

Return ZipOutputStream using Servlet

Posted on 2016-08-28
2
71 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
[X]
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
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

738 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