KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > vfs > ZipDownload


1 package com.sslexplorer.vfs;
2
3 import java.io.IOException JavaDoc;
4 import java.io.InputStream JavaDoc;
5 import java.io.OutputStream JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.zip.ZipEntry JavaDoc;
8 import java.util.zip.ZipOutputStream JavaDoc;
9
10 import javax.servlet.http.HttpServletRequest JavaDoc;
11 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /**
22  * Implementation of {@link com.sslexplorer.core.AbstractDownloadContent} for
23  * downloading an of VFS uri's.
24  * <p>
25  * The download will be presented as a zip archive with a filename of either the
26  * only URI select or of the basename of the root folder (both suffixed with
27  * .zip).
28  *
29  * @author James D Robinson <a HREF="mailto:james@3sp.com">&lt;james@3sp.com&gt;</a>
30  *
31  */

32 public class ZipDownload extends AbstractDownloadContent {
33
34     private String JavaDoc[] uris;
35     private String JavaDoc rootPath;
36     private ActionForward messageForward;
37     private int downloadCount;
38     private LaunchSession launchSession;
39
40     /**
41      * @param launchSession launch session
42      * @param messageForward forward to use to display message
43      * @param rootPath The current location.
44      * @param uris The Array of source uri's
45      * @param forward The forward to go to after the operation.
46      * @param messageKey The message key
47      * @param messageResourcesKey The message resource key.
48      */

49     public ZipDownload(LaunchSession launchSession, ActionForward messageForward, String JavaDoc rootPath, String JavaDoc[] uris, ActionForward forward, String JavaDoc messageKey,
50                        String JavaDoc 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     /*
59      * (non-Javadoc)
60      *
61      * @see com.sslexplorer.core.DownloadContent#sendDownload(javax.servlet.http.HttpServletResponse,
62      * javax.servlet.http.HttpServletRequest)
63      */

64     public void sendDownload(HttpServletResponse JavaDoc response, HttpServletRequest JavaDoc request) throws Exception JavaDoc {
65         OutputStream JavaDoc out = null;
66         try {
67             ZipOutputStream JavaDoc zos = new ZipOutputStream JavaDoc(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 JavaDoc ioe) {
74             throw ioe;
75         } finally {
76             downloadCount++;
77             Util.closeStream(out);
78         }
79     }
80
81     /**
82      * @param zos The ZipOutputStream going to the destination.
83      * @param res The DAVResource to zip
84      * @param path The path after the current location.
85      * @throws Exception
86      */

87     private void zipit(ZipOutputStream JavaDoc zos, VFSResource res, String JavaDoc path) throws Exception JavaDoc {
88         if (res.isCollection()) {
89             Iterator JavaDoc 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 JavaDoc zipPath = DAVUtilities.stripLeadingSlash(DAVUtilities.concatenatePaths(path, res.getDisplayName()));
96             ZipEntry JavaDoc idEntry = new ZipEntry JavaDoc(zipPath);
97             zos.putNextEntry(idEntry);
98             InputStream JavaDoc 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     /*
117      * (non-Javadoc)
118      *
119      * @see com.sslexplorer.core.DownloadContent#getFilename()
120      */

121     public String JavaDoc getFilename() {
122         return (uris.length == 1 ? DAVUtilities.stripTrailingSlash(uris[0]) : DAVUtilities.basename(rootPath, '/')) + ".zip";
123     }
124
125     /*
126      * (non-Javadoc)
127      *
128      * @see com.sslexplorer.core.DownloadContent#getMessageForward()
129      */

130     public ActionForward getMessageForward() {
131         return messageForward;
132     }
133
134     public int getDownloadCount() {
135         return downloadCount;
136     }
137 }
Popular Tags