我有这么一个需求,将apk追加隐藏到图片里面,图片可以正常打开,利用解压算法可以讲apk提取出来,安装使用!
刚刚开始确实头疼,因为这种东西资料比较少了,各方面查阅之后了解:
1、图片的格式,大部分图片是以16进制存储的,并且末尾都有一个结束标志位FFD9,具体的大家可以百度了解下
2、apk的格式,是以字节数组的格式进行存储的
那么有了思路:
1、先将apk处理,转换成byte数组
2、通过RandomAccessFile方法将数组追加到图片末尾
3、将合成的图片再次进行提取成byte数组
4、将byte数组转换成字符串截取FFD9+4后面的长度
5、将截取到的字符串转换成byte数组,写入的文件中,重命名apk,即可大功告成
<span style="font-size:24px;color:#ff0000;">合并:</span>
package jpgtest;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.Scanner;
/**
* 追加
* @author Clance
*
*/
public class AddFileToImage {
public static void main(String[] args) {
//d:/123456.jpg
//d:/smple.apk
String apkPath = "";
String imagePath = "";
Scanner in = new Scanner(System.in);
System.out.println("请输入apkPath:");
apkPath = in.nextLine();
System.out.println("请输入imagePath:");
imagePath = in.nextLine();
System.out.println("-------------开始合并----------" );
try {
add(imagePath, fileToBytes(apkPath));
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 追加文件:使用RandomAccessFile
*
* @param fileName
* 文件名
* @param content
* 追加的内容
*/
public static void add(String fileName, byte[] content) {
try {
// 打开一个随机访问文件流,按读写方式
RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
// 文件长度,字节数
long fileLength = randomFile.length();
System.out.println("-------------合并前大小----------" +fileLength);
// 将写文件指针移到文件尾。
randomFile.seek(fileLength);
randomFile.write(content);
randomFile.close();
System.out.println("-------------合并成功----------" );
} catch (