?
Solved

Getting teh files in a zip/jar file

Posted on 2002-06-08
2
Medium Priority
?
214 Views
Last Modified: 2010-03-31
Hi!
I would like to get a small example of how to get files from a jar/zip file.
(My program now works on a directory - get all the files reqursive, and I want that it will get all the files from a jar file instead).
Thanks!
0
Comment
Question by:rzvika2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 2

Accepted Solution

by:
nir2002 earned 800 total points
ID: 7065092
you need those packages to import:
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

//for enumerate the entries. use to look your specfic file
//if exist at jar/zip or just know what is in the zip/jar
  try {
      //path - the path to the jar/zip file
      // fileName - the zip/jar file name
      ZipFile zipfile = new ZipFile(path + File.separatorChar + fileName);
      String entryName = null;
      if(zf != null) {
        Enumeration e = zf.entries();
        while (e.hasMoreElements()) {
          // Loop entries
             ZipEntry ze=(ZipEntry)e.nextElement();
          entryName = ze.getName();
        }
        zf.close();
        zf = null;
      }
    } catch(java.io.IOException ioe) {}

//for get the file as byte[] you can use this example
//note you can use the InputStream as you want
    try {
        JarFile jarFile = new JarFile(path + File.separatorChar + filename);
        JarEntry jarEntry = jarFile.getJarEntry(fileNameLookingFor);
        length = (int) jarEntry.getSize();
        bytes = new byte[length];
        DataInputStream in = new DataInputStream(jarFile.getInputStream((ZipEntry) jarEntry));
        in.readFully(bytes);
        in.close();
      } catch(java.io.IOException ioe) {
      }
0
 

Expert Comment

by:pouli
ID: 7065855
http://developer.java.sun.com/developer/technicalArticles/Programming/compression/

Look there:
Compressing and Decompressing Data using JavaTM APIs
by Qusay H. Mahmoud
with contributions from Konstantin Kladko
February 2002
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question