Solved

Getting teh files in a zip/jar file

Posted on 2002-06-08
2
201 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 200 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

730 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