1 | package com.medias.megapoli.struts.actions; |
---|
2 | |
---|
3 | import java.io.BufferedInputStream; |
---|
4 | import java.io.File; |
---|
5 | import java.io.FileInputStream; |
---|
6 | import java.io.InputStream; |
---|
7 | import java.io.OutputStream; |
---|
8 | |
---|
9 | import com.medias.Context; |
---|
10 | import com.medias.zip.ZipUtilities; |
---|
11 | |
---|
12 | import javax.servlet.ServletException; |
---|
13 | import javax.servlet.http.HttpServletRequest; |
---|
14 | import javax.servlet.http.HttpServletResponse; |
---|
15 | |
---|
16 | import org.apache.struts.action.Action; |
---|
17 | import org.apache.struts.action.ActionError; |
---|
18 | import org.apache.struts.action.ActionErrors; |
---|
19 | import org.apache.struts.action.ActionForm; |
---|
20 | import org.apache.struts.action.ActionForward; |
---|
21 | import org.apache.struts.action.ActionMapping; |
---|
22 | |
---|
23 | |
---|
24 | /** |
---|
25 | * @author combaz |
---|
26 | * |
---|
27 | * Created on 22 févr. 2005 |
---|
28 | */ |
---|
29 | public class ZipGameAction extends Action { |
---|
30 | |
---|
31 | public ActionForward execute (ActionMapping mapping, |
---|
32 | ActionForm form, |
---|
33 | HttpServletRequest request, |
---|
34 | HttpServletResponse response) |
---|
35 | throws ServletException { |
---|
36 | |
---|
37 | // String target = null; |
---|
38 | ActionErrors errors = new ActionErrors (); |
---|
39 | |
---|
40 | String path = request.getParameter("path"); |
---|
41 | String uploadDir = (String)request.getSession().getServletContext().getAttribute("uploadDir"); |
---|
42 | String accessDir = (String)request.getSession().getServletContext().getAttribute("accessDir"); |
---|
43 | |
---|
44 | File f = new File (accessDir+"/"+path+"/"); |
---|
45 | if (!f.exists() || f.list().length==0) { |
---|
46 | f = new File (uploadDir+"/"+path); |
---|
47 | } |
---|
48 | // if (this.isCancelled (request)) { |
---|
49 | // target = "cancel"; |
---|
50 | // } else { |
---|
51 | try { |
---|
52 | // ZipUtilities.zipFilesInPath(ZipUtilities.getPathFromFilePath (path), archive); |
---|
53 | ZipUtilities.zipFilesInPath(f); |
---|
54 | // target = "success"; |
---|
55 | } |
---|
56 | catch (Exception e) { |
---|
57 | System.out.println ("exception_zipGameAction"); |
---|
58 | e.printStackTrace (); |
---|
59 | errors.add (ActionErrors.GLOBAL_MESSAGE, new ActionError ("errors.zipgame.failed")); |
---|
60 | } |
---|
61 | // } |
---|
62 | |
---|
63 | // if (!errors.isEmpty ()){ |
---|
64 | // saveErrors (request, errors); |
---|
65 | // target = "failure"; |
---|
66 | // } |
---|
67 | |
---|
68 | // return (mapping.findForward (target)); |
---|
69 | |
---|
70 | File file = new File (f.getAbsolutePath()+"/"+f.getName()+"_archive.zip"); |
---|
71 | |
---|
72 | response.setContentType ("multipart/zip"); |
---|
73 | response.setHeader ("Content-Disposition","attachment; filename=\"" +file.getName().trim () + "\";"); |
---|
74 | response.setContentLength((int)file.length()); |
---|
75 | |
---|
76 | try { |
---|
77 | OutputStream os = response.getOutputStream (); |
---|
78 | FileInputStream stream = new FileInputStream (file); |
---|
79 | BufferedInputStream bis = new BufferedInputStream (stream); |
---|
80 | InputStream is = new BufferedInputStream (bis); |
---|
81 | |
---|
82 | int count; |
---|
83 | byte buf[] = new byte[4096]; |
---|
84 | while ((count = is.read (buf)) > -1) { |
---|
85 | os.write (buf, 0, count); |
---|
86 | } |
---|
87 | is.close (); |
---|
88 | os.close (); |
---|
89 | } |
---|
90 | catch (Exception ex) { |
---|
91 | System.out.println ("exception_sendFileToClient"); |
---|
92 | errors.add (ActionErrors.GLOBAL_MESSAGE, new ActionError ("errors.access.send.failed",Context.getWebmaster(request))); |
---|
93 | ex.printStackTrace (); |
---|
94 | } |
---|
95 | if(!errors.isEmpty ()){ |
---|
96 | System.out.println ("errors"); |
---|
97 | saveErrors(request, errors); |
---|
98 | } |
---|
99 | file.delete(); |
---|
100 | return null; |
---|
101 | } |
---|
102 | |
---|
103 | } |
---|