制作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;
    }

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值