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:edu.clemson.cs.nestbed.common.util.ZipUtils.java

public static File unzip(byte[] zipData, File directory) throws IOException {
    ByteArrayInputStream bais = new ByteArrayInputStream(zipData);
    ZipInputStream zis = new ZipInputStream(bais);
    ZipEntry entry = zis.getNextEntry();
    File root = null;//www.  j a va2  s. c o m

    while (entry != null) {
        if (entry.isDirectory()) {
            File f = new File(directory, entry.getName());
            f.mkdir();

            if (root == null) {
                root = f;
            }
        } else {
            BufferedOutputStream out;
            out = new BufferedOutputStream(new FileOutputStream(new File(directory, entry.toString())),
                    BUFFER_SIZE);

            // ZipInputStream can only give us one byte at a time...
            for (int data = zis.read(); data != -1; data = zis.read()) {
                out.write(data);
            }
            out.close();
        }

        zis.closeEntry();
        entry = zis.getNextEntry();
    }
    zis.close();

    return root;
}

From source file:io.github.jeddict.jcode.parser.ejs.EJSUtil.java

public static Map<String, String> getResource(String inputResource) {
    Map<String, String> data = new HashMap<>();
    InputStream inputStream = loadResource(inputResource);
    try (ZipInputStream zipInputStream = new ZipInputStream(inputStream)) {
        ZipEntry entry;
        while ((entry = zipInputStream.getNextEntry()) != null) {
            if (entry.getName().lastIndexOf('.') == -1) {
                continue;
            }/*ww w  . j  ava2 s .c  o  m*/
            StringWriter writer = new StringWriter();
            IOUtils.copy(zipInputStream, writer, StandardCharsets.UTF_8.name());
            String fileName = entry.getName();
            fileName = fileName.substring(fileName.lastIndexOf('/') + 1, fileName.lastIndexOf('.'));
            data.put(fileName, writer.toString());
            zipInputStream.closeEntry();
        }
    } catch (Exception ex) {
        Exceptions.printStackTrace(ex);
        System.out.println("InputResource : " + inputResource);
    }
    return data;
}

From source file:com.termmed.utils.ResourceUtils.java

/**
 * Gets the resource scripts.//from w ww  . j a  va 2 s.  c o m
 *
 * @param path the path
 * @return the resource scripts
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static Collection<String> getResourceScripts(String path) throws IOException {
    System.out.println("getting files from " + path);
    List<String> strFiles = new ArrayList<String>();

    //      InputStream is=ResourceUtils.class.getResourceAsStream("/" + path);
    //      InputStreamReader risr = new InputStreamReader(is);
    //      BufferedReader br=new BufferedReader(risr);
    //      String line;
    //      while((line=br.readLine())!=null){
    //         strFiles.add(line);
    //      }

    String file = null;
    if (new File("stats-build.jar").exists()) {
        file = "stats-build.jar";
    } else {
        file = "target/stats-build.jar";
    }
    ZipInputStream zip = new ZipInputStream(new FileInputStream(file));
    while (true) {
        ZipEntry e = zip.getNextEntry();
        if (e == null)
            break;
        String name = e.getName();
        if (name.startsWith(path) && !name.endsWith("/")) {
            //            if (!name.startsWith("/")){
            //               name="/" + name;
            //            }
            strFiles.add(name);
        }
    }

    System.out.println("files:" + strFiles.toString());
    return strFiles;
}

From source file:net.sf.jabref.importer.ZipFileChooser.java

/**
 * Entries that can be selected with this dialog.
 *
 * @param zipFile  Zip-File//from  w ww.  ja va  2  s . co m
 * @return  entries that can be selected
 */
private static ZipEntry[] getSelectableZipEntries(ZipFile zipFile) {
    List<ZipEntry> entries = new ArrayList<>();
    Enumeration<? extends ZipEntry> e = zipFile.entries();
    for (ZipEntry entry : Collections.list(e)) {
        if (!entry.isDirectory() && entry.getName().endsWith(".class")) {
            entries.add(entry);
        }
    }
    return entries.toArray(new ZipEntry[entries.size()]);
}

