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

/**
 * Reads a Zip file, iterating through each entry and dumping the contents
 * to the console./* w w  w. ja v  a2  s.c o m*/
 */
public static void readZipFile(String fileName) {
    ZipFile zipFile = null;
    try {
        // ZipFile offers an Enumeration of all the files in the Zip file
        zipFile = new ZipFile(fileName);
        for (Enumeration e = zipFile.entries(); e.hasMoreElements();) {
            ZipEntry zipEntry = (ZipEntry) e.nextElement();
            System.out.println(zipEntry.getName() + " contains:");
            // use BufferedReader to get one line at a time
            BufferedReader zipReader = new BufferedReader(
                    new InputStreamReader(zipFile.getInputStream(zipEntry)));
            while (zipReader.ready()) {
                System.out.println(zipReader.readLine());
            }
            zipReader.close();
        }
    } catch (IOException ioe) {
        System.out.println("An IOException occurred: " + ioe.getMessage());
    } finally {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (IOException ioe) {
            }
        }
    }
}

From source file:com.microsoft.azure.hdinsight.util.HDInsightJobViewUtils.java

public static void unzip(String zipFilePath, String destDirectory) throws IOException {
    File destDir = new File(destDirectory);
    if (!destDir.exists()) {
        destDir.mkdir();//from   www. j a  va 2  s.c o m
    }
    ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
    ZipEntry entry = zipIn.getNextEntry();
    while (entry != null) {
        String filePath = destDirectory + File.separator + entry.getName();
        if (!entry.isDirectory()) {
            extractFile(zipIn, filePath);
        } else {
            File dir = new File(filePath);
            dir.mkdir();
        }
        zipIn.closeEntry();
        entry = zipIn.getNextEntry();
    }
    zipIn.close();
}

From source file:Main.java

/***
 * Extract zipfile to outdir with complete directory structure
 * @param zipfile Input .zip file/*from w w w.ja  va  2  s.  c  o  m*/
 * @param outdir Output directory
 */
public static void extract(InputStream zipfile, File outdir) {
    try {
        ZipInputStream zin = new ZipInputStream(zipfile);
        ZipEntry entry;
        String name, dir;
        Log.i("OF", "uncompressinggggg ");
        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 e) {
        e.printStackTrace();
    }
}

From source file:com.freedomotic.util.Unzip.java

/**
 *
 * @param zipFile//from  w w  w.  jav  a  2s. co  m
 * @throws ZipException
 * @throws IOException
 */
public static void unzip(String zipFile) throws IOException {

    if (StringUtils.isEmpty(zipFile)) {
        LOG.error("File path not provided, no unzipping performed");
        return;
    }

    File file = new File(zipFile);

    if (!file.exists()) {
        LOG.error("File not existing, no unzipping performed");
        return;
    }

    try (ZipFile zip = new ZipFile(file);) {
        String newPath = zipFile.substring(0, zipFile.length() - 4);
        //simulates the unzip here feature
        newPath = newPath.substring(0, newPath.lastIndexOf(File.separator));

        Enumeration<? extends ZipEntry> zipFileEntries = zip.entries();

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

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

            if (!entry.isDirectory()) {
                try (BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry));
                        FileOutputStream fos = new FileOutputStream(destFile);
                        BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);) {

                    int currentByte;

                    // establish buffer for writing file
                    byte[] data = new byte[BUFFER];

                    // read and write until last byte is encountered
                    while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                        dest.write(data, 0, currentByte);
                    }
                } catch (IOException ex) {
                    LOG.error(Freedomotic.getStackTraceInfo(ex));
                }
            }

            if (currentEntry.endsWith(".zip")) {
                // found a zip file, try to open
                unzip(destFile.getAbsolutePath());
            }
        }
    }
}

From source file:ZipFileIO.java

/**
 * Return the first directory of this archive. This is needed to determine
 * the plugin directory./*  w w  w  .j  av a  2  s .  c  om*/
 * 
 * @param zipFile
 * @return <class>File</class> containing the first entry of this archive
 */
public static File getFirstFile(File zipFile) throws IOException {
    ZipInputStream in = null;
    try {
        // Open the ZIP file
        in = new ZipInputStream(new FileInputStream(zipFile));

        // Get the first entry
        ZipEntry entry = null;

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

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

From source file:com.ibm.util.merge.CompareArchives.java

/**
 * @param archive/*from w  w  w.  ja  va2 s .com*/
 * @return
 * @throws IOException
 */
private static final HashMap<String, ZipEntry> getMembers(ZipFile archive) throws IOException {
    HashMap<String, ZipEntry> map = new HashMap<>();
    @SuppressWarnings("unchecked")
    Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) archive.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        map.put(entry.getName(), entry);
    }
    return map;
}

