【问题标题】:I'm trying to search a '.class' file within jar files using Python我正在尝试使用 Python 在 jar 文件中搜索“.class”文件
【发布时间】:2015-04-08 14:34:47
【问题描述】:
#! /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 脚本不会返回任何内容。 如何使上述脚本通过罐子扫描?

【问题讨论】:

  • 您必须取消归档 jar。扫描解压后的文件夹,返回路径……必要时删除文件夹

标签: python python-2.7 jar scripting ipython


【解决方案1】:

Jars 类似于 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 文件。

【讨论】:

    【解决方案2】:

    文件名: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
    

    您可以双击 CallSearchForFiles.bat 轻松运行。

    【讨论】:

      【解决方案3】:
      #! /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 .. 为你们干杯,我终于编写了完成最后工作的上述脚本。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多