Java实现解压Apk、往apk中增加文件
2016-01-15 14:14
435 查看
import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.util.Enumeration; import org.apache.commons.compress.archivers.ArchiveInputStream; import org.apache.commons.compress.archivers.ArchiveStreamFactory; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; import org.apache.commons.compress.archivers.zip.ZipFile; /** * @Title: Apk.java * @Description: 解压Apk、往apk中增加文件的 实现 * @author xieyuan 734584250@qq.com * @date 2016-1-15 上午9:54:17 */ /** * @author xieyuan */ public class ApkUtils { private void copy(InputStream inputStream, OutputStream outputStream) throws IOException{ byte[] b = new byte[2048]; int len = 0; while((len = inputStream.read(b)) != -1){ outputStream.write(b, 0, len); } } /** * 往Apk中byte[] * @param apkPath * @param outputPath * @param addFilePath 文件路径 * @param entryName 希望在apk中显示的路径、名称 */ public void addByteToApk(String apkPath,String outputPath,byte[] addFileBytes,String entryName){ File file = new File(apkPath); File outputFile = new File(outputPath); if(!file.exists()){ System.out.println("not found "+apkPath); return; } if(outputFile.exists()){ outputFile.delete(); } try { ZipArchiveOutputStream zipArchiveOutputStream = new ZipArchiveOutputStream(outputFile); ZipFile zipFile = new ZipFile(file); Enumeration<ZipArchiveEntry> enumeration = zipFile.getEntries(); //原样拷贝 while (enumeration.hasMoreElements()) { ZipArchiveEntry zipArchiveEntry = enumeration.nextElement(); if(zipArchiveEntry.getName().equals(entryName)){ System.out.println("Wranning! found "+entryName+" cover it !"); continue; } zipArchiveOutputStream.putArchiveEntry(zipArchiveEntry); if(!zipArchiveEntry.isDirectory()){ copy(zipFile.getInputStream(zipArchiveEntry), zipArchiveOutputStream); } zipArchiveOutputStream.closeArchiveEntry(); } //添加文件 ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(entryName); zipArchiveOutputStream.putArchiveEntry(zipArchiveEntry); zipArchiveOutputStream.write(addFileBytes); zipArchiveOutputStream.closeArchiveEntry(); zipFile.close(); zipArchiveOutputStream.close(); } catch (Exception e) { e.printStackTrace(); } } public void addStrToApk(String apkPath,String outputPath,String addStr,String entryName){ try { addByteToApk(apkPath, outputPath, addStr.getBytes("utf-8"), entryName); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 往Apk中添加文件 * @param apkPath * @param outputPath * @param addFilePath 文件路径 * @param entryName 希望在apk中显示的路径、名称 */ public void addFileToApk(String apkPath,String outputPath,String addFilePath,String entryName){ try { File addFile = new File(addFilePath); FileInputStream fileInputStream = new FileInputStream(addFile); BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream); ByteArrayOutputStream bos = new ByteArrayOutputStream(); int len =0; byte[] buffer = new byte[2048]; while((len = bufferedInputStream.read(buffer)) != -1){ bos.write(buffer, 0 ,len); } bos.flush(); bufferedInputStream.close(); fileInputStream.close(); addByteToApk(apkPath, outputPath, bos.toByteArray(), entryName); } catch (IOException e) { // TODO: handle exception } } /** * 解压apk * @param apkPath * @param outPath */ public void unZipApk(String apkPath,String outPath){ File file = new File(apkPath); if(!file.exists()){ System.out.println("not found "+apkPath); return; } try { File outputDir = new File(outPath); outputDir.mkdir(); BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file)); ArchiveInputStream inputStream = new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.ZIP, bufferedInputStream); ZipArchiveEntry zipArchiveEntry = null; try { while((zipArchiveEntry = (ZipArchiveEntry) inputStream.getNextEntry()) != null){ if(zipArchiveEntry.isDirectory()){ new File(outputDir,zipArchiveEntry.getName()).mkdirs(); }else{ File tempFile = new File(outputDir,zipArchiveEntry.getName()); if(!tempFile.getParentFile().exists()){ tempFile.getParentFile().mkdirs(); } FileOutputStream fos = null; try { fos = new FileOutputStream(tempFile); //IOUtils.copy(inputStream, fos); byte[] b = new byte[2048]; int len = 0; while((len = inputStream.read(b)) != -1){ fos.write(b, 0, len); } fos.flush(); } catch (IOException e) { e.printStackTrace(); }finally{ if(fos != null){ fos.close(); } } } } } catch (Exception e) { e.printStackTrace(); }finally{ if(inputStream != null){ inputStream.close(); } } System.out.println("unZipApk done !"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args){ ApkUtils apkUtils = new ApkUtils(); //apkUtils.unZipApk("d:\\Administrator\\Desktop\\ApkIDE_ApkIDE_Apk.apk", "d:\\Administrator\\Desktop\\ApkIDE_ApkIDE_Apk"); apkUtils.addStrToApk("d:\\Administrator\\Desktop\\ApkIDE_ApkIDE_Apk.apk", "d:\\Administrator\\Desktop\\new.apk", "d:\\Administrator\\Desktop\\DEV信息APP版本-cyl(4).doc", "DEV信息APP版本-cyl(4).txt"); } }
相关文章推荐
- java 后台 如何实现 谷歌 消息推送
- spring整合Hibernate事务不能自动回滚
- java 收集正则表达式
- jave中的Random中rand.nextInt(int n )的取值范围
- 无法删除Java(TM) Platform SE binary打开问题解决(修改jar中的文件)
- Java创建和解析Json数据方法(五)——Google Gson包的使用
- Spring4.0学习笔记001——搭建Spring开发环境
- mysql , java 中文乱码如何解决
- [转]java反射机制
- JAVA动态(可变)参数
- Spring tool Suite 安装配置+maven安装配置,搭建maven项目
- Spring2.5.6定时器的使用
- spring事务管理获取不到事务(transaction)的可能
- java JPA之EntityManager
- Spring MVC中的视图解析ViewResolver
- java图像
- java 对象占用内存
- java搜集电脑cpu、内存、磁盘空间信息
- SpringMVC mybatis or hibernate ehcache二级缓存maven非和maven版本
- netbeans提示后光标消失