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:it.greenvulcano.configuration.BaseConfigurationManager.java

@Override
public byte[] extract(String name, String entry) {

    Path configurationArchivePath = getConfigurationPath(name);

    try (ZipInputStream configurationArchive = new ZipInputStream(
            Files.newInputStream(configurationArchivePath, StandardOpenOption.READ))) {

        ZipEntry zipEntry = null;
        while ((zipEntry = configurationArchive.getNextEntry()) != null) {

            if (zipEntry.getName().equals(entry)) {
                byte[] entryData = IOUtils.toByteArray(configurationArchive);
                return entryData;
            }// w w w . j av a 2s  .  c  o m
        }
    } catch (Exception e) {
        LOG.error("Failed to extract entry " + entry + " from archive " + configurationArchivePath, e);
    }

    return new byte[] {};
}

From source file:com.joliciel.jochre.lexicon.TextFileLexicon.java

public static TextFileLexicon deserialize(File memoryBaseFile) {
    LOG.debug("deserializeMemoryBase");
    boolean isZip = false;
    if (memoryBaseFile.getName().endsWith(".zip"))
        isZip = true;//w w w. j  av a 2s. c  o  m

    TextFileLexicon memoryBase = null;
    ZipInputStream zis = null;
    FileInputStream fis = null;
    ObjectInputStream in = null;

    try {
        fis = new FileInputStream(memoryBaseFile);
        if (isZip) {
            zis = new ZipInputStream(fis);
            memoryBase = TextFileLexicon.deserialize(zis);
        } else {
            in = new ObjectInputStream(fis);
            memoryBase = (TextFileLexicon) in.readObject();
            in.close();
        }
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    } catch (ClassNotFoundException cnfe) {
        throw new RuntimeException(cnfe);
    }

    return memoryBase;
}

From source file:JarMaker.java

/**
 * @param f_name : source zip file path name
 * @param dir_name : target dir file path
 *//*from w w  w .j  a  v a 2  s  .c o m*/
public static void unpackJar(String f_name, String dir_name) throws IOException {

    BufferedInputStream bism = new BufferedInputStream(new FileInputStream(f_name));

    ZipInputStream zism = new ZipInputStream(bism);

    for (ZipEntry z = zism.getNextEntry(); z != null; z = zism.getNextEntry()) {
        File f = new File(dir_name, z.getName());
        if (z.isDirectory()) {
            f.mkdirs();
        } else {
            f.getParentFile().mkdirs();

            BufferedOutputStream bosm = new BufferedOutputStream(new FileOutputStream(f));
            int i;
            byte[] buffer = new byte[1024 * 512];
            try {
                while ((i = zism.read(buffer, 0, buffer.length)) != -1)
                    bosm.write(buffer, 0, i);
            } catch (IOException ie) {
                throw ie;
            }
            bosm.close();
        }
    }
    zism.close();
    bism.close();
}

From source file:com.webpagebytes.cms.controllers.FlatStorageImporterExporter.java

public void importFromZipStep1(InputStream is) throws WPBIOException {
    ZipInputStream zis = new ZipInputStream(is);
    // we need to stop the notifications during import
    dataStorage.stopNotifications();//from ww  w. ja va  2s.c o m
    try {
        ZipEntry ze = null;
        while ((ze = zis.getNextEntry()) != null) {
            String name = ze.getName();
            if (name.indexOf(PATH_URIS) >= 0) {
                if (name.indexOf("/parameters/") >= 0 && name.indexOf("metadata.xml") >= 0) {
                    importParameter(zis);
                } else if (name.indexOf("metadata.xml") >= 0) {
                    // this is a web site url
                    importUri(zis);
                }
            } else if (name.indexOf(PATH_GLOBALS) >= 0) {
                if (name.indexOf("metadata.xml") >= 0) {
                    importParameter(zis);
                }
            } else if (name.indexOf(PATH_SITE_PAGES) >= 0) {
                if (name.indexOf("/parameters/") >= 0 && name.indexOf("metadata.xml") >= 0) {
                    importParameter(zis);
                } else if (name.indexOf("metadata.xml") >= 0) {
                    importWebPage(zis);
                }
            } else if (name.indexOf(PATH_SITE_PAGES_MODULES) >= 0) {
                if (name.indexOf("metadata.xml") >= 0) {
                    importPageModule(zis);
                }
            } else if (name.indexOf(PATH_ARTICLES) >= 0) {
                if (name.indexOf("metadata.xml") >= 0) {
                    importArticle(zis);
                }
            } else if (name.indexOf(PATH_MESSAGES) >= 0) {
                if (name.indexOf("metadata.xml") >= 0) {
                    importMessage(zis);
                }
            } else if (name.indexOf(PATH_FILES) >= 0) {
                if (name.indexOf("metadata.xml") >= 0) {
                    importFile(zis);
                }
            } else if (name.indexOf(PATH_LOCALES) >= 0) {
                if (name.indexOf("metadata.xml") >= 0) {
                    importProject(zis);
                }
            }
            zis.closeEntry();
        }
    } catch (Exception e) {
        log.log(Level.SEVERE, e.getMessage(), e);
        throw new WPBIOException("cannot import from  zip step 1", e);
    }
}

From source file:com.fdt.sdl.admin.ui.action.UploadAction.java

