蟒蛇。无法打开ZipFile

时间:2016-07-22 12:19:50

标签: python python-3.x

我有一组*.tar.xz档案。它们中的每一个都可能包含APK或JAR文件,这些文件确实是zip存档。我正在尝试在zip档案的内容中搜索某些模式。我使用下一个代码来完成它:

#! /usr/bin/env python3

import os
import glob
import tarfile
import shutil
import zipfile

def check(filename):
    if 'my_awesome_pattern' in open(file).read():
        print('matches')

def process_zip(f):
    z = zipfile.ZipFile(f, 'r')     # <- here problem occurs
    z.extractall('tmp')
    z.close()  

def process_jar(file):
    print('JAR')
    process_zip(file)

def process_apk(file):
    print('APK')
    process_zip(file)

def process_xml(file):
    print('XML')
    check(file)

def process_tar(filename):
    print(filename)
    tar = tarfile.open(filename)
    for entry in tar.getnames():
        print(">>> " + entry)
        if entry.endswith('xml'):
            tar.extract(entry)
            process_xml(entry)
            os.remove(entry)
        elif entry.endswith('jar'):
            tar.extract(entry)
            process_jar(entry)
            os.remove(entry)
        elif entry.endswith('apk'):
            tar.extract(entry)
            process_apk(entry)
            os.remove(entry)

    tar.close()


for file in glob.glob("*.tar.xz"):
    process_tar(file)

但运行时停止:

setupwizardtablet-all.tar.xz
>>> setupwizardtablet-all
>>> setupwizardtablet-all/nodpi
>>> setupwizardtablet-all/nodpi/priv-app
>>> setupwizardtablet-all/nodpi/priv-app/SetupWizard
>>> setupwizardtablet-all/nodpi/priv-app/SetupWizard/SetupWizard.apk
APK
Traceback (most recent call last):
  File "./scan.py", line 56, in <module>
    process_tar(file)
  File "./scan.py", line 49, in process_tar
    process_apk(entry)
  File "./scan.py", line 27, in process_apk
    process_zip(file)
  File "./scan.py", line 16, in process_zip
    z = zipfile.ZipFile(f, 'r')     # <- here problem occurs
  File "/usr/lib/python3.4/zipfile.py", line 937, in __init__
    self._RealGetContents()
  File "/usr/lib/python3.4/zipfile.py", line 1034, in _RealGetContents
    x._decodeExtra()
  File "/usr/lib/python3.4/zipfile.py", line 415, in _decodeExtra
    tp, ln = unpack('<HH', extra[:4])
struct.error: unpack requires a bytes object of length 4

我坚持这个错误。 Python不是我的一杯茶,所以我正在寻求帮助。

提前致谢!

0 个答案:

没有答案