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

/**
 * Reads the in_stream and extracts them to out_dir.
 * @param in_stream Input stream corresponding to the zip file.
 * @param out_dir Output directory for the zip file contents.
 * @throws IOException//from   w  ww.java2 s.  c  o m
 */
public static void zipExtract(InputStream in_stream, File out_dir) throws IOException {
    if (!out_dir.exists()) {
        if (!out_dir.mkdirs()) {
            throw new IOException("Could not create output directory: " + out_dir.getAbsolutePath());
        }
    }
    ZipInputStream zis = new ZipInputStream(in_stream);
    ZipEntry ze;
    byte[] buffer = new byte[BUFFER_SIZE];
    int count;
    while ((ze = zis.getNextEntry()) != null) {
        if (ze.isDirectory()) {
            File fmd = new File(out_dir.getAbsolutePath() + "/" + ze.getName());
            fmd.mkdirs();
            continue;
        }
        FileOutputStream fout = new FileOutputStream(out_dir.getAbsolutePath() + "/" + ze.getName());
        while ((count = zis.read(buffer)) != -1) {
            fout.write(buffer, 0, count);
        }

        fout.close();
        zis.closeEntry();
    }
    zis.close();
}

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

private static void unzipEntries(File folder, ZipInputStream zis, int itemCount, long time,
        ZipListener listener) throws IOException, FileNotFoundException {
    ZipEntry ze = null;

    int item = 0;

    while ((ze = zis.getNextEntry()) != null) {
        item++;/*from  w w  w . j  a  v a2s  . c  o  m*/

        String fileName = ze.getName();
        File newFile = new File(folder, fileName);

        LOG.debug("unzip: " + newFile.getAbsoluteFile());

        if (ze.isDirectory()) {
            if (!newFile.mkdirs()) {
                break;
            }
            continue;
        }

        if (listener != null) {
            int progress = (item == itemCount) ? 100 : (int) (((double) (item * 100)) / (double) (itemCount));
            listener.onUnzipping(fileName, progress);
        }

        FileOutputStream fos = new FileOutputStream(newFile);

        try {
            int n;
            byte[] buffer = new byte[1024];
            while ((n = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, n);

                if (listener != null && listener.isCanceled()) { // not the best way
                    throw new IOException("Uncompress operation cancelled");
                }
            }
        } finally {
            fos.close();
            zis.closeEntry();
        }

        newFile.setLastModified(time);
    }
}

From source file:com.dv.util.DataViewerZipUtil.java

public static void doUnzipFile(ZipFile zipFile, File dest) throws IOException {
    if (!dest.exists()) {
        FileUtils.forceMkdir(dest);//from   ww w  . j av a  2 s . c  om
    }
    Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        File file = new File(dest, entry.getName());
        if (entry.getName().endsWith(File.separator)) {
            FileUtils.forceMkdir(file);
        } else {
            OutputStream out = FileUtils.openOutputStream(file);
            InputStream in = zipFile.getInputStream(entry);
            try {
                IOUtils.copy(in, out);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                IOUtils.closeQuietly(out);
            }
        }
    }
    zipFile.close();
}

From source file:com.isomorphic.maven.util.ArchiveUtils.java

/**
 * Unzips the <code>source</code> file to the <code>target</code> directory.
 * /*from w  w  w  .  j a va  2 s  .  c o m*/
 * @param source The file to be unzipped
 * @param target The directory to which the source file should be unzipped
 * @throws IOException
 */
public static void unzip(File source, File target) throws IOException {

    ZipFile zip = new ZipFile(source);
    Enumeration<? extends ZipEntry> entries = zip.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        File file = new File(target, entry.getName());
        FileUtils.copyInputStreamToFile(zip.getInputStream(entry), file);
    }
}

From source file:com.yunrang.hadoop.app.utils.CustomizedUtil.java

/**
 * Add entries to <code>packagedClasses</code> corresponding to class files
 * contained in <code>jar</code>.
 * //  w  ww.j  a  v  a 2 s  .  co  m
 * @param jar
 *            The jar who's content to list.
 * @param packagedClasses
 *            map[class -> jar]
 */
public static void updateMap(String jar, Map<String, String> packagedClasses) throws IOException {
    if (null == jar || jar.isEmpty()) {
        return;
    }
    ZipFile zip = null;
    try {
        zip = new ZipFile(jar);
        for (Enumeration<? extends ZipEntry> iter = zip.entries(); iter.hasMoreElements();) {
            ZipEntry entry = iter.nextElement();
            if (entry.getName().endsWith("class")) {
                packagedClasses.put(entry.getName(), jar);
            }
        }
    } finally {
        if (null != zip)
            zip.close();
    }
}

From source file:org.cloudfoundry.tools.io.zip.ZipArchive.java

/**
 * Unzip the specified input stream into a folder.
 * /*from   ww  w .  j ava  2s .c  om*/
 * @param inputStream the input stream to unzip (this must contain zip contents)
 * @param destination the destination folder
 * @see #unpack(File, Folder)
 */
public static void unpack(InputStream inputStream, Folder destination) {
    Assert.notNull(inputStream, "InputStream must not be null");
    Assert.notNull(destination, "Destination must not be null");
    destination.createIfMissing();
    ZipInputStream zip = new ZipInputStream(new BufferedInputStream(inputStream));
    try {
        InputStream noCloseZip = new NoCloseInputStream(zip);
        ZipEntry entry = zip.getNextEntry();
        while (entry != null) {
            if (entry.isDirectory()) {
                destination.getFolder(entry.getName()).createIfMissing();
            } else {
                destination.getFile(entry.getName()).getContent().write(noCloseZip);
            }
            entry = zip.getNextEntry();
        }
    } catch (IOException e) {
        throw new ResourceException(e);
    } finally {
        try {
            zip.close();
        } catch (IOException e) {
        }
    }
}