public void extractZip(InputStream is) throws IOException {
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is));
    int BUFFER = 4096;
    BufferedOutputStream dest = null;
    ZipEntry entry;/*from w w w . j  a  v  a2  s.  com*/
    while ((entry = zis.getNextEntry()) != null) {
        // System.out.println("Extracting: " +entry);
        // out.println(entry.getName());
        String entryPath = entry.getName();
        if (entryPath.indexOf('\\') >= 0) {
            entryPath = entryPath.replace('\\', File.separatorChar);
        }
        String destFilePath = WebserverStatic.getRootDirectory() + entryPath;
        int count;
        byte data[] = new byte[BUFFER];
        // write the files to the disk
        File f = new File(destFilePath);

        if (!f.getParentFile().exists()) {
            f.getParentFile().mkdirs();
        }
        if (!f.exists()) {
            FileUtil.createNewFile(f);
            logger.info("creating file: " + f);
        } else {
            logger.info("already existing file: " + f);
            if (f.isDirectory()) {
                continue;
            }
        }

        FileOutputStream fos = new FileOutputStream(f);
        dest = new BufferedOutputStream(fos, BUFFER);
        while ((count = zis.read(data, 0, BUFFER)) != -1) {
            dest.write(data, 0, count);
        }
        dest.flush();
        dest.close();
    }
    zis.close();
}

From source file:com.obnsoft.ptcm3.MyApplication.java

private boolean downloadZipFile(String url, String target, String fileName, boolean force) {
    if (!force && getFileStreamPath(fileName).exists()) {
        return true;
    }//from   w  w  w. j av a  2 s . c o  m
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse httpResponse = httpclient.execute(new HttpGet(url));
        ZipInputStream zin = new ZipInputStream(httpResponse.getEntity().getContent());
        for (ZipEntry entry = zin.getNextEntry(); entry != null; entry = zin.getNextEntry()) {
            if (target.equals(entry.getName())) {
                OutputStream out = openFileOutput(fileName, MODE_PRIVATE);
                byte[] buffer = new byte[1024 * 1024];
                int length;
                while ((length = zin.read(buffer)) >= 0) {
                    out.write(buffer, 0, length);
                }
                out.close();
                break;
            }
        }
        zin.close();
    } catch (Exception e) {
        e.printStackTrace();
        getFileStreamPath(fileName).delete();
        return false;
    }
    return true;
}

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

@Override
protected Document getEnvelopingDocument() throws ParserConfigurationException, IOException, SAXException {
    FileInputStream fileInputStream = new FileInputStream(this.tmpFile);
    ZipInputStream zipInputStream = new ZipInputStream(fileInputStream);
    ZipEntry zipEntry;// w  w w . j a v  a 2 s  . c o m
    while (null != (zipEntry = zipInputStream.getNextEntry())) {
        if (ASiCUtil.isSignatureZipEntry(zipEntry)) {
            Document documentSignaturesDocument = ODFUtil.loadDocument(zipInputStream);
            return documentSignaturesDocument;
        }
    }
    Document document = ASiCUtil.createNewSignatureDocument();
    return document;
}

From source file:org.ktunaxa.referral.server.mvc.UploadGeometryController.java

private URL unzipShape(byte[] fileContent) throws IOException {
    String tempDir = System.getProperty("java.io.tmpdir");

    URL url = null;/*w ww  .  ja  v a  2 s  . c  om*/
    ZipInputStream zin = new ZipInputStream(new ByteArrayInputStream(fileContent));
    try {
        ZipEntry entry;
        while ((entry = zin.getNextEntry()) != null) {
            log.info("Extracting: " + entry);
            String name = tempDir + "/" + entry.getName();
            tempFiles.add(name);
            if (name.endsWith(".shp")) {
                url = new URL("file://" + name);
            }
            int count;
            byte[] data = new byte[BUFFER];
            // write the files to the disk
            deleteFileIfExists(name);
            FileOutputStream fos = new FileOutputStream(name);
            BufferedOutputStream destination = new BufferedOutputStream(fos, BUFFER);
            try {
                while ((count = zin.read(data, 0, BUFFER)) != -1) {
                    destination.write(data, 0, count);
                }
                destination.flush();
            } finally {
                destination.close();
            }
        }
    } finally {
        zin.close();
    }
    if (url == null) {
        throw new IllegalArgumentException("Missing .shp file");
    }
    return url;
}

From source file:eu.openanalytics.rsb.message.MultiFilesJob.java

/**
 * Adds all the files contained in a Zip archive to a job. Rejects Zips that
 * contain sub-directories.// ww w  .j  a v a2s .c o  m
 * 
 * @param data
 * @param job
 * @throws IOException
 */
public static void addZipFilesToJob(final InputStream data, final MultiFilesJob job) throws IOException {
    final ZipInputStream zis = new ZipInputStream(data);
    ZipEntry ze = null;

    while ((ze = zis.getNextEntry()) != null) {
        if (ze.isDirectory()) {
            job.destroy();
            throw new IllegalArgumentException("Invalid zip archive: nested directories are not supported");
        }
        job.addFile(ze.getName(), zis);
        zis.closeEntry();
    }

    IOUtils.closeQuietly(zis);
}

From source file:it.digitalhumanities.dhcpublisher.DHCPublisher.java

private void unzipFile(int counter, File file, File targetDir) throws IOException {

    String subDirName = counter + "_" + file.getName().substring(0, file.getName().length() - 4);
    if (subDirName.length() > 60) {
        subDirName = subDirName.substring(0, 60);
    }/*from   ww  w  .  java2s .  c o m*/
    ;

    File subDir = new File(targetDir, subDirName);
    if (!subDir.exists()) {
        subDir.mkdirs();
    }

    try (ZipInputStream zis = new ZipInputStream(new FileInputStream(file))) {

        ZipEntry ze = zis.getNextEntry();

        while (ze != null) {
            File targetFile = new File(subDir, ze.getName());
            targetFile.getParentFile().mkdirs();

            try (FileOutputStream fos = new FileOutputStream(targetFile)) {
                IOUtils.copy(zis, fos);
            }

            ze = zis.getNextEntry();
        }
    }
}