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:com.gabrielluz.megasenaanalyzer.MegaSenaAnalyzer.java

public static byte[] unZipIt(InputStream is) throws IOException {
    byte[] buffer = new byte[1024];
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try (ZipInputStream zis = new ZipInputStream(is)) {
        ZipEntry ze = zis.getNextEntry();
        while (ze != null) {
            if (ze.getName().compareToIgnoreCase("D_MEGA.HTM") == 0) {
                int len;
                while ((len = zis.read(buffer)) > 0) {
                    out.write(buffer, 0, len);
                }//from   www  .java  2  s . c om
            }
            ze = zis.getNextEntry();
        }
        zis.closeEntry();
    }
    return out.toByteArray();
}

From source file:com.clank.launcher.install.ZipExtract.java

@Override
public void run() {
    Closer closer = Closer.create();// w  w w  .j  a  v a 2s  .  c  om

    try {
        InputStream is = closer.register(source.openBufferedStream());
        ZipInputStream zis = closer.register(new ZipInputStream(is));
        ZipEntry entry;

        destination.getParentFile().mkdirs();

        while ((entry = zis.getNextEntry()) != null) {
            if (matches(entry)) {
                File file = new File(getDestination(), entry.getName());
                writeEntry(zis, file);
            }
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            closer.close();
        } catch (IOException e) {
        }
    }
}

From source file:com.plotsquared.iserver.util.FileUtils.java

/**
 * Add files to a zip file//from www .  j av  a  2 s  . c  o m
 *
 * @param zipFile Zip File
 * @param files   Files to add to the zip
 * @param delete  If the original files should be deleted
 * @throws Exception If anything goes wrong
 */
public static void addToZip(final File zipFile, final File[] files, final boolean delete) throws Exception {
    Assert.notNull(zipFile, files);

    if (!zipFile.exists()) {
        if (!zipFile.createNewFile()) {
            throw new RuntimeException("Couldn't create " + zipFile);
        }
    }

    final File temporary = File.createTempFile(zipFile.getName(), "");
    //noinspection ResultOfMethodCallIgnored
    temporary.delete();

    if (!zipFile.renameTo(temporary)) {
        throw new RuntimeException("Couldn't rename " + zipFile + " to " + temporary);
    }

    final byte[] buffer = new byte[1024 * 16]; // 16mb

    ZipInputStream zis = new ZipInputStream(new FileInputStream(temporary));
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));

    ZipEntry e = zis.getNextEntry();
    while (e != null) {
        String n = e.getName();

        boolean no = true;
        for (File f : files) {
            if (f.getName().equals(n)) {
                no = false;
                break;
            }
        }

        if (no) {
            zos.putNextEntry(new ZipEntry(n));
            int len;
            while ((len = zis.read(buffer)) > 0) {
                zos.write(buffer, 0, len);
            }
        }
        e = zis.getNextEntry();
    }
    zis.close();
    for (File file : files) {
        InputStream in = new FileInputStream(file);
        zos.putNextEntry(new ZipEntry(file.getName()));

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

        zos.closeEntry();
        in.close();
    }
    zos.close();
    temporary.delete();

    if (delete) {
        for (File f : files) {
            f.delete();
        }
    }
}

From source file:ee.ria.DigiDoc.configuration.Configuration.java

private static void unpackSchema(Context context) {
    File schemaPath = FileUtils.getSchemaCacheDirectory(context);
    try (ZipInputStream zis = new ZipInputStream(context.getResources().openRawResource(R.raw.schema))) {
        ZipEntry ze;/* w w w. j a v a 2  s  . co  m*/
        while ((ze = zis.getNextEntry()) != null) {
            File entryFile = new File(schemaPath, ze.getName());
            FileOutputStream out = new FileOutputStream(entryFile);
            IOUtils.copy(zis, out);
            out.close();
        }
    } catch (IOException e) {
        Timber.e(e, "Library configuration initialization failed");
    }
}

From source file:ee.ria.xroad.common.asic.AsicHelper.java

static AsicContainer read(InputStream is) throws Exception {
    Map<String, String> entries = new HashMap<>();
    ZipInputStream zip = new ZipInputStream(is);
    ZipEntry zipEntry;/*from   ww  w  .j av a 2s  .c  o  m*/

    while ((zipEntry = zip.getNextEntry()) != null) {
        for (Object expectedEntry : AsicContainerEntries.getALL_ENTRIES()) {
            if (matches(expectedEntry, zipEntry.getName())) {
                String data;

                if (ENTRY_TIMESTAMP.equalsIgnoreCase(zipEntry.getName())) {
                    data = encodeBase64(getBinaryData(zip));
                } else {
                    data = getData(zip);
                }

                entries.put(zipEntry.getName(), data);

                break;
            }
        }
    }

    return new AsicContainer(entries);
}

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

