使用java.util.zip压缩apk目录为apk后不能被系统识别,用winrar却可以有人遇到吗?

SouthMa 2017-05-03 03:04:42
我想用java写一个apk打包和签名工具。用java.util.zip压缩了apk目录为一个zip文件,然后改后缀为.apk,然后用jarsigner签名,却发现签名后的apk用adb install安装到手机图标无法识别,并且打开后找不到assets下的资源,但是assets中确实是有文件的。
如果换成用winrar压缩成zip,重复上述步骤却没有问题,求解,有人遇到吗?
classes.dex是可以正常访问的,但是资源都找不到了。总不能调用cmd用winrar实现这部分功能吧。


压缩代码如下:
/**压缩
* @param sourcePath
* @param zipPath
*/
public static void zip(String sourcePath, String zipPath){
try {
OutputStream os = new FileOutputStream(zipPath);
BufferedOutputStream bos = new BufferedOutputStream(os);
ZipOutputStream zos = new ZipOutputStream(bos);
File file = new File(sourcePath);
String basePath = null;
if(file.isDirectory()){//要压缩的是文件夹
basePath = file.getPath();
}else{
basePath = file.getParent();
}
zipFile(file, basePath, zos);
zos.closeEntry();
zos.close();
bos.close();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}

private static void zipFile(File sourceFile, String basePath, ZipOutputStream zos) {
File[] files = new File[0];
if(sourceFile.isDirectory()){
files = sourceFile.listFiles();
}else{
files = new File[1];
files[0] = sourceFile;
}
byte[] buffer = new byte[1024];
int length = 0;
try {
for(File file : files){
if(file.isDirectory()){
String pathName = file.getPath().substring(basePath.length() + 1) + "/";
zos.putNextEntry(new ZipEntry(pathName));
zipFile(file, basePath, zos);//迭代
}else{
String pathName = file.getPath().substring(basePath.length() + 1);
zos.putNextEntry(new ZipEntry(pathName));
InputStream is = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
while((length = bis.read(buffer)) > 0){
zos.write(buffer, 0, length);
}
is.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
...全文
165 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
SouthMa 2017-05-03
  • 打赏
  • 举报
回复
无人能解呀,暂时通过java调用cmd执行winrar指令的方法做了,就是不太优雅,要配置winrar.exe的路径 private static void packageApkFiles(String unzipFilePath, String newAppPath) throws IOException, InterruptedException { // TODO Auto-generated method stub String winRARPath = getConfig().winRARPath; String zipCommand = "cd "+unzipFilePath +" && \""+winRARPath+"\" a -r "+newAppPath+" ./*"; System.out.println("cmd:"+zipCommand); SystemCommand.execute(zipCommand); }

80,359

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