diff --git a/README_zh.md b/README_zh.md index 4e6bcb394e833effcb2e6189884253ca0b048aed..055e387c468ea0f1c0a5bf5e61ab89ae7005ee0f 100644 --- a/README_zh.md +++ b/README_zh.md @@ -56,7 +56,7 @@ java -jar app_packing_tool.jar --mode hap --json-path --maple-so-path [ #### 1.2.1 示例 ``` -java -jar app_packing_tool.jar --mode har --json-path [option] --jar-path [option]--lib-path [option] --resources-path [option] --out-path [option] --force [option] +java -jar app_packing_tool.jar --mode har --json-path [option] --jar-path [option] --lib-path [option] --resources-path [option] --out-path [option] --force [option] ``` #### 1.2.2 参数含义及规范 @@ -106,13 +106,14 @@ java -jar app_packing_tool.jar --mode multiApp --hap-list 1.hap,2.hap --app-list #### 1.4.2 参数含义及规范 -| 指令 | 是否必选项 | 选项 | 描述 | -|------------|-------|-----------|-----------------------------------------------------------------------------------------------------| -| --mode | 是 | multiApp | 打包类型,在将多个hap打入同一个app时,需保证每个hap满足合法性校验规则。 | -| --hap-list | 否 | hap的路径 | 1.hap包文件路径,文件名必须以.hap为后缀。如果是多个hap包需要”,“分隔。2.hap文件路径也可以是目录。 | -| --hsp-list | 否 | hsp的路径 | 1.hsp包文件路径,文件名必须以.hsp为后缀。如果是多个hsp包需要”,“分隔。2.hsp文件路径也可以是目录。 | +| 指令 | 是否必选项 | 选项 | 描述 | +|------------|-------|-----------|---------------------------------------------------------------------------------------------------| +| --mode | 是 | multiApp | 打包类型,在将多个hap打入同一个app时,需保证每个hap满足合法性校验规则。 | +| --hap-list | 否 | hap的路径 | 1.hap包文件路径,文件名必须以.hap为后缀。如果是多个hap包需要”,“分隔。2.hap文件路径也可以是目录。 | +| --hsp-list | 否 | hsp的路径 | 1.hsp包文件路径,文件名必须以.hsp为后缀。如果是多个hsp包需要”,“分隔。2.hsp文件路径也可以是目录。 | | --app-list | 否 | app的路径 | 1.app文件路径,文件名必须以.app为后缀。如果是多个app包需要用”,“分隔。2.app文件路径也可以是目录。3.--hap-list,--hsp-list,--app-list不可以都不传。 | -| --force | 否 | 默认值为false | 默认值为false,如果为true,表示当目标文件存在时,强制删除。 | +| --out-path | 是 | NA | 目标文件路径,文件名必须以.app为后缀。 | +| --force | 否 | 默认值为false | 默认值为false,如果为true,表示当目标文件存在时,强制删除。 | #### 1.4.3 多工程打包hap合法性校验 @@ -123,7 +124,7 @@ java -jar app_packing_tool.jar --mode multiApp --hap-list 1.hap,2.hap --app-list #### 1.5.1 示例 ``` -java -jar app_packing_tool.jar --mode hqf --json-path ---lib-path --ets-path --out-path +java -jar app_packing_tool.jar --mode hqf --json-path --lib-path --ets-path --out-path ``` #### 1.5.2 参数含义及规范 diff --git a/adapter/ohos/Compressor.java b/adapter/ohos/Compressor.java index 3382df1b53243e407c524d1509a8f3174277e90d..07621daa0b49bab729ba6de57228469f6192963f 100644 --- a/adapter/ohos/Compressor.java +++ b/adapter/ohos/Compressor.java @@ -115,7 +115,7 @@ public class Compressor { private static final String SHA_256 = "SHA-256"; private static final Integer ONE = 1; private static final String ATOMIC_SERVICE = "atomicService"; - + private static final String JSON_SUFFIX = ".json"; // set timestamp to get fixed MD5 private static final long FILE_TIME = 1546272000000L; private static final int ENTRY_FILE_LIMIT_DEFAULT = 2; @@ -357,8 +357,37 @@ public class Compressor { } } + private static boolean hasGenerateBuildHash(Utility utility) throws BundleException { + File file = new File(utility.getJsonPath()); + if (!file.exists()) { + LOG.error("Compressor::hasGenerateBuildHash failed for json file not exist"); + throw new BundleException("Compressor::hasGenerateBuildHash failed for json file not exist"); + } + InputStream json = null; + boolean res = false; + try { + json = new FileInputStream(file); + JSONObject jsonObject = JSON.parseObject(json, JSONObject.class); + if (!jsonObject.containsKey(APP) || !jsonObject.containsKey(MODULE)) { + LOG.error("json file is invalid."); + throw new BundleException("json file is invalid."); + } + JSONObject appJson = jsonObject.getJSONObject(APP); + JSONObject moduleJson = jsonObject.getJSONObject(MODULE); + if (appJson.containsKey(GENERATE_BUILD_HASH) || moduleJson.containsKey(GENERATE_BUILD_HASH)) { + res = true; + } + } catch (BundleException | IOException exception) { + LOG.error("Compressor::hasGenerateBuildHash failed."); + throw new BundleException("Compressor::hasGenerateBuildHash failed."); + } finally { + FileUtils.closeStream(json); + } + return res; + } + private static void setGenerateBuildHash(Utility utility) throws BundleException { - if (utility.isBuildHashFinish()) { + if (utility.isBuildHashFinish() || !hasGenerateBuildHash(utility)) { return; } copyFileToTempDir(utility); @@ -449,11 +478,6 @@ public class Compressor { private static void buildHash(Utility utility) throws BundleException { if (utility.isBuildHashFinish() || (!utility.getGenerateBuildHash())) { - deleteTempFileForHash(utility); - return; - } - // check whether a hash value needs to be generated - if (!utility.getGenerateBuildHash()) { return; } String filePath = utility.getOutPath(); @@ -466,20 +490,8 @@ public class Compressor { } } - private static void deleteTempFileForHash(Utility utility) throws BundleException { - try { - String jsonPath = utility.getJsonPath(); - File jsonFile = new File(jsonPath); - File tempFile = jsonFile.getParentFile(); - deleteFile(tempFile.getCanonicalPath()); - } catch (IOException e) { - LOG.error("Compressor::deleteTempFileForHash failed, " + e.getMessage()); - throw new BundleException("Compressor::deleteTempFileForHash failed."); - } - } - private static byte[] checkSum(String filename) throws BundleException { - try (InputStream fis = new FileInputStream(filename)){ + try (InputStream fis = new FileInputStream(filename)) { byte[] buffer = new byte[BUFFER_BYTE_SIZE]; MessageDigest complete = MessageDigest.getInstance(SHA_256); int numRead; @@ -1782,8 +1794,7 @@ public class Compressor { try { String entryName = (baseDir + srcFile.getName()).replace(File.separator, LINUX_FILE_SEPARATOR); ZipEntry zipEntry = new ZipEntry(entryName); - String srcName = srcFile.getName().toLowerCase(Locale.ENGLISH); - if (CONFIG_JSON.equals(srcName) || MODULE_JSON.equals(srcName)) { + if (srcFile.getName().toLowerCase(Locale.ENGLISH).endsWith(JSON_SUFFIX)) { zipEntry.setMethod(ZipEntry.STORED); jsonSpecialProcess(utility, srcFile, zipEntry); return;