Example usage for java.util.zip ZipInputStream ZipInputStream

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

Introduction

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

Prototype

public ZipInputStream(InputStream in) 

Source Link

Document

Creates a new ZIP input stream.

Usage

From source file:Main.java

/** Returns an InputStream that will read a specific entry
 * from a zip file./*w  w w  . ja va2 s.com*/
 * @param file the zip file.
 * @param entryName the name of the entry.
 * @return an InputStream that reads that entry.
 * @throws IOException
 */
public static InputStream getZipEntry(File file, String entryName) throws IOException {
    FileInputStream in = new FileInputStream(file);
    ZipInputStream zipIn = new ZipInputStream(in);
    ZipEntry e = zipIn.getNextEntry();
    while (e != null) {
        if (e.getName().equals(entryName))
            return zipIn;
        e = zipIn.getNextEntry();
    }
    return null;
}

From source file:be.fedict.eid.applet.service.signer.ooxml.OOXMLSignatureVerifier.java

/**
 * Checks whether the file referred by the given URL is an OOXML document.
 * /*  w w w.ja  v  a 2  s .  c  om*/
 * @param url
 * @return
 * @throws IOException
 */
public static boolean isOOXML(URL url) throws IOException {
    ZipInputStream zipInputStream = new ZipInputStream(url.openStream());
    ZipEntry zipEntry;
    while (null != (zipEntry = zipInputStream.getNextEntry())) {
        if (false == "[Content_Types].xml".equals(zipEntry.getName())) {
            continue;
        }
        return true;
    }
    return false;
}

From source file:com.joliciel.talismane.parser.ParsingConstrainerImpl.java

public static ParsingConstrainer loadFromFile(File inFile) {
    try {/* ww w . j a  v a 2 s.  c o  m*/
        ZipInputStream zis = new ZipInputStream(new FileInputStream(inFile));
        zis.getNextEntry();
        ObjectInputStream in = new ObjectInputStream(zis);
        ParsingConstrainer parsingConstrainer = null;
        try {
            parsingConstrainer = (ParsingConstrainer) in.readObject();
        } catch (ClassNotFoundException e) {
            LogUtils.logError(LOG, e);
            throw new RuntimeException(e);
        }
        return parsingConstrainer;
    } catch (IOException ioe) {
        LogUtils.logError(LOG, ioe);
        throw new RuntimeException(ioe);
    }
}

From source file:com.joliciel.talismane.GenericLanguageImplementation.java

@SuppressWarnings("unused")
private static ZipInputStream getZipInputStreamFromResource(String resource) {
    InputStream inputStream = getInputStreamFromResource(resource);
    ZipInputStream zis = new ZipInputStream(inputStream);

    return zis;//ww  w .  ja  v a2s.co m
}

From source file:com.silverpeas.wiki.WikiInstanciator.java

protected void createPages(File directory, String componentId) throws IOException, WikiException {
    ZipInputStream zipFile = new ZipInputStream(loader.getResourceAsStream("pages.zip"));
    ZipEntry page = zipFile.getNextEntry();
    while (page != null) {
        String pageName = page.getName();
        File newPage = new File(directory, pageName);
        if (page.isDirectory()) {
            newPage.mkdirs();/* w w w . j  ava  2s .co  m*/
        } else {
            FileUtil.writeFile(newPage, new InputStreamReader(zipFile, "UTF-8"));
            PageDetail newPageDetail = new PageDetail();
            newPageDetail.setInstanceId(componentId);
            newPageDetail.setPageName(pageName.substring(0, pageName.lastIndexOf('.')));
            wikiDAO.createPage(newPageDetail);
            zipFile.closeEntry();
            page = zipFile.getNextEntry();
        }
    }
}

From source file:es.juntadeandalucia.panelGestion.negocio.utiles.file.shape.LocalShapeReader.java

