File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes JarInputStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "JarInputStream" Watch "JarInputStream" New topic
Author

JarInputStream

Jason Kretzer
Ranch Hand

Joined: May 31, 2001
Posts: 280
Hello all:
I am using an executable jar to extract some files from said jar. I am not familiar with the JarInputStream class and was wondering if someone could show me how to extract several files from a jar using this class.
Thanks!
------------------
Jason R. Kretzer
Java Programmer


Jason R. Kretzer<br />Software Engineer<br />System Administrator<br /><a href="http://alia.iwarp.com" target="_blank" rel="nofollow">http://alia.iwarp.com</a>
raghavender rao
Ranch Hand

Joined: Jul 20, 2001
Posts: 45
hi ther...

hope this code wil help u....

this is for creating jar file
import java.io.*;
import java.util.*;
import java.util.jar.*;
public class JarCreate {

public static void main(String[] args) throws IOException {

// Create the jar file, using the first command line argument as the
// file name (clobbering the file if it exists).
String jarName = args[0];
JarOutputStream jar = new JarOutputStream(new FileOutputStream(jarName), new Manifest());

System.out.println(jarName + " created.");
try {

// Allocate a buffer for reading the input files.
byte[] buffer = new byte[1024];
int bytesRead;
// Loop through the file names provided on the command-line.
for (int i = 1; i < args.length; i ++) {

// Get the file name.
String fileName = args[i];

try {
// Open the file.

FileInputStream file = new FileInputStream(fileName);
try {

// Create a jar entry and add it to the jar.

JarEntry entry = new JarEntry(fileName);
jar.putNextEntry(entry);
// Read the file the file and write it to the jar.
while ((bytesRead = file.read(buffer)) != -1) {

jar.write(buffer, 0, bytesRead);

}
System.out.println(entry.getName() + " added.");
} catch (Exception ex) {


System.out.println(ex);
}

finally {

file.close();

}

} catch (IOException ex) {

System.out.println(ex);
}
}
} finally {

jar.close();
System.out.println(jarName + " closed.");
}
}
}

this is for extracting jar files
import java.io.*;
import java.util.*;
import java.util.jar.*;
public class JarExtract {

public static void main(String[] args) throws IOException {

// Get the jar name and the entry name.
String jarName = args[0];
String entryName = args[1];
// Open the jar.
JarFile jar = new JarFile(jarName);
System.out.println(jarName + " opened.");
try {
// Get the entry and its input stream.
JarEntry entry = jar.getJarEntry(entryName);
// If the entry is not null, extract it. Otherwise, print a
// message.
if (entry != null) {

// Get an input stream for the entry.

InputStream entryStream = jar.getInputStream(entry);
try {
// Create the output file (clobbering the file if it exists).
FileOutputStream file = new FileOutputStream(entry.getName());
try {

// Allocate a buffer for reading the entry data.
byte[] buffer = new byte[1024];
int bytesRead;
// Read the entry data and write it to the output file.

while ((bytesRead = entryStream.read(buffer)) != -1) {

file.write(buffer, 0, bytesRead);
}
System.out.println(entry.getName() + " extracted.");
} finally {

file.close();
}
} finally {

entryStream.close();
}
} else {

System.out.println(entryName + " not found.");
} // end if
} finally {

jar.close();
System.out.println(jarName + " closed.");
}
}
}

any doubts dont hesitate to mail me back

Rao

Rao<BR>Sun Certified Programmer for the Java��� 2 Platform
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JarInputStream