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:abfab3d.shapejs.Project.java

private static void extractZip(String zipFile, String outputFolder, Map<String, String> sceneFiles,
        List<String> resources) {
    byte[] buffer = new byte[1024];

    try {/*from  ww w .  j ava 2s . com*/
        //create output directory is not exists
        File folder = new File(outputFolder);
        if (!folder.exists()) {
            folder.mkdir();
        }

        ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
        ZipEntry ze = zis.getNextEntry();

        while (ze != null) {
            // Ignore directories
            if (ze.isDirectory())
                continue;

            String fileName = ze.getName();
            File newFile = new File(outputFolder + File.separator + fileName);
            System.out.println("file unzip : " + newFile.getAbsoluteFile());

            FileOutputStream fos = new FileOutputStream(newFile);

            int len;
            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }

            // Save path to the script and parameters files
            if (fileName.endsWith(".json")) {
                sceneFiles.put("paramFile", newFile.getAbsolutePath());
            } else if (fileName.endsWith(".js")) {
                sceneFiles.put("scriptFile", newFile.getAbsolutePath());
            } else {
                resources.add(newFile.getAbsolutePath());
            }

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

        zis.closeEntry();
        zis.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:net.solarnetwork.node.backup.DefaultBackupManager.java

@Override
public void importBackupArchive(InputStream archive) throws IOException {
    final ZipInputStream zin = new ZipInputStream(archive);
    while (true) {
        final ZipEntry entry = zin.getNextEntry();
        if (entry == null) {
            break;
        }/*w ww  .jav  a 2s .  co  m*/
        final String path = entry.getName();
        log.debug("Restoring backup resource {}", path);
        final int providerIndex = path.indexOf('/');
        if (providerIndex != -1) {
            final String providerKey = path.substring(0, providerIndex);
            for (BackupResourceProvider provider : resourceProviders) {
                if (providerKey.equals(provider.getKey())) {
                    provider.restoreBackupResource(new BackupResource() {

                        @Override
                        public String getBackupPath() {
                            return path.substring(providerIndex + 1);
                        }

                        @Override
                        public InputStream getInputStream() throws IOException {
                            return new FilterInputStream(zin) {

                                @Override
                                public void close() throws IOException {
                                    // don't close me
                                }

                            };
                        }

                        @Override
                        public long getModificationDate() {
                            return entry.getTime();
                        }

                    });
                    break;
                }
            }
        }
    }
}

From source file:org.metaeffekt.dcc.agent.DccAgentTest.java

private Set<String> getStates(HttpResponse response) throws IOException {

    Set<String> states = new HashSet<>();

    try (ZipInputStream zipStream = new ZipInputStream(response.getEntity().getContent());) {

        ZipEntry zipEntry = zipStream.getNextEntry();

        while (zipEntry != null) {
            String name = zipEntry.getName();
            name = name.substring(0, name.lastIndexOf("."));
            states.add(name);/*from  w  ww.j  a  v a  2 s  . co  m*/
            if (zipStream.available() > 0) {
                zipEntry = zipStream.getNextEntry();
            }
        }
    } finally {
        EntityUtils.consume(response.getEntity());
    }

    return states;
}

From source file:net.firejack.platform.core.utils.ArchiveUtils.java

/**
 * @param zipFile/*from  w ww.  j a v a  2  s  .c om*/
 * @param files
 * @throws java.io.IOException
 */
public static void addFilesToZip(File zipFile, Map<String, File> files) throws IOException {
    // get a temp file
    File tempFile = File.createTempFile(zipFile.getName(), null);
    // delete it, otherwise you cannot rename your existing zip to it.
    FileUtils.deleteQuietly(tempFile);

    boolean renameOk = zipFile.renameTo(tempFile);
    if (!renameOk) {
        throw new RuntimeException(
                "could not rename the file " + zipFile.getAbsolutePath() + " to " + tempFile.getAbsolutePath());
    }

    ZipInputStream zin = new ZipInputStream(new FileInputStream(tempFile));
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));

    ZipEntry entry = zin.getNextEntry();
    while (entry != null) {
        String name = entry.getName();
        boolean notInFiles = true;
        for (Map.Entry<String, File> e : files.entrySet()) {
            if (e.getKey().equals(name)) {
                notInFiles = false;
                break;
            }
        }
        if (notInFiles) {
            out.putNextEntry(new ZipEntry(name));
            IOUtils.copy(zin, out);
        }
        entry = zin.getNextEntry();
    }
    // Close the streams
    zin.close();
    // Compress the files
    for (Map.Entry<String, File> e : files.entrySet()) {
        InputStream in = new FileInputStream(e.getValue());
        // Add ZIP entry to output stream.
        out.putNextEntry(new ZipEntry(e.getKey()));
        // Transfer bytes from the file to the ZIP file

        IOUtils.copy(in, out);
        // Complete the entry
        out.closeEntry();
        IOUtils.closeQuietly(in);
    }
    // Complete the ZIP file
    IOUtils.closeQuietly(out);
    FileUtils.deleteQuietly(tempFile);
}

From source file:gov.va.chir.tagline.dao.FileDao.java

