1 package org.contineo.core.transfer; 2 3 import java.io.BufferedInputStream ; 4 import java.io.ByteArrayOutputStream ; 5 import java.io.FileInputStream ; 6 import java.io.IOException ; 7 import java.io.InputStream ; 8 import java.util.Collection ; 9 import java.util.Iterator ; 10 import java.util.zip.ZipEntry ; 11 import java.util.zip.ZipOutputStream ; 12 import org.contineo.admin.Menu; 13 import org.contineo.admin.dao.MenuDAO; 14 import org.contineo.core.config.SettingConfigurator; 15 16 20 public class ZipExport implements Export { 21 22 private ZipOutputStream zos; 23 private String username; 24 private SettingConfigurator settings; 25 private boolean allLevel; 26 27 30 public ZipExport() { 31 zos = null; 32 username = ""; 33 settings = new SettingConfigurator(); 34 allLevel = false; 35 } 36 37 40 public ByteArrayOutputStream process(Menu menu, String user) throws IOException { 41 username = user; 42 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 43 zos = new ZipOutputStream (bos); 44 appendChildren(menu, 0); 45 zos.flush(); 46 zos.close(); 47 return bos; 48 } 49 50 53 public ByteArrayOutputStream process(int menuId, String user) throws IOException { 54 MenuDAO menuDao = new MenuDAO(); 55 Menu menu = menuDao.findMenuByMenuId(menuId); 56 return process(menu, user); 57 } 58 59 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 menupath = menu.getMenuPath(); 74 String 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 children = menuDao.findByUserName(username, menu.getMenuId()); 81 Iterator iter = children.iterator(); 82 while (iter.hasNext()) { 83 appendChildren((Menu)iter.next(), level + 1); 84 } 85 } 86 } 87 88 protected void addFile(String filepath) { 89 try { 90 InputStream is = new FileInputStream (filepath); 91 BufferedInputStream bis = new BufferedInputStream (is); 92 ZipEntry entry = new ZipEntry (filepath); 93 zos.putNextEntry(entry); 94 int len; 96 while ((len = bis.read()) != -1) { 97 zos.write(len); 98 } 99 bis.close(); 100 } catch (Exception e) { 101 e.printStackTrace(); 102 } 103 } 104 } 105 | Popular Tags |