/************************************************************************ * * SimpleZipReader.java * * Copyright: 2002-2012 by Henrik Just * * This file is part of Writer2LaTeX. * * Writer2LaTeX 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. * * Writer2LaTeX 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 Writer2LaTeX. If not, see . * * Version 1.4 (2012-03-27) * */ package writer2latex.util; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.Map; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class SimpleZipReader { private final static int BUFFERSIZE = 1024; private Map entries = new HashMap(); /** Read a zipped stream * * @param is InputStream to read * * @throws IOException if an I/O error occurs */ public void read(InputStream is) throws IOException { ZipInputStream zis = new ZipInputStream(is); ZipEntry entry = null; while ((entry=zis.getNextEntry())!=null) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); int nLen = 0; byte buffer[] = new byte[BUFFERSIZE]; while ((nLen = zis.read(buffer)) > 0) { baos.write(buffer, 0, nLen); } byte bytes[] = baos.toByteArray(); entries.put(entry.getName(), bytes); } zis.close(); } /** Get an entry from the ZIP file. Getting should be taken quite literally here: * You can only get an entry once: The SimpleZipReader removes the entry from the * collection when this method is called (memory optimization). * * @param sName the name (path) of the ZIP entry * * @return a byte array with the contents of the entry, or null if the entry does not exist */ public byte[] getEntry(String sName) { if (entries.containsKey(sName)) { byte[] bytes = entries.get(sName); entries.remove(sName); return bytes; } return null; } }