Solved

Getting teh files in a zip/jar file

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now