@Override
public String getScript() throws IOException {
    ZipInputStream zippedIn = null;
    try {/*from  w ww .j  a v  a  2s .co m*/
        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:com.oprisnik.semdroid.app.parser.manifest.AXMLConverter.java

public static String getManifestString(File apk) {
    String result = null;/*from  w w  w  .j a  va 2 s .co m*/
    ZipInputStream zis = null;
    FileInputStream fis = null;
    try {

        fis = new FileInputStream(apk);

        zis = new ZipInputStream(fis);
        for (ZipEntry entry = zis.getNextEntry(); entry != null; entry = zis.getNextEntry()) {
            if (entry.getName().equals("AndroidManifest.xml")) {
                result = getManifestString(zis);
                break;
            }
        }
    } catch (FileNotFoundException e) {
        IOUtils.closeQuietly(fis);
    } catch (IOException e) {
    } finally {
        IOUtils.closeQuietly(zis);
    }
    return result;
}

From source file:ch.ivyteam.ivy.maven.engine.TestClasspathJar.java

@Test
public void readWriteClasspath() throws IOException {
    File jarFile = Files.createTempFile("my", ".jar").toFile();
    ClasspathJar jar = new ClasspathJar(jarFile);
    File content = Files.createTempFile("content", ".jar").toFile();
    jar.createFileEntries(Arrays.asList(content));

    assertThat(jar.getClasspathFiles()).contains(content.getName());

    ZipInputStream jarStream = new ZipInputStream(new FileInputStream(jarFile));
    ZipEntry first = jarStream.getNextEntry();
    assertThat(first.getName()).isEqualTo("META-INF/MANIFEST.MF");
    String manifest = IOUtils.toString(jarStream);
    assertThat(manifest)/*from  w w  w.  j a  v  a2 s. c  o m*/
            .as("Manifest should not start with a whitespace or it will not be interpreted by the JVM")
            .startsWith("Manifest-Version:");
}

From source file:io.github.jeddict.jcode.parser.ejs.EJSUtil.java

public static void copyDynamicResource(Consumer<FileTypeStream> parserManager, String inputResource,
        FileObject webRoot, Function<String, String> pathResolver, ProgressHandler handler) throws IOException {
    InputStream inputStream = loadResource(inputResource);
    try (ZipInputStream zipInputStream = new ZipInputStream(inputStream)) {
        ZipEntry entry;//from  w ww . j  ava  2 s  .c  om
        while ((entry = zipInputStream.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                continue;
            }
            boolean skipParsing = true;
            String entryName = entry.getName();
            if (entryName.endsWith(".ejs")) {
                skipParsing = false;
                entryName = entryName.substring(0, entryName.lastIndexOf("."));
            }
            String targetPath = pathResolver.apply(entryName);
            if (targetPath == null) {
                continue;
            }
            handler.progress(targetPath);
            FileObject target = org.openide.filesystems.FileUtil.createData(webRoot, targetPath);
            FileLock lock = target.lock();
            try (OutputStream outputStream = target.getOutputStream(lock);) {
                parserManager.accept(new FileTypeStream(entryName, zipInputStream, outputStream, skipParsing));
                zipInputStream.closeEntry();
            } finally {
                lock.releaseLock();
            }
        }
    } catch (Throwable ex) {
        Exceptions.printStackTrace(ex);
        System.out.println("InputResource : " + inputResource);
    }
}

From source file:cn.edu.zju.acm.onlinejudge.util.ProblemManager.java

public static ProblemPackage importProblem(InputStream in, ActionMessages messages) {

    Map<String, byte[]> files = new HashMap<String, byte[]>();
    ZipInputStream zis = null;/*from   w  ww . j  ava  2 s .  c  om*/
    try {

        zis = new ZipInputStream(new BufferedInputStream(in));
        // zis = new ZipInputStream(new BufferedInputStream(new FileInputStream("d:/100.zip")));
        ZipEntry entry = zis.getNextEntry();
        while (entry != null) {
            if (!entry.isDirectory()) {

                // TODO the file may be too big. > 100M
                /*
                 * byte data[] = new byte[(int) entry.getSize()]; int l = 0; while (l < data.length) { int ll =
                 * zis.read(data, l, data.length - l); if (ll < 0) { break; } l += ll; }
                 */
                ByteArrayOutputStream buf = new ByteArrayOutputStream();
                CopyUtils.copy(zis, buf);
                files.put(entry.getName(), buf.toByteArray());

            }
            entry = zis.getNextEntry();
        }

    } catch (IOException ioe) {
        messages.add("error", new ActionMessage("onlinejudge.importProblem.invalidzip"));
        return null;
    } finally {
        try {
            if (zis != null) {
                zis.close();
            }
        } catch (IOException e) {
            // ignore
        }
    }
    /*
     * files.clear(); files.put("problems.csv", "3,a\n2,b,true,1,2,3,4,a,b,c\n1,c".getBytes());
     * files.put("1\\checker", "checker".getBytes()); files.put("1\\input", "input".getBytes());
     * files.put("1\\output", "output".getBytes()); files.put("3\\checker", "checker3".getBytes());
     * files.put("3\\input", "input3".getBytes()); files.put("3\\output", "output3".getBytes());
     * files.put("images\\1.jpg", "1".getBytes()); files.put("images\\2\\2.jpg", "2".getBytes());
     */
    if (!files.containsKey(ProblemManager.PROBLEM_CSV_FILE)) {
        messages.add("error", new ActionMessage("onlinejudge.importProblem.missingproblemscsv"));
        return null;
    }
    ProblemPackage problemPackage = ProblemManager.parse(files, messages);
    if (messages.size() > 0) {
        return null;
    }
    return problemPackage;
}