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:Main.java

public static void unzipEPub(String inputZip, String destinationDirectory) throws IOException {
    int BUFFER = 2048;
    List zipFiles = new ArrayList();
    File sourceZipFile = new File(inputZip);
    File unzipDestinationDirectory = new File(destinationDirectory);
    unzipDestinationDirectory.mkdir();/*  ww  w  . ja  va2s  .c  o  m*/

    ZipFile zipFile;
    zipFile = new ZipFile(sourceZipFile, ZipFile.OPEN_READ);
    Enumeration zipFileEntries = zipFile.entries();

    // Process each entry
    while (zipFileEntries.hasMoreElements()) {

        ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
        String currentEntry = entry.getName();
        File destFile = new File(unzipDestinationDirectory, currentEntry);

        if (currentEntry.endsWith(".zip")) {
            zipFiles.add(destFile.getAbsolutePath());
        }

        File destinationParent = destFile.getParentFile();
        destinationParent.mkdirs();

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

            FileOutputStream fos = new FileOutputStream(destFile);
            BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);

            while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, currentByte);
            }
            dest.flush();
            dest.close();
            is.close();

        }

    }
    zipFile.close();

    for (Iterator iter = zipFiles.iterator(); iter.hasNext();) {
        String zipName = (String) iter.next();
        unzipEPub(zipName,
                destinationDirectory + File.separatorChar + zipName.substring(0, zipName.lastIndexOf(".zip")));
    }
}

From source file:Main.java

/***
 * Extract zipfile to outdir with complete directory structure
 * disclaimer, I didn't wrote that ...//from   ww w  . j  a v  a 2s. c  om
 * @param zipfile Input .zip file
 * @param outdir Output directory
 */
public static void extract(File zipfile, File outdir) {
    // todo: replace with more trustful method
    try {
        ZipInputStream zin = new ZipInputStream(new FileInputStream(zipfile));
        ZipEntry entry;
        String name, dir;
        while ((entry = zin.getNextEntry()) != null) {
            name = entry.getName();
            if (entry.isDirectory()) {
                mkdirs(outdir, name);
                continue;
            }
            dir = dirpart(name);
            if (dir != null) {
                mkdirs(outdir, dir);
            }
            extractFile(zin, outdir, name);
        }
        zin.close();
    } catch (IOException err) {
        throw new RuntimeException("Unable to extract quiz", err);
    }
}

From source file:Utils.java

/**
 * Unpack a zip file//  w  ww .  j  ava 2s.co m
 * 
 * @param theFile
 * @param targetDir
 * @return the file
 * @throws IOException
 */
public static File unpackArchive(File theFile, File targetDir) throws IOException {
    if (!theFile.exists()) {
        throw new IOException(theFile.getAbsolutePath() + " does not exist");
    }
    if (!buildDirectory(targetDir)) {
        throw new IOException("Could not create directory: " + targetDir);
    }
    ZipFile zipFile = new ZipFile(theFile);
    for (Enumeration entries = zipFile.entries(); entries.hasMoreElements();) {
        ZipEntry entry = (ZipEntry) entries.nextElement();
        File file = new File(targetDir, File.separator + entry.getName());
        if (!buildDirectory(file.getParentFile())) {
            throw new IOException("Could not create directory: " + file.getParentFile());
        }
        if (!entry.isDirectory()) {
            copyInputStream(zipFile.getInputStream(entry),
                    new BufferedOutputStream(new FileOutputStream(file)));
        } else {
            if (!buildDirectory(file)) {
                throw new IOException("Could not create directory: " + file);
            }
        }
    }
    zipFile.close();
    return theFile;
}

From source file:Main.java

/**
 * kudos: http://www.jondev.net/articles/Unzipping_Files_with_Android_(Programmatically)
 * @param zipFile/*from w  w w .j a  va  2s  .  c o m*/
 * @param destinationDirectory
 */
public static void unZip(String zipFile, String destinationDirectory) {
    try {
        FileInputStream fin = new FileInputStream(zipFile);
        ZipInputStream zin = new ZipInputStream(fin);
        ZipEntry ze = null;
        while ((ze = zin.getNextEntry()) != null) {
            Log.v("Decompress", "Unzipping " + ze.getName());
            String destinationPath = destinationDirectory + File.separator + ze.getName();
            if (ze.isDirectory()) {
                dirChecker(destinationPath);
            } else {
                FileOutputStream fout;
                try {
                    File outputFile = new File(destinationPath);
                    if (!outputFile.getParentFile().exists()) {
                        dirChecker(outputFile.getParentFile().getPath());
                    }
                    fout = new FileOutputStream(destinationPath);
                    for (int c = zin.read(); c != -1; c = zin.read()) {
                        fout.write(c);
                    }
                    zin.closeEntry();
                    fout.close();
                } catch (Exception e) {
                    // ok for now.
                    Log.v("Decompress", "Error: " + e.getMessage());
                }
            }
        }
        zin.close();
    } catch (Exception e) {
        Log.e("Decompress", "unzip", e);
    }
}

From source file:net.sf.sripathi.ws.mock.util.FileUtil.java

