Example usage for java.util.jar JarEntry getSize

List of usage examples for java.util.jar JarEntry getSize

Introduction

In this page you can find the example usage for java.util.jar JarEntry getSize.

Prototype

public long getSize() 

Source Link

Document

Returns the uncompressed size of the entry data.

Usage

From source file:org.jdesktop.wonderland.modules.service.PendingManager.java

/**
 * Takes a base directory (which must exist and be readable) and expands
 * the contents of the archive module into that directory given the
 * URL of the module encoded as a jar file
 * /* ww  w.java 2 s .  c  o  m*/
 * @param root The base directory in which the module is expanded
 * @throw IOException Upon error
 */
private void expand(File root, File jar) throws IOException {
    /*
     * Loop through each entry, fetch its input stream, and write to an
     * output stream for the file.
     */
    JarFile jarFile = new JarFile(jar);
    Enumeration<JarEntry> entries = jarFile.entries();
    while (entries.hasMoreElements() == true) {
        /* Fetch the next entry, its name is the relative file name */
        JarEntry entry = entries.nextElement();
        String entryName = entry.getName();
        long size = entry.getSize();

        /* Don't expand anything that beings with META-INF */
        if (entryName.startsWith("META-INF") == true) {
            continue;
        }

        /* Ignore if it is a directory, then create it */
        if (entryName.endsWith("/") == true) {
            File file = new File(root, entryName);
            file.mkdirs();
            continue;
        }

        /* Write out to a file in 'root' */
        File file = new File(root, entryName);
        InputStream jis = jarFile.getInputStream(entry);
        FileOutputStream os = new FileOutputStream(file);
        byte[] b = new byte[PendingManager.CHUNK_SIZE];
        long read = 0;
        while (read < size) {
            int len = jis.read(b);
            if (len == -1) {
                break;
            }
            read += len;
            os.write(b, 0, len);
        }
        jis.close();
        os.close();
    }
}

From source file:org.silverpeas.applicationbuilder.ReadOnlyArchive.java

/**
 * Gets the available entries in the archive. It also opens the archive for reading.
 *
 * @return the entries of this archive/* w  w w .j av a  2  s .com*/
 * @since 1.0
 * @roseuid 3AAFB0770391
 */
public ApplicationBuilderItem[] getEntries() {
    if (getJar() == null) {
        return null;
    }
    List<ApplicationBuilderItem> entries = new ArrayList<ApplicationBuilderItem>(getJar().size());
    for (Enumeration<JarEntry> e = getJar().entries(); e.hasMoreElements();) {
        JarEntry jarEntry = e.nextElement();
        if (!jarEntry.isDirectory()) {
            File oneFile = new File(jarEntry.getName());
            ApplicationBuilderItem item = new ApplicationBuilderItem(oneFile.getParent(), oneFile.getName());
            item.setSize(jarEntry.getSize());
            entries.add(item);
        }
    }
    return entries.toArray(new ApplicationBuilderItem[entries.size()]);
}

From source file:org.silverpeas.applicationbuilder.ReadOnlyArchive.java

/**
 * @param entry the entry to read//from  w w  w . j a  v  a 2s  .co m
 * @return the stream for reading the contents of the entry
 * @since 1.0/B
 * @roseuid 3AB080F602D2
 */
public long getEntrySize(ApplicationBuilderItem entry) {
    long size = 0L;
    JarEntry jarEntry = getJarEntry(entry);
    if (jarEntry != null) {
        size = jarEntry.getSize();
    }
    return size;
}

From source file:org.tobarsegais.webapp.ContentServlet.java

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String path = req.getPathInfo();
    if (path == null) {
        path = req.getServletPath();//from   www  .  j a  v  a  2s .  c  o m
    }
    int index = path.indexOf(PLUGINS_ROOT);
    if (index != -1) {
        path = path.substring(index + PLUGINS_ROOT.length() - 1);
    }
    Map<String, String> bundles = (Map<String, String>) getServletContext().getAttribute("bundles");
    for (index = path.indexOf('/'); index != -1; index = path.indexOf('/', index + 1)) {
        String key = path.substring(0, index);
        if (key.startsWith("/")) {
            key = key.substring(1);
        }
        if (bundles.containsKey(key)) {
            key = bundles.get(key);
        }
        URL resource = getServletContext()
                .getResource(ServletContextListenerImpl.BUNDLE_PATH + "/" + key + ".jar");
        if (resource == null) {
            continue;
        }
        URL jarResource = new URL("jar:" + resource + "!/");
        URLConnection connection = jarResource.openConnection();
        if (!(connection instanceof JarURLConnection)) {
            continue;
        }
        JarURLConnection jarConnection = (JarURLConnection) connection;
        JarFile jarFile = jarConnection.getJarFile();
        try {
            int endOfFileName = path.indexOf('#', index);
            endOfFileName = endOfFileName == -1 ? path.length() : endOfFileName;
            String fileName = path.substring(index + 1, endOfFileName);
            JarEntry jarEntry = jarFile.getJarEntry(fileName);
            if (jarEntry == null) {
                continue;
            }
            long size = jarEntry.getSize();
            if (size > 0 && size < Integer.MAX_VALUE) {
                resp.setContentLength((int) size);
            }
            resp.setContentType(getServletContext().getMimeType(fileName));
            InputStream in = null;
            OutputStream out = resp.getOutputStream();
            try {
                in = jarFile.getInputStream(jarEntry);
                IOUtils.copy(in, out);
            } finally {
                IOUtils.closeQuietly(in);
                out.close();
            }
            return;
        } finally {
            //jarFile.close();
        }
    }
    resp.sendError(404);
}