RandomAccessFile in JAR

B

Ben_

Hello,

If what you want to do is modify a ZipEntry without extracting it, then I
don't see it feasible, because you have to extract the ZipEntry to alter it
and then re-zip it. My assumption is based on the fact that the API doesn't
mention this and on the fact that it's what Zip file managers do (like
WinZip, etc).
 
B

Ben_

Hmm, I don't want to change anything. Just create RandomAccessFile.
The idea you have is obvious to you, but not to us. You should give details
if you want support... :)
 
A

ak

That simple, i wrote RandomAccessByteArray (which extends
RandomAccessFile) - so I can read byte array as RandomAccessFile.
Constructor of RandomAccessFile needs valid File.
I used before some temporary file. But this approach dont work if my
programm runs in applet.


hier is my code:

//create file in same directory with my JAR file
private static File f = new File("..\\xxx.tmp");

int fp;

private static File createFile() throws IOException {
FileOutputStream fos = new FileOutputStream(f);
//ensure that file really created
fos.write(100);
IOutils.closeStream(fos);
return f;
}

byte[] buf;
String mode;
int length;
int offset;

/**
* create new RandomAccessByteArray that looks like RandomAccessFile
* @param mode ignored
* @param data byte array
* @throws IOException
*/
public RandomAccessByteArray(String mode, byte[] data) throws IOException {
this(mode, data, 0, data.length);
}

/**
* create new RandomAccessByteArray that looks like RandomAccessFile
* @param mode ignored
* @param data byte array
* @param off index of the first byte
* @param length number of bytes
* @throws IOException
*/
public RandomAccessByteArray(String mode, byte[] data, int off, int length)
throws IOException {
super(createFile(), "r");
this.mode = mode;
this.buf = data;
this.length = length;
this.offset = off;

//clean up
try {
super.close();
}
catch(IOException ex) {
ex.printStackTrace();
}
finally {
f.delete();
}
}
 
M

Michael Borgwardt

ak said:
That simple, i wrote RandomAccessByteArray (which extends
RandomAccessFile) - so I can read byte array as RandomAccessFile.
Constructor of RandomAccessFile needs valid File.
I used before some temporary file. But this approach dont work if my
programm runs in applet.

And you thought you could put it in the applet's own JAR???
That's even more impossible.

Do you actually need *random* access? Or just the readXX methods?
In the latter case, a DataInputStream wrapped around a ByteArrayInputStream
offers the same.
 
A

ak

Yes, I really need *random* access.


Michael Borgwardt said:
And you thought you could put it in the applet's own JAR???
That's even more impossible.

Do you actually need *random* access? Or just the readXX methods?
In the latter case, a DataInputStream wrapped around a ByteArrayInputStream
offers the same.
 
M

Michael Borgwardt

ak said:
Yes, I really need *random* access.

Then I think the best method would be to implement the methods from
RandomAccessFile that you need yourself to work directly on a byte
array. Shouldn't be too difficult.

Having a temporary file is simply not possible in an applet, at least
not with the standard security policy.
 
A

ak

The goal of RandomAccessByteArray is that there was no need to implement
same things second time for byte arrays - just create RandomAccessByteArray
and pass it to my standard reader which works on files.

Michael Borgwardt said:
Then I think the best method would be to implement the methods from
RandomAccessFile that you need yourself to work directly on a byte
array. Shouldn't be too difficult.

I think I should create a new Interface and use it instead of
RandomAccessFile:

public interface MyRandomAccess extends DataInput, DataOutput {
//methods from RandomAccessFile
}

public class MyRandomAccessFile extends RandomAccessFile implements
MyRandomAccess {
public MyRandomAccessFile(String s, File f) {
super(s, f)
}
}

public class MyRandomAccessByteArray implements MyRandomAccess {

}

Thanks for all comments!

Andrei
 
B

Ben_

Zip classes uses Streams to inflate/deflate zipentries. So maybe you can do
the work in memory.
 
R

Roedy Green

Hi, I need to create RandomAccessFile for file which is in JAR. Is it
possible?

I think you mean you want to read elements in random order. For that
use ZipFile and ZipEntry. We aware that if you created the file with
ZipOutputStream the length fields will be wrong in ZipEntry.


If you actually want to read the jar at a low level because you want
to roll you own ZipFile, you would use RandomAccessFile. See
http://mindprod.com/fileio.html for how.

Most likely you just want to read:
http://mindprod.com/jgloss/zip.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top