From source file:com.opengamma.util.ZipUtils.java

/**
 * Unzips a ZIP archive./*from  w ww.jav a2 s  .  c o m*/
 * 
 * @param zipFile  the archive file, not null
 * @param outputDir  the output directory, not null
 */
public static void unzipArchive(final ZipFile zipFile, final File outputDir) {
    ArgumentChecker.notNull(zipFile, "zipFile");
    ArgumentChecker.notNull(outputDir, "outputDir");

    try {
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (entry.isDirectory()) {
                FileUtils.forceMkdir(new File(outputDir, entry.getName()));
                continue;
            }
            File entryDestination = new File(outputDir, entry.getName());
            entryDestination.getParentFile().mkdirs();
            InputStream in = zipFile.getInputStream(entry);
            OutputStream out = new FileOutputStream(entryDestination);
            IOUtils.copy(in, out);
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
        }
        zipFile.close();
    } catch (IOException ex) {
        throw new OpenGammaRuntimeException(
                "Error while extracting file: " + zipFile.getName() + " to: " + outputDir, ex);
    }
}

From source file:Main.java

public static String getJarSignature(String packagePath) throws IOException {
    Pattern signatureFilePattern = Pattern.compile("META-INF/[A-Z]+\\.SF");

    ZipFile packageZip = null;//  ww  w  .  ja v  a 2  s .  c  om
    try {
        packageZip = new ZipFile(packagePath);
        // For each file in the zip.
        for (ZipEntry entry : Collections.list(packageZip.entries())) {
            // Ignore non-signature files.
            if (!signatureFilePattern.matcher(entry.getName()).matches()) {
                continue;
            }

            BufferedReader sigContents = null;
            try {
                sigContents = new BufferedReader(new InputStreamReader(packageZip.getInputStream(entry)));
                // For each line in the signature file.
                while (true) {
                    String line = sigContents.readLine();
                    if (line == null || line.equals("")) {
                        throw new IllegalArgumentException(
                                "Failed to find manifest digest in " + entry.getName());
                    }
                    String prefix = "SHA1-Digest-Manifest: ";
                    if (line.startsWith(prefix)) {
                        return line.substring(prefix.length());
                    }
                }
            } finally {
                if (sigContents != null) {
                    sigContents.close();
                }
            }
        }
    } finally {
        if (packageZip != null) {
            packageZip.close();
        }
    }

    throw new IllegalArgumentException("Failed to find signature file.");
}

From source file:Main.java

public static void unzip(InputStream fin, String targetPath, Context context) throws IOException {
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fin));
    try {/*w  w  w .  ja va  2s .c o m*/
        ZipEntry zipEntry;
        int count;
        byte[] buffer = new byte[8192];
        while ((zipEntry = zis.getNextEntry()) != null) {
            File file = new File(targetPath, zipEntry.getName());
            File dir = zipEntry.isDirectory() ? file : file.getParentFile();
            if (!dir.isDirectory() && !dir.mkdirs()) {
                throw new FileNotFoundException("Failed to get directory: " + dir.getAbsolutePath());
            }
            if (zipEntry.isDirectory()) {
                continue;
            }
            FileOutputStream fout = new FileOutputStream(file);
            try {
                while ((count = zis.read(buffer)) != -1)
                    fout.write(buffer, 0, count);
            } finally {
                fout.close();
            }
            Log.d("TEST", "Unzipped " + file.getAbsolutePath());
        }
    } finally {
        zis.close();
    }
}

From source file:Main.java

/**
 * Decompresses a given byte array that is a compressed folder.
 * /*from   w  ww .  j a v  a2  s . c o  m*/
 * @param folderAsCompressedArray to decompress
 * @param unzippedLocation where the decompressed folder should be
 * @throws IOException e
 * @throws FileNotFoundException e
 */
public static void decompressFolderByteArray(byte[] folderAsCompressedArray, File unzippedLocation)
        throws IOException, FileNotFoundException {
    ZipInputStream zipFile = new ZipInputStream(new ByteArrayInputStream(folderAsCompressedArray));
    ZipEntry ze = null;
    final int minusOne = -1;
    while ((ze = zipFile.getNextEntry()) != null) {
        FileOutputStream fout = new FileOutputStream(
                new File(unzippedLocation, ze.getName()).getAbsolutePath());
        for (int c = zipFile.read(); c != minusOne; c = zipFile.read()) {
            fout.write(c);
        }
        zipFile.closeEntry();
        fout.close();
    }
    zipFile.close();
}