From source file:com.BibleQuote.utils.FsUtils.java

public static BufferedReader getTextFileReaderFromZipArchive(String archivePath, String textFileInArchive,
        String textFileEncoding) throws FileAccessException {
    File zipFile = new File(archivePath);
    try {/*  w ww .  j a  va  2  s  .  com*/
        InputStream moduleStream = new FileInputStream(zipFile);
        ZipInputStream zStream = new ZipInputStream(moduleStream);
        ZipEntry entry;
        while ((entry = zStream.getNextEntry()) != null) {
            String entryName = entry.getName().toLowerCase();
            if (entryName.contains(File.separator)) {
                entryName = entryName.substring(entryName.lastIndexOf(File.separator) + 1);
            }
            String fileName = textFileInArchive.toLowerCase();
            if (entryName.equals(fileName)) {
                InputStreamReader iReader = new InputStreamReader(zStream, textFileEncoding);
                return new BufferedReader(iReader);
            }
            ;
        }
        String message = String.format("File %1$s in zip-arhive %2$s not found", textFileInArchive,
                archivePath);
        Log.e(TAG, message);
        throw new FileAccessException(message);
    } catch (UTFDataFormatException e) {
        String message = String.format("Archive %1$s contains the file names not in the UTF format",
                zipFile.getName());
        Log.e(TAG, message);
        throw new FileAccessException(message);
    } catch (FileNotFoundException e) {
        String message = String.format("File %1$s in zip-arhive %2$s not found", textFileInArchive,
                archivePath);
        throw new FileAccessException(message);
    } catch (IOException e) {
        Log.e(TAG, String.format("getTextFileReaderFromZipArchive(%1$s, %2$s, %3$s)", archivePath,
                textFileInArchive, textFileEncoding), e);
        throw new FileAccessException(e);
    }
}

From source file:de.uni_hildesheim.sse.ant.versionReplacement.PluginVersionReplacer.java

/**
 * Unpacks an existing JAR/Zip archive.//  w w w  .  ja va 2 s  .c o  m
 * 
 * @param zipFile The Archive file to unzip.
 * @param outputFolder The destination folder where the unzipped content shall be saved.
 * @see <a href="http://www.mkyong.com/java/how-to-decompress-files-from-a-zip-file/">
 * http://www.mkyong.com/java/how-to-decompress-files-from-a-zip-file/</a>
 */
private static void unZipIt(File zipFile, File outputFolder) {

    byte[] buffer = new byte[1024];

    try {

        // create output directory is not exists
        File folder = outputFolder;
        if (folder.exists()) {
            FileUtils.deleteDirectory(folder);
        }
        folder.mkdir();

        // get the zip file content
        ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
        // get the zipped file list entry
        ZipEntry ze = zis.getNextEntry();

        while (ze != null) {

            String fileName = ze.getName();
            File newFile = new File(outputFolder, fileName);

            // create all non exists folders
            // else you will hit FileNotFoundException for compressed folder
            new File(newFile.getParent()).mkdirs();

            if (!ze.isDirectory()) {
                FileOutputStream fos = new FileOutputStream(newFile);

                int len;
                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }

                fos.close();
            }
            ze = zis.getNextEntry();
        }

        zis.closeEntry();
        zis.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static int upZipFile(File zipFile, String folderPath) throws IOException {
    ZipFile zfile = new ZipFile(zipFile);
    Enumeration zList = zfile.entries();
    ZipEntry ze = null;
    byte[] buf = new byte[1024];
    while (zList.hasMoreElements()) {
        ze = (ZipEntry) zList.nextElement();
        if (ze.isDirectory()) {
            String dirstr = folderPath + ze.getName();
            dirstr = new String(dirstr.getBytes("8859_1"), "GB2312");
            File f = new File(dirstr);
            f.mkdirs();//from   w  w  w  .  j  av a2s.c  o  m
            continue;
        }
        OutputStream os = new BufferedOutputStream(
                new FileOutputStream(getRealFileName(folderPath, ze.getName())));
        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();
    }
    zfile.close();
    return 0;
}

From source file:com.vmware.admiral.closures.util.ClosureUtils.java

private static void buildTarData(URL dirURL, String folderNameFilter, OutputStream outputStream)
        throws IOException {
    final JarURLConnection jarConnection = (JarURLConnection) dirURL.openConnection();
    final ZipFile jar = jarConnection.getJarFile();
    final Enumeration<? extends ZipEntry> entries = jar.entries();

    try (TarArchiveOutputStream tarArchiveOutputStream = buildTarStream(outputStream)) {
        while (entries.hasMoreElements()) {
            final ZipEntry entry = entries.nextElement();
            final String name = entry.getName();
            if (!name.startsWith(folderNameFilter)) {
                // entry in wrong subdir -- don't copy
                continue;
            }/*from w ww .  jav a2s  . c  om*/
            TarArchiveEntry tarEntry = new TarArchiveEntry(entry.getName().replaceAll(folderNameFilter, ""));
            try (InputStream is = jar.getInputStream(entry)) {
                putTarEntry(tarArchiveOutputStream, tarEntry, is, entry.getSize());
            }
        }

        tarArchiveOutputStream.flush();
        tarArchiveOutputStream.close();
    }
}