Example usage for java.util.zip ZipInputStream closeEntry

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

Introduction

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

Prototype

public void closeEntry() throws IOException 

Source Link

Document

Closes the current ZIP entry and positions the stream for reading the next entry.

Usage

From source file:org.onosproject.common.app.ApplicationArchive.java

private void expandZippedApplication(InputStream stream, ApplicationDescription desc) throws IOException {
    ZipInputStream zis = new ZipInputStream(stream);
    ZipEntry entry;/*from w  ww  .j  a  va 2 s . c  o  m*/
    File appDir = new File(appsDir, desc.name());
    while ((entry = zis.getNextEntry()) != null) {
        if (!entry.isDirectory()) {
            byte[] data = ByteStreams.toByteArray(zis);
            zis.closeEntry();
            File file = new File(appDir, entry.getName());
            createParentDirs(file);
            write(data, file);
        }
    }
    zis.close();
}

From source file:org.roda.core.util.ZipUtility.java

private static List<File> extractFilesFromInputStream(InputStream inputStream, File outputDir,
        boolean filesWithAbsolutePath) throws IOException {
    ZipInputStream zipInputStream = new ZipInputStream(inputStream);
    ZipEntry zipEntry = zipInputStream.getNextEntry();

    if (zipEntry == null) {
        zipInputStream.close();/*from   w ww .  j  a va 2 s  .co  m*/
        throw new IOException("No files inside ZIP");
    } else {

        List<File> extractedFiles = new ArrayList<>();

        while (zipEntry != null) {

            // for each entry to be extracted
            String entryName = zipEntry.getName();
            LOGGER.debug("Extracting {}", entryName);

            File newFile = new File(outputDir, entryName);

            if (filesWithAbsolutePath) {
                extractedFiles.add(newFile);
            } else {
                extractedFiles.add(new File(entryName));
            }

            if (zipEntry.isDirectory()) {
                newFile.mkdirs();
            } else {

                if (newFile.getParentFile() != null && (!newFile.getParentFile().exists())) {
                    newFile.getParentFile().mkdirs();
                }

                FileOutputStream newFileOutputStream = new FileOutputStream(newFile);

                // copyLarge returns a long instead of int
                IOUtils.copyLarge(zipInputStream, newFileOutputStream);

                newFileOutputStream.close();
                zipInputStream.closeEntry();
            }

            zipEntry = zipInputStream.getNextEntry();
        }

        zipInputStream.close();
        return extractedFiles;
    }
}

From source file:com.espringtran.compressor4j.processor.LzmaProcessor.java

/**
 * Read from compressed file/*ww  w  .ja  va  2 s  .  co  m*/
 * 
 * @param srcPath
 *            path of compressed file
 * @param fileCompressor
 *            FileCompressor object
 * @throws Exception
 */
@Override
public void read(String srcPath, FileCompressor fileCompressor) throws Exception {
    long t1 = System.currentTimeMillis();
    byte[] data = FileUtil.convertFileToByte(srcPath);
    ByteArrayInputStream bais = new ByteArrayInputStream(data);
    LZMACompressorInputStream cis = new LZMACompressorInputStream(bais);
    ZipInputStream zis = new ZipInputStream(cis);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        byte[] buffer = new byte[1024];
        int readByte;
        ZipEntry entry = zis.getNextEntry();
        while (entry != null) {
            long t2 = System.currentTimeMillis();
            baos = new ByteArrayOutputStream();
            readByte = zis.read(buffer);
            while (readByte != -1) {
                baos.write(buffer, 0, readByte);
                readByte = zis.read(buffer);
            }
            zis.closeEntry();
            BinaryFile binaryFile = new BinaryFile(entry.getName(), baos.toByteArray());
            fileCompressor.addBinaryFile(binaryFile);
            LogUtil.createAddFileLog(fileCompressor, binaryFile, t2, System.currentTimeMillis());
            entry = zis.getNextEntry();
        }
    } catch (Exception e) {
        FileCompressor.LOGGER.error("Error on get compressor file", e);
    } finally {
        baos.close();
        zis.close();
        cis.close();
        bais.close();
    }
    LogUtil.createReadLog(fileCompressor, srcPath, data.length, t1, System.currentTimeMillis());
}

From source file:jeffaschenk.tomcat.instance.generator.builders.TomcatInstanceBuilderHelper.java

/**
 * unZip Archive//from   www  .ja v a 2s .  c o m
 *
 * @param zipFilePath  input zip file
 * @param outputFolder zip file output folder
 */
