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:com.sangupta.jerry.util.ZipUtils.java

/**
 * Extract the given ZIP file into the given destination folder.
 * //  w ww.j a  v a2s.c om
 * @param zipFile
 *            file to extract
 *            
 * @param baseFolder
 *            destination folder to extract in
 */
public static void extractZipToFolder(File zipFile, File baseFolder) {
    try {
        byte[] buf = new byte[1024];

        ZipInputStream zipinputstream = new ZipInputStream(new FileInputStream(zipFile));
        ZipEntry zipentry = zipinputstream.getNextEntry();

        while (zipentry != null) {
            // for each entry to be extracted
            String entryName = zipentry.getName();

            entryName = entryName.replace('/', File.separatorChar);
            entryName = entryName.replace('\\', File.separatorChar);

            int numBytes;
            FileOutputStream fileoutputstream;
            File newFile = new File(baseFolder, entryName);
            if (zipentry.isDirectory()) {
                if (!newFile.mkdirs()) {
                    break;
                }
                zipentry = zipinputstream.getNextEntry();
                continue;
            }

            fileoutputstream = new FileOutputStream(newFile);
            while ((numBytes = zipinputstream.read(buf, 0, 1024)) > -1) {
                fileoutputstream.write(buf, 0, numBytes);
            }

            fileoutputstream.close();
            zipinputstream.closeEntry();
            zipentry = zipinputstream.getNextEntry();
        }

        zipinputstream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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);

    {//from  w  ww  .  j a  v a  2  s.c o  m
        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:ZipTest.java

/**
 * Scans the contents of the ZIP archive and populates the combo box.
 *///  w  w w .  java  2 s. c  o m
public void scanZipFile() {
    new SwingWorker<Void, String>() {
        protected Void doInBackground() throws Exception {
            ZipInputStream zin = new ZipInputStream(new FileInputStream(zipname));
            ZipEntry entry;
            while ((entry = zin.getNextEntry()) != null) {
                publish(entry.getName());
                zin.closeEntry();
            }
            zin.close();
            return null;
        }

        protected void process(List<String> names) {
            for (String name : names)
                fileCombo.addItem(name);

        }
    }.execute();
}

From source file:net.rptools.lib.FileUtil.java

public static void unzip(ZipInputStream in, File destDir) throws IOException {
    if (in == null)
        throw new IOException("input stream cannot be null");

    // Prepare destination
    destDir.mkdirs();/*w w w  .  j  ava  2 s  .  co  m*/
    File absDestDir = destDir.getAbsoluteFile();

    // Pull out the files
    OutputStream out = null;
    ZipEntry entry = null;
    try {
        while ((entry = in.getNextEntry()) != null) {
            if (entry.isDirectory())
                continue;

            // Prepare file destination
            File entryFile = new File(absDestDir, entry.getName());
            entryFile.getParentFile().mkdirs();

            out = new FileOutputStream(entryFile);
            IOUtils.copy(in, out);
            IOUtils.closeQuietly(out);
            in.closeEntry();
        }
    } finally {
        IOUtils.closeQuietly(out);
    }
}

From source file:es.juntadeandalucia.panelGestion.negocio.utiles.Utils.java

public static byte[] getShapeDataFromZip(byte[] localSourceData) {
    byte[] data = null;
    InputStream in = new ByteArrayInputStream(localSourceData);
    ZipInputStream zis = new ZipInputStream(in);
    ZipEntry ze;/*from  w ww.j  av a 2 s  .  c o  m*/
    try {
        while ((ze = zis.getNextEntry()) != null) {
            if (!ze.isDirectory()) {
                String fileName = ze.getName().toLowerCase();
                if (fileName.endsWith(".shp")) {
                    long entrySize = ze.getSize();
                    if (entrySize != -1) {
                        data = IOUtils.toByteArray(zis, entrySize);
                        break;
                    }
                }
            }
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            zis.closeEntry();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            zis.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return data;
}

From source file:org.openremote.beehive.utils.ZipUtil.java

public static boolean unzip(InputStream inputStream, String targetDir, String filterdFileName) {
    ZipInputStream zipInputStream = new ZipInputStream(inputStream);
    ZipEntry zipEntry;/*from   w  w w .  j a  v a  2s  . co  m*/
    FileOutputStream fileOutputStream = null;
    File zippedFile = null;
    try {
        while ((zipEntry = zipInputStream.getNextEntry()) != null) {
            if (!zipEntry.isDirectory()) {
                if (filterdFileName != null && !zipEntry.getName().equals(filterdFileName)) {
                    continue;
                }
                targetDir = targetDir.endsWith("/") || targetDir.endsWith("\\") ? targetDir : targetDir + "/";
                zippedFile = new File(targetDir, zipEntry.getName());
                FileUtils.deleteQuietly(zippedFile);
                FileUtils.touch(zippedFile);
                fileOutputStream = new FileOutputStream(zippedFile);
                int b;
                while ((b = zipInputStream.read()) != -1) {
                    fileOutputStream.write(b);
                }
                fileOutputStream.close();
            }
        }
    } catch (IOException e) {
        logger.error("Can't unzip file to " + zippedFile.getPath(), e);
        return false;
    } finally {
        try {
            zipInputStream.closeEntry();
            if (fileOutputStream != null) {
                fileOutputStream.close();
            }
        } catch (IOException e) {
            logger.error("Error while closing stream.", e);
        }

    }
    return true;
}

From source file:com.sldeditor.test.unit.tool.vector.VectorToolTest.java

/**
 * Unzip a zip file containing shp file.
 *
 * @param zipFilePath the zip file path//from   w  ww  . ja va 2s  .co  m
 * @param destDirectory the dest directory
 * @throws IOException Signals that an I/O exception has occurred.
 */
private static void unzip(String zipFilePath, String destDirectory) throws IOException {
    File destDir = new File(destDirectory);
    if (!destDir.exists()) {
        destDir.mkdir();
    }
    ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
    ZipEntry entry = zipIn.getNextEntry();
    // iterates over entries in the zip file
    while (entry != null) {
        String filePath = destDirectory + File.separator + entry.getName();
        if (!entry.isDirectory()) {
            // if the entry is a file, extracts it
            extractFile(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:cn.ipanel.apps.portalBackOffice.util.CommonsFiend.java

/**
 * marqueezip//  ww  w  .  j  a  v a2 s  .co  m
 * @param imageList
 * @param zipfilePath
 * @return
 */
public static boolean validateMarqueeZipPic(List imageList, String zipfilePath) {
    if (imageList.size() == 0) {
        return true; //zip
    }
    if (FileFiend.judgeFileZip(zipfilePath) == false) { //zip
        return false;
    }
    boolean result = true;
    InputStream in = null;
    ZipInputStream zipInput = null;
    List zipPicList = new ArrayList();
    try {
        File file = new File(zipfilePath);
        in = new FileInputStream(file);
        zipInput = new ZipInputStream(in);
        ZipEntry zipEntry = null;
        while ((zipEntry = zipInput.getNextEntry()) != null) {
            String fileName = zipEntry.getName();
            if (CommonsFiend.validateImgFormat(fileName) == false) { //marqueeContent
                return false;
            }
            zipPicList.add(fileName);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (zipInput != null) {
                zipInput.closeEntry();
                zipInput.close();
            }
            if (in != null) {
                in.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    for (int i = 0; i < imageList.size(); i++) {
        String imageName = (String) imageList.get(i);
        boolean contain = false;
        for (int j = 0; j < zipPicList.size(); j++) {
            String fileName = (String) zipPicList.get(j);
            if (imageName.equals(fileName)) {
                contain = true; //
                break;
            }
        }
        if (contain == false) { //
            result = false;
            break;
        }
    }

    return result;
}

From source file:org.exoplatform.services.cms.impl.Utils.java

/**
 * This function is used to get the version history data which is kept inside the xml files
 * @param versionHistorySourceStream//from  w  w  w. j  a  va 2 s .  co m
 * @return a map saving version history data with format: [file name, version history data]
 * @throws IOException
 */
private static Map<String, byte[]> getVersionHistoryData(InputStream versionHistorySourceStream)
        throws IOException {
    Map<String, byte[]> mapVersionHistoryData = new HashMap<String, byte[]>();
    ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(versionHistorySourceStream));
    byte[] data = new byte[1024];
    ZipEntry entry = zipInputStream.getNextEntry();
    while (entry != null) {
        //get binary data inside the zip entry
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int available = -1;
        while ((available = zipInputStream.read(data, 0, 1024)) > -1) {
            out.write(data, 0, available);
        }

        //save data into map
        mapVersionHistoryData.put(entry.getName(), out.toByteArray());

        //go to next entry
        out.close();
        zipInputStream.closeEntry();
        entry = zipInputStream.getNextEntry();
    }

    zipInputStream.close();
    return mapVersionHistoryData;
}

From source file:org.olat.core.util.ZipUtil.java

/**
 * Check if a file in the zip is already in the path
 * @param zipLeaf//  w ww  . jav  a 2s .  com
 * @param targetDir
 * @param identity
 * @param isAdmin
 * @return the list of files which already exist
 */
public static List<String> checkLockedFileBeforeUnzip(VFSLeaf zipLeaf, VFSContainer targetDir,
        Identity identity, Roles isAdmin) {
    List<String> lockedFiles = new ArrayList<String>();

    InputStream in = zipLeaf.getInputStream();
    ZipInputStream oZip = new ZipInputStream(in);

    VFSLockManager vfsLockManager = CoreSpringFactory.getImpl(VFSLockManager.class);

    try {
        // unzip files
        ZipEntry oEntr = oZip.getNextEntry();
        while (oEntr != null) {
            if (oEntr.getName() != null && !oEntr.getName().startsWith(DIR_NAME__MACOSX)) {
                if (oEntr.isDirectory()) {
                    // skip MacOSX specific metadata directory
                    // directories aren't locked
                    oZip.closeEntry();
                    oEntr = oZip.getNextEntry();
                    continue;
                } else {
                    // search file
                    VFSContainer createIn = targetDir;
                    String name = oEntr.getName();
                    // check if entry has directories which did not show up as
                    // directories above
                    int dirSepIndex = name.lastIndexOf('/');
                    if (dirSepIndex == -1) {
                        // try it windows style, backslash is also valid format
                        dirSepIndex = name.lastIndexOf('\\');
                    }
                    if (dirSepIndex > 0) {
                        // get subdirs
                        createIn = getAllSubdirs(targetDir, name.substring(0, dirSepIndex), identity, false);
                        if (createIn == null) {
                            //sub directories don't exist, and aren't locked
                            oZip.closeEntry();
                            oEntr = oZip.getNextEntry();
                            continue;
                        }
                        name = name.substring(dirSepIndex + 1);
                    }

                    VFSLeaf newEntry = (VFSLeaf) createIn.resolve(name);
                    if (vfsLockManager.isLockedForMe(newEntry, identity, isAdmin)) {
                        lockedFiles.add(name);
                    }
                }
            }
            oZip.closeEntry();
            oEntr = oZip.getNextEntry();
        }
    } catch (IOException e) {
        return null;
    } finally {
        FileUtils.closeSafely(oZip);
        FileUtils.closeSafely(in);
    }

    return lockedFiles;
}