Example usage for java.util.zip ZipEntry getName

List of usage examples for java.util.zip ZipEntry getName

Introduction

In this page you can find the example usage for java.util.zip ZipEntry getName.

Prototype

public String getName() 

Source Link

Document

Returns the name of the entry.

Usage

From source file:com.google.gdt.eclipse.designer.gwtext.actions.ConfigureGwtExtOperation.java

private static void extractZip(InputStream zipFile, IFolder targetFolder) throws Exception {
    ZipInputStream zipInputStream = new ZipInputStream(zipFile);
    while (true) {
        ZipEntry zipEntry = zipInputStream.getNextEntry();
        if (zipEntry == null) {
            break;
        }/*from  w w w .  j a  va2s.  c o m*/
        if (!zipEntry.isDirectory()) {
            String entryName = zipEntry.getName();
            byte[] byteArray = IOUtils.toByteArray(zipInputStream);
            IOUtils2.setFileContents(targetFolder.getFile(new Path(entryName)),
                    new ByteArrayInputStream(byteArray));
        }
        zipInputStream.closeEntry();
    }
    zipInputStream.close();
}

From source file:functionalTests.vfsprovider.TestProActiveProvider.java

public static void extractZip(final ZipInputStream zipStream, final File dstFile) throws IOException {
    ZipEntry zipEntry;
    while ((zipEntry = zipStream.getNextEntry()) != null) {
        final File dstSubFile = new File(dstFile, zipEntry.getName());

        if (zipEntry.isDirectory()) {
            dstSubFile.mkdirs();/*w ww  .  jav a2 s .c  o  m*/
            if (!dstSubFile.exists() || !dstSubFile.isDirectory())
                throw new IOException("Could not create directory: " + dstSubFile);
        } else {
            final OutputStream os = new BufferedOutputStream(new FileOutputStream(dstSubFile));
            try {
                int data;
                while ((data = zipStream.read()) != -1)
                    os.write(data);
            } finally {
                os.close();
            }
        }
    }
}

From source file:com.microsoft.azurebatch.jenkins.utils.ZipHelper.java

private static void unzipFile(File zipFile, String outputFolderPath) throws FileNotFoundException, IOException {
    ZipInputStream zip = null;//from   w w w .  ja  v  a 2 s. c  o m
    try {
        zip = new ZipInputStream(new FileInputStream(zipFile));
        ZipEntry entry;

        while ((entry = zip.getNextEntry()) != null) {
            File entryFile = new File(outputFolderPath, entry.getName());
            if (entry.isDirectory()) {
                if (!entryFile.exists()) {
                    if (!entryFile.mkdirs()) {
                        throw new IOException(String.format("Failed to create folder %s %s", outputFolderPath,
                                entry.getName()));
                    }
                }
            } else {
                // Create parent folder if it's not exist
                File folder = entryFile.getParentFile();
                if (!folder.exists()) {
                    if (!folder.mkdirs()) {
                        throw new IOException(
                                String.format("Failed to create folder %s", folder.getAbsolutePath()));
                    }
                }

                // Create the target file
                if (!entryFile.createNewFile()) {
                    throw new IOException(
                            String.format("Failed to create entry file %s", entryFile.getAbsolutePath()));
                }

                // And rewrite data from stream
                OutputStream os = null;
                try {
                    os = new FileOutputStream(entryFile);
                    IOUtils.copy(zip, os);
                } finally {
                    IOUtils.closeQuietly(os);
                }
            }
        }
    } finally {
        IOUtils.closeQuietly(zip);
    }
}

From source file:com.ikon.util.ArchiveUtils.java

/**
 * Read file from ZIP//from  w  w w . j  av a  2s .  com
 */
public static byte[] readFileFromZip(ZipInputStream zis, String filename) throws IOException {
    ZipEntry zi = null;
    byte content[] = null;

    while ((zi = zis.getNextEntry()) != null) {
        if (filename.equals(zi.getName())) {
            IOUtils.toByteArray(zis);
            break;
        }
    }

    return content;
}

From source file:com.ikon.util.ArchiveUtils.java

/**
 * Read file from ZIP//from ww w .ja  v  a2 s.  c  o m
 */
