如何在apk中添加文件夹

时间:2015-09-09 02:13:47

标签: apk add

我想知道如何在apk中添加文件夹。不给我一个关于直接使用压缩软件的解决方案。 apk由'apktool.jar'重新编译。我需要一些代码来实现这个问题。希望能尽快解决我的问题。谢谢〜

1 个答案:

答案 0 :(得分:0)

    public static int zipMetaInfFolderToApk(String apkName, String folderName) throws IOException {
    if(!new File(apkName).exists()){
        return ConstantValue.ISQUESTION;
    }

    String zipName = apkName.substring(0, apkName.lastIndexOf(".")) + "."
            + "zip";
    String bak_zipName = apkName.substring(0, apkName.lastIndexOf("."))
            + "_bak." + "zip";

    FileUtils.renameFile(apkName, zipName);

    ZipFile war = new ZipFile(zipName);

    ZipOutputStream append = new ZipOutputStream(new FileOutputStream(
            bak_zipName));

    Enumeration<? extends ZipEntry> entries = war.entries();
    while (entries.hasMoreElements()) {
        ZipEntry e = entries.nextElement();
        System.out.println("copy: " + e.getName());
        append.putNextEntry(e);
        if (!e.isDirectory()) {
            copy(war.getInputStream(e), append);
        }
        append.closeEntry();
    }

    String name = "";
    if(folderName.equals("")){
        name = "META-INF/" + folderName;
    }else{
        name = "META-INF/" + folderName + "/";
    }
    ZipEntry e = new ZipEntry(name);

    try{
        append.putNextEntry(e);
    }catch(ZipException e1){
        append.closeEntry();
        war.close();
        append.close();

        FileUtils.renameFile(zipName,apkName);
        FileUtils.deleteFolder(bak_zipName);
        e1.printStackTrace();
        return ConstantValue.ISQUESTION;
    }
    append.closeEntry();

    war.close();
    append.close();

    FileUtils.deleteFolder(zipName);

    FileUtils.renameFile(bak_zipName, apkName);
    return ConstantValue.ISNORMAL;
}