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:edu.uci.ics.asterix.event.service.AsterixEventServiceUtil.java

public static void unzip(String sourceFile, String destDir) throws IOException {
    BufferedOutputStream dest = null;
    FileInputStream fis = new FileInputStream(sourceFile);
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
    ZipEntry entry = null;//  ww w. j ava  2 s  .  c om

    int BUFFER_SIZE = 4096;
    while ((entry = zis.getNextEntry()) != null) {
        String dst = destDir + File.separator + entry.getName();
        if (entry.isDirectory()) {
            createDir(destDir, entry);
            continue;
        }
        int count;
        byte data[] = new byte[BUFFER_SIZE];

        // write the file to the disk
        FileOutputStream fos = new FileOutputStream(dst);
        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:com.aurel.track.admin.customize.category.report.ReportBL.java

/**
 * Save the new template at given location(repository, project)
 * @param repository/* w  w  w  .  j a v a2 s  .c  o  m*/
 * @param personID
 * @param project
 * @param templateName
 * @param uploadZip
 * @return an error meassage if exist
 */
public static void saveTemplate(Integer id, ZipInputStream zis) throws IOException {
    // specify buffer size for extraction
    final int BUFFER = 2048;
    // Open Zip file for reading
    File unzipDestinationDirectory = getDirTemplate(id);
    BufferedOutputStream dest = null;
    ZipEntry entry;
    while ((entry = zis.getNextEntry()) != null) {
        File destFile = new File(unzipDestinationDirectory, entry.getName());
        if (destFile.exists()) {
            destFile.delete();
        }
        // grab file's parent directory structure
        File destinationParent = destFile.getParentFile();
        // create the parent directory structure if needed
        destinationParent.mkdirs();
        int count;
        byte data[] = new byte[BUFFER];
        // write the files to the disk
        FileOutputStream fos = new FileOutputStream(destFile);
        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:com.glaf.core.util.ZipUtils.java

public static byte[] getBytes(InputStream inputStream, String name) {
    byte[] bytes = null;
    ZipInputStream zipInputStream = null;
    try {/*from w  ww .  java2 s .  co  m*/
        zipInputStream = new ZipInputStream(inputStream);
        ZipEntry zipEntry = zipInputStream.getNextEntry();
        while (zipEntry != null) {
            String entryName = zipEntry.getName();
            if (entryName.equalsIgnoreCase(name)) {
                bytes = FileUtils.getBytes(zipInputStream);
                if (bytes != null) {
                    break;
                }
            }
            zipEntry = zipInputStream.getNextEntry();
        }
        return bytes;
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        IOUtils.closeStream(zipInputStream);
    }
}

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

/****************************************************************************
 **/*from  ww w  .  j av a  2 s .c  om*/
 ** Method:   locateMetadata()
 ** Input:  String zipFileName  --  The name of the zip file to be used
 ** Output: Vector  --  A vector of the names of xml files.  
 **
 ** Description: This method takes in the name of a zip file and locates 
 **              all files with an .xml extension an adds their names to a 
 **              vector. 
 **              
 *****************************************************************************/
public static Vector locateMetadata(String zipFileName) {
    if (_Debug) {
        System.out.println("***********************");
        System.out.println("in locateMetadata()    ");
        System.out.println("***********************\n");
    }

    //  An array of names of xml files to be returned to ColdFusion
    Vector metaDataVector = new Vector();
    String suffix = ".xml";

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

        if (_Debug) {
            System.out.println("Other meta-data located:");
        }
        while ((in.available() != 0)) {
            entry = in.getNextEntry();

            if (in.available() != 0) {
                if ((entry.getName()).endsWith(suffix)) {
                    if (_Debug) {
                        System.out.println(entry.getName());
                    }
                    metaDataVector.addElement(entry.getName());
                }
            }
        }
        in.close();

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

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

public static InputStream getShapefileFromCompressed(InputStream is, ShpFileType type) {
    InputStream shapefile = null;
    ZipInputStream zis = new ZipInputStream(is);
    ZipEntry ze;/*ww w . j a  v a  2  s.  com*/
    try {
        while ((ze = zis.getNextEntry()) != null) {
            if (!ze.isDirectory()) {
                String fileName = ze.getName().toLowerCase();
                String baseShapefileName = type.toBase(fileName);
                if (baseShapefileName != null) {
                    shapefile = zis;
                    break;
                }
            }
        }
    } catch (IOException e) {
        try {
            zis.closeEntry();
        } catch (IOException e2) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            zis.close();
        } catch (IOException e2) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return shapefile;
}

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

public static String getShapefileNameFromCompressed(InputStream is) {
    String shapefileName = null;/*from  w  w  w  .ja va2  s. c om*/
    ZipInputStream zis = new ZipInputStream(is);
    ZipEntry ze;
    try {
        while (((ze = zis.getNextEntry()) != null) && (shapefileName == null)) {
            if (!ze.isDirectory()) {
                String fileName = ze.getName().toLowerCase();
                if (fileName.endsWith(".shp")) {
                    shapefileName = fileName;
                }
            }
        }
    } 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 shapefileName;
}

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 w  w  .ja  va2  s. c  om
    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:com.taobao.android.utils.ZipUtils.java

/**
 * zip/*  ww w . j a v a 2  s  .c  o m*/
 *
 * @param zipFile
 * @param file
 * @param destPath  ?
 * @param overwrite ?
 * @throws IOException
 */
public static void addFileToZipFile(File zipFile, File outZipFile, File file, String destPath,
        boolean overwrite) throws IOException {
    byte[] buf = new byte[1024];
    ZipInputStream zin = new ZipInputStream(new FileInputStream(zipFile));
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outZipFile));
    ZipEntry entry = zin.getNextEntry();
    boolean addFile = true;
    while (entry != null) {
        boolean addEntry = true;
        String name = entry.getName();
        if (StringUtils.equalsIgnoreCase(name, destPath)) {
            if (overwrite) {
                addEntry = false;
            } else {
                addFile = false;
            }
        }
        if (addEntry) {
            ZipEntry zipEntry = null;
            if (ZipEntry.STORED == entry.getMethod()) {
                zipEntry = new ZipEntry(entry);
            } else {
                zipEntry = new ZipEntry(name);
            }
            out.putNextEntry(zipEntry);
            int len;
            while ((len = zin.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        }
        entry = zin.getNextEntry();
    }

    if (addFile) {
        InputStream in = new FileInputStream(file);
        // Add ZIP entry to output stream.
        ZipEntry zipEntry = new ZipEntry(destPath);
        out.putNextEntry(zipEntry);
        // Transfer bytes from the file to the ZIP file
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        // Complete the entry
        out.closeEntry();
        in.close();
    }
    // Close the streams
    zin.close();
    out.close();
}

From source file:com.t3.persistence.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  av  a 2s  . co  m*/
    String absDestDir = destDir.getAbsolutePath() + File.separator;

    // Pull out the files
    ZipEntry entry = null;

    while ((entry = in.getNextEntry()) != null) {
        if (entry.isDirectory())
            continue;

        // Prepare file destination
        String path = absDestDir + entry.getName();
        File file = new File(path);
        file.getParentFile().mkdirs();

        try (OutputStream out = new FileOutputStream(file)) {
            IOUtils.copy(in, out);
        }
        in.closeEntry();
    }
}

From source file:net.firejack.platform.core.utils.ArchiveUtils.java

public static void unzip(InputStream stream, ArchiveCallback callback, String... matches) {
    ZipInputStream in = new ZipInputStream(stream);
    ZipEntry entry;// w ww .j a va  2 s  .  c o m
    try {
        Map<String, File> skipped = new HashMap<String, File>();
        matches = (String[]) ArrayUtils.add(matches, ".*");
        while ((entry = in.getNextEntry()) != null) {
            String name = entry.getName();
            if (!entry.isDirectory()) {
                if (name.contains("/") && !File.separator.equals("/"))
                    name = name.replaceAll("/", File.separator + File.separator);
                if (name.contains("\\") && !File.separator.equals("\\"))
                    name = name.replaceAll("\\\\", File.separator);

                String dir = name.replaceAll("^(.*?)(?:\\\\|/?)([~@\\$\\{\\}\\w\\-\\.\\+]+$)", "$1");
                String file = name.replaceAll("^(.*?)(?:\\\\|/?)([~@\\$\\{\\}\\w\\-\\.\\+]+$)", "$2");

                if (file.matches(matches[0])) {
                    callback.callback(dir, file, in);
                } else {
                    File path = FileUtils.getTempFile(SecurityHelper.generateRandomSequence(16));
                    OutputStream out = FileUtils.openOutputStream(path);
                    IOUtils.copy(in, out);
                    IOUtils.closeQuietly(out);
                    skipped.put(name, path);
                }
            }
        }

        for (int i = 1; i < matches.length; i++) {
            for (Iterator<Map.Entry<String, File>> iterator = skipped.entrySet().iterator(); iterator
                    .hasNext();) {
                Map.Entry<String, File> next = iterator.next();
                String name = next.getKey();
                File path = next.getValue();

                String dir = name.replaceAll("(.*?)(?:\\\\|/?)([~@\\$\\{\\}\\w\\-\\.\\+]+$)", "$1");
                String file = name.replaceAll("(.*?)(?:\\\\|/?)([~@\\$\\{\\}\\w\\-\\.\\+]+$)", "$2");

                if (file.matches(matches[i])) {
                    iterator.remove();
                    FileInputStream inputStream = FileUtils.openInputStream(path);
                    callback.callback(dir, file, inputStream);
                    IOUtils.closeQuietly(inputStream);
                    FileUtils.forceDelete(path);
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}