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; 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 org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * This contains some zip functions. * * @author Smitha Gudur (smitha@redbasin.com) * @version $Revision: 1.1 $ */ public class TraversalZipTool { /** Logger for this class and subclasses */ protected final Log logger = LogFactory.getLog(getClass()); private final int CHUNK = 500; private final int MAX_MEDIA_SIZE = 10000000; private volatile ValidateImage 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; } /** * Return just the fileName given the full path name. Does not check * for nulls. * * @param fullPath * @return String */ public String fileName(String fullPath) { return fullPath.substring(fullPath.lastIndexOf(File.separatorChar) + 1); } /** * Return the suffix of the file name. * * @param name * @return String */ public String getSuffix(String name) { return name.substring(name.lastIndexOf(".") + 1).toLowerCase(); } public void setValidImage(ValidateImage validImage) { this.validImage = validImage; } }