?
Solved

Editing Bytes in Java, Hex Editing?

Posted on 2003-03-10
15
Medium Priority
?
175 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:tommyboy2214
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 4
  • +1
15 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8103335
Are you sure that altering the last two bytes will have the effect you want?
0
 
LVL 2

Expert Comment

by:antonsigur
ID: 8103376
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8103416
Please verify that this can be donw before anything else. I am sceptical, but willing to be persuaded ;-)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:antonsigur
ID: 8103420
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
 

Author Comment

by:tommyboy2214
ID: 8103442
>>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
 

Author Comment

by:tommyboy2214
ID: 8103475
thanx,
I understand how to do it using bufferedoutputstream,
 Any idea how to determine the last two bytes of the zip file?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 8103514
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
 
LVL 2

Expert Comment

by:antonsigur
ID: 8103532
You could determine the size of the file, and count the bytes you read/write
0
 
LVL 2

Expert Comment

by:antonsigur
ID: 8103541
Or just use CEHJ's solution :-)
0
 
LVL 2

Expert Comment

by:antonsigur
ID: 8103558
Could you post a link to the bin and text file u use to make this goodies...
0
 

Author Comment

by:tommyboy2214
ID: 8103648
thanx alot for solution, very helpful

here is a link to sfx.exe stuff http://www.geocities.com/kennethxu/TechInfo/CreateZIPSFX/index.html 
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8103664
Thanks. Did it work then?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8103684
Ah i see. kennethxu's a frequent contributor over here actually as you may know.
0
 

Expert Comment

by:corback
ID: 8103699
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
 

Author Comment

by:tommyboy2214
ID: 8103947
yep, worked great,
now just working on dynamically creating my zip file

thanx again
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month12 days, 18 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question