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:com.glaf.core.util.ZipUtils.java

public static void unzip(File zipFile, String dir) throws Exception {
    File file = new File(dir);
    FileUtils.mkdirsWithExistsCheck(file);
    FileInputStream fileInputStream = null;
    ZipInputStream zipInputStream = null;
    FileOutputStream fileoutputstream = null;
    BufferedOutputStream bufferedoutputstream = null;
    try {/*from  w  ww.ja v  a2  s.co m*/
        fileInputStream = new FileInputStream(zipFile);
        zipInputStream = new ZipInputStream(fileInputStream);
        fileInputStream = new FileInputStream(zipFile);
        zipInputStream = new ZipInputStream(fileInputStream);
        java.util.zip.ZipEntry zipEntry;
        while ((zipEntry = zipInputStream.getNextEntry()) != null) {
            boolean isDirectory = zipEntry.isDirectory();
            byte abyte0[] = new byte[BUFFER];
            String s1 = zipEntry.getName();
            s1 = convertEncoding(s1);

            String s2 = dir + "/" + s1;
            s2 = FileUtils.getJavaFileSystemPath(s2);

            if (s2.indexOf('/') != -1 || isDirectory) {
                String s4 = s2.substring(0, s2.lastIndexOf('/'));
                File file2 = new File(s4);
                FileUtils.mkdirsWithExistsCheck(file2);
            }

            if (isDirectory) {
                continue;
            }

            fileoutputstream = new FileOutputStream(s2);
            bufferedoutputstream = new BufferedOutputStream(fileoutputstream, BUFFER);
            int i = 0;
            while ((i = zipInputStream.read(abyte0, 0, BUFFER)) != -1) {
                bufferedoutputstream.write(abyte0, 0, i);
            }
            bufferedoutputstream.flush();
            IOUtils.closeStream(fileoutputstream);
            IOUtils.closeStream(bufferedoutputstream);
        }

    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        IOUtils.closeStream(fileInputStream);
        IOUtils.closeStream(zipInputStream);
        IOUtils.closeStream(fileoutputstream);
        IOUtils.closeStream(bufferedoutputstream);
    }
}

From source file:com.openkm.misc.ZipTest.java

public void testJava() throws IOException {
    log.debug("testJava()");
    File zip = File.createTempFile("java_", ".zip");

    // Create zip
    FileOutputStream fos = new FileOutputStream(zip);
    ZipOutputStream zos = new ZipOutputStream(fos);
    zos.putNextEntry(new ZipEntry("coeta"));
    zos.closeEntry();/*from ww w  . j av  a2 s . c o m*/
    zos.close();

    // Read zip
    FileInputStream fis = new FileInputStream(zip);
    ZipInputStream zis = new ZipInputStream(fis);
    ZipEntry ze = zis.getNextEntry();
    System.out.println(ze.getName());
    assertEquals(ze.getName(), "coeta");
    zis.close();
}

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  ww w.j  av  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:JarResources.java

public JarResources(String jarFileName) throws Exception {
    this.jarFileName = jarFileName;
    ZipFile zf = new ZipFile(jarFileName);
    Enumeration e = zf.entries();
    while (e.hasMoreElements()) {
        ZipEntry ze = (ZipEntry) e.nextElement();

        htSizes.put(ze.getName(), new Integer((int) ze.getSize()));
    }//from ww w.j a  va  2 s .  c  o m
    zf.close();

    // extract resources and put them into the hashtable.
    FileInputStream fis = new FileInputStream(jarFileName);
    BufferedInputStream bis = new BufferedInputStream(fis);
    ZipInputStream zis = new ZipInputStream(bis);
    ZipEntry ze = null;
    while ((ze = zis.getNextEntry()) != null) {
        if (ze.isDirectory()) {
            continue;
        }

        int size = (int) ze.getSize();
        // -1 means unknown size.
        if (size == -1) {
            size = ((Integer) htSizes.get(ze.getName())).intValue();
        }

        byte[] b = new byte[(int) size];
        int rb = 0;
        int chunk = 0;
        while (((int) size - rb) > 0) {
            chunk = zis.read(b, rb, (int) size - rb);
            if (chunk == -1) {
                break;
            }
            rb += chunk;
        }

        htJarContents.put(ze.getName(), b);
    }
}

From source file:com.jcalvopinam.core.Unzipping.java

private static void joinAndUnzipFile(File inputFile, CustomFile customFile) throws IOException {

    ZipInputStream is = null;
    File path = inputFile.getParentFile();

    List<InputStream> fileInputStream = new ArrayList<>();
    for (String fileName : path.list()) {
        if (fileName.startsWith(customFile.getFileName()) && fileName.endsWith(Extensions.ZIP.getExtension())) {
            fileInputStream.add(new FileInputStream(String.format("%s%s", customFile.getPath(), fileName)));
            System.out.println(String.format("File found: %s", fileName));
        }/*from   ww  w.j a  va 2s .c  o  m*/
    }

    if (fileInputStream.size() > 0) {
        String fileNameOutput = String.format("%s%s", customFile.getPath(), customFile.getFileName());
        OutputStream os = new BufferedOutputStream(new FileOutputStream(fileNameOutput));
        try {
            System.out.println("Please wait while the files are joined: ");

            ZipEntry ze;
            for (InputStream inputStream : fileInputStream) {
                is = new ZipInputStream(inputStream);
                ze = is.getNextEntry();
                customFile.setFileName(String.format("%s_%s", REBUILT, ze.getName()));

                byte[] buffer = new byte[CustomFile.BYTE_SIZE];

                for (int readBytes; (readBytes = is.read(buffer, 0, CustomFile.BYTE_SIZE)) > -1;) {
                    os.write(buffer, 0, readBytes);
                    System.out.print(".");
                }
            }
        } finally {
            os.flush();
            os.close();
            is.close();
            renameFinalFile(customFile, fileNameOutput);
        }
    } else {
        throw new FileNotFoundException("Error: The file not exist!");
    }
    System.out.println("\nEnded process!");
}

