Editing Bytes in Java, Hex Editing?

Hello

Need help in editing the last two bytes of a zip file in java. I need to modify the last two bytes of the zip file to values 58 00. I need
to do this in order to create a self-extracting exe which includes a head.bin, XXX.zip and tail.txt.


thanx for your help
tommyboy2214Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
CEHJConnect With a Mentor Commented:
If you're right then this should work:

import java.io.*;

public class SelfExtract {
  final static byte[] BYTES = { (byte)58, (byte)0 };

  public static void main(String[] args){
    if(args.length < 1){
     System.out.println("Usage: java SelfExtract <file name>");
     System.exit(-1);
    }
    try {
      MakeSelfExtracting(args[0]);
    }
    catch(IOException e){
      e.printStackTrace();
    }
  }

  public static void MakeSelfExtracting(String fileName) throws IOException {
    RandomAccessFile raf = new RandomAccessFile(fileName, "rw");
    long start = raf.length() - 1;
    raf.seek(start);
    raf.write(BYTES);
    raf.close();
  }

}
0
 
CEHJCommented:
Are you sure that altering the last two bytes will have the effect you want?
0
 
antonsigurCommented:
you mean you need to combine head.bin, xxx.zip and tail.txt into one exe file, and change xxx.zip last two bytes? Are the zip files big?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
CEHJCommented:
Please verify that this can be donw before anything else. I am sceptical, but willing to be persuaded ;-)
0
 
antonsigurCommented:
You could use bufferedInputStream and read the file into you application - and use bufferdedOutputStream to write out the new file. But when writing the last bytes, change the last two bytes to 58 00.
This will tak alot of time if the zip files are very big...
0
 
tommyboy2214Author Commented:
>>Are you sure that altering the last two bytes will have the effect you want?
Yes, i changed the last two bytes using a hex editor, and when i put the files togethor, it worked.


>>you mean you need to combine head.bin, xxx.zip and tail.txt into one exe file, and change xxx.zip last two bytes?
yes, exactly what i need to do

>>Are the zip files big?
yes, some of the files are fairly big, largest one being around 100Megs,
0
 
tommyboy2214Author Commented:
thanx,
I understand how to do it using bufferedoutputstream,
 Any idea how to determine the last two bytes of the zip file?
0
 
antonsigurCommented:
You could determine the size of the file, and count the bytes you read/write
0
 
antonsigurCommented:
Or just use CEHJ's solution :-)
0
 
antonsigurCommented:
Could you post a link to the bin and text file u use to make this goodies...
0
 
tommyboy2214Author Commented:
thanx alot for solution, very helpful

here is a link to sfx.exe stuff http://www.geocities.com/kennethxu/TechInfo/CreateZIPSFX/index.html 
0
 
CEHJCommented:
Thanks. Did it work then?
0
 
CEHJCommented:
Ah i see. kennethxu's a frequent contributor over here actually as you may know.
0
 
corbackCommented:
Well if you re doing a program doing everything from the modification of the last 2 bytes to the concatenation i m really interested in seeing the source code
Thank you
0
 
tommyboy2214Author Commented:
yep, worked great,
now just working on dynamically creating my zip file

thanx again
0
All Courses

From novice to tech pro — start learning today.