Solved

Getting teh files in a zip/jar file

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Cipher Configuration on Apache HTTPD 4 62
Java JRE greater than 1.6 5 62
how to add new optional parameter to JSP 1 40
varialbe initialization 11 32
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…
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 learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…

808 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