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.t3.persistence.FileUtil.java

public static void unzipFile(File sourceFile, File destDir) throws IOException {
    if (!sourceFile.exists())
        throw new IOException("source file does not exist: " + sourceFile);

    try (ZipFile zipFile = new ZipFile(sourceFile)) {
        Enumeration<? extends ZipEntry> entries = zipFile.entries();

        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (entry.isDirectory())
                continue;

            String path = destDir.getAbsolutePath() + File.separator + entry.getName();
            File file = new File(path);
            file.getParentFile().mkdirs();

            //System.out.println("Writing file: " + path);
            try (InputStream is = zipFile.getInputStream(entry);
                    BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(path));) {
                IOUtils.copy(is, os);/*from  w  w w .  j  ava  2 s .  c om*/
            }
        }
    }
}

From source file:com.mc.printer.model.utils.ZipHelper.java

public static void unZip(String sourceZip, String outDirName) throws IOException {
    log.info("unzip source:" + sourceZip);
    log.info("unzip to :" + outDirName);
    ZipFile zfile = new ZipFile(sourceZip);
    System.out.println(zfile.getName());
    Enumeration zList = zfile.entries();
    ZipEntry ze = null;
    byte[] buf = new byte[1024];
    while (zList.hasMoreElements()) {
        //ZipFileZipEntry
        ze = (ZipEntry) zList.nextElement();
        if (ze.isDirectory()) {
            continue;
        }/*w ww . ja v  a2 s  . c  o m*/
        //ZipEntry?InputStreamOutputStream
        File fil = getRealFileName(outDirName, ze.getName());

        OutputStream os = new BufferedOutputStream(new FileOutputStream(fil));
        InputStream is = new BufferedInputStream(zfile.getInputStream(ze));
        int readLen = 0;
        while ((readLen = is.read(buf, 0, 1024)) != -1) {
            os.write(buf, 0, readLen);
        }
        is.close();
        os.close();
        log.debug("Extracted: " + ze.getName());
    }
    zfile.close();

}

From source file:com.pari.mw.api.execute.reports.template.ReportTemplateRunner.java

public static File getTopLevelFolder(File zipFile) throws IOException {
    ZipInputStream in = null;// ww w . j  a  v  a  2s  .  com
    try {
        in = new ZipInputStream(new FileInputStream(zipFile));
        ZipEntry entry = null;

        while ((entry = in.getNextEntry()) != null) {
            String outFilename = entry.getName();

            if (entry.isDirectory()) {
                return new File(outFilename);
            }
        }
    } finally {
        if (in != null) {
            in.close();
        }
    }
    return null;
}

From source file:com.cenrise.test.azkaban.Utils.java

public static void unzip(final ZipFile source, final File dest) throws IOException {
    final Enumeration<?> entries = source.entries();
    while (entries.hasMoreElements()) {
        final ZipEntry entry = (ZipEntry) entries.nextElement();
        final File newFile = new File(dest, entry.getName());
        if (entry.isDirectory()) {
            newFile.mkdirs();/*  w  w  w .  j av a 2  s  . com*/
        } else {
            newFile.getParentFile().mkdirs();
            final InputStream src = source.getInputStream(entry);
            try {
                final OutputStream output = new BufferedOutputStream(new FileOutputStream(newFile));
                try {
                    IOUtils.copy(src, output);
                } finally {
                    output.close();
                }
            } finally {
                src.close();
            }
        }
    }
}

From source file:es.eucm.eadandroid.homeapp.repository.resourceHandler.RepoResourceHandler.java

/**
 * Uncompresses any zip file//  w  w  w.j  a  v  a 2 s.  c  o  m
 */
