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:localization.split.java

public static Vector<String> readzipfile(String filepath) {
    Vector<String> v = new Vector<String>();
    byte[] buffer = new byte[1024];
    String outputFolder = filepath.substring(0, filepath.lastIndexOf("."));
    System.out.println(outputFolder);
    try {//  w  w  w. j  a va2  s  .  com
        File folder = new File(outputFolder);
        if (!folder.exists()) {
            folder.mkdir();
        }
        ZipInputStream zis = new ZipInputStream(new FileInputStream(filepath));
        ZipEntry ze = zis.getNextEntry();
        while (ze != null) {
            String fileName = ze.getName();
            File newFile = new File(outputFolder + "\\" + fileName);
            v.addElement(newFile.getAbsolutePath());
            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 (Exception e) {

    }
    return v;
}

From source file:org.geoserver.fgdb.FGDBDataStoreFactoryTest.java

/***
 * Unpack archive to directory (maintaining directory structure).
 * //www.j a  va2  s.c  o  m
 * @param archive
 * @param directory
 */
public static void unpack(File archive, File directory) throws IOException {
    // see http://stackoverflow.com/questions/10633595/java-zip-how-to-unzip-folder
    ZipInputStream zip = new ZipInputStream(new FileInputStream(archive));
    try {
        for (ZipEntry entry; (entry = zip.getNextEntry()) != null;) {
            String name = entry.getName();
            if (entry.isDirectory()) {
                mkdirs(directory, name);
                continue;
            }
            /*
             * this part is necessary because file entry can come before directory entry where is file located i.e.: /foo/foo.txt /foo/
             */
            String dir = directoryName(name);
            if (dir != null) {
                mkdirs(directory, dir);
            }
            unpackFile(zip, directory, name);
        }
        zip.close();
    } finally {
        zip.close();
    }
}

From source file:com.microsoft.intellij.AzurePlugin.java

private static void unzip(String zipFilePath, String destDirectory) throws IOException {
    File destDir = new File(destDirectory);
    if (!destDir.exists()) {
        destDir.mkdir();//from w w  w.  jav  a2  s  .  c om
    }
    ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
    ZipEntry entry = zipIn.getNextEntry();
    while (entry != null) {
        String filePath = destDirectory + File.separator + entry.getName();
        if (!entry.isDirectory()) {
            extractFile(zipIn, filePath);
        } else {
            File dir = new File(filePath);
            dir.mkdir();
        }
        zipIn.closeEntry();
        entry = zipIn.getNextEntry();
    }
    zipIn.close();
}

From source file:Main.java

public static byte[] readBytesFormZipFile(String fileName, String fname) {
    byte[] bytes = null;
    ByteArrayOutputStream os = null;
    ZipInputStream in = null;
    try {/*w  w  w .  j ava  2 s . c o m*/
        os = new ByteArrayOutputStream(4096);
        in = new ZipInputStream(new FileInputStream(fileName));
        boolean found = false;
        while (!found) {
            ZipEntry entry = in.getNextEntry();
            if (fname.equalsIgnoreCase(entry.getName())) {
                found = true;
            }
        }
        int read;
        byte[] buffer = new byte[4096];
        while ((read = in.read(buffer)) >= 0) {
            os.write(buffer, 0, read);
        }
        bytes = os.toByteArray();
    } catch (Exception e) {
        bytes = null;
    } finally {
        try {
            if (os != null) {
                os.flush();
                os = null;
            }
            if (in != null) {
                in.close();
                in = null;
            }
        } catch (Exception e) {
        }
    }

    return bytes;
}

From source file:com.jlgranda.fede.ejb.url.reader.FacturaElectronicaURLReader.java

/**
 * Obtiene la lista de objetos factura para el sujeto en fede
 *
 * @param urls URLs hacia los archivo XML o ZIP a leer
 * @return una lista de instancias FacturaReader
 *///from  w  w  w .  j  a  v a  2  s  . c o  m
public static FacturaReader getFacturaElectronica(String url) throws Exception {
    FacturaReader facturaReader = null;

    if (url.endsWith(".xml")) {
        String xml = FacturaElectronicaURLReader.read(url);
        facturaReader = new FacturaReader(FacturaUtil.read(xml), xml, url);
    } else if (url.endsWith(".zip")) {
        URL url_ = new URL(url);
        InputStream is = url_.openStream();
        ZipInputStream zis = new ZipInputStream(is);
        try {

            ZipEntry entry = null;
            String tmp = null;
            ByteArrayOutputStream fout = null;
            while ((entry = zis.getNextEntry()) != null) {
                if (entry.getName().toLowerCase().endsWith(".xml")) {
                    fout = new ByteArrayOutputStream();
                    for (int c = zis.read(); c != -1; c = zis.read()) {
                        fout.write(c);
                    }

                    tmp = new String(fout.toByteArray(), Charset.defaultCharset());
                    facturaReader = new FacturaReader(FacturaUtil.read(tmp), tmp, url);
                    fout.close();
                }
                zis.closeEntry();
            }
            zis.close();

        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(zis);
        }
    }

    return facturaReader;
}

From source file:Main.java

public static void unZip(String zipFile, String outputFolder) throws IOException {

    byte[] buffer = new byte[1024];

    //create output directory is not exists
    File folder = new File(outputFolder);
    if (!folder.exists()) {
        folder.mkdir();/*ww  w.j  a va2  s .c  om*/
    }

    //get the zip file content
    ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
    //get the zipped file list entry
    ZipEntry ze = zis.getNextEntry();

    while (ze != null) {

        String fileName = ze.getName();
        File newFile = new File(outputFolder + File.separator + fileName);

        //create all non exists folders
        //else you will hit FileNotFoundException for compressed folder
        if (ze.isDirectory())
            newFile.mkdirs();
        else {
            newFile.getParentFile().mkdirs();

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

}

From source file:Main.java

public static void unzip(File zipFile, File targetDirectory) throws IOException {
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile)));
    try {//from  w w w  .ja v a  2  s.  c om
        ZipEntry ze;
        int count;
        byte[] buffer = new byte[8192];
        while ((ze = zis.getNextEntry()) != null) {
            File file = new File(targetDirectory, ze.getName());
            File dir = ze.isDirectory() ? file : file.getParentFile();
            if (!dir.isDirectory() && !dir.mkdirs())
                throw new FileNotFoundException("Failed to ensure directory: " + dir.getAbsolutePath());
            if (ze.isDirectory())
                continue;
            FileOutputStream fout = new FileOutputStream(file);
            try {
                while ((count = zis.read(buffer)) != -1)
                    fout.write(buffer, 0, count);
            } finally {
                fout.close();
            }
        }
    } finally {
        zis.close();
    }
}

From source file:org.ancoron.osgi.test.glassfish.GlassfishHelper.java

public static void unzip(String zipFile, String targetPath) throws IOException {
    log.log(Level.INFO, "Extracting {0} ...", zipFile);
    log.log(Level.INFO, "...target directory: {0}", targetPath);
    File path = new File(targetPath);
    path.delete();//from  w  w  w . j  a va2s  . co m
    path.mkdirs();

    BufferedOutputStream dest = null;
    FileInputStream fis = new FileInputStream(zipFile);
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
    ZipEntry entry;
    while ((entry = zis.getNextEntry()) != null) {
        if (entry.isDirectory()) {
            File f = new File(targetPath + "/" + entry.getName());
            f.mkdirs();
            continue;
        }
        int count;
        byte data[] = new byte[BUFFER];

        // write the files to the disk
        FileOutputStream fos = new FileOutputStream(targetPath + "/" + 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();
}

From source file:org.adl.samplerte.server.LMSPackageHandler.java

/****************************************************************************
 **/*w w  w .  j a va  2s.c  o m*/
 ** Method:   findMetadata()
 ** Input:  String zipFileName  --  The name of the zip file to be used
 ** Output: Boolean  --  Whether or not any xml files were found  
 **
 ** Description: This method takes in the name of a zip file and locates 
 **              all files with an .xml extension 
 **              
 *****************************************************************************/
public static boolean findMetadata(String zipFileName) {
    if (_Debug) {
        System.out.println("***********************");
        System.out.println("in findMetadata()      ");
        System.out.println("***********************\n");
    }

    boolean rtn = false;
    String suffix = ".xml";

    try {
        //  The zip file being searched.
        ZipInputStream in = new ZipInputStream(new FileInputStream(zipFileName));
        //  An entry in the zip file
        ZipEntry entry;

        while ((in.available() != 0)) {
            entry = in.getNextEntry();

            if (in.available() != 0) {
                if ((entry.getName()).endsWith(suffix)) {
                    rtn = true;
                    if (_Debug) {
                        System.out.println("Other Meta-data located... returning true");
                    }
                }
            }
        }

        in.close();
    } catch (IOException e) {
        if (_Debug) {
            System.out.println("IO Exception Caught: " + e);
        }
    }

    return rtn;
}

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

/**
 * Add files to a zip file/*from  w  w  w . ja va2  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();
        }
    }
}