1 25 26 package net.killingar.forum.servlet; 27 28 import net.killingar.servlet.FileZipOutputStream; 29 30 import javax.servlet.ServletException ; 31 import javax.servlet.ServletOutputStream ; 32 import javax.servlet.http.HttpServlet ; 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.http.HttpServletResponse ; 35 import java.io.File ; 36 import java.io.IOException ; 37 38 public final class ZipArchive extends HttpServlet 39 { 40 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException 41 { 42 String base = "/inetpub/sk/archive"; 43 44 String tmp; 45 if ((tmp = request.getParameter("directory")) != null) 46 { 47 String q = base+tmp; 48 File dir = new File (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 out = response.getOutputStream(); 63 out.println("invalid directory "+q); 64 } 65 } 66 else if ((tmp = request.getParameter("file")) != null) 67 { 68 String q = base+tmp; 69 File file = new File (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 out = response.getOutputStream(); 84 out.println("invalid file "+q); 85 } 86 } 87 } 88 89 public void traverse(File f, String path, FileZipOutputStream out, boolean recursive, int step) throws IOException 90 { 91 if (f.isDirectory()) 92 { 93 if (!recursive && step > 0)return; 94 95 File files[] = f.listFiles(); 96 String 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 112 out.putFile(f, path); 114 } 115 catch (IndexOutOfBoundsException e) 116 { 117 } 119 } 120 } 121 } | Popular Tags |