Solved

Getting teh files in a zip/jar file

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

696 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