Example usage for java.util.zip ZipInputStream close

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

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes this input stream and releases any system resources associated with the stream.

Usage

From source file:cn.ipanel.apps.portalBackOffice.util.CommonsFiend.java

/**
 * marqueezip/*from   www.ja  v a 2s  .c om*/
 * @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:com.oeg.oops.VocabUtils.java

/**
 * Code to unzip a file. Inspired from/*from   ww w .  ja v a 2  s. c  om*/
 * http://www.mkyong.com/java/how-to-decompress-files-from-a-zip-file/
 * Taken from
 * @param resourceName
 * @param outputFolder
 */
public static void unZipIt(String resourceName, String outputFolder) {

    byte[] buffer = new byte[1024];

    try {
        ZipInputStream zis = new ZipInputStream(VocabUtils.class.getResourceAsStream(resourceName));
        ZipEntry ze = zis.getNextEntry();

        while (ze != null) {

            String fileName = ze.getName();
            File newFile = new File(outputFolder + File.separator + fileName);
            System.out.println("file unzip : " + newFile.getAbsoluteFile());
            if (ze.isDirectory()) {
                String temp = newFile.getAbsolutePath();
                new File(temp).mkdirs();
            } else {
                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();

    } catch (IOException ex) {
        System.err.println("Error while extracting the reosurces: " + ex.getMessage());
    }
}

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  w w .  j a  va  2s .  c  o  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:com.wavemaker.StudioInstallService.java

public static File unzipFile(File zipfile) {
    int BUFFER = 2048;

    String zipname = zipfile.getName();
    int extindex = zipname.lastIndexOf(".");

    try {/*from www  . j  a v a2  s . co  m*/
        File zipFolder = new File(zipfile.getParentFile(), zipname.substring(0, extindex));
        if (zipFolder.exists())
            org.apache.commons.io.FileUtils.deleteDirectory(zipFolder);
        zipFolder.mkdir();

        File currentDir = zipFolder;
        //File currentDir = zipfile.getParentFile();

        BufferedOutputStream dest = null;
        FileInputStream fis = new FileInputStream(zipfile.toString());
        ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            System.out.println("Extracting: " + entry);
            if (entry.isDirectory()) {
                File f = new File(currentDir, entry.getName());
                if (f.exists())
                    f.delete(); // relevant if this is the top level folder
                f.mkdir();
            } else {
                int count;
                byte data[] = new byte[BUFFER];
                //needed for non-dir file ace/ace.js created by 7zip
                File destFile = new File(currentDir, entry.getName());
                // write the files to the disk
                FileOutputStream fos = new FileOutputStream(currentDir.toString() + "/" + entry.getName());
                dest = new BufferedOutputStream(fos, BUFFER);
                while ((count = zis.read(data, 0, BUFFER)) != -1) {
                    dest.write(data, 0, count);
                }
                dest.flush();
                dest.close();
            }
        }
        zis.close();

        return currentDir;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return (File) null;

}

From source file:Main.java

public static boolean unzipFile(InputStream fis, String destDir) {
    final byte[] buffer = new byte[4096];
    ZipInputStream zis = null;
    Log.e("Unzip", "destDir = " + destDir);
    try {/*from   w w w .  j  a  v a2  s .c o m*/
        // make sure the directory is existent
        File dstFile = new File(destDir);
        if (!dstFile.exists()) {
            dstFile.mkdirs();
        } else {
            int fileLenght = dstFile.listFiles().length;
            if (fileLenght >= 2) {
                return true;
            }
        }
        zis = new ZipInputStream(fis);
        ZipEntry entry;

        while ((entry = zis.getNextEntry()) != null) {
            String fileName = entry.getName();

            if (entry.isDirectory()) {
                new File(destDir, fileName).mkdirs();

            } else {
                BufferedOutputStream bos = new BufferedOutputStream(
                        new FileOutputStream(new File(destDir, fileName)));
                int lenRead;

                while ((lenRead = zis.read(buffer)) != -1) {
                    bos.write(buffer, 0, lenRead);
                }

                bos.close();
            }

            zis.closeEntry();
        }

        return true;

    } catch (IOException e) {
        e.printStackTrace();

    } finally {
        if (zis != null) {
            try {
                zis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return false;
}

From source file:org.apache.asterix.event.service.AsterixEventServiceUtil.java

public static void unzip(String sourceFile, String destDir) throws IOException {
    final FileInputStream fis = new FileInputStream(sourceFile);
    final ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
    final File destDirFile = new File(destDir);
    final byte[] data = new byte[BUFFER_SIZE];

    ZipEntry entry;//from  w w w  .  java2 s  .c  o m
    Set<String> visitedDirs = new HashSet<>();
    createDir(destDir);
    while ((entry = zis.getNextEntry()) != null) {
        createDir(destDirFile, entry, visitedDirs);
        if (entry.isDirectory()) {
            continue;
        }
        int count;

        // write the file to the disk
        File dst = new File(destDir, entry.getName());
        FileOutputStream fos = new FileOutputStream(dst);
        BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER_SIZE);
        while ((count = zis.read(data, 0, BUFFER_SIZE)) != -1) {
            dest.write(data, 0, count);
        }
        // close the output streams
        dest.flush();
        dest.close();
    }

    zis.close();
}

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  ww. jav a 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:com.ecofactor.qa.automation.platform.util.FileUtil.java

/**
 * Un zip a file to a destination folder.
 * @param zipFile the zip file/*from  ww  w . j  a v  a  2s .c  om*/
 * @param outputFolder the output folder
 */
public static void unZipFile(final String zipFile, final String outputFolder) {

    LogUtil.setLogString("UnZip the File", true);
    final byte[] buffer = new byte[1024];
    int len;
    try {
        // create output directory is not exists
        final File folder = new File(outputFolder);
        if (!folder.exists()) {
            folder.mkdir();
        }
        // get the zip file content
        final ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
        final StringBuilder outFolderPath = new StringBuilder();
        final StringBuilder fileLogPath = new StringBuilder();
        ZipEntry zipEntry;
        while ((zipEntry = zis.getNextEntry()) != null) {
            final String fileName = zipEntry.getName();
            final File newFile = new File(
                    outFolderPath.append(outputFolder).append(File.separator).append(fileName).toString());
            LogUtil.setLogString(
                    fileLogPath.append("file unzip : ").append(newFile.getAbsoluteFile()).toString(), true);
            // create all non exists folders
            // else you will hit FileNotFoundException for compressed folder
            new File(newFile.getParent()).mkdirs();
            final FileOutputStream fos = new FileOutputStream(newFile);

            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }
            fos.close();
            fileLogPath.setLength(0);
            outFolderPath.setLength(0);
        }

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

        LogUtil.setLogString("Done", true);

    } catch (IOException ex) {
        LOGGER.error("Error in unzip file", ex);
    }
}

From source file:com.esminis.server.library.service.server.installpackage.InstallerPackage.java

private void install(Context context, File file, File targetDirectory) throws Throwable {
    sendBroadcast(context, STATE_INSTALL, 0);
    InputStream input = null;/*from   w  w w. ja  v  a2s . co m*/
    ZipInputStream zip = null;
    try {
        zip = new ZipInputStream(input = new FileInputStream(file));
        ZipEntry entry;
        int totalEntries = 0;
        while (zip.getNextEntry() != null) {
            totalEntries++;
        }
        zip.close();
        input.close();
        zip = new ZipInputStream(input = new FileInputStream(file));
        int position = 0;
        while ((entry = zip.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                final File directory = new File(targetDirectory, entry.getName());
                if (!directory.isDirectory() && !directory.mkdirs()) {
                    throw new IOException("Cannot create directory: " + directory.getAbsolutePath());
                }
            } else {
                install(targetDirectory, entry.getName(), zip);
            }
            sendBroadcast(context, STATE_INSTALL, ((float) ++position / (float) totalEntries) * 0.99f);
        }
    } finally {
        if (zip != null) {
            try {
                zip.close();
            } catch (IOException ignored) {
            }
        }
        if (input != null) {
            try {
                input.close();
            } catch (IOException ignored) {
            }
        }
    }
    onInstallComplete(context);
    sendBroadcast(context, STATE_INSTALL, 1);
}

From source file:com.thinkbiganalytics.feedmgr.util.ImportUtil.java

public static Set<ImportComponentOption> inspectZipComponents(InputStream inputStream, ImportType importType)
        throws IOException {
    Set<ImportComponentOption> options = new HashSet<>();
    ZipInputStream zis = new ZipInputStream(inputStream);
    ZipEntry entry;/*from w  ww . j  a v a2s  .co  m*/
    while ((entry = zis.getNextEntry()) != null) {
        if (entry.getName().startsWith(ExportImportTemplateService.NIFI_TEMPLATE_XML_FILE)) {
            options.add(new ImportComponentOption(ImportComponent.NIFI_TEMPLATE,
                    importType.equals(ImportType.TEMPLATE) ? true : false));
        } else if (entry.getName().startsWith(ExportImportTemplateService.TEMPLATE_JSON_FILE)) {
            options.add(new ImportComponentOption(ImportComponent.TEMPLATE_DATA,
                    importType.equals(ImportType.TEMPLATE) ? true : false));
        } else if (entry.getName()
                .startsWith(ExportImportTemplateService.NIFI_CONNECTING_REUSABLE_TEMPLATE_XML_FILE)) {
            options.add(new ImportComponentOption(ImportComponent.REUSABLE_TEMPLATE, false));
        } else if (importType.equals(ImportType.FEED)
                && entry.getName().startsWith(ExportImportFeedService.FEED_JSON_FILE)) {
            options.add(new ImportComponentOption(ImportComponent.FEED_DATA, true));
            options.add(new ImportComponentOption(ImportComponent.USER_DATASOURCES, true));
        }
    }
    zis.closeEntry();
    zis.close();

    return options;
}