用python解压分析jar包实例

yipeiwu_com6年前Python基础

写这个玩意的背景:在u8多渠道打包里,需要分析jar包,并把里面的文件按目录和类型分别放在root和assets文件夹里,之前师兄都是手动解压,一个一个文件夹找文件,效率比较低,刚好最近手上的android项目已经做完了,就决定写一个自动化分析jar文件并复制粘贴到指定文件夹的脚本。

# -*- coding: utf-8 -*-
import os
import shutil
import zipfile
 
count = 1
def getSumDir():
  sumfilelist = os.listdir(os.getcwd())
  for dir in sumfilelist:
    if ".idea" not in dir:
      classify(dir)
 
 
def getlibDir():
  sumfilelist = os.listdir(os.getcwd())
  for dir in sumfilelist:
    if "libs" in dir:
      jieyajar(dir)
 
 
def jieyajar(dir):
  files = os.listdir(dir)
  for jars in files:
    if "jar" in jars:
      zfile = zipfile.ZipFile('libs/' + jars, 'r')
      if not os.path.exists(os.getcwd() + '/jarlog/' + jars):
        os.makedirs(os.getcwd() + '/jarlog/' + jars)
      zfile.extractall(os.getcwd() + '/jarlog/' + jars)
 
      if not os.path.exists(os.getcwd() + '/jars/'):
        os.makedirs(os.getcwd() + '/jars/')
      zfile.extractall(os.getcwd() + '/jars/')
      zfile.close
 
  for file in os.listdir(os.getcwd()):
    if "jars" in file:
      classify(file)
 
 
# def classify(path):
#   if os.path.isfile(path):
#     if ".class" not in path:
#       if "assets" in os.path.dirname(path):
#         if not os.path.exists(os.path.dirname(os.getcwd()) + '/assets/' + os.path.dirname(path)):
#           os.makedirs(os.path.dirname(os.getcwd()) + '/assets/' + os.path.dirname(path))
#         shutil.copy(path, os.path.dirname(os.getcwd()) + '/assets/' + os.path.dirname(path))
#       else:
#         if not os.path.exists(os.path.dirname(os.getcwd())+'/root/'+os.path.dirname(path)):
#          os.makedirs(os.path.dirname(os.getcwd())+'/root/'+os.path.dirname(path))
#         shutil.copy(path,os.path.dirname(os.getcwd())+'/root/'+os.path.dirname(path))
#   else :
#     list = os.listdir(path)
#     for dir in list:
#       classify(path+"/"+dir)
 
def classify(path):
  global count
  if os.path.isfile(path):
    if ".class" not in path:
      if not os.path.exists(os.getcwd() + '/root/' + os.path.dirname(path)):
        os.makedirs(os.getcwd() + '/root/' + os.path.dirname(path))
      shutil.copy(path, os.getcwd() + '/root/' + os.path.dirname(path))
  else:
    if 'assets' in path and count == 1:
      count = count + 1
      shutil.copytree(os.getcwd()+'/'+path, os.getcwd() + '/assets')
    elif 'META-INF' not in path:
      list = os.listdir(path)
      for dir in list:
        classify(path + "/" + dir)
 
 
# getSumDir()
getlibDir()

嗯,主要就是对文件夹和文件的操作。。

以上这篇用python解压分析jar包实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python 对给定可迭代集合统计出现频率,并排序的方法

给定一个可迭代sequence,对其中的值进行出现次数统计: 方法1: def get_counts(sequence): counts = {} for x in sequen...

对pycharm 修改程序运行所需内存详解

编辑PyCharm安装目录下PyCharm 4.5.3\bin下的pycharm.exe.vmoptions文件, 如下 -server -Xms128m -Xmx512m -XX:...

详解python实现小波变换的一个简单例子

详解python实现小波变换的一个简单例子

最近工作需要,看了一下小波变换方面的东西,用python实现了一个简单的小波变换类,将来可以用在工作中。 简单说几句原理,小波变换类似于傅里叶变换,都是把函数用一组正交基函数展开,选取不...

Pytorch.nn.conv2d 过程验证方式(单,多通道卷积过程)

Pytorch.nn.conv2d 过程验证方式(单,多通道卷积过程)

今天在看文档的时候,发现pytorch 的conv操作不是很明白,于是有了一下记录 首先提出两个问题: 1.输入图片是单通道情况下的filters是如何操作的? 即一通道卷积核卷积过程...

对python使用telnet实现弱密码登录的方法详解

系统环境: 64位win7企业版 python2.7.10 2016.08.16修改内容: 1)read_until()函数是可以设置timeout的,之前不能获取到password之后...