public static void createFilesAndFolder(String root, ZipFile zip) {

    @SuppressWarnings("unchecked")
    Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zip.entries();
    Set<String> files = new TreeSet<String>();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();

        if (entry.getName().toLowerCase().endsWith(".wsdl") || entry.getName().toLowerCase().endsWith(".xsd"))
            files.add(entry.getName());//from  ww w .j av a2  s.c om
    }

    File rootFolder = new File(root);
    if (!rootFolder.exists()) {
        throw new MockException("Unable to create file - " + root);
    }

    for (String fileStr : files) {

        String folder = root;

        String[] split = fileStr.split("/");

        if (split.length > 1) {

            for (int i = 0; i < split.length - 1; i++) {

                folder = folder + "/" + split[i];
                File f = new File(folder);
                if (!f.exists()) {
                    f.mkdir();
                }
            }
        }

        File file = new File(folder + "/" + split[split.length - 1]);
        FileOutputStream fos = null;
        InputStream zipStream = null;
        try {
            fos = new FileOutputStream(file);
            zipStream = zip.getInputStream(zip.getEntry(fileStr));
            fos.write(IOUtils.toByteArray(zipStream));
        } catch (Exception e) {
            throw new MockException("Unable to create file - " + fileStr);
        } finally {
            try {
                fos.close();
            } catch (Exception e) {
            }
            try {
                zipStream.close();
            } catch (Exception e) {
            }
        }
    }
}

From source file:com.netflix.nicobar.core.utils.__JDKPaths.java

static void processJar(final Set<String> pathSet, final File file) throws IOException {
    final ZipFile zipFile = new ZipFile(file);
    try {//  w  w w .  j ava 2  s .  c o m
        final Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            final ZipEntry entry = entries.nextElement();
            final String name = entry.getName();
            final int lastSlash = name.lastIndexOf('/');
            if (lastSlash != -1) {
                pathSet.add(name.substring(0, lastSlash));
            }
        }
        zipFile.close();
    } finally {
        IOUtils.closeQuietly(zipFile);
    }
}

From source file:Main.java

public static void unpack(InputStream in, File targetDir) throws IOException {
    ZipInputStream zin = new ZipInputStream(in);
    ZipEntry entry;
    while ((entry = zin.getNextEntry()) != null) {
        String extractFilePath = entry.getName();
        if ((extractFilePath.startsWith("/")) || (extractFilePath.startsWith("\\"))) {
            extractFilePath = extractFilePath.substring(1);
        }/*from ww  w .j a  v  a  2s  .  c  o m*/
        File extractFile = new File(new StringBuilder().append(targetDir.getPath()).append(File.separator)
                .append(extractFilePath).toString());
        if (entry.isDirectory()) {
            if (!extractFile.exists())
                extractFile.mkdirs();
        } else {
            File parent = extractFile.getParentFile();
            if (!parent.exists()) {
                parent.mkdirs();
            }

            FileOutputStream os = new FileOutputStream(extractFile);
            copyFile(zin, os);
            os.flush();
            os.close();
        }
    }
}

From source file:eurecom.constrained.devices.ReadZipFile.java

protected static File file(final File root, final ZipEntry entry) throws IOException {

    final File file = new File(root, entry.getName());

    File parent = file;/*w  w  w .  j  av a 2s.c  o m*/
    if (!entry.isDirectory()) {
        final String name = entry.getName();

        //get name file from the zip
        FILE_TO_LOAD.add(name);

        final int index = name.lastIndexOf('/');
        if (index != -1) {
            parent = new File(root, name.substring(0, index));
        }
    }
    if (parent != null && !parent.isDirectory() && !parent.mkdirs()) {
        throw new IOException("failed to create a directory: " + parent.getPath());
    }

    return file;
}

From source file:Main.java

public static void unzip(InputStream is, String dir) throws IOException {
    File dest = new File(dir);
    if (!dest.exists()) {
        dest.mkdirs();/*w  ww.  ja  v  a2s  . c  om*/
    }

    if (!dest.isDirectory())
        throw new IOException("Invalid Unzip destination " + dest);
    if (null == is) {
        throw new IOException("InputStream is null");
    }

    ZipInputStream zip = new ZipInputStream(is);

    ZipEntry ze;
    while ((ze = zip.getNextEntry()) != null) {
        final String path = dest.getAbsolutePath() + File.separator + ze.getName();

        String zeName = ze.getName();
        char cTail = zeName.charAt(zeName.length() - 1);
        if (cTail == File.separatorChar) {
            File file = new File(path);
            if (!file.exists()) {
                if (!file.mkdirs()) {
                    throw new IOException("Unable to create folder " + file);
                }
            }
            continue;
        }

        FileOutputStream fout = new FileOutputStream(path);
        byte[] bytes = new byte[1024];
        int c;
        while ((c = zip.read(bytes)) != -1) {
            fout.write(bytes, 0, c);
        }
        zip.closeEntry();
        fout.close();
    }
}

From source file:Main.java

public static void unZipFolder(InputStream input, String outPathString) throws Exception {
    java.util.zip.ZipInputStream inZip = new java.util.zip.ZipInputStream(input);
    java.util.zip.ZipEntry zipEntry = null;
    String szName = "";

    while ((zipEntry = inZip.getNextEntry()) != null) {
        szName = zipEntry.getName();

        if (zipEntry.isDirectory()) {

            // get the folder name of the widget
            szName = szName.substring(0, szName.length() - 1);
            java.io.File folder = new java.io.File(outPathString + java.io.File.separator + szName);
            folder.mkdirs();/*w  w w. j  av a  2s .c o m*/

        } else {

            java.io.File file = new java.io.File(outPathString + java.io.File.separator + szName);
            file.createNewFile();
            // get the output stream of the file
            java.io.FileOutputStream out = new java.io.FileOutputStream(file);
            int len;
            byte[] buffer = new byte[1024];
            // read (len) bytes into buffer
            while ((len = inZip.read(buffer)) != -1) {
                // write (len) byte from buffer at the position 0
                out.write(buffer, 0, len);
                out.flush();
            }
            out.close();
        }
    } //end of while

    inZip.close();
}