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.diffplug.gradle.ZipMisc.java

/**
 * Modifies only the specified entries in a zip file. 
 *
 * @param input       a source from a zip file
 * @param output      an output to a zip file
 * @param toModify      a map from path to an input stream for the entries you'd like to change
 * @param toOmit      a set of entries you'd like to leave out of the zip
 * @throws IOException/*from w  w w  .j a v  a  2 s . com*/
 */
public static void modify(ByteSource input, ByteSink output, Map<String, Function<byte[], byte[]>> toModify,
        Predicate<String> toOmit) throws IOException {
    try (ZipInputStream zipInput = new ZipInputStream(input.openBufferedStream());
            ZipOutputStream zipOutput = new ZipOutputStream(output.openBufferedStream())) {
        while (true) {
            // read the next entry
            ZipEntry entry = zipInput.getNextEntry();
            if (entry == null) {
                break;
            }

            Function<byte[], byte[]> replacement = toModify.get(entry.getName());
            if (replacement != null) {
                byte[] clean = ByteStreams.toByteArray(zipInput);
                byte[] modified = replacement.apply(clean);
                // if it's the entry being modified, enter the modified stuff
                try (InputStream replacementStream = new ByteArrayInputStream(modified)) {
                    ZipEntry newEntry = new ZipEntry(entry.getName());
                    newEntry.setComment(entry.getComment());
                    newEntry.setExtra(entry.getExtra());
                    newEntry.setMethod(entry.getMethod());
                    newEntry.setTime(entry.getTime());

                    zipOutput.putNextEntry(newEntry);
                    copy(replacementStream, zipOutput);
                }
            } else if (!toOmit.test(entry.getName())) {
                // if it isn't being modified, just copy the file stream straight-up
                ZipEntry newEntry = new ZipEntry(entry);
                newEntry.setCompressedSize(-1);
                zipOutput.putNextEntry(newEntry);
                copy(zipInput, zipOutput);
            }

            // close the entries
            zipInput.closeEntry();
            zipOutput.closeEntry();
        }
    }
}

From source file:Main.java

public static void unZip(String path) {
    int count = -1;
    int index = -1;

    String savepath = "";

    savepath = path.substring(0, path.lastIndexOf("."));
    try {/* ww w.j a va 2  s . c o m*/
        BufferedOutputStream bos = null;
        ZipEntry entry = null;
        FileInputStream fis = new FileInputStream(path);
        ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));

        while ((entry = zis.getNextEntry()) != null) {
            byte data[] = new byte[buffer];

            String temp = entry.getName();
            index = temp.lastIndexOf("/");
            if (index > -1)
                temp = temp.substring(index + 1);
            String tempDir = savepath + "/zip/";
            File dir = new File(tempDir);
            dir.mkdirs();
            temp = tempDir + temp;
            File f = new File(temp);
            f.createNewFile();

            FileOutputStream fos = new FileOutputStream(f);
            bos = new BufferedOutputStream(fos, buffer);

            while ((count = zis.read(data, 0, buffer)) != -1) {
                bos.write(data, 0, count);
            }

            bos.flush();
            bos.close();
        }

        zis.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:dk.netarkivet.common.utils.ZipUtils.java

/** Unzip a zipFile into a directory.  This will create subdirectories
 * as needed./*from  www. ja v  a2s.  com*/
 *
 * @param zipFile The file to unzip
 * @param toDir The directory to create the files under.  This directory
 * will be created if necessary.  Files in it will be overwritten if the
 * filenames match.
 */
