View Javadoc
1   /*
2    * Copyright 2011 The Kuali Foundation.
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.sys.util;
17  
18  import java.io.ByteArrayOutputStream;
19  import java.io.IOException;
20  import java.util.Iterator;
21  import java.util.Map;
22  import java.util.zip.ZipEntry;
23  import java.util.zip.ZipOutputStream;
24  
25  import javax.servlet.http.HttpServletResponse;
26  
27  public class KfsWebUtils {
28  
29      /**
30       * Zipoutput files that are not of type text/plain or text/html.
31       * 
32       * @param response
33       * @param contentType
34       * @param outputStreamMap<filename, outputStream>
35       * @param zipFileName
36       * @throws IOException
37       */
38      public static void saveMimeZipOutputStreamAsFile(HttpServletResponse response, String contentType, Map<String, ByteArrayOutputStream> outputStreamMap, String zipFileName) throws IOException {
39      
40          // set response
41          response.setContentType(contentType);
42          response.setHeader("Content-disposition", "attachment; filename=" + zipFileName);
43          response.setHeader("Expires", "0");
44          response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
45          response.setHeader("Pragma", "public");
46      
47          // write to zipoutput
48          ZipOutputStream zout = new ZipOutputStream(response.getOutputStream());
49          int totalSize = 0;
50          Iterator<String> fileNames = outputStreamMap.keySet().iterator();
51          while (fileNames.hasNext()) {
52              String fileName = fileNames.next();
53              ByteArrayOutputStream pdfStream = outputStreamMap.get(fileName);
54              totalSize += pdfStream.size();
55              zout.putNextEntry(new ZipEntry(fileName));
56              zout.write(pdfStream.toByteArray());
57              zout.closeEntry();
58          }
59          response.setContentLength(totalSize);
60          zout.flush();
61          zout.close();        
62      }
63      
64  }