现收到需求:用户下载exe时,自动往exe文件中添加或覆盖某文件。
思路:1、由于自解压的exe文件由sfx、config.txt、7z压缩文件组成。
所以直接用SevenZFile是打不开该文件的。
2、从exe文件中,找到config.txt结尾标识的位置(pos).3、将exe文件拆会成两具临时文件件:sfx+config.txt文件,z7.7z压缩包文件。
4、调用SevenZFile,添加中覆盖文件组成新的压缩文件newz7.7z压缩包。
5、合并sfx+config.txt文件、newz7.7z压缩包成exe生解压文件。
代码如下:其中:d:\test\7z自解压.exe 为自解压exe文件。
/** * 查找文件中的config.txt结尾位置 * * @throws IOException */@Testpublic void getConfigEndPosTest() throws IOException {final File exeFile = new File("d:\test\7z自解压.exe");final byte[] configEnd= ";!@InstallEnd@!".getBytes("ISO-8859-1");final BufferedInputStream exeBis = new BufferedInputStream(new FileInputStream(exeFile));// sfx假定大于124928exeBis.skip(124928);int b;long pos = 124928;int macth = 0;while ((b = exeBis.read()) != -1) {pos++;if (configEnd[macth] == b) {macth++;} else {macth = 0;}if (macth == 15) {System.out.print(pos);break;}}exeBis.close();}/** * 自解压文件拆分成: sfx+config, 7z两个临时文件 * * @throws IOException */@Testpublic void splitFileTest() throws IOException {final File exeFile = new File("d:\test\7z自解压.exe");final FileInputStream exeIn = new FileInputStream(exeFile);final File sfxFile = new File("d:\test\sfx.tmp");sfxFile.createNewFile();final FileOutputStream sfxOs = new FileOutputStream(sfxFile);// 125070 第一步求得的posbyte[] buffer = new byte[125070];int length;length = exeIn.read(buffer);sfxOs.write(buffer, 0, length);sfxOs.close();final File z7File = new File("d:\test\z7.7z");z7File.createNewFile();final FileOutputStream z7Os = new FileOutputStream(z7File);while ((length = exeIn.read(buffer)) > 0) {z7Os.write(buffer, 0, length);}z7Os.close();exeIn.close();}/** * 添加或覆盖的文件到7z * * @throws IOException */@Testpublic void writeFileTo7z() throws IOException { //略,7z文件处理}/** * sfx+config + 新的7z文件成exe. * @throws IOException */@Testpublic void mergeFile() throws IOException {//略, 参考前一篇的《JAVA JAR制作可自运行的EXE包》}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)