public static void unzip(String path_from, String path_to, String name, boolean deleteZip) {

    StringTokenizer separator = new StringTokenizer(name, ".", true);
    String file_name = separator.nextToken();

    File f = new File(path_to + file_name);

    if (f.exists())
        removeDirectory(f);

    separator = new StringTokenizer(path_to + file_name, "/", true);

    String partial_path = null;
    String total_path = separator.nextToken();

    while (separator.hasMoreElements()) {

        partial_path = separator.nextToken();
        total_path = total_path + partial_path;
        if (!new File(total_path).exists()) {

            if (separator.hasMoreElements())
                total_path = total_path + separator.nextToken();
            else
                (new File(total_path)).mkdir();

        } else
            total_path = total_path + separator.nextToken();

    }

    Enumeration<? extends ZipEntry> entries = null;
    ZipFile zipFile = null;

    try {
        String location_ead = path_from + name;
        zipFile = new ZipFile(location_ead);

        entries = zipFile.entries();

        BufferedOutputStream file;

        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();

            separator = new StringTokenizer(entry.getName(), "/", true);
            partial_path = null;
            total_path = "";

            while (separator.hasMoreElements()) {

                partial_path = separator.nextToken();
                total_path = total_path + partial_path;
                if (!new File(entry.getName()).exists()) {

                    if (separator.hasMoreElements()) {
                        total_path = total_path + separator.nextToken();
                        (new File(path_to + file_name + "/" + total_path)).mkdir();
                    } else {

                        file = new BufferedOutputStream(
                                new FileOutputStream(path_to + file_name + "/" + total_path));

                        System.err.println("Extracting file: " + entry.getName());
                        copyInputStream(zipFile.getInputStream(entry), file);
                    }
                } else {
                    total_path = total_path + separator.nextToken();
                }
            }

        }

        zipFile.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
        return;
    }

    if (deleteZip)
        (new File(path_from + name)).delete();

}

From source file:hu.sztaki.lpds.pgportal.services.dspace.DSpaceUtil.java

/**
 * Extracts any file from <code>inputFile</code> that begins with
 * 'bitstream' to <code>outputFile</code>.
 * Adapted from examples on/*from  ww w.  j  a v  a 2 s .  c o  m*/
 * http://java.sun.com/developer/technicalArticles/Programming/compression/
 *
 * @param inputFile  File to extract 'bitstream...' from
 * @param outputFile  File to extract 'bitstream...' to
 */
private static void unzipBitstream(InputStream is, File outputFile) throws IOException {
    final int BUFFER = 2048;
    BufferedOutputStream dest = null;
    ZipInputStream zis = new ZipInputStream(is);
    ZipEntry entry;
    try {

        while ((entry = zis.getNextEntry()) != null) {
            if (entry.getName().matches("bitstream.*")) {
                int count;
                byte data[] = new byte[BUFFER];

                // write the files to the disk
                FileOutputStream fos = new FileOutputStream(outputFile.getAbsoluteFile());
                dest = new BufferedOutputStream(fos, BUFFER);
                while ((count = zis.read(data, 0, BUFFER)) != -1) {
                    dest.write(data, 0, count);
                    //System.out.println("Writing: " + outputFile.getAbsoluteFile());
                }
                dest.flush();
                dest.close();
            }
        }
    } finally {
        zis.close();
        try {
            dest.close();
        } catch (Exception e) {
        }
    }
}

From source file:io.github.wysohn.triggerreactor.tools.ReflectionUtil.java

public static List<String> getAllClasses(ClassLoader cl, String packageName) {
    packageName = packageName.replace('.', '/');

    List<String> classes = new ArrayList<>();

    URL[] urls = ((URLClassLoader) cl).getURLs();
    for (URL url : urls) {
        //System.out.println(url.getFile());
        File jar = new File(url.getFile());

        if (jar.isDirectory()) {
            File subdir = new File(jar, packageName);
            if (!subdir.exists())
                continue;
            File[] files = subdir.listFiles();
            for (File file : files) {
                if (!file.isFile())
                    continue;
                if (file.getName().endsWith(".class"))
                    classes.add(file.getName().substring(0, file.getName().length() - 6).replace('/', '.'));
            }//from   ww w .j  av a2s.co  m
        }

        else {
            // try to open as ZIP
            try {
                ZipFile zip = new ZipFile(jar);
                for (Enumeration<? extends ZipEntry> entries = zip.entries(); entries.hasMoreElements();) {
                    ZipEntry entry = entries.nextElement();
                    String name = entry.getName();
                    if (!name.startsWith(packageName))
                        continue;
                    if (name.endsWith(".class") && name.indexOf('$') < 0)
                        classes.add(name.substring(0, name.length() - 6).replace('/', '.'));
                }
                zip.close();
            } catch (ZipException e) {
                //System.out.println("Not a ZIP: " + e.getMessage());
            } catch (IOException e) {
                System.err.println(e.getMessage());
            }
        }
    }

    return classes;
}

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