private void unzipShapefiles(File shapefileCompressedData) throws IOException {
    IOException error = null;//from w w  w  .  ja v  a 2  s  .  co m
    if (!shapefilesFolder.exists()) {
        shapefilesFolder.mkdirs();
    }
    InputStream bis = new FileInputStream(shapefileCompressedData);
    ZipInputStream zis = new ZipInputStream(bis);
    ZipEntry ze;
    try {
        while ((ze = zis.getNextEntry()) != null) {
            if (!ze.isDirectory()) {
                String fileName = ze.getName();
                File unzippedFile = new File(shapefilesFolder, fileName);
                byte[] buffer = new byte[1024];
                FileOutputStream fos = new FileOutputStream(unzippedFile);
                int len;
                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }
                fos.close();
                if (fileName.toLowerCase().endsWith(".shp")) {
                    shapefilePath = unzippedFile.toURI();
                }
            }
        }
    } catch (IOException e) {
        error = e;
    } finally {
        try {
            zis.closeEntry();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            zis.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    if (error != null) {
        throw error;
    }
}

From source file:com.metamx.druid.loading.S3ZippedSegmentGetter.java

@Override
public File getSegmentFiles(Map<String, Object> loadSpec) throws StorageAdapterLoadingException {
    String s3Bucket = MapUtils.getString(loadSpec, "bucket");
    String s3Path = MapUtils.getString(loadSpec, "key");

    if (s3Path.startsWith("/")) {
        s3Path = s3Path.substring(1);
    }//from   w  w  w  .  j  a  va2s.com

    log.info("Loading index at path[s3://%s/%s]", s3Bucket, s3Path);

    S3Object s3Obj = null;
    File tmpFile = null;
    try {
        if (!s3Client.isObjectInBucket(s3Bucket, s3Path)) {
            throw new StorageAdapterLoadingException("IndexFile[s3://%s/%s] does not exist.", s3Bucket, s3Path);
        }

        File cacheFile = new File(config.getCacheDirectory(), computeCacheFilePath(s3Bucket, s3Path));

        if (cacheFile.exists()) {
            S3Object objDetails = s3Client.getObjectDetails(new S3Bucket(s3Bucket), s3Path);
            DateTime cacheFileLastModified = new DateTime(cacheFile.lastModified());
            DateTime s3ObjLastModified = new DateTime(objDetails.getLastModifiedDate().getTime());
            if (cacheFileLastModified.isAfter(s3ObjLastModified)) {
                log.info("Found cacheFile[%s] with modified[%s], which is after s3Obj[%s].  Using.", cacheFile,
                        cacheFileLastModified, s3ObjLastModified);
                return cacheFile;
            }
            FileUtils.deleteDirectory(cacheFile);
        }

        long currTime = System.currentTimeMillis();

        tmpFile = File.createTempFile(s3Bucket, new DateTime().toString());
        log.info("Downloading file[s3://%s/%s] to local tmpFile[%s] for cacheFile[%s]", s3Bucket, s3Path,
                tmpFile, cacheFile);

        s3Obj = s3Client.getObject(new S3Bucket(s3Bucket), s3Path);
        StreamUtils.copyToFileAndClose(s3Obj.getDataInputStream(), tmpFile);
        final long downloadEndTime = System.currentTimeMillis();
        log.info("Download of file[%s] completed in %,d millis", cacheFile, downloadEndTime - currTime);

        if (cacheFile.exists()) {
            FileUtils.deleteDirectory(cacheFile);
        }
        cacheFile.mkdirs();

        ZipInputStream zipIn = null;
        OutputStream out = null;
        ZipEntry entry = null;
        try {
            zipIn = new ZipInputStream(new BufferedInputStream(new FileInputStream(tmpFile)));
            while ((entry = zipIn.getNextEntry()) != null) {
                out = new FileOutputStream(new File(cacheFile, entry.getName()));
                IOUtils.copy(zipIn, out);
                zipIn.closeEntry();
                Closeables.closeQuietly(out);
                out = null;
            }
        } finally {
            Closeables.closeQuietly(out);
            Closeables.closeQuietly(zipIn);
        }

        long endTime = System.currentTimeMillis();
        log.info("Local processing of file[%s] done in %,d millis", cacheFile, endTime - downloadEndTime);

        log.info("Deleting tmpFile[%s]", tmpFile);
        tmpFile.delete();

        return cacheFile;
    } catch (Exception e) {
        throw new StorageAdapterLoadingException(e, e.getMessage());
    } finally {
        S3Utils.closeStreamsQuietly(s3Obj);
        if (tmpFile != null && tmpFile.exists()) {
            log.warn("Deleting tmpFile[%s] in finally block.  Why?", tmpFile);
            tmpFile.delete();
        }
    }
}

From source file:it.greenvulcano.configuration.BaseConfigurationManager.java

@Override
public void install(String name, byte[] archive) throws IOException {

    Path configurationPath = getConfigurationPath(name);
    try (ZipInputStream zipArchive = new ZipInputStream(new ByteArrayInputStream(archive))) {
        if (zipArchive.getNextEntry() != null) {
            Files.write(configurationPath, archive, StandardOpenOption.WRITE, StandardOpenOption.CREATE,
                    StandardOpenOption.TRUNCATE_EXISTING);
        } else {/*from  www  . j av  a2s. com*/
            throw new IOException("Empty or invalid zip archive");
        }

    }

}

From source file:com.pontecultural.flashcards.ReadSpreadsheet.java

public void run() {
    try {//from w w w .j  a v a 2s . c  o  m
        // This class is used to upload a zip file via 
        // the web (ie,fileData). To test it, the file is 
        // give to it directly via odsFile. One of 
        // which must be defined. 
        assertFalse(odsFile == null && fileData == null);
        XMLReader reader = null;
        final String ZIP_CONTENT = "content.xml";
        // Open office files are zipped. 
        // Walk through it and find "content.xml"

        ZipInputStream zin = null;
        try {
            if (fileData != null) {
                zin = new ZipInputStream(new BufferedInputStream(fileData.getInputStream()));
            } else {
                zin = new ZipInputStream(new BufferedInputStream(new FileInputStream(odsFile)));
            }

            ZipEntry entry;
            while ((entry = zin.getNextEntry()) != null) {
                if (entry.getName().equals(ZIP_CONTENT)) {
                    break;
                }
            }
            SAXParserFactory spf = SAXParserFactory.newInstance();
            //spf.setValidating(validate);

            SAXParser parser = spf.newSAXParser();
            reader = parser.getXMLReader();

            // reader.setErrorHandler(new MyErrorHandler());
            reader.setContentHandler(this);
            // reader.parse(new InputSource(zf.getInputStream(entry)));

            reader.parse(new InputSource(zin));

        } catch (ParserConfigurationException pce) {
            pce.printStackTrace();
        } catch (SAXParseException spe) {
            spe.printStackTrace();
        } catch (SAXException se) {
            se.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (zin != null)
                zin.close();
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
}

From source file:net.grinder.util.LogCompressUtils.java

/**
 * Decompress the given the {@link InputStream} into the given {@link OutputStream}.
 *
 * @param inputStream  input stream of the compressed file
 * @param outputStream file to be written
 * @param limit        the limit of the output
 *//*from ww w  .j  av  a  2s  .  c o  m*/
public static void decompress(InputStream inputStream, OutputStream outputStream, long limit) {
    ZipInputStream zipInputStream = null;
    try {
        zipInputStream = new ZipInputStream(inputStream);
        byte[] buffer = new byte[COMPRESS_BUFFER_SIZE];
        int count;
        long total = 0;
        checkNotNull(zipInputStream.getNextEntry(), "In zip, it should have at least one entry");
        do {
            while ((count = zipInputStream.read(buffer, 0, COMPRESS_BUFFER_SIZE)) != -1) {
                total += count;
                if (total >= limit) {
                    break;
                }
                outputStream.write(buffer, 0, count);
            }
        } while (zipInputStream.getNextEntry() != null);
        outputStream.flush();
    } catch (IOException e) {
        LOGGER.error("Error occurs while decompressing {}", e.getMessage());
        LOGGER.debug("Details : ", e);
    } finally {
        IOUtils.closeQuietly(zipInputStream);
    }
}