Example usage for java.util.zip ZipInputStream getNextEntry

List of usage examples for java.util.zip ZipInputStream getNextEntry

Introduction

In this page you can find the example usage for java.util.zip ZipInputStream getNextEntry.

Prototype

public ZipEntry getNextEntry() throws IOException 

Source Link

Document

Reads the next ZIP file entry and positions the stream at the beginning of the entry data.

Usage

From source file:mj.ocraptor.extraction.tika.parser.epub.EpubParser.java

public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context)
        throws IOException, SAXException, TikaException {
    // Because an EPub file is often made up of multiple XHTML files,
    // we need explicit control over the start and end of the document
    XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
    xhtml.startDocument();//from w  w  w .j  av  a 2 s.c  o  m
    ContentHandler childHandler = new EmbeddedContentHandler(new BodyContentHandler(xhtml));

    ZipInputStream zip = new ZipInputStream(stream);
    ZipEntry entry = zip.getNextEntry();

    TikaImageHelper helper = new TikaImageHelper(metadata);
    try {
        while (entry != null) {
            // TODO: images
            String entryExtension = null;
            try {
                entryExtension = FilenameUtils.getExtension(new File(entry.getName()).getName());
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (entryExtension != null && FileType.isValidImageFileExtension(entryExtension)
                    && Config.inst().getProp(ConfigBool.ENABLE_IMAGE_OCR)) {
                File imageFile = null;
                try {
                    imageFile = TikaImageHelper.saveZipEntryToTemp(zip, entry);
                    helper.addImage(imageFile);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (imageFile != null) {
                        imageFile.delete();
                    }
                }
            } else if (entry.getName().equals("mimetype")) {
                String type = IOUtils.toString(zip, "UTF-8");
                metadata.set(Metadata.CONTENT_TYPE, type);
            } else if (entry.getName().equals("metadata.xml")) {
                meta.parse(zip, new DefaultHandler(), metadata, context);
            } else if (entry.getName().endsWith(".opf")) {
                meta.parse(zip, new DefaultHandler(), metadata, context);
            } else if (entry.getName().endsWith(".html") || entry.getName().endsWith(".xhtml")) {
                content.parse(zip, childHandler, metadata, context);
            }
            entry = zip.getNextEntry();
        }
        helper.addTextToHandler(xhtml);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (helper != null) {
            helper.close();
        }
    }

    // Finish everything
    xhtml.endDocument();
}

From source file:ZipTest.java

/**
 * Scans the contents of the ZIP archive and populates the combo box.
 *//*w  w  w . ja  v a  2s .c  o m*/
public void scanZipFile() {
    new SwingWorker<Void, String>() {
        protected Void doInBackground() throws Exception {
            ZipInputStream zin = new ZipInputStream(new FileInputStream(zipname));
            ZipEntry entry;
            while ((entry = zin.getNextEntry()) != null) {
                publish(entry.getName());
                zin.closeEntry();
            }
            zin.close();
            return null;
        }

        protected void process(List<String> names) {
            for (String name : names)
                fileCombo.addItem(name);

        }
    }.execute();
}

From source file:be.fedict.eid.applet.service.signer.asic.ASiCSignatureOutputStream.java

@Override
public void close() throws IOException {
    super.close();

    byte[] signatureData = toByteArray();

    /*//from  ww  w  .j  a  v a  2s. c o  m
     * Copy the original ZIP content.
     */
    ZipOutputStream zipOutputStream = new ZipOutputStream(this.targetOutputStream);
    ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(this.originalZipFile));
    ZipEntry zipEntry;
    while (null != (zipEntry = zipInputStream.getNextEntry())) {
        if (!zipEntry.getName().equals(ASiCUtil.SIGNATURE_FILE)) {
            ZipEntry newZipEntry = new ZipEntry(zipEntry.getName());
            zipOutputStream.putNextEntry(newZipEntry);
            LOG.debug("copying " + zipEntry.getName());
            IOUtils.copy(zipInputStream, zipOutputStream);
        }
    }
    zipInputStream.close();

    /*
     * Add the XML signature file to the ASiC package.
     */
    zipEntry = new ZipEntry(ASiCUtil.SIGNATURE_FILE);
    LOG.debug("writing " + zipEntry.getName());
    zipOutputStream.putNextEntry(zipEntry);
    IOUtils.write(signatureData, zipOutputStream);
    zipOutputStream.close();
}

From source file:br.com.ingenieux.mojo.jbake.SeedMojo.java

private void unpackZip(File tmpZipFile) throws IOException {
    ZipInputStream zis = new ZipInputStream(new FileInputStream(tmpZipFile));
    //get the zipped file list entry
    ZipEntry ze = zis.getNextEntry();

    while (ze != null) {
        if (ze.isDirectory()) {
            ze = zis.getNextEntry();/*from w  w w . ja v  a 2  s  . c  om*/
            continue;
        }

        String fileName = stripLeadingPath(ze.getName());
        File newFile = new File(outputDirectory + File.separator + fileName);

        new File(newFile.getParent()).mkdirs();

        FileOutputStream fos = new FileOutputStream(newFile);

        IOUtils.copy(zis, fos);

        fos.close();
        ze = zis.getNextEntry();
    }

    zis.closeEntry();
    zis.close();
}

From source file:org.pdfgal.pdfgalweb.utils.impl.ZipUtilsImpl.java