public static void unzip(File zipFile, File toDir) {
    ArgumentNotValid.checkNotNull(zipFile, "File zipFile");
    ArgumentNotValid.checkNotNull(toDir, "File toDir");
    ArgumentNotValid.checkTrue(toDir.getAbsoluteFile().getParentFile().canWrite(),
            "can't write to '" + toDir + "'");
    ArgumentNotValid.checkTrue(zipFile.canRead(), "can't read '" + zipFile + "'");
    InputStream inputStream = null;
    ZipFile unzipper = null;
    try {
        try {
            unzipper = new ZipFile(zipFile);
            Enumeration<? extends ZipEntry> entries = unzipper.entries();
            while (entries.hasMoreElements()) {
                ZipEntry ze = entries.nextElement();
                File target = new File(toDir, ze.getName());
                // Ensure that its dir exists
                FileUtils.createDir(target.getCanonicalFile().getParentFile());
                if (ze.isDirectory()) {
                    target.mkdir();
                } else {
                    inputStream = unzipper.getInputStream(ze);
                    FileUtils.writeStreamToFile(inputStream, target);
                    inputStream.close();
                }
            }
        } finally {
            if (unzipper != null) {
                unzipper.close();
            }
            if (inputStream != null) {
                inputStream.close();
            }
        }
    } catch (IOException e) {
        throw new IOFailure("Failed to unzip '" + zipFile + "'", e);
    }
}

From source file:com.googlecode.osde.internal.profiling.Profile.java

/**
 * Unzips a zip content into a physical folder.
 *
 * @return The newly-created folder with the unzipped content.
 *//*from  w  ww.j  a  va2  s .  com*/
private static File unzip(InputStream resource, File output) throws IOException {
    // Allocate a 16K buffer to read the XPI file faster.
    ZipInputStream zipStream = new ZipInputStream(new BufferedInputStream(resource, 16 * 1024));
    ZipEntry entry = zipStream.getNextEntry();
    while (entry != null) {
        final File target = new File(output, entry.getName());
        if (entry.isDirectory()) {
            forceMkdir(target);
        } else {
            unzipFile(target, zipStream);
        }
        entry = zipStream.getNextEntry();
    }

    return output;
}

From source file:de.suse.swamp.util.FileUtils.java

/**
 * Decompress the provided Stream to "targetPath"
 *//*  w w w. j a  v  a 2 s .c  o m*/
public static void uncompress(InputStream inputFile, String targetPath) throws Exception {
    ZipInputStream zin = new ZipInputStream(new BufferedInputStream(inputFile));
    BufferedOutputStream dest = null;
    ZipEntry entry;
    while ((entry = zin.getNextEntry()) != null) {
        int count;
        byte data[] = new byte[2048];
        // write the files to the disk
        if (entry.isDirectory()) {
            org.apache.commons.io.FileUtils.forceMkdir(new File(targetPath + "/" + entry.getName()));
        } else {
            FileOutputStream fos = new FileOutputStream(targetPath + "/" + entry.getName());
            dest = new BufferedOutputStream(fos, 2048);
            while ((count = zin.read(data, 0, 2048)) != -1) {
                dest.write(data, 0, count);
            }
            dest.flush();
            dest.close();
        }
    }
}

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

/**
 * Get all of the directory paths in a zip/jar file
 * @param pathToJarFile location of the jarfile. can also be a zipfile
 * @return set of directory paths relative to the root of the jar
 *//*from www.j ava2 s . c o m*/
public static Set<Path> getDirectoriesFromJar(Path pathToJarFile) throws IOException {
    Set<Path> result = new HashSet<Path>();
    ZipFile jarfile = new ZipFile(pathToJarFile.toFile());
    try {
        final Enumeration<? extends ZipEntry> entries = jarfile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (entry.isDirectory()) {
                result.add(Paths.get(entry.getName()));
            }
        }
        jarfile.close();
    } finally {
        IOUtils.closeQuietly(jarfile);
    }
    return result;
}

From source file:com.sangupta.jerry.util.ZipUtils.java

/**
 * Extract the given ZIP file into the given destination folder.
 * // www  .j  av a  2  s .  com
 * @param zipFile
 *            file to extract
 *            
 * @param baseFolder
 *            destination folder to extract in
 */