protected static void unZipArchive(GenerationLogger GENERATION_LOGGER, String zipFilePath, String outputFolder)
        throws IOException {
    /**
     * Create Output Directory, but should already Exist.
     */
    File folder = new File(outputFolder);
    if (!folder.exists()) {
        folder.mkdir();
    }
    ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
    ZipEntry entry = zipIn.getNextEntry();
    // iterates over entries in the zip file
    while (entry != null) {
        String filePath = outputFolder + File.separator + entry.getName();
        if (!entry.isDirectory()) {
            // if the entry is a file, extracts it
            extractFile(GENERATION_LOGGER, zipIn, filePath);
        } else {
            // if the entry is a directory, make the directory
            File dir = new File(filePath);
            dir.mkdir();
        }
        zipIn.closeEntry();
        entry = zipIn.getNextEntry();
    }
    zipIn.close();
}

From source file:com.espringtran.compressor4j.processor.Bzip2Processor.java

/**
 * Read from compressed file/*from  www  .j a v a2s . co  m*/
 * 
 * @param srcPath
 *            path of compressed file
 * @param fileCompressor
 *            FileCompressor object
 * @throws Exception
 */
@Override
public void read(String srcPath, FileCompressor fileCompressor) throws Exception {
    long t1 = System.currentTimeMillis();
    byte[] data = FileUtil.convertFileToByte(srcPath);
    ByteArrayInputStream bais = new ByteArrayInputStream(data);
    BZip2CompressorInputStream cis = new BZip2CompressorInputStream(bais);
    ZipInputStream zis = new ZipInputStream(cis);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        byte[] buffer = new byte[1024];
        int readByte;
        ZipEntry entry = zis.getNextEntry();
        while (entry != null) {
            long t2 = System.currentTimeMillis();
            baos = new ByteArrayOutputStream();
            readByte = zis.read(buffer);
            while (readByte != -1) {
                baos.write(buffer, 0, readByte);
                readByte = zis.read(buffer);
            }
            zis.closeEntry();
            BinaryFile binaryFile = new BinaryFile(entry.getName(), baos.toByteArray());
            fileCompressor.addBinaryFile(binaryFile);
            LogUtil.createAddFileLog(fileCompressor, binaryFile, t2, System.currentTimeMillis());
            entry = zis.getNextEntry();
        }
    } catch (Exception e) {
        FileCompressor.LOGGER.error("Error on get compressor file", e);
    } finally {
        baos.close();
        zis.close();
        cis.close();
        bais.close();
    }
    LogUtil.createReadLog(fileCompressor, srcPath, data.length, t1, System.currentTimeMillis());
}

From source file:com.espringtran.compressor4j.processor.GzipProcessor.java

/**
 * Read from compressed file//w w w. ja v a 2  s . co  m
 * 
 * @param srcPath
 *            path of compressed file
 * @param fileCompressor
 *            FileCompressor object
 * @throws Exception
 */
@Override
public void read(String srcPath, FileCompressor fileCompressor) throws Exception {
    long t1 = System.currentTimeMillis();
    byte[] data = FileUtil.convertFileToByte(srcPath);
    ByteArrayInputStream bais = new ByteArrayInputStream(data);
    GzipCompressorInputStream cis = new GzipCompressorInputStream(bais);
    ZipInputStream zis = new ZipInputStream(cis);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        byte[] buffer = new byte[1024];
        int readByte;
        ZipEntry entry = zis.getNextEntry();
        while (entry != null) {
            long t2 = System.currentTimeMillis();
            baos = new ByteArrayOutputStream();
            readByte = zis.read(buffer);
            while (readByte != -1) {
                baos.write(buffer, 0, readByte);
                readByte = zis.read(buffer);
            }
            zis.closeEntry();
            BinaryFile binaryFile = new BinaryFile(entry.getName(), baos.toByteArray());
            fileCompressor.addBinaryFile(binaryFile);
            LogUtil.createAddFileLog(fileCompressor, binaryFile, t2, System.currentTimeMillis());
            entry = zis.getNextEntry();
        }
    } catch (Exception e) {
        FileCompressor.LOGGER.error("Error on get compressor file", e);
    } finally {
        baos.close();
        zis.close();
        cis.close();
        bais.close();
    }
    LogUtil.createReadLog(fileCompressor, srcPath, data.length, t1, System.currentTimeMillis());
}

From source file:org.ow2.clif.jenkins.jobs.Zip.java

/**
 * unzips to directory//from ww  w  . j  a  va  2 s. co m
 *
 * @param dir the target directory
 * @throws IOException the zip archive could not be correctly unzipped
 * @return this Zip object
 */
