aspose file tools
The moose likes Java in General and the fly likes How can I open a zip thru Java code Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "How can I open a zip thru Java code" Watch "How can I open a zip thru Java code" New topic
Author

How can I open a zip thru Java code

Waez Ali
Ranch Hand

Joined: Jan 24, 2005
Posts: 42
Hi,
I have such requirement where I could read a text file which is inside
some jar file.SO can anybodoy please tell me how can do this???

Thanks & Regards,
Waez Ali.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14670
    
  11

You should have a look at java.util.jar.JarFile


[My Blog]
All roads lead to JavaRanch
Waez Ali
Ranch Hand

Joined: Jan 24, 2005
Posts: 42
well thanks for the reply,but JarFile class couldn't help that much as it contains some constatns ,dont know how to use them........
I would appreciate if someone could give me sample code
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35224
    
    7
JarFile can do this, just ignore the parts you don't understand. If you just want to read a jar file, they're irrelevant. There are some code examples here.


Android appsImageJ pluginsJava web charts
Shilpa Asuthkar
Greenhorn

Joined: Dec 22, 2005
Posts: 10
hi Waez,
check out the code i have given here.... if the filelength returns -1 u must try to read the data from the jar entry in chunks or set the filelength to some max value say 100 bytes.... replace temp.class with any file that u want to read

hope this code help you


import java.util.jar.*;
import java.io.*;
public class Readfromjar
{


public static void main(String[] args)
{
try {
FileInputStream fis = new FileInputStream ( "xxx.jar" );
JarInputStream j = new JarInputStream ( fis );


while ( true )
{
JarEntry entry = j.getNextJarEntry();
String elementName = entry.getName();
System.out.println(elementName);
if(elementName.equals("temp.class"))
{

File elementFile = new File("myfile");

int filelen = (int)entry.getSize();
//int filelen=100;
System.out.println(filelen);

byte[] wholeFile = new byte[ filelen ];
int bytesRead = j.read( wholeFile , 0 , filelen );

FileOutputStream fos = new FileOutputStream ( elementFile );
fos.write( wholeFile, 0, filelen );
fos.close();

j.closeEntry();

}

}

}
catch (IOException e) {

}


}
}
Waez Ali
Ranch Hand

Joined: Jan 24, 2005
Posts: 42
Thanks a lot shilpa & thanks to ulf also
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14670
    
  11

I thank myself too for not giving a sample to a lazy chap,
and send you to my "no help" list
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: How can I open a zip thru Java code
 
Similar Threads
I've bought Sybex Java 2 Certification Virtual Trainer
Hide Standart Buttons in Taskbar
Assertion Doubt.
ConnectionFactory lookup returns null
Increase memory for tomcat