diff --git a/src/main/java/net/geedge/asw/module/app/service/impl/PackageServiceImpl.java b/src/main/java/net/geedge/asw/module/app/service/impl/PackageServiceImpl.java index bcaa267..9f862fa 100644 --- a/src/main/java/net/geedge/asw/module/app/service/impl/PackageServiceImpl.java +++ b/src/main/java/net/geedge/asw/module/app/service/impl/PackageServiceImpl.java @@ -25,15 +25,10 @@ import org.springframework.core.io.Resource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.io.BufferedReader; import java.io.File; -import java.io.InputStream; -import java.io.InputStreamReader; import java.nio.file.Path; import java.util.List; import java.util.Map; -import java.util.zip.ZipEntry; -import java.util.zip.ZipFile; @Service public class PackageServiceImpl extends ServiceImpl implements IPackageService { @@ -98,27 +93,12 @@ public class PackageServiceImpl extends ServiceImpl i entity.setVersion(apkInfo.getVersionName()); entity.setIdentifier(apkInfo.getPackageName()); } else { - ZipFile zipFile = new ZipFile(destination); - ZipEntry entry = zipFile.getEntry("manifest.json"); - InputStream inputStream = zipFile.getInputStream(entry); - StringBuilder manifestJson = new StringBuilder(); - BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); - String line; - while ((line = reader.readLine()) != null) { - manifestJson.append(line).append("\n"); - } - Map manifest = T.JSONUtil.toBean(manifestJson.toString(), Map.class); - ZipEntry packageFile = zipFile.getEntry(T.StrUtil.concat(true,T.MapUtil.getStr(manifest, "package_name"),".apk")); - File temp = T.FileUtil.file(Constants.TEMP_PATH, packageFile.getName()); - FileUtil.writeBytes(zipFile.getInputStream(packageFile).readAllBytes(), temp); - ApkInfo apkInfo = apkUtil.parseApk(temp.getPath()); - String versionName = apkInfo.getVersionName(); - String packageName = apkInfo.getPackageName(); - if (!T.BooleanUtil.and(versionName.equals(T.MapUtil.getStr(manifest, "version_name")), packageName.equals(T.MapUtil.getStr(manifest, "package_name")))){ + ApkInfo apkInfo = apkUtil.parseXapk(destination.getPath()); + if (T.ObjectUtil.isNull(apkInfo)) { throw new ASWException(RCode.PACKAGE_FILE_TYPE_ERROR); } - entity.setVersion(versionName); - entity.setIdentifier(packageName); + entity.setVersion(apkInfo.getSdkVersion()); + entity.setIdentifier(apkInfo.getPackageName()); } } catch (Exception e) { log.error(e, "[savePackage] [save package error] [file: {}]", fileResource.getFilename()); diff --git a/src/main/java/net/geedge/asw/module/app/util/ApkUtil.java b/src/main/java/net/geedge/asw/module/app/util/ApkUtil.java index 1d34e79..b969591 100644 --- a/src/main/java/net/geedge/asw/module/app/util/ApkUtil.java +++ b/src/main/java/net/geedge/asw/module/app/util/ApkUtil.java @@ -1,9 +1,14 @@ package net.geedge.asw.module.app.util; +import cn.hutool.core.io.FileUtil; import cn.hutool.log.Log; +import net.geedge.asw.common.util.Constants; import net.geedge.asw.common.util.T; import java.io.*; +import java.util.Map; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; public class ApkUtil { @@ -60,6 +65,9 @@ public class ApkUtil { while ((temp = bufferedReader.readLine()) != null) { setApkInfoProperty(apkInfo, temp); } + if (T.StrUtil.isBlank(apkInfo.getPackageName()) || T.StrUtil.isBlank(apkInfo.getVersionName())) { + return null; + } return apkInfo; } catch (IOException e) { log.error(e, "[parseApk] [error] [path: {}]", apkPath); @@ -73,6 +81,43 @@ public class ApkUtil { } } + public ApkInfo parseXapk(String xapkPath) { + InputStream inputStream = null; + BufferedReader reader = null; + File tempFile = null; + try { + ZipFile zipFile = new ZipFile(T.FileUtil.file(xapkPath)); + ZipEntry entry = zipFile.getEntry("manifest.json"); + inputStream = zipFile.getInputStream(entry); + StringBuilder manifestJson = new StringBuilder(); + reader = new BufferedReader(new InputStreamReader(inputStream)); + String line; + while ((line = reader.readLine()) != null) { + manifestJson.append(line).append("\n"); + } + Map manifest = T.JSONUtil.toBean(manifestJson.toString(), Map.class); + ZipEntry packageFile = zipFile.getEntry(T.StrUtil.concat(true, T.MapUtil.getStr(manifest, "package_name"), ".apk")); + tempFile = T.FileUtil.file(Constants.TEMP_PATH, packageFile.getName()); + FileUtil.writeBytes(zipFile.getInputStream(packageFile).readAllBytes(), tempFile); + ApkInfo apkInfo = this.parseApk(tempFile.getPath()); + if (apkInfo == null) { + return null; + } + if (!T.BooleanUtil.and(apkInfo.getVersionName().equals(T.MapUtil.getStr(manifest, "version_name")), + apkInfo.getPackageName().equals(T.MapUtil.getStr(manifest, "package_name")))) { + return null; + } + return apkInfo; + } catch (Exception e) { + log.error(e, "[parseXapk] [error] [path: {}]", xapkPath); + return null; + } finally { + T.FileUtil.del(tempFile); + T.IoUtil.close(inputStream); + T.IoUtil.close(reader); + } + } + private void setApkInfoProperty(ApkInfo apkInfo, String source) { if (source.startsWith(APPLICATION)) { String[] rs = source.split("( icon=')|'");