Re: JarFile/ZipFile from byte array without temp file
Karsten Wutzke wrote:
On 27 Jun., 18:20, Nigel Wade <n...@ion.le.ac.uk> wrote:
Karsten Wutzke wrote:
Subject says it all... how do I create a JarFile/ZipFile instance from
a byte array without outputting the byte to a temporary file and
reading it back via the JarFile/ZipFile constructors??
Currently I do it via temp file (which sucks):
File flTempJar = new File(RuntimeConfig.getIoTempDir(),
FileOutputStream fos = new FileOutputStream(flTempJar);
System.out.println("Saving extracted library temporarily as file '" +
flTempJar + "' - it sucks......");
JarFile jar = new JarFile(flTempJar);
boolean wasSuccessful = flTempJar.delete();
catch ( Exception e )
System.err.println("Temporary JAR file '" + flTempJar + "'
couldn't be deleted!");
//now do something with the JarFile instance....
I can't and don't want anyone using this code to require disk access.
When a SecurityManager prohibits this, this code becomes useless.
Furthermore, since this is CLASSLOADER code, all classes to be found
and loaded by this class loader will never be available...
Can anyone help what to do here?
Looks like I have to create my own JarFile subclass to provide the
If there's a different way, I'm all ears...
I wonder who wrote the ZipFile and JarFile classes... how could they
forget byte and/or stream constructors? beats me...
Probably because those classes are specific to reading from FileInputStreams?
Try looking for other classes related to Jar and Zip streams...
I would try wrapping a ByteArrayInputStream with a JarInputStream. Something
along the lines of:
ByteArrayInputStream byteIS = new ByteArrayInputStream(byteArr);
JarInputStream jarIS = new JarInputStream(byteIS);
Hmmm I tried to go via JarInputStream now instead of JarFile. My loop
now goes something like this:
JarInputStream jis = ...; //valid JarInputStream
while ( true )
JarEntry je = jis.getNextJarEntry();
if ( je == null )
//no more entries
String strName = je.getName();
int size = (int)je.getSize(); //<- RETURNS -1 *always*
System.out.println(strName + " has " + size + " bytes
byte uncompressedBytes = new byte[size]; //CAUSES
//"jar" was an instance to the JarFile, JarInputStream doesn't
have equivalent method!
//DataInputStream dis = new
DataInputStream dis = new
DataInputStream(jis.getInputStream(je)); //UNKNOWN METHOD
dis.readFully(uncompressedBytes, 0, uncompressedBytes.length);
How do I get the uncompressed bytes of a JarEntry?? Hmmm I looked at
the code too many hours now, maybe I'm just blind...
I think you have hit one of the problems of streamed ZIP, that size isn't set. I
think I came across this problem when using streamed ZIPs. I don't really
remember, but I have a recollection that getSize() returned -1 when the ZIP
wasn't created from an actual file. Presumably when the compression is
performed on-the-fly, and the ZipEntry is written to the stream, the size of
the uncompressed data is unknown.
I think you just need to modify your code so that it doesn't rely on the
getSize(). Read until read() returns -1, appending to, and resizing, the
buffer as necessary. As I understand it ZipInputStream.read() reads from a
single ZIP entry until the end of that entry, then returns -1.
ZipInputStream.getNextEntry() can then be used to position the stream to the
next ZIP entry.
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : email@example.com
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555