制作7Z格式可安装程序包
引言:
现在制作自解压安装包主要有两种方式,一种是通过rar方式制作,另一种是通过7Z格式制作。因rar是收费的,所以使用7Z来制作自解压安装包。
具体代码如下:
import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZMethod;
import org.apache.commons.compress.archivers.sevenz.SevenZOutputFile;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
/**
* 制作exe.
*/
public class MakeSfx {
/**
* 主入口.
*
* @param args args
* @throws IOException IOException
*/
public static void main(final String[] args) throws IOException {
final MakeSfx makeSfx = new MakeSfx();
File orgFile = null;
//exe包所在路径
final File directory = new File(new File("path").getAbsolutePath());
for (File file : directory.listFiles()) {
//找到目标exe文件
if (file.getName().endsWith("exe")) {
orgFile = file;
System.out.println("对文件" + file.getAbsoluteFile() + "进行打包。");
break;
}
}
// orgFile = new File("E:\\Test\\all\\143\\YG-RPA 4.0.1.9.1ALL(内部版本).exe");
if (null == orgFile) {
System.out.println("该文件夹没有找到包含“YG-RPA”的exe文件。");
return;
}
final File zipFile = makeSfx.customizeRobotGen7z(orgFile);
makeSfx.customizeRobotGenExe(zipFile);
makeSfx.delay();
FileUtils.forceDelete(zipFile);
}
/**
* 延时.
*/
private void delay() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 定制机器人,添加新增授权.exe,license压缩成7z文件.
*
* @param exeFile jar包
* @return 7z文件
* @throws IOException IOException
*/
private File customizeRobotGen7z(final File exeFile)
throws IOException {
System.out.println("开始生成7zip压缩包");
//生成7Z包的路径
final File compress7z = new File("path" + File.separator
+ exeFile.getName().substring(0, exeFile.getName().lastIndexOf(".")) + ".7z");
if (!compress7z.exists()) {
FileUtils.forceMkdirParent(compress7z);
compress7z.createNewFile();
}
final SevenZOutputFile outArchive = new SevenZOutputFile(compress7z);
//新改动,压缩方式选择COPY
outArchive.setContentCompression(SevenZMethod.COPY);
final SevenZArchiveEntry entryJar = new SevenZArchiveEntry();
entryJar.setName(exeFile.getName());
outArchive.putArchiveEntry(entryJar);
final BufferedInputStream in = new BufferedInputStream(new FileInputStream(exeFile));
final int bufSize = 1024;
final byte[] buffer = new byte[bufSize];
int len = 0;
while (-1 != (len = in.read(buffer, 0, bufSize))) {
outArchive.write(buffer, 0, len);
}
outArchive.closeArchiveEntry();
IOUtils.closeQuietly(outArchive);
System.out.println("生成7zip压缩包完成。");
return compress7z;
}
/**
* 机器人7z文件制作成可自解压的exe文件(7z方案).
*
* @param compress7z 7z文件
* @return exe文件大小
* @throws IOException IOException
*/
private long customizeRobotGenExe(final File compress7z)
throws IOException {
System.out.println("开始生成exe");
final String exeName = compress7z.getName().substring(0, compress7z.getName().lastIndexOf(".")) + "1" + ".exe";
final File configFile = createConfig(exeName);
InputStream is7zSD = null;
InputStream isConfig = null;
File robotExe = null;
SeekableByteChannel robotExeSbc = null;
SeekableByteChannel z7Sbc = null;
try {
is7zSD = new FileInputStream(new File("path\\7zSD.sfx"));
isConfig = new FileInputStream("config.txt");
// isConfig = this.getClass().getResourceAsStream("/" + this.getClass().getName() + "/config.txt");
robotExe = new File("path\\" + exeName);
if (!robotExe.exists()) {
robotExe.createNewFile();
}
FileUtils.writeByteArrayToFile(robotExe, IOUtils.toByteArray(is7zSD));
FileUtils.writeByteArrayToFile(robotExe, IOUtils.toByteArray(isConfig), true);
// 改动,考虑到jar可能比较大,不再采用FileUtils.writeByteArrayToFile方法
robotExeSbc = Files.newByteChannel(robotExe.toPath(), StandardOpenOption.APPEND);
z7Sbc = Files.newByteChannel(compress7z.toPath(), StandardOpenOption.READ);
final ByteBuffer z7Bf = ByteBuffer.allocate(1024);
while ((z7Sbc.read(z7Bf)) > 0) {
z7Bf.flip();
robotExeSbc.write(z7Bf);
z7Bf.clear();
}
} finally {
IOUtils.closeQuietly(is7zSD, isConfig, z7Sbc, robotExeSbc);
}
delay();
FileUtils.forceDelete(configFile);
System.out.println("生成exe完成");
return FileUtils.sizeOf(robotExe);
}
/**
* 创建配置文件.
*
* @param exeName exeName
* @return 配置文件
* @throws IOException IOException
*/
private File createConfig(final String exeName) throws IOException {
final File configFile = new File("config.txt");
if (!configFile.exists()) {
configFile.createNewFile();
System.err.println(configFile.getAbsolutePath() + "已创建!");
}
final RandomAccessFile randomAccessFile = new RandomAccessFile(configFile, "rw");
randomAccessFile.write((";!@Install@!UTF-8!\r\n"
+ "Title=\"安装\"\r\n"
+ "RunProgram=\"" + exeName + " /T:%%T\"\r\n"
+ ";!@InstallEnd@!").getBytes("UTF-8"));
if (randomAccessFile != null) {
try {
randomAccessFile.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
return configFile;
}
}