`

Java之apk 解压、修改、打包、签名(2)

阅读更多
简介: 
    上篇文章http://showlike.iteye.com/admin/blogs/1688679中,通过Runtime.getRuntime().exec 调用命令的方式对APK进行 解压、打包、签名。此文不同之处在于应用java.util.zip对APK进行解压、打包,感觉说得有点多,直接上代码。


代码实现:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Enumeration;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipOutputStream;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;

/**
 * @author showlike
 * @version v1.0 
 * 2012-9-28 上午11:03:36 
 * explain : 文件解压/打包工具
 */
public class ZipUtil {
	private static final int BUFFER = 1024; 
	private static final String BASE_DIR = "";
	/**符号"/"用来作为目录标识判断符*/
	private static final String PATH = "/";
	
	/**签名目录*/
	private static final String SIGN_PATH_NAME = "META-INF";	
	/**修改文件目录*/
	private static final String UPDATE_PATH_NAME = "\\res\\raw\\channel";
	/**解压源文件目录*/
	private static final String SOURCE_PATH_NAME = "\\source\\";	
	/**打包目录*/
	private static final String TARGET_PATH_NAME = "\\target\\";
	/**签名目录*/
	private static final String RESULT_PATH_NAME = "\\result\\";
	/**JDK BIN 目录*/
	private static final String JDK_BIN_PATH = "C:\\Program Files\\Java\\jdk1.6.0_26\\bin";
	/**密钥 目录*/
	private static final String SECRET_KEY_PATH = "F:\\document\\APK\\";
	/**密钥 名称*/
	private static final String SECRET_KEY_NAME = "sdk.keystore";
	
	/** 
    * 解压缩zip文件  
    * @param fileName 要解压的文件名 包含路径 如:"c:\\test.zip" 
    * @param filePath 解压后存放文件的路径 如:"c:\\temp" 
    * @throws Exception 
    */  
    @SuppressWarnings("rawtypes")
	public static void unZip(String fileName, String filePath) throws Exception{  
       ZipFile zipFile = new ZipFile(fileName);   
       Enumeration emu = zipFile.getEntries();
        
       while(emu.hasMoreElements()){  
            ZipEntry entry = (ZipEntry) emu.nextElement();  
            if (entry.isDirectory()){  
                new File(filePath+entry.getName()).mkdirs();  
                continue;  
            }  
            BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));  
             
            File file = new File(filePath + entry.getName());  
            File parent = file.getParentFile();  
            if(parent != null && (!parent.exists())){  
                parent.mkdirs();  
            }  
            FileOutputStream fos = new FileOutputStream(file);  
            BufferedOutputStream bos = new BufferedOutputStream(fos,BUFFER);  
      
            byte [] buf = new byte[BUFFER];  
            int len = 0;  
            while((len=bis.read(buf,0,BUFFER))!=-1){  
                fos.write(buf,0,len);  
            }  
            bos.flush();  
            bos.close();  
            bis.close();  
           }  
           zipFile.close();  
    }  
    
	/**
	 * 压缩文件
	 * 
	 * @param srcFile
	 * @param destPath
	 * @throws Exception
	 */
	public static void compress(String srcFile, String destPath) throws Exception {
		compress(new File(srcFile), new File(destPath));
	}
	
	/**
	 * 压缩
	 * 
	 * @param srcFile
	 *            源路径
	 * @param destPath
	 *            目标路径
	 * @throws Exception
	 */
	public static void compress(File srcFile, File destFile) throws Exception {
		// 对输出文件做CRC32校验
		CheckedOutputStream cos = new CheckedOutputStream(new FileOutputStream(
				destFile), new CRC32());

		ZipOutputStream zos = new ZipOutputStream(cos);
		compress(srcFile, zos, BASE_DIR);

		zos.flush();
		zos.close();
	}
	
	/**
	 * 压缩
	 * 
	 * @param srcFile
	 *            源路径
	 * @param zos
	 *            ZipOutputStream
	 * @param basePath
	 *            压缩包内相对路径
	 * @throws Exception
	 */
	private static void compress(File srcFile, ZipOutputStream zos,
			String basePath) throws Exception {
		if (srcFile.isDirectory()) {
			compressDir(srcFile, zos, basePath);
		} else {
			compressFile(srcFile, zos, basePath);
		}
	}
	
	/**
	 * 压缩目录
	 * 
	 * @param dir
	 * @param zos
	 * @param basePath
	 * @throws Exception
	 */
	private static void compressDir(File dir, ZipOutputStream zos,
			String basePath) throws Exception {
		File[] files = dir.listFiles();
		// 构建空目录
		if (files.length < 1) {
			ZipEntry entry = new ZipEntry(basePath + dir.getName() + PATH);

			zos.putNextEntry(entry);
			zos.closeEntry();
		}
		
		String dirName = "";
		String path = "";
		for (File file : files) {
			//当父文件包名为空时,则不把包名添加至路径中(主要是解决压缩时会把父目录文件也打包进去)
			if(basePath!=null && !"".equals(basePath)){
				dirName=dir.getName(); 
			}
			path = basePath + dirName + PATH;
			// 递归压缩
			compress(file, zos, path);
		}
	}

	/**
	 * 文件压缩
	 * 
	 * @param file
	 *            待压缩文件
	 * @param zos
	 *            ZipOutputStream
	 * @param dir
	 *            压缩文件中的当前路径
	 * @throws Exception
	 */
	private static void compressFile(File file, ZipOutputStream zos, String dir)
			throws Exception {
		/**
		 * 压缩包内文件名定义
		 * 
		 * <pre>
		 * 如果有多级目录,那么这里就需要给出包含目录的文件名
		 * 如果用WinRAR打开压缩包,中文名将显示为乱码
		 * </pre>
		 */
		if("/".equals(dir))dir="";
		else if(dir.startsWith("/"))dir=dir.substring(1,dir.length());
		
		ZipEntry entry = new ZipEntry(dir + file.getName());
		zos.putNextEntry(entry);
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
		int count;
		byte data[] = new byte[BUFFER];
		while ((count = bis.read(data, 0, BUFFER)) != -1) {
			zos.write(data, 0, count);
		}
		bis.close();

		zos.closeEntry();
	}
	
	public static void main(String[] args)throws Exception{
		StringBuffer buffer = new StringBuffer();
		BufferedReader br =null;
		OutputStreamWriter osw =null;
		String srcPath = "F:\\document\\APK\\new\\iGouShop.apk";
		String content= "channel_id=LD20120926";
		
		File srcFile = new File(srcPath);
		String parentPath = srcFile.getParent();	//源文件目录
		String fileName = srcFile.getName();		//源文件名称
		String prefixName = fileName.substring(0, fileName.lastIndexOf("."));
		//解压源文件保存路径
		String sourcePath = buffer.append(parentPath).append(SOURCE_PATH_NAME).
								append(prefixName).append("\\").toString();
		
		//------解压
		unZip(srcPath, sourcePath);
		
		//------删除解压后的签名文件
		String signPathName = sourcePath+SIGN_PATH_NAME;
		File signFile = new File(signPathName);
		if(signFile.exists()){
			File sonFiles[] = signFile.listFiles();
			if(sonFiles!=null && sonFiles.length>0){
				//循环删除签名目录下的文件
				for(File f : sonFiles){
					f.delete();
				}
			}
			signFile.delete();
		}
		
		//------修改内容
		buffer.setLength(0);
		String path = buffer.append(parentPath).append(SOURCE_PATH_NAME)
				.append(prefixName).append(UPDATE_PATH_NAME).toString();
		br = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
		while((br.readLine())!=null)
		{
			osw = new OutputStreamWriter(new FileOutputStream(path));  
			osw.write(content,0,content.length());  
			osw.flush();  
		}
		
		//------打包
		String targetPath = parentPath+TARGET_PATH_NAME;
		//判断创建文件夹
		File targetFile = new File(targetPath);
		if(!targetFile.exists()){
			targetFile.mkdir();
		}
		compress(parentPath+SOURCE_PATH_NAME+prefixName,targetPath+fileName);
		
		//------签名
		File ff =new File(JDK_BIN_PATH);
		String resultPath = parentPath+RESULT_PATH_NAME;
		//判断创建文件夹
		File resultFile = new File(resultPath);
		if(!resultFile.exists()){
			resultFile.mkdir();
		}
		
		//组合签名命令
		buffer.setLength(0);
		buffer.append("cmd.exe /c jarsigner -keystore ")
		.append(SECRET_KEY_PATH).append(SECRET_KEY_NAME)
		.append(" -storepass winadsdk -signedjar ")
		.append(resultPath).append(fileName).append(" ")	//签名保存路径应用名称
		.append(targetPath).append(fileName).append(" ")	//打包保存路径应用名称
		.append(SECRET_KEY_NAME);
		//利用命令调用JDK工具命令进行签名
		Process process = Runtime.getRuntime().exec(buffer.toString(),null,ff);
		if(process.waitFor()!=0)System.out.println("文件打包失败!!!");
	}
}


注:此实现在应用解压上有些不能通过,但在解压我司应用时未出现该异常,原因未能解决,希望大牛们给予指点。

关于java zip推荐博文:http://snowolf.iteye.com/blog/465433#bc2283658
0
0
分享到:
评论
1 楼 eleven20070525 2014-12-16  
为什么一直报无法找到文件 java.io.FileNotFoundException 异常

相关推荐

    apk-tool-1.52

    使用 1).解压APK D:\My Documents\Desktop\... 对新产生的xxx1.apk 进行签名,在签名之前,请将原生的xxx.apk 更改为其他名字 或删除。 $ java -jar signapk.jar testkey.x509.pem testkey.pk8 xxx1.apk xxx.apk

    APK反编译.zip

    还可以将反编译之后的apk重新打包成apk文件,但需要重新签名,才能安装使用。 二、看源码 2、dex2jar 官方下载地址:https://sourceforge.net/projects/dex2jar/ 作用:将APK直接解压后,目录下包含的一个classes....

    安卓反编译,打包,签名助手

    安卓逆向助手,快速反编译、打包、签名、编辑 安卓软件 使用方法:首先解压,然后把得到的文件夹放入任意目录,再把那个快捷方式拖至桌面,然后就可以启动了。(需要安装Java)

    apktoolmv2.4.0_downcc.com.apk

    2.再打开 apks 合并分包 就能把apks打包成apk 提取出来到别的设备安装了 主要功能 Apktool-反编译和编译Android?安装包(* .apk),包括系统应用程序。 包括自动模式在内 的应用程序翻译(支持100多种语言)。 已...

    ROM开发工具箱 4.01正式版

    * 全新的解压打包方式,无需再复制大文件到指定目录。 * 一键安装JAVA脚本升至2.0 * 修复使用获取分区大小、APK/ZIP签名等功能导致程序无响应的问题 * 修复加入文件后导致boot/ramdisk/recovery无法解压或打包的问题...

    APKTool批处理版l

    改完了strings.xml,下面就是打包看看效果了……有了leasea大侠的批处理,就很简单了,什么都不用管,双击运行“打包签名.BAT”就可以了。 如果出现的是上面的信息,那么恭喜你,过关了…… 如果有多余的信息,并...

    ROM开发工具箱4.0测试版

    * 全新的解压打包方式,无需再复制大文件到指定目录。 * 一键安装JAVA脚本升至2.0 * 修复使用获取分区大小、APK/ZIP签名等功能导致程序无响应的问题

    ionic混合开发APP

    3.1.3.2 生产release版apk(签名后安装) 3.1.3.3 生成debug版apk 3.1.4 签名 3.1.4.1 生成签名文件 keytool -genkey -v -keystore testapp.keystore -alias testapp.keystore -keyalg RSA -validity ...

    网狐荣耀版开发使用常见问题解答

    pc端与安卓端相对简单,苹果端的修改、发布与打包成了多数开发者的一个主要问题。下面就说说这个苹果打包的基本过程。 一、环境准备 macos电脑一台,安装xcodes开发工作,同时安装证书服务器与相关软件环境。 ...

Global site tag (gtag.js) - Google Analytics