一种使用Zip文件的Comment字段实现的多渠道方案

转载于此
本功能所涉及到的原理在上述文章中已有阐述, 这里主要是将实现代码贴出来

  • 向apk文件中写入Comment的代码段

    public static void setZipComment(String filePath, String comment) {
        File file = new File(filePath);
        ZipFile zipFile = null;
        ByteArrayOutputStream outputStream = null;
        RandomAccessFile accessFile = null;
        try {
            zipFile = new ZipFile(file);
            outputStream = new ByteArrayOutputStream();
    
            byte[] byteComment = comment.getBytes();
            outputStream.write(byteComment);
            outputStream.write(short2Stream((short) byteComment.length));
    
            byte[] data = outputStream.toByteArray();
    
            accessFile = new RandomAccessFile(file, "rw");
            accessFile.seek(file.length() - 2);
            accessFile.write(short2Stream((short) data.length));
            accessFile.write(data);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (zipFile != null) {
                    zipFile.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
                if (accessFile != null) {
                    accessFile.close();
                }
            } catch (Exception e) {
    
            }
    
        }
    }
    
    public static byte[] short2Stream(short data) {
        ByteBuffer buffer = ByteBuffer.allocate(2);
        buffer.order(ByteOrder.LITTLE_ENDIAN);
        buffer.putShort(data);
        buffer.flip();
        return buffer.array();
    }
  • 在apk源代码中读取写入的Comment

    public static String getZipComment(String filePath) {
        File file = new File(filePath);
        byte[] bytes;
        RandomAccessFile randomAccessFile = null;
        try {
            randomAccessFile = new RandomAccessFile(file, "r");
            long index = randomAccessFile.length();
    
            bytes = new byte[2];
            index = index - bytes.length;
            randomAccessFile.seek(index);
            randomAccessFile.readFully(bytes);
    
            bytes = new byte[stream2Short(bytes, 0)];
            index = index - bytes.length;
            randomAccessFile.seek(index);
            randomAccessFile.readFully(bytes);
    
            return new String(bytes, "utf-8");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (randomAccessFile != null) {
                try {
                    randomAccessFile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return "";
    }
    
    public static short stream2Short(byte[] stream, int offset) {
        ByteBuffer buffer = ByteBuffer.allocate(2);
        buffer.order(ByteOrder.LITTLE_ENDIAN);
        buffer.put(stream[offset]);
        buffer.put(stream[offset + 1]);
        return buffer.getShort(0);
    }

    使用如下代码调用getZipComment即可获得写入的注释

    Context#getPackageCodePath()

    实现代码及其工程-百度网盘
    实现代码及其工程-CSDN

    1. 使用方法java -jar zip-comment.jar app-release.apk markets.txt
    2. zip-comment.jar 是代码生成的可执行jar
    3. app-release.apk是待多渠道的原始包
    4. markets.txt 是各个渠道代码, 尽量简短
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值