public static InputStream getInputStreamFromZip(ZipInputStream zis, String filename) throws IOException {
    ZipEntry zi = null;
    InputStream is = null;

    while ((zi = zis.getNextEntry()) != null) {
        if (filename.equals(zi.getName())) {
            is = zis;
            break;
        }
    }

    return is;
}

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;
    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));
        }//from  w  w w  . ja va2 s  .  c  o m
    }
    zis.closeEntry();
    zis.close();

    return options;
}

From source file:io.vertx.config.vault.utils.VaultDownloader.java

private static void unzip(File zipFilePath, File destDir) throws IOException {
    if (!destDir.exists()) {
        destDir.mkdir();// ww w.  ja  v a 2  s.c  o m
    }
    ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
    ZipEntry entry = zipIn.getNextEntry();
    // iterates over entries in the zip file
    while (entry != null) {
        String filePath = destDir.getAbsolutePath() + 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.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
 *//*  ww  w .j a va  2 s  . co  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:com.chinamobile.bcbsp.fault.tools.Zip.java

/**
 * Uncompress *.zip files.//www . ja  v a  2  s.  co  m
 * @param fileName
 *        : file to be uncompress
 */
@SuppressWarnings("unchecked")
public static void decompress(String fileName) {
    File sourceFile = new File(fileName);
    String filePath = sourceFile.getParent() + "/";
    try {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        ZipFile zipFile = new ZipFile(fileName);
        Enumeration en = zipFile.entries();
        byte[] data = new byte[BUFFER];
        while (en.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) en.nextElement();
            if (entry.isDirectory()) {
                new File(filePath + entry.getName()).mkdirs();
                continue;
            }
            bis = new BufferedInputStream(zipFile.getInputStream(entry));
            File file = new File(filePath + entry.getName());
            File parent = file.getParentFile();
            if (parent != null && (!parent.exists())) {
                parent.mkdirs();
            }
            bos = new BufferedOutputStream(new FileOutputStream(file));
            int count;
            while ((count = bis.read(data, 0, BUFFER)) != -1) {
                bos.write(data, 0, count);
            }
            bis.close();
            bos.close();
        }
        zipFile.close();
    } catch (IOException e) {
        //LOG.error("[compress]", e);
        throw new RuntimeException("[compress]", e);
    }
}

From source file:Main.java

public static String unzip(String filename) throws IOException {
    BufferedOutputStream origin = null;
    String path = null;//www.j  a  v  a  2 s  .c  o m
    Integer BUFFER_SIZE = 20480;
    if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {

        File flockedFilesFolder = new File(
                Environment.getExternalStorageDirectory() + File.separator + "FlockLoad");
        System.out.println("FlockedFileDir: " + flockedFilesFolder);
        String compressedFile = flockedFilesFolder.toString() + "/" + filename;
        System.out.println("compressed File name:" + compressedFile);
        //String uncompressedFile = flockedFilesFolder.toString()+"/"+"flockUnZip";
        File purgeFile = new File(compressedFile);

        try {
            ZipInputStream zin = new ZipInputStream(new FileInputStream(compressedFile));
            BufferedInputStream bis = new BufferedInputStream(zin);
            try {
                ZipEntry ze = null;
                while ((ze = zin.getNextEntry()) != null) {
                    byte data[] = new byte[BUFFER_SIZE];
                    path = flockedFilesFolder.toString() + "/" + ze.getName();
                    System.out.println("path is:" + path);
                    if (ze.isDirectory()) {
                        File unzipFile = new File(path);
                        if (!unzipFile.isDirectory()) {
                            unzipFile.mkdirs();
                        }
                    } else {
                        FileOutputStream fout = new FileOutputStream(path, false);
                        origin = new BufferedOutputStream(fout, BUFFER_SIZE);
                        try {
                            /* for (int c = bis.read(data, 0, BUFFER_SIZE); c != -1; c = bis.read(data)) {
                            origin.write(c);
                             }
                             */
                            int count;
                            while ((count = bis.read(data, 0, BUFFER_SIZE)) != -1) {
                                origin.write(data, 0, count);
                            }
                            zin.closeEntry();
                        } finally {
                            origin.close();
                            fout.close();

                        }
                    }
                }
            } finally {
                bis.close();
                zin.close();
                purgeFile.delete();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return path;
    }
    return null;
}