使用java.util.zip压缩apk目录为apk后不能被系统识别,用winrar却可以有人遇到吗?
我想用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();
}
}