From source file:android.databinding.tool.util.GenerationalClassUtil.java

private static void loadFomZipFile(File file) throws IOException {
    ZipFile zipFile = new ZipFile(file);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        for (ExtensionFilter filter : ExtensionFilter.values()) {
            if (!filter.accept(entry.getName())) {
                continue;
            }/*from  w w w .  ja v  a2s  . com*/
            InputStream inputStream = null;
            try {
                inputStream = zipFile.getInputStream(entry);
                Serializable item = fromInputStream(inputStream);
                L.d("loaded item %s from zip file", item);
                if (item != null) {
                    //noinspection unchecked
                    sCache[filter.ordinal()].add(item);
                }
            } catch (IOException e) {
                L.e(e, "Could not merge in Bindables from %s", file.getAbsolutePath());
            } catch (ClassNotFoundException e) {
                L.e(e, "Could not read Binding properties intermediate file. %s", file.getAbsolutePath());
            } finally {
                IOUtils.closeQuietly(inputStream);
            }
        }
    }
}

From source file:com.alibaba.antx.util.ZipUtil.java

/**
 * //from  w w  w.  j a  v  a 2s  .com
 *
 * @param todir     
 * @param zipStream ?
 * @param zipEntry  zip
 * @param overwrite ?
 * @throws IOException Zip?
 */
protected static void extractFile(File todir, InputStream zipStream, ZipEntry zipEntry, boolean overwrite)
        throws IOException {
    String entryName = zipEntry.getName();
    Date entryDate = new Date(zipEntry.getTime());
    boolean isDirectory = zipEntry.isDirectory();
    File targetFile = FileUtil.getFile(todir, entryName);

    if (!overwrite && targetFile.exists() && targetFile.lastModified() >= entryDate.getTime()) {
        log.debug("Skipping " + targetFile + " as it is up-to-date");
        return;
    }

    log.info("expanding " + entryName + " to " + targetFile);

    if (isDirectory) {
        targetFile.mkdirs();
    } else {
        File dir = targetFile.getParentFile();

        dir.mkdirs();

        byte[] buffer = new byte[8192];
        int length = 0;
        OutputStream ostream = null;

        try {
            ostream = new BufferedOutputStream(new FileOutputStream(targetFile), 8192);

            while ((length = zipStream.read(buffer)) >= 0) {
                ostream.write(buffer, 0, length);
            }
        } finally {
            if (ostream != null) {
                try {
                    ostream.close();
                } catch (IOException e) {
                }
            }
        }
    }

    targetFile.setLastModified(entryDate.getTime());
}

From source file:Main.java

/** Returns an InputStream that will read a specific entry
 * from a zip file./*  w w w. ja v  a  2s  . c  o m*/
 * @param file the zip file.
 * @param entryName the name of the entry.
 * @return an InputStream that reads that entry.
 * @throws IOException
 */
public static InputStream getZipEntry(File file, String entryName) throws IOException {
    FileInputStream in = new FileInputStream(file);
    ZipInputStream zipIn = new ZipInputStream(in);
    ZipEntry e = zipIn.getNextEntry();
    while (e != null) {
        if (e.getName().equals(entryName))
            return zipIn;
        e = zipIn.getNextEntry();
    }
    return null;
}

From source file:be.i8c.sag.util.FileUtils.java

/**
 * Extract files from a zipped (and jar) file
 *
 * @param internalDir The directory you want to copy
 * @param zipFile The file that contains the content you want to copy
 * @param to The directory you want to copy to
 * @param deleteOnExit If true, delete the files once the application has closed.
 * @throws IOException When failed to write to a file
 * @throws FileNotFoundException If a file could not be found
 *//*w  ww  . j av a2  s .  c  o  m*/
