KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > contineo > core > transfer > ZipExport


1 package org.contineo.core.transfer;
2
3 import java.io.BufferedInputStream JavaDoc;
4 import java.io.ByteArrayOutputStream JavaDoc;
5 import java.io.FileInputStream JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.io.InputStream JavaDoc;
8 import java.util.Collection JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.zip.ZipEntry JavaDoc;
11 import java.util.zip.ZipOutputStream JavaDoc;
12 import org.contineo.admin.Menu;
13 import org.contineo.admin.dao.MenuDAO;
14 import org.contineo.core.config.SettingConfigurator;
15
16 /**
17  * Exports a folder hierarchie and all documents in it as a zip file.
18  * Created on 09.12.2004
19  */

20 public class ZipExport implements Export {
21     
22     private ZipOutputStream JavaDoc zos;
23     private String JavaDoc username;
24     private SettingConfigurator settings;
25     private boolean allLevel;
26
27     /**
28      *
29      */

30     public ZipExport() {
31         zos = null;
32         username = "";
33         settings = new SettingConfigurator();
34         allLevel = false;
35     }
36
37     /* (non-Javadoc)
38      * @see org.contineo.core.export.Export#process(org.contineo.admin.Menu, java.lang.String)
39      */

40     public ByteArrayOutputStream JavaDoc process(Menu menu, String JavaDoc user) throws IOException JavaDoc {
41         username = user;
42         ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
43         zos = new ZipOutputStream JavaDoc(bos);
44         appendChildren(menu, 0);
45         zos.flush();
46         zos.close();
47         return bos;
48     }
49
50     /* (non-Javadoc)
51      * @see org.contineo.core.export.Export#process(int, java.lang.String)
52      */

53     public ByteArrayOutputStream JavaDoc process(int menuId, String JavaDoc user) throws IOException JavaDoc {
54         MenuDAO menuDao = new MenuDAO();
55         Menu menu = menuDao.findMenuByMenuId(menuId);
56         return process(menu, user);
57     }
58     
59     /**
60      * If allLevel set true all children of a specified menu will be export. Otherwise only the first level will be export.
61      * @param b
62      * @uml.property name="allLevel"
63      */

64     public void setAllLevel(boolean b) {
65         allLevel = b;
66     }
67     
68     protected void appendChildren(Menu menu, int level) {
69         if (!allLevel && level > 1)
70             return;
71         else {
72             if (menu.getMenuType() == 5) {
73                 String JavaDoc menupath = menu.getMenuPath();
74                 String JavaDoc path = settings.getValue("docdir");
75                 path += menupath + "/" + String.valueOf(menu.getMenuId()) + "/";
76                 path += menu.getMenuRef();
77                 addFile(path);
78             }
79             MenuDAO menuDao = new MenuDAO();
80             Collection JavaDoc children = menuDao.findByUserName(username, menu.getMenuId());
81             Iterator JavaDoc iter = children.iterator();
82             while (iter.hasNext()) {
83                 appendChildren((Menu)iter.next(), level + 1);
84             }
85         }
86     }
87
88     protected void addFile(String JavaDoc filepath) {
89         try {
90             InputStream JavaDoc is = new FileInputStream JavaDoc(filepath);
91             BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(is);
92             ZipEntry JavaDoc entry = new ZipEntry JavaDoc(filepath);
93             zos.putNextEntry(entry);
94              // Transfer bytes from the file to the ZIP file
95
int len;
96             while ((len = bis.read()) != -1) {
97                 zos.write(len);
98             }
99             bis.close();
100         } catch (Exception JavaDoc e) {
101             e.printStackTrace();
102         }
103     }
104 }
105
Popular Tags