public static void extractZipToFolder(File zipFile, File baseFolder) {
    try {
        byte[] buf = new byte[1024];

        ZipInputStream zipinputstream = new ZipInputStream(new FileInputStream(zipFile));
        ZipEntry zipentry = zipinputstream.getNextEntry();

        while (zipentry != null) {
            // for each entry to be extracted
            String entryName = zipentry.getName();

            entryName = entryName.replace('/', File.separatorChar);
            entryName = entryName.replace('\\', File.separatorChar);

            int numBytes;
            FileOutputStream fileoutputstream;
            File newFile = new File(baseFolder, entryName);
            if (zipentry.isDirectory()) {
                if (!newFile.mkdirs()) {
                    break;
                }
                zipentry = zipinputstream.getNextEntry();
                continue;
            }

            fileoutputstream = new FileOutputStream(newFile);
            while ((numBytes = zipinputstream.read(buf, 0, 1024)) > -1) {
                fileoutputstream.write(buf, 0, numBytes);
            }

            fileoutputstream.close();
            zipinputstream.closeEntry();
            zipentry = zipinputstream.getNextEntry();
        }

        zipinputstream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.jsonstore.util.JSONStoreUtil.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);
        }// w w  w .j  av  a  2  s  . c  o  m
        File extractFile = new File(targetDir.getPath() + File.separator + extractFilePath);
        if (entry.isDirectory()) {
            if (!extractFile.exists()) {
                extractFile.mkdirs();
            }
            continue;
        } else {
            File parent = extractFile.getParentFile();
            if (!parent.exists()) {
                parent.mkdirs();
            }
        }

        // if not directory instead of the previous check and continue
        OutputStream os = new BufferedOutputStream(new FileOutputStream(extractFile));
        copyFile(zin, os);
        os.flush();
        os.close();
    }
}

From source file:net.ftb.util.FileUtils.java

/**
 * Extracts given zip to given location/*from  ww  w . j a  va  2s  .c o m*/
 * @param zipLocation - the location of the zip to be extracted
 * @param outputLocation - location to extract to
 */
public static void extractZipTo(String zipLocation, String outputLocation) {
    ZipInputStream zipinputstream = null;
    try {
        byte[] buf = new byte[1024];
        zipinputstream = new ZipInputStream(new FileInputStream(zipLocation));
        ZipEntry zipentry = zipinputstream.getNextEntry();
        while (zipentry != null) {
            String entryName = zipentry.getName();
            int n;
            if (!zipentry.isDirectory() && !entryName.equalsIgnoreCase("minecraft")
                    && !entryName.equalsIgnoreCase(".minecraft") && !entryName.equalsIgnoreCase("instMods")) {
                new File(outputLocation + File.separator + entryName).getParentFile().mkdirs();
                FileOutputStream fileoutputstream = new FileOutputStream(
                        outputLocation + File.separator + entryName);
                while ((n = zipinputstream.read(buf, 0, 1024)) > -1) {
                    fileoutputstream.write(buf, 0, n);
                }
                fileoutputstream.close();
            }
            zipinputstream.closeEntry();
            zipentry = zipinputstream.getNextEntry();
        }
    } catch (Exception e) {
        Logger.logError("Error while extracting zip", e);
        backupExtract(zipLocation, outputLocation);
    } finally {
        try {
            zipinputstream.close();
        } catch (IOException e) {
        }
    }
}

From source file:com.predic8.membrane.examples.DistributionExtractingTestcase.java

public static final void unzip(File zip, File target) throws IOException {
    ZipFile zipFile = new ZipFile(zip);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        if (entry.isDirectory()) {
            // Assume directories are stored parents first then children.
            // This is not robust, just for demonstration purposes.
            new File(target, entry.getName()).mkdir();
        } else {/*from   w  ww  .  ja  va 2  s . c om*/
            FileOutputStream fos = new FileOutputStream(new File(target, entry.getName()));
            try {
                copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream(fos));
            } finally {
                fos.close();
            }
        }
    }
    zipFile.close();
}