Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

JarInputStream

 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic