001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.web.controllers.files;
034:
035: import org.libresource.Libresource;
036:
037: import org.libresource.core.FileData;
038:
039: import org.libresource.files.FileConstants;
040: import org.libresource.files.ejb.model.FileResourceValue;
041: import org.libresource.files.interfaces.LibresourceFilesService;
042:
043: import org.libresource.kernel.LibresourceSecurityException;
044:
045: import org.libresource.web.Controller;
046: import org.libresource.web.config.Config;
047:
048: import java.io.BufferedInputStream;
049: import java.io.BufferedOutputStream;
050: import java.io.File;
051: import java.io.FileInputStream;
052: import java.io.FileOutputStream;
053: import java.io.InputStream;
054: import java.io.OutputStream;
055:
056: import java.net.URI;
057:
058: import java.util.ArrayList;
059: import java.util.zip.ZipEntry;
060: import java.util.zip.ZipOutputStream;
061:
062: import javax.servlet.http.HttpServletRequest;
063: import javax.servlet.http.HttpServletResponse;
064:
065: public class ExportAsZipController implements Controller {
066: public Object process(URI uri, HttpServletRequest request,
067: HttpServletResponse response) throws Exception {
068: LibresourceFilesService libresourceFilesService = (LibresourceFilesService) Libresource
069: .getService(FileConstants.SERVICE);
070:
071: FileResourceValue[] files = null;
072: String repositoryName = null;
073:
074: try {
075: repositoryName = libresourceFilesService.getRepository(uri)
076: .getName();
077: files = libresourceFilesService.listFilesInRepository(uri);
078: } catch (Exception e) {
079: repositoryName = libresourceFilesService.getDropBox(uri)
080: .getName();
081: files = libresourceFilesService.listFilesInDropBox(uri);
082: }
083:
084: // zip exists ?
085: String zipFileName = Config.getInstance(null).getLsWebBuffer()
086: + File.separator + repositoryName.replaceAll(" ", "_")
087: + ".zip";
088: File zipFile = new File(zipFileName);
089:
090: if (!zipFile.exists()) {
091: zipFile.delete();
092: }
093:
094: // create zip
095: byte[] buf = new byte[1024 * 1024 * 2];
096: ZipOutputStream out = new ZipOutputStream(
097: new BufferedOutputStream(new FileOutputStream(zipFile)));
098:
099: ArrayList entryName = new ArrayList();
100: String name = null;
101:
102: for (int i = 0; i < files.length; i++) {
103: try {
104: FileData fileData = libresourceFilesService
105: .getFileContent(files[i].getUri(), null);
106: InputStream in = new BufferedInputStream(fileData
107: .getInputStream());
108: name = files[i].getName();
109:
110: if (entryName.contains(name)) {
111: int index = 1;
112:
113: while (entryName.contains(index + "-" + name)) {
114: index++;
115: }
116:
117: name = index + "-" + name;
118: }
119:
120: entryName.add(name);
121:
122: out.putNextEntry(new ZipEntry(name));
123:
124: // Transfer bytes from the file to the ZIP file
125: int len;
126:
127: while ((len = in.read(buf)) > 0) {
128: out.write(buf, 0, len);
129: }
130:
131: // Complete the entry
132: out.closeEntry();
133: in.close();
134: } catch (LibresourceSecurityException e) {
135: // do not add file in zip...
136: System.out.println("File not added in zip: pb secu");
137: }
138: }
139:
140: out.close();
141:
142: // download zip file
143: response.setContentLength((int) zipFile.length());
144: response.setContentType("application/octetstream");
145: response.addHeader("Content-Disposition",
146: "attachment; filename=" + zipFile.getName());
147:
148: // set body
149: OutputStream outResp = response.getOutputStream();
150: FileInputStream fis = new FileInputStream(zipFile);
151:
152: byte[] buffer = new byte[1024 * 1024];
153: int length = -1;
154:
155: while ((length = fis.read(buffer)) != -1) {
156: outResp.write(buffer, 0, length);
157: }
158:
159: fis.close();
160: outResp.close();
161: System.out.println("zip end");
162:
163: return null;
164: }
165: }
|