1 package com.sslexplorer.vfs; 2 3 import java.io.IOException ; 4 import java.io.InputStream ; 5 import java.io.OutputStream ; 6 import java.util.Iterator ; 7 import java.util.zip.ZipEntry ; 8 import java.util.zip.ZipOutputStream ; 9 10 import javax.servlet.http.HttpServletRequest ; 11 import javax.servlet.http.HttpServletResponse ; 12 13 import org.apache.struts.action.ActionForward; 14 15 import com.sslexplorer.boot.Util; 16 import com.sslexplorer.core.AbstractDownloadContent; 17 import com.sslexplorer.policyframework.LaunchSession; 18 import com.sslexplorer.vfs.webdav.DAVServlet; 19 import com.sslexplorer.vfs.webdav.DAVUtilities; 20 21 32 public class ZipDownload extends AbstractDownloadContent { 33 34 private String [] uris; 35 private String rootPath; 36 private ActionForward messageForward; 37 private int downloadCount; 38 private LaunchSession launchSession; 39 40 49 public ZipDownload(LaunchSession launchSession, ActionForward messageForward, String rootPath, String [] uris, ActionForward forward, String messageKey, 50 String messageResourcesKey) { 51 super("application/x-zip", forward, messageKey, messageResourcesKey, null, null, null, null, null); 52 this.uris = uris; 53 this.launchSession = launchSession; 54 this.rootPath = rootPath; 55 this.messageForward = messageForward; 56 } 57 58 64 public void sendDownload(HttpServletResponse response, HttpServletRequest request) throws Exception { 65 OutputStream out = null; 66 try { 67 ZipOutputStream zos = new ZipOutputStream (response.getOutputStream()); 68 for (int i = 0; i < uris.length; i++) { 69 VFSResource res = DAVServlet.getDAVResource(launchSession, request, response, rootPath + "/" + uris[i]); 70 zipit(zos, res, ""); 71 } 72 zos.close(); 73 } catch (IOException ioe) { 74 throw ioe; 75 } finally { 76 downloadCount++; 77 Util.closeStream(out); 78 } 79 } 80 81 87 private void zipit(ZipOutputStream zos, VFSResource res, String path) throws Exception { 88 if (res.isCollection()) { 89 Iterator children = res.getChildren(); 90 while (children.hasNext()) { 91 FileObjectVFSResource childResource = (FileObjectVFSResource) children.next(); 92 zipit(zos, childResource, DAVUtilities.concatenatePaths(path, res.getDisplayName())); 93 } 94 } else { 95 String zipPath = DAVUtilities.stripLeadingSlash(DAVUtilities.concatenatePaths(path, res.getDisplayName())); 96 ZipEntry idEntry = new ZipEntry (zipPath); 97 zos.putNextEntry(idEntry); 98 InputStream in = res.getInputStream(); 99 try { 100 byte[] buf = new byte[4096]; 101 int read; 102 while (true) { 103 read = in.read(buf, 0, buf.length); 104 if (read == -1) { 105 break; 106 } 107 zos.write(buf, 0, read); 108 } 109 } finally { 110 zos.closeEntry(); 111 in.close(); 112 } 113 } 114 } 115 116 121 public String getFilename() { 122 return (uris.length == 1 ? DAVUtilities.stripTrailingSlash(uris[0]) : DAVUtilities.basename(rootPath, '/')) + ".zip"; 123 } 124 125 130 public ActionForward getMessageForward() { 131 return messageForward; 132 } 133 134 public int getDownloadCount() { 135 return downloadCount; 136 } 137 } | Popular Tags |