public static void extractDirectory(String internalDir, File zipFile, File to, boolean deleteOnExit)
        throws IOException {
    try (ZipInputStream zip = new ZipInputStream(new FileInputStream(zipFile))) {
        while (true) {
            ZipEntry zipEntry = zip.getNextEntry();
            if (zipEntry == null)
                break;

            if (zipEntry.getName().startsWith(internalDir) && !zipEntry.isDirectory()) {
                File f = createFile(new File(to, zipEntry.getName()
                        .replace((internalDir.endsWith("/") ? internalDir : internalDir + "/"), "")));
                if (deleteOnExit)
                    f.deleteOnExit();
                OutputStream bos = new FileOutputStream(f);
                try {
                    IOUtils.copy(zip, bos);
                } finally {
                    bos.flush();
                    bos.close();
                }
            }

            zip.closeEntry();
        }
    }
}

From source file:de.thischwa.pmcms.tool.compression.Zip.java

/**
 * Method to extract all {@link ZipInfo}s into 'destDir'. Inner directory structure will be copied.
 * // ww w.  j ava2s .  c  o m
 * @param destDir
 * @param zipInfo
 * @param monitor must be initialized by the caller.
 * @throws IOException
 */
public static void extract(final File destDir, final ZipInfo zipInfo, final IProgressMonitor monitor)
        throws IOException {
    if (!destDir.exists())
        destDir.mkdirs();

    for (String key : zipInfo.getEntryKeys()) {
        ZipEntry entry = zipInfo.getEntry(key);
        InputStream in = zipInfo.getInputStream(entry);
        File entryDest = new File(destDir, entry.getName());
        entryDest.getParentFile().mkdirs();
        if (!entry.isDirectory()) {
            OutputStream out = new FileOutputStream(new File(destDir, entry.getName()));
            try {
                IOUtils.copy(in, out);
                out.flush();
                if (monitor != null)
                    monitor.worked(1);
            } finally { // cleanup
                IOUtils.closeQuietly(in);
                IOUtils.closeQuietly(out);
            }
        }
    }
    if (monitor != null)
        monitor.done();
}

From source file:edu.harvard.i2b2.eclipse.plugins.metadataLoader.util.FileUtil.java

public static void unzip(String zipname) throws IOException {

    FileInputStream fis = new FileInputStream(zipname);
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
    //??    GZIPInputStream gzis = new GZIPInputStream(new BufferedInputStream(fis));

    // get directory of the zip file
    if (zipname.contains("\\"))
        zipname = zipname.replace("\\", "/");
    //   String zipDirectory = zipname.substring(0, zipname.lastIndexOf("/")+1) ;
    String zipDirectory = zipname.substring(0, zipname.lastIndexOf("."));
    new File(zipDirectory).mkdir();

    RunData.getInstance().setMetadataDirectory(zipDirectory);

    ZipEntry entry;
    while ((entry = zis.getNextEntry()) != null) {
        System.out.println("Unzipping: " + entry.getName());
        if (entry.getName().contains("metadata")) {
            RunData.getInstance().setMetadataFile(zipDirectory + "/" + entry.getName());
        } else if (entry.getName().contains("schemes")) {
            RunData.getInstance().setSchemesFile(zipDirectory + "/" + entry.getName());
        } else if (entry.getName().contains("access")) {
            RunData.getInstance().setTableAccessFile(zipDirectory + "/" + entry.getName());
        }/*ww  w. j  a  va 2  s .c o m*/

        int size;
        byte[] buffer = new byte[2048];

        FileOutputStream fos = new FileOutputStream(zipDirectory + "/" + entry.getName());
        BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length);

        while ((size = zis.read(buffer, 0, buffer.length)) != -1) {
            bos.write(buffer, 0, size);
        }
        bos.flush();
        bos.close();
    }
    zis.close();
    fis.close();
}