From source file:JarResource.java

private List<String> load(InputStream is) throws IOException {
    List<String> jarContents = new ArrayList<String>();
    try {/*w  ww. j  a  va  2  s. c o  m*/
        ZipInputStream zis = new ZipInputStream(is);
        ZipEntry ze = zis.getNextEntry();
        while (ze != null) {
            if (ze.isDirectory()) {
                continue;
            }
            jarContents.add(ze.getName());
            ze = zis.getNextEntry();
        }
    } catch (NullPointerException e) {
        System.out.println("done.");
    }

    return jarContents;
}

From source file:com.googlecode.dex2jar.reader.ZipExtractor.java

public byte[] extract(byte[] data, String name) throws IOException {
    ZipInputStream zis = null;
    try {/*from  ww  w  .  j  a v  a 2 s .c o m*/
        zis = new ZipInputStreamHack(new ByteArrayInputStream(data));
        for (ZipEntry entry = zis.getNextEntry(); entry != null; entry = zis.getNextEntry()) {
            if (entry.getName().equals(name)) {
                data = IOUtils.toByteArray(zis);
                zis.close();
                return data;
            }
        }
    } finally {
        IOUtils.closeQuietly(zis);
    }
    throw new IOException("can't find classes.dex in the zip");
}

From source file:io.github.tsabirgaliev.ZipperInputStreamTest.java

public void testJDKCompatibility() throws IOException {
    ZipperInputStream lzis = new ZipperInputStream(enumerate(file1, file2));

    ZipInputStream zis = new ZipInputStream(lzis);

    {/*  w ww  .  ja  v  a2 s.  c  om*/
        ZipEntry entry1 = zis.getNextEntry();

        assert file1.getPath().equals(entry1.getName());

        assert IOUtils.contentEquals(zis, file1.getStream());

        zis.closeEntry();
    }

    {
        ZipEntry entry2 = zis.getNextEntry();

        assert file2.getPath().equals(entry2.getName());

        assert IOUtils.contentEquals(zis, file2.getStream());

        zis.closeEntry();
    }

}

From source file:de.tudarmstadt.ukp.clarin.webanno.brat.dao.DaoUtilsTest.java

@SuppressWarnings("resource")
@Test/*from   w ww  .ja va 2  s .  c om*/
public void testZipFolder() {

    File toBeZippedFiles = new File("src/test/resources");
    File zipedFiles = new File("target/" + toBeZippedFiles.getName() + ".zip");
    try {
        ZipUtils.zipFolder(toBeZippedFiles, zipedFiles);
    } catch (Exception e) {
        LOG.info("Zipping fails" + ExceptionUtils.getRootCauseMessage(e));
    }

    FileInputStream fs;
    try {
        fs = new FileInputStream(zipedFiles);
        ZipInputStream zis = new ZipInputStream(fs);
        ZipEntry zE;
        while ((zE = zis.getNextEntry()) != null) {
            for (File toBeZippedFile : toBeZippedFiles.listFiles()) {
                if ((FilenameUtils.getBaseName(toBeZippedFile.getName()))
                        .equals(FilenameUtils.getBaseName(zE.getName()))) {
                    // Extract the zip file, save it to temp and compare
                    ZipFile zipFile = new ZipFile(zipedFiles);
                    ZipEntry zipEntry = zipFile.getEntry(zE.getName());
                    InputStream is = zipFile.getInputStream(zipEntry);
                    File temp = File.createTempFile("temp", ".txt");
                    OutputStream out = new FileOutputStream(temp);
                    IOUtils.copy(is, out);
                    FileUtils.contentEquals(toBeZippedFile, temp);
                }
            }
            zis.closeEntry();
        }
    } catch (FileNotFoundException e) {
        LOG.info("zipped file not found " + ExceptionUtils.getRootCauseMessage(e));
    } catch (IOException e) {
        LOG.info("Unable to get file " + ExceptionUtils.getRootCauseMessage(e));
    }

}

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

private static boolean hasPropertyIndexInZipStream(ZipInputStream zis) {
    try {/*w ww  .ja  v  a2  s. co m*/
        ZipEntry entry = null;
        String entryName$;
        while ((entry = zis.getNextEntry()) != null) {
            entryName$ = entry.getName();
            if (entryName$.equals(Entigrator.PROPERTY_INDEX)) {
                zis.close();
                return true;
            }
        }
        zis.close();
        return false;
    } catch (Exception e) {
        Logger.getLogger(ArchiveHandler.class.getName()).severe(e.toString());
        return false;
    }
}