@SuppressFBWarnings(value = "RV_RETURN_VALUE_IGNORED_BAD_PRACTICE", justification = "Return from File.mkdirs() not meaningful/reliable")
public Zip extractTo(File dir) throws IOException {
    FileUtils.forceMkdir(dir);
    if (logger.isLoggable(Level.INFO)) {
        logger.info("extracting " + file + " to " + dir.getAbsolutePath());
    }
    byte[] buf = new byte[1024];
    ZipEntry zipentry;
    ZipInputStream zip = newStream();
    try {
        for (zipentry = zip.getNextEntry(); zipentry != null; zipentry = zip.getNextEntry()) {
            String entryName = zipentry.getName();

            File dest = new File(dir, entryName);
            if (zipentry.isDirectory()) {
                dest.mkdirs();
            } else {
                dest.getParentFile().mkdirs();
                //               dest.createNewFile())
                writeCurrentFile(zip, buf, dest);
            }
            zip.closeEntry();
        }
    } finally {
        zip.close();
    }
    return this;
}

From source file:org.tonguetied.datatransfer.ExportServiceTest.java

public final void testCreateArchiveWithEmptyFiles() throws Exception {
    archiveTestDirectory = new File(USER_DIR, "testCreateArchiveWithEmptyFiles");
    FileUtils.forceMkdir(archiveTestDirectory);
    FileUtils.touch(new File(archiveTestDirectory, "temp"));
    assertEquals(1, archiveTestDirectory.listFiles().length);
    assertTrue(archiveTestDirectory.isDirectory());
    dataService.createArchive(archiveTestDirectory);
    assertEquals(2, archiveTestDirectory.listFiles().length);
    // examine zip file
    File[] files = archiveTestDirectory.listFiles(new FileExtensionFilter(".zip"));
    assertEquals(1, files.length);//w  ww  .ja v  a 2  s. co  m
    ZipInputStream zis = null;
    try {
        zis = new ZipInputStream(new FileInputStream(files[0]));
        ZipEntry zipEntry = zis.getNextEntry();
        assertEquals("temp", zipEntry.getName());
        zis.closeEntry();
    } finally {
        IOUtils.closeQuietly(zis);
    }
}

From source file:com.taobao.android.tpatch.utils.JarSplitUtils.java

public static void splitZipToFolder(File inputFile, File outputFolder, Set<String> includeEnties)
        throws IOException {
    if (!outputFolder.exists()) {
        outputFolder.mkdirs();//ww w .j  a v  a  2  s. com
    }
    if (null == includeEnties || includeEnties.size() < 1) {
        return;
    }
    final byte[] buffer = new byte[8192];
    FileInputStream fis = new FileInputStream(inputFile);
    ZipInputStream zis = new ZipInputStream(fis);

    try {
        // loop on the entries of the jar file package and put them in the final jar
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            // do not take directories or anything inside a potential META-INF folder.
            if (entry.isDirectory()) {
                continue;
            }
            String name = entry.getName();
            if (!includeEnties.contains(name)) {
                continue;
            }
            File destFile = new File(outputFolder, name);
            destFile.getParentFile().mkdirs();
            // read the content of the entry from the input stream, and write it into the archive.
            int count;
            FileOutputStream fout = FileUtils.openOutputStream(destFile);
            while ((count = zis.read(buffer)) != -1) {
                fout.write(buffer, 0, count);
            }
            fout.close();
            zis.closeEntry();
        }
    } finally {
        zis.close();
    }
    fis.close();
}

From source file:org.pentaho.reporting.engine.classic.demo.ancient.demo.swingicons.SwingIconsDemoTableModel.java

/**
 * Reads the icon data from the jar file.
 *
 * @param in the input stream./*  w ww.  j ava 2 s  .c o m*/
 */
private boolean readData(final InputStream in) {
    try {
        final ZipInputStream iconJar = new ZipInputStream(in);
        ZipEntry ze = iconJar.getNextEntry();
        while (ze != null) {
            final String fullName = ze.getName();
            if (fullName.endsWith(".gif")) {
                final String category = getCategory(fullName);
                final String name = getName(fullName);
                final Image image = getImage(iconJar);
                final Long bytes = new Long(ze.getSize());
                //logger.debug ("Add Icon: " + name);
                addIconEntry(name, category, image, bytes);
            }
            iconJar.closeEntry();
            ze = iconJar.getNextEntry();
        }
    } catch (IOException e) {
        logger.warn("Unable to load the Icons", e);
        return false;
    }
    return true;
}