Open-source Languages & Tools for z/OS

 View Only
  • 1.  Zipfile throws error in python 2.7

    Posted 11-11-2018 14:16

    Hi Folks,

    I seem to be getting a ‘BadZipfile’ error when I try to execute the following against any zip file.

    import zipfile
    if __name__=='__main__':
        zipped_path = './test.zip'
        z = zipfile.ZipFile(zipped_path)
    

    Gives the following -

    Traceback (most recent call last):
      File "zip.py", line 5, in <module>
        pdb.run('zipfile.ZipFile(zipped_path)')
      File "/etc/rocket/python-2017-04-12-py27/lib/python2.7/pdb.py", line 1238, in run
        Pdb().run(statement, globals, locals)
      File "/etc/rocket/python-2017-04-12-py27/lib/python2.7/bdb.py", line 400, in run
        exec cmd in globals, locals
      File "<string>", line 1, in <module>
      File "/test_python/zipfile.py", line 766, in __init__
        self._RealGetContents()
      File "/test_python/zipfile.py", line 807, in _RealGetContents
        raise BadZipfile, "File is not a zip file"
    zipfile.BadZipfile: File is not a zip file
    

    Here is some info about my environment -

    >echo $_BPXK_AUTOCVT
    ON
    
    >echo $_CEE_RUNOPTS
    FILETAG(AUTOCVT,AUTOTAG) POSIX(ON)
    
    >python --version
    Python 2.7.6
    

    The same zip file (test.zip) is read properly with Rocket’s unzip under USS. I looked into why zipfile was throwing the error and it looks like its comparing a string that its reading from ‘test.zip’ that is represented as ASCII with a string hardcoded in zipfile that is represented as EBCDIC. Here is some output from the debugger that illustrates what I mean -

    > <string>(1)<module>()
    (Pdb) b zipfile:219
    Breakpoint 1 at /test_python/zipfile.py:219
    (Pdb) c
    > /test_python/zipfile.py(219)_EndRecData()
    -> if (len(data) == sizeEndCentDir and
    (Pdb) data[0]
    '&'
    (Pdb) data[0].encode('hex')
    '50'
    (Pdb) stringEndArchive[0]
    'P'
    (Pdb) stringEndArchive[0].encode('hex')
    'd7'
    

    Any recommendations on how I can fix this?

    Thanks,
    Aaron



  • 2.  RE: Zipfile throws error in python 2.7

    Posted 11-13-2018 06:39

    Have you tried with the newer port Python 2.7.13 ?

    This works with ASCII internally rather than with EBCDIC.

    There is a chance that this fixes the problem.



  • 3.  RE: Zipfile throws error in python 2.7

    Posted 11-15-2018 19:58

    Thanks, that worked!