/**
 * @param zip// www . j a v a2 s. co  m
 * @param name
 * @return
 * @throws java.io.IOException
 */
public static InputStream getFile(InputStream zip, String name) throws IOException {
    ZipInputStream in = new ZipInputStream(zip);
    ZipEntry entry;
    while ((entry = in.getNextEntry()) != null) {
        String entityName = entry.getName();
        if (!entry.isDirectory() && entityName.contains(name)) {
            return in;
        }
    }
    in.close();
    return null;
}

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

/**
 * @param stream zip input stream//w ww  .ja va2  s .c om
 * @param dir directory to unzip
 * @param unique if need add unique suffix to every file name then true
 * @throws java.io.IOException
 * @return list of unzipped file names
 */
public static List<String> unZIP(InputStream stream, File dir, boolean unique) throws IOException {
    Long createdTime = new Date().getTime();
    String randomName = SecurityHelper.generateRandomSequence(16);
    List<String> fileNames = new ArrayList<String>();
    ZipInputStream in = new ZipInputStream(stream);
    ZipEntry entry;
    while ((entry = in.getNextEntry()) != null) {
        String name = entry.getName();
        if (entry.isDirectory()) {
            FileUtils.forceMkdir(new File(dir, name));
        } else {
            String dirName = "";
            if (name.contains("/") && !File.separator.equals("/"))
                name = name.replaceAll("/", File.separator + File.separator);
            if (name.contains("\\") && !File.separator.equals("\\"))
                name = name.replaceAll("\\\\", File.separator);

            if (name.lastIndexOf(File.separator) != -1)
                dirName = name.substring(0, name.lastIndexOf(File.separator));
            String fileName = name.substring(name.lastIndexOf(File.separator) + 1, name.length());
            if (unique) {
                fileName = fileName + "." + randomName + "." + createdTime;
            }
            OutputStream out = FileUtils.openOutputStream(FileUtils.create(dir, dirName, fileName));
            IOUtils.copy(in, out);
            IOUtils.closeQuietly(out);
            fileNames.add(fileName);
        }
    }
    IOUtils.closeQuietly(in);
    return fileNames;
}

From source file:com.aurel.track.lucene.util.FileUtil.java

public static void unzipFile(File uploadZip, File unzipDestinationDirectory) throws IOException {
    if (!unzipDestinationDirectory.exists()) {
        unzipDestinationDirectory.mkdirs();
    }//w w  w.  ja v a  2 s.  co m
    final int BUFFER = 2048;
    // Open Zip file for reading
    ZipFile zipFile = new ZipFile(uploadZip, ZipFile.OPEN_READ);

    // Create an enumeration of the entries in the zip file
    Enumeration zipFileEntries = zipFile.entries();

    // Process each entry
    while (zipFileEntries.hasMoreElements()) {
        // grab a zip file entry
        ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
        String currentEntry = entry.getName();
        File destFile = new File(unzipDestinationDirectory, currentEntry);
        // grab file's parent directory structure
        File destinationParent = destFile.getParentFile();

        // create the parent directory structure if needed
        destinationParent.mkdirs();

        // extract file if not a directory
        if (!entry.isDirectory()) {
            BufferedInputStream is = new BufferedInputStream(zipFile.getInputStream(entry));
            int currentByte;
            // establish buffer for writing file
            byte data[] = new byte[BUFFER];

            // write the current file to disk
            FileOutputStream fos = new FileOutputStream(destFile);
            BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);

            // read and write until last byte is encountered
            while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, currentByte);
            }
            dest.flush();
            dest.close();
            is.close();
        }
    }
    zipFile.close();
}