public static TagLineModel loadTagLineModel(final File file) throws Exception {
    final TagLineModel model = new TagLineModel();

    // Unzip each file to temp
    final File temp = new File(System.getProperty("java.io.tmpdir"));

    byte[] buffer = new byte[BUFFER_SIZE];

    final ZipInputStream zis = new ZipInputStream(new FileInputStream(file));

    ZipEntry entry = zis.getNextEntry();

    while (entry != null) {
        final String name = entry.getName();

        File tempFile = new File(temp, name);

        // Write out file
        final FileOutputStream fos = new FileOutputStream(tempFile);
        int len;//w w  w  .  j  av a2s  . c  o m
        while ((len = zis.read(buffer)) > 0) {
            fos.write(buffer, 0, len);
        }

        fos.close();

        // Determine which file was written
        if (name.equalsIgnoreCase(FILENAME_FEATURES)) {
            model.setFeatures(loadFeatures(tempFile));
        } else if (name.equalsIgnoreCase(FILENAME_HEADER)) {
            model.setHeader(loadHeader(tempFile));
        } else if (name.equalsIgnoreCase(FILENAME_MODEL)) {
            model.setModel(loadModel(tempFile));
        } else {
            throw new IllegalStateException(String.format("Unknown file in TagLine model file (%s)", name));
        }

        // Delete temp file
        tempFile.delete();

        // Get next entry
        zis.closeEntry();
        entry = zis.getNextEntry();
    }

    zis.close();

    return model;
}

From source file:edu.umd.cs.marmoset.modelClasses.ZipFileAggregator.java

public static boolean isValidZipfile(byte[] byteArr) {
    ZipInputStream zipIn = new ZipInputStream(new ByteArrayInputStream(byteArr));
    boolean areBytesAZipfile = false;
    try {//from  w w  w.j a  va 2  s  .com
        while (true) {
            ZipEntry entry = zipIn.getNextEntry();
            if (entry == null)
                break;
            // We need to read at least one valid entry for this to be a valid zipfile
            areBytesAZipfile = true;
        }
    } catch (Throwable e) {
        return false;
    }
    return areBytesAZipfile;
}

From source file:com.carolinarollergirls.scoreboard.jetty.MediaServlet.java

protected void processZipFileItem(FileItemFactory factory, FileItem zip, List<FileItem> fileItems)
        throws IOException {
    ZipInputStream ziS = new ZipInputStream(zip.getInputStream());
    ZipEntry zE;/*from w w w  . j a v  a2 s. com*/
    try {
        while (null != (zE = ziS.getNextEntry())) {
            if (zE.isDirectory() || !uploadFileNameFilter.accept(null, zE.getName()))
                continue;
            FileItem item = factory.createItem(null, null, false, zE.getName());
            OutputStream oS = item.getOutputStream();
            IOUtils.copyLarge(ziS, oS);
            oS.close();
            fileItems.add(item);
        }
    } finally {
        ziS.close();
    }
}

From source file:com.joliciel.talismane.fr.TalismaneFrench.java

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

    return zis;//from  w w w.  jav a  2s . com
}

From source file:de.static_interface.sinklibrary.Updater.java

/**
 * Part of Zip-File-Extractor, modified by Gravity for use with Bukkit
 *//*w w w.j  a  v a2  s.c  om*/
private void unzip(File zipFile) {
    byte[] buffer = new byte[1024];

    try {
        File outputFolder = zipFile.getParentFile();
        if (!outputFolder.exists()) {
            outputFolder.mkdir();
        }

        ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
        ZipEntry ze = zis.getNextEntry();

        while (ze != null) {

            String fileName = ze.getName();
            File newFile = new File(outputFolder + File.separator + fileName);
            if (!newFile.exists() && newFile.getName().toLowerCase().endsWith(".jar")) {
                ze = zis.getNextEntry();
                continue;
            }

            FileOutputStream fos = new FileOutputStream(newFile);

            int len;
            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }

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

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

        zipFile.delete();

    } catch (IOException ex) {
        SinkLibrary.getInstance().getLogger()
                .warning("The auto-updater tried to unzip a new update file, but was unsuccessful.");
        result = Updater.UpdateResult.FAIL_DOWNLOAD;
        ex.printStackTrace();
    }
}

From source file:JarMaker.java

/**
 * Combine several jar files and some given files into one jar file.
 * @param outJar The output jar file's filename.
 * @param inJarsList The jar files to be combined
 * @param filter A filter that exclude some entries of input jars. User
 *        should implement the EntryFilter interface.
 * @param inFileList The files to be added into the jar file.
 * @param prefixs The prefixs of files to be added into the jar file.
 *        inFileList and prefixs should be paired.
 * @throws FileNotFoundException//from w  w w .ja va  2s .c om
 * @throws IOException
 */
@SuppressWarnings("unchecked")
public static void combineJars(String outJar, String[] inJarsList, EntryFilter filter, String[] inFileList,
        String[] prefixs) throws FileNotFoundException, IOException {

    JarOutputStream jout = new JarOutputStream(new FileOutputStream(outJar));
    ArrayList entryList = new ArrayList();
    for (int i = 0; i < inJarsList.length; i++) {
        BufferedInputStream bufferedinputstream = new BufferedInputStream(new FileInputStream(inJarsList[i]));
        ZipInputStream zipinputstream = new ZipInputStream(bufferedinputstream);
        byte abyte0[] = new byte[1024 * 4];
        for (ZipEntry zipentry = zipinputstream.getNextEntry(); zipentry != null; zipentry = zipinputstream
                .getNextEntry()) {
            if (filter.accept(zipentry)) {
                if (!entryList.contains(zipentry.getName())) {
                    jout.putNextEntry(zipentry);
                    if (!zipentry.isDirectory()) {
                        int j;
                        try {
                            while ((j = zipinputstream.read(abyte0, 0, abyte0.length)) != -1)
                                jout.write(abyte0, 0, j);
                        } catch (IOException ie) {
                            throw ie;
                        }
                    }
                    entryList.add(zipentry.getName());
                }
            }
        }
        zipinputstream.close();
        bufferedinputstream.close();
    }
    for (int i = 0; i < inFileList.length; i++) {
        add(jout, new File(inFileList[i]), prefixs[i]);
    }
    jout.close();
}