KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > killingar > forum > servlet > ZipArchive


1 /* Copyright 2000-2005 Anders Hovmöller
2  *
3  * The person or persons who have associated their work with
4  * this document (the "Dedicator") hereby dedicate the entire
5  * copyright in the work of authorship identified below (the
6  * "Work") to the public domain.
7  *
8  * Dedicator makes this dedication for the benefit of the
9  * public at large and to the detriment of Dedicator's heirs
10  * and successors. Dedicator intends this dedication to be an
11  * overt act of relinquishment in perpetuity of all present
12  * and future rights under copyright law, whether vested or
13  * contingent, in the Work. Dedicator understands that such
14  * relinquishment of all rights includes the relinquishment of
15  * all rights to enforce (by lawsuit or otherwise) those
16  * copyrights in the Work.
17  *
18  * Dedicator recognizes that, once placed in the public
19  * domain, the Work may be freely reproduced, distributed,
20  * transmitted, used, modified, built upon, or otherwise
21  * exploited by anyone for any purpose, commercial or non-
22  * commercial, and in any way, including by methods that have
23  * not yet been invented or conceived.
24  */

25
26 package net.killingar.forum.servlet;
27
28 import net.killingar.servlet.FileZipOutputStream;
29
30 import javax.servlet.ServletException JavaDoc;
31 import javax.servlet.ServletOutputStream JavaDoc;
32 import javax.servlet.http.HttpServlet JavaDoc;
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35 import java.io.File JavaDoc;
36 import java.io.IOException JavaDoc;
37
38 public final class ZipArchive extends HttpServlet JavaDoc
39 {
40   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc
41   {
42         String JavaDoc base = "/inetpub/sk/archive";
43
44         String JavaDoc tmp;
45         if ((tmp = request.getParameter("directory")) != null)
46         {
47             String JavaDoc q = base+tmp;
48             File JavaDoc dir = new File JavaDoc(q);
49
50             if (dir.exists() && dir.isDirectory())
51             {
52                 response.setContentType("application/x-zip-compressed");
53
54                 FileZipOutputStream out = new FileZipOutputStream(response.getOutputStream());
55
56                 traverse(dir, "", out, request.getParameter("recursive") != null, 0);
57
58                 out.finish();
59             }
60             else
61             {
62                 ServletOutputStream JavaDoc out = response.getOutputStream();
63                 out.println("invalid directory "+q);
64             }
65         }
66         else if ((tmp = request.getParameter("file")) != null)
67         {
68             String JavaDoc q = base+tmp;
69             File JavaDoc file = new File JavaDoc(q);
70
71             if (file.exists() && !file.isDirectory())
72             {
73                 response.setContentType("application/x-zip-compressed");
74
75                 FileZipOutputStream out = new FileZipOutputStream(response.getOutputStream());
76
77                 out.putFile(file, file.getName());
78
79                 out.finish();
80             }
81             else
82             {
83                 ServletOutputStream JavaDoc out = response.getOutputStream();
84                 out.println("invalid file "+q);
85             }
86         }
87   }
88
89     public void traverse(File JavaDoc f, String JavaDoc path, FileZipOutputStream out, boolean recursive, int step) throws IOException JavaDoc
90     {
91         if (f.isDirectory())
92         {
93             if (!recursive && step > 0)return;
94
95             File JavaDoc files[] = f.listFiles();
96             String JavaDoc subPath = path;
97             if (path.length() != 0)
98                 subPath += "\\";
99
100             for (int i = 0; i < files.length; i++)
101             {
102                 traverse(files[i], subPath+files[i].getName(), out, recursive, step+1);
103             }
104         }
105         else
106         {
107             try
108             {
109                 //String name = f.getName();
110
//String extension = name.substring(name.lastIndexOf('.')+1).toLowerCase();
111

112                 //if (extensions.contains(extension))
113
out.putFile(f, path);
114             }
115             catch (IndexOutOfBoundsException JavaDoc e)
116             {
117                 // this file has no extension, ignore it
118
}
119         }
120     }
121 }
Popular Tags