@Override
public List<String> saveFilesFromZip(final InputStream inputStream) throws IOException {

    final List<String> result = new ArrayList<String>();

    if (inputStream != null) {
        final ZipInputStream zis = new ZipInputStream(inputStream);

        ZipEntry entry = zis.getNextEntry();

        while (entry != null) {
            final byte[] buffer = new byte[1024];
            final String fileName = this.fileUtils.getAutogeneratedName(entry.getName());
            final File file = new File(fileName);

            final FileOutputStream fos = new FileOutputStream(file);

            int len;
            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }//from  www.  j ava  2 s  .  c  o m

            fos.close();
            result.add(fileName);
            entry = zis.getNextEntry();
        }

        zis.closeEntry();
        zis.close();
    }

    return result;
}

From source file:com.opengamma.util.db.script.ZipFileDbScript.java

@Override
public String getScript() throws IOException {
    ZipInputStream zippedIn = null;
    try {//www .jav  a  2 s  . com
        zippedIn = new ZipInputStream(new FileInputStream(getZipFile()));
        ZipEntry entry;
        while ((entry = zippedIn.getNextEntry()) != null) {
            if (entry.getName().equals(getEntryName())) {
                break;
            }
        }
        if (entry == null) {
            throw new OpenGammaRuntimeException(
                    "No entry found in zip file " + getZipFile() + " with name " + getEntryName());
        }
        return IOUtils.toString(zippedIn);
    } catch (FileNotFoundException e) {
        throw new OpenGammaRuntimeException("Zip file not found: " + getZipFile());
    } catch (IOException e) {
        throw new OpenGammaRuntimeException("Error reading from zip file: " + getZipFile());
    } finally {
        if (zippedIn != null) {
            try {
                zippedIn.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:ImportStream.java

/**
 * Wraps a stream with an import stream/*  ww w .j  a v a  2s . c  om*/
 * 
 * @param wrap The exported stream to wrap
 * @throws IOException If an error occurs wrapping the stream
 */
public ImportStream(java.io.InputStream wrap) throws IOException {
    java.util.zip.ZipInputStream zis;
    zis = new java.util.zip.ZipInputStream(ObfuscatingStream.unobfuscate(wrap));
    zis.getNextEntry();
    theInput = zis;
}

From source file:de.forsthaus.backend.service.impl.IpToCountryServiceImpl.java

@Override
public int importIP2CountryCSV() {
    try {//from  w w  w.  j a v  a 2s. c  om

        // first, delete all records in the ip2Country table
        getIpToCountryDAO().deleteAll();

        final URL url = new URL(this.updateUrl);
        final URLConnection conn = url.openConnection();
        final InputStream istream = conn.getInputStream();

        final ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(istream));

        zipInputStream.getNextEntry();

        final BufferedReader in = new BufferedReader(new InputStreamReader(zipInputStream));

        final CsvBeanReader csvb = new CsvBeanReader(in, CsvPreference.STANDARD_PREFERENCE);

        // List<IpToCountry> list = new ArrayList<IpToCountry>();

        IpToCountry tmp = null;
        int id = 1;
        while (null != (tmp = csvb.read(IpToCountry.class, this.stringNameMapping))) {
            tmp.setId(id++);
            getIpToCountryDAO().saveOrUpdate(tmp);

        }
        // close the stream !!!
        in.close();

        return getIpToCountryDAO().getCountAllIpToCountries();

    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.celements.photo.utilities.Unzip.java

/**
 * Get a List of names of all files contained in the zip file.
 * /*from ww  w  . ja va 2 s .  co m*/
 * @param zipFile byte array of the zip file.
 * @return List of all filenames (and directory names - ending with a file seperator) contained in the zip file.
 */
public List<String> getZipContentList(byte[] zipFile) {
    String fileSep = System.getProperty("file.separator");
    List<String> contentList = new ArrayList<String>();
    ZipInputStream zipStream = getZipInputStream(zipFile);

    try {
        while (zipStream.available() > 0) {
            ZipEntry entry = zipStream.getNextEntry();
            if (entry != null) {
                String fileName = entry.getName();
                if (entry.isDirectory() && !fileName.endsWith(fileSep)) {
                    fileName += fileSep;
                }
                contentList.add(fileName);
            }
        }
    } catch (IOException e) {
        mLogger.error(e);
    }

    return contentList;
}

From source file:gdt.data.entity.ArchiveHandler.java

/**
 * Extract zip archive stream into the target directory
 * @param targetDirectory$ the path of the target directory
 * @param zis the zip archive input stream.
 *///from w  w  w. j av a  2  s. c o m
public static void extractEntitiesFromZip(String targetDirectory$, ZipInputStream zis) {
    try {
        ZipEntry entry = null;
        File outputFile;
        File outputDir;
        FileOutputStream outputStream;
        while ((entry = zis.getNextEntry()) != null) {
            outputFile = new File(targetDirectory$ + "/" + entry.getName());
            outputDir = outputFile.getParentFile();
            if (!outputDir.exists())
                outputDir.mkdirs();
            if (entry.isDirectory()) {
                outputFile.mkdir();
            } else {
                outputStream = new FileOutputStream(outputFile);
                IOUtils.copy(zis, outputStream);
                outputStream.close();
            }
        }
        zis.close();
    } catch (Exception e) {
        Logger.getLogger(ArchiveHandler.class.getName()).severe(e.toString());
    }
}