1. 程式人生 > 程式設計 >用python解壓分析jar包例項

用python解壓分析jar包例項

寫這個玩意的背景:在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包例項就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。