Java tutorial
/** * Copyright (c) 2001-2012 "Redbasin Networks, INC" [http://redbasin.org] * * This file is part of Redbasin OpenDocShare community project. * * Redbasin OpenDocShare is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package util; /** * Author: Smitha Gudur * FileName: CreationZipTool.java * It provides methods for zip */ import java.io.ByteArrayInputStream; import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; import javax.servlet.http.HttpServletResponse; import model.Photo; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * @author Smitha Gudur (smitha@redbasin.com) * @version $Revision: 1.1 $ */ public class CreationZipTool { protected final Log logger = LogFactory.getLog(getClass()); /** Logger for this class and subclasses */ private final int CHUNK = 500; private final int MAX_MEDIA_SIZE = 10000000; //private volatile ValidImage validImage = null; /** * Returns the zip entries as Media beans. * * @param zipBytes * @return List of Media beans */ public List getEntries(byte[] zipBytes) throws SomeZipException { ByteArrayInputStream bis = null; ZipInputStream zis = null; ZipEntry zipEntry = null; List entries = new ArrayList(); byte[] b = null; try { bis = new ByteArrayInputStream(zipBytes); zis = new ZipInputStream(bis); boolean notDone = true; zipEntry = zis.getNextEntry(); while (zipEntry != null) { if (zipEntry.isDirectory()) { zipEntry = zis.getNextEntry(); continue; } b = new byte[MAX_MEDIA_SIZE]; int offset = 0; int rb = 0; while ((zis.available() != 0) && ((rb = zis.read(b, offset, CHUNK)) != -1)) { offset += rb; } if ((zis.available() == 0) || (rb == -1)) { String entryName = zipEntry.getName(); String suffix = getSuffix(entryName); //if (validImage.isValidSuffix(suffix)) { Media media = new Media(); logger.info("Found New Image " + offset + " bytes"); media.setData(new String(b, 0, offset).getBytes()); media.setSize(offset); logger.info("ZipEntry name = " + entryName); media.setName(fileName(entryName)); entries.add(media); //} else { //logger.warn("Bad image file in zip, ignoring " + entryName); //} zis.closeEntry(); zipEntry = zis.getNextEntry(); } } zis.close(); } catch (Exception e) { throw new SomeZipException("Error processing zip bytes", e); } return entries; } public void createZip(List zipfiles, HttpServletResponse response, String filename) throws Exception { try { if ((zipfiles == null) || (response == null) || (filename == null)) { return; } ZipOutputStream zos = new ZipOutputStream(response.getOutputStream()); String header = "attachment; filename=" + filename; response.setHeader("Content-Disposition", header); response.setContentType("application/octet-stream"); //response.setHeader("Content-Disposition", "attachment; filename=photos.zip"); /** * Get the list of files from an array */ for (int i = 0; i < zipfiles.size(); i++) { // logger.info("filename " + ((Photo)zipfiles.get(i)).getValue(DbConstants.BTITLE)); /** * Add a file name & file content to the entry */ ZipEntry entry = new ZipEntry(((Photo) zipfiles.get(i)).getValue(DbConstants.BTITLE)); zos.putNextEntry(entry); byte[] b = ((Photo) zipfiles.get(i)).getBlob(); zos.write(b, 0, b.length); zos.closeEntry(); } zos.flush(); zos.close(); } catch (Exception e) { throw new Exception("Could not create zip file: ", e); } } public String getSuffix(String name) { return name.substring(name.lastIndexOf(".") + 1).toLowerCase(); } public String fileName(String fullPath) { return fullPath.substring(fullPath.lastIndexOf(File.separatorChar) + 1); } }