Re: JarFile/ZipFile from byte array without temp file
On 27 Jun., 19:30, ben...@gmail.com wrote:
On Jun 27, 12:20 pm, Nigel Wade <n...@ion.le.ac.uk> wrote:
Karsten Wutzke wrote:
Hi all!
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(),
"deleteme.jar");
FileOutputStream fos = new FileOutputStream(flTempJar);
fos.write(uncompressedBytes);
fos.close();
System.out.println("Saving extracted library temporarily as file '" +
flTempJar + "' - it sucks......");
JarFile jar = new JarFile(flTempJar);
try
{
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
byte[] constructor.
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:
byte[] byteArr;
...
ByteArrayInputStream byteIS = new ByteArrayInputStream(byteArr);
JarInputStream jarIS = new JarInputStream(byteIS);
--
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : n...@ion.le.ac.uk
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
I happen to have done this before, let me hunt you down the source
code...
package com.plink.dolphinnet.assignments;
import com.plink.dolphinnet.*;
import java.util.*;
import java.util.zip.*;
import java.io.*;
/**
This assignment is passed an assignment as a constructor and zips it.
Used for distributing large assignments more efficiently.
*/
public class ZippedAssignment extends Assignment implements
Serializable{
///////////////////////////////
// Data.
private byte file[]=null;
/////////////////////////
// Constructor.
public ZippedAssignment(int id,Assignment ZAssignment){
super(id);
loadFile(ZAssignment);
}
/////////////////////////
// Getters.
public Assignment getAssignment(){
try{
ByteArrayOutputStream Buffer=new ByteArrayOutputStream();
ZipInputStream in=new ZipInputStream(new
ByteArrayInputStream(file));
ZipEntry data=data=in.getNextEntry();
if(data==null)
return(null);
int i=0;
byte buffer[]=new byte[512];
while((i=in.read(buffer))>0)
Buffer.write(buffer,0,i);
ObjectInputStream oin=new ObjectInputStream(new
ByteArrayInputStream(Buffer.toByteArray()));
return((Assignment)oin.readObject());
}catch(Exception e){
e.printStackTrace();
}
return(null);
}
private void loadFile(Assignment ZAssignment){
try{
ByteArrayOutputStream bout=new ByteArrayOutputStream();
ByteArrayOutputStream bout2=new ByteArrayOutputStream();
ZipOutputStream zout=new ZipOutputStream(bout);
zout.setLevel(9);
ObjectOutputStream oout=new ObjectOutputStream(bout2);
oout.writeObject(ZAssignment);
ByteArrayInputStream bin=new
ByteArrayInputStream(bout2.toByteArray());
zout.putNextEntry(new ZipEntry("Assignment"));
byte buffer[]=new byte[512];
int index;
while((index=bin.read(buffer))>0)
zout.write(buffer,0,index);
zout.closeEntry();
zout.close();
file=bout.toByteArray();
}catch(Exception e){
e.printStackTrace();
}
}
/** Run the assignments implemented task. */
public Object execute(DataHandler DH){
finish();
return(null);
}
}
That code is from my DolphinNet API, it zips something into a
ByteArray on the fly, sends it and has methods for un-zipping it.
Hmm a tried a similar approach...
As far as I understood, the trick here was to write a Serializable
object to an output stream being backed by a ByteArrayOutputStream,
and then retrieving the raw bytes from the ByteArrayOutputStream...
right?
I can't say anything about the size of my JarEntry, its getSize()
method always returns -1, whyever it does so. So analogously I create
a JarOutputStream backed by a ByteArrayOutputStream, write the
JarEntry to the JarOutputStream and retrieve the bytes then.
while ( true )
{
JarEntry je = jis.getNextJarEntry();
if ( je == null )
{
break;
}
//can grow!
ByteArrayOutputStream baos = new ByteArrayOutputStream();
JarOutputStream jos = new JarOutputStream(baos);
jos.setLevel(9); //0 - 9 but which one is no compression?
//write JarEntry, now baos should contain the raw bytes only?
jos.putNextEntry(je);
int size = baos.size();
byte[] bytes = baos.toByteArray();
jos.close();
...
}
Technically it works, however ALL of my entries are only ~60 - 100
bytes long and their sizes correllate with the length of their entry
names plus a few bytes. So consequently, it didn't and doesn't work.
Input anyone?
Hmm I'll have to look at my input stream more closely, maybe something
is not quite right there...
I create the JarInputStream like:
//just a resource file in the relative path
URL url = getClass().getResource("/lib/bcel-5.2.jar");
URLConnection uc = url.openConnection();
FileURLConnection fuc = (FileURLConnection)uc;
JarInputStream jis = new JarInputStream(fuc.getInputStream());
When reading from that jis like
byte[] buffer = new byte[8192];
int numTotal = 0;
while ( true )
{
int numRead = jis.read(buffer, 0, 8192);
if ( numRead <= 0 )
{
break;
}
numTotal += numRead;
}
numTotal *ALWAYS* is zero! Either now I'm completely off off
everything or I must have forgotton to initialize something. No bytes
read from the stream at all times.
Please help I'm really starting to get nuts on this. All I want to do
is read a JAR file from "lib/bcel-5.2.jar" resource, whether it's
currently found in that local dir or another JAR file and read the
entries (class files) to decompressed byte arrays.
Karsten
PS: Going home now, 22:22 PM sucks