我正在尝试使用 Python 在 jar 文件中搜索“.class”文件

I'm trying to search a '.class' file within jar files using Python

#! /usr/bin/python -tt
import os
def searchFile(path1,ext1,fileName1):
    pathList = []
    for root, dirs, files in os.walk(path1):
        for file in files:
            if file.endswith(ext1):
               pathList.append(os.path.join(root,file))
    print "-----The file is present under the below path------\n"
    for ele in pathList:
        if fileName1 in ele:
            print ele
def main():
    path = raw_input("Please enter the path you wish to spider. Also make sure that the files/subfolders have the correct permissions.\n")
    ext = raw_input("Enter the extension you wish to search/ find. Eg: For class files enter .class / For text file enter .txt \n")
    fileName = raw_input("Enter the filename without extension. Eg For example.class, input only 'example'\n")
    searchFile(path,ext,fileName)
if __name__ == '__main__':
main()

对于普通文件/子文件夹,它可以正确获取路径/文件名,但是当通过 'jars'、python 脚本搜索时,它不会 return 任何东西。 我怎样才能让上面的脚本扫描整个罐子?

Jar 类似于 Zip 档案。要扫描 jar 文件,您可以使用 Python 模块 zipfile 获取其内容列表,或者您甚至可以阅读内容。您可以使用Zipfile.namelist()方法获取jar中的内容列表,然后使用此列表检查您要搜索的文件是否存在。

这是获取 jar 中存在的文件列表的示例代码。

import zipfile
archive = zipfile.ZipFile('<path to jar file>/test.jar', 'r')
list = archive.namelist()

如果您将 运行 在逗号行或终端中执行此操作,您将得到如下输出:

['file1.class', 'file2.class' ]

其中 file1 和 file2 是我的 jar 文件中的两个 .class 文件。

#! /usr/bin/python -tt
import os
import time
import zipfile
def searchFile(path1,ext1,fileName1):
    pathList1 = []
    list = []
    for root, dirs, files in os.walk(path1):
        for file in files:
            if file.endswith(ext1):
               pathList1.append(os.path.join(root,file))
    print "-----All The jar files present got collected------\n"
    for ele in pathList1:
        archive = zipfile.ZipFile(ele,'r')
        list1 = archive.namelist()
        newList1 = [ele+item for item in list1]
        list = list + newList1

    print "-----Jar files unzip done------\n"
    print "----- Now fetching filename along with the path------\n"
    for ele in list:
    if fileName1 in ele:
        print ele
def main():
    path = raw_input("Please enter the path you wish to spider. Also make sure that the files/subfolders have the correct permissions.\n")
    fileName = raw_input("Enter the filename '\n")
    fileName = "/" + fileName
    searchFile(path,".jar",fileName)
if __name__ == '__main__':
    main()

@bonney @heinst .. 为你们干杯,我终于写了上面的脚本来完成最后的工作。

文件名:searchForFiles.py

import os, zipfile, glob, sys

def main():
    searchFile = sys.argv[1] #class file to search for, sent from batch file below (optional, see batch file code in second code section)
    listOfFilesInJar = []
    for file in glob.glob("*.jar"):
        archive = zipfile.ZipFile(file, 'r')
        for x in archive.namelist():
            if str(searchFile) in str(x):
                listOfFilesInJar.append(file)

    for something in listOfFilesInJar:
        print("location of "+str(searchFile)+": ",something)

if __name__ == "__main__":
    sys.exit(main())

您可以轻松 运行 通过使用以下文本制作 .bat 文件(将 "AddWorkflows.class" 替换为您正在搜索的文件):

(文件:CallSearchForFiles.bat)

@echo off
python -B -c "import searchForFiles;x=searchForFiles.main();" AddWorkflows.class
pause

你可以double-clickCallSearchForFiles.bat轻松运行它。