zipfile.BadZipfile: Bad CRC-32 for file | Read only file
Asked Answered
D

0

0

Got a read-only file within a zip file which are password protected and I need to extract it to the /tmp directory.

I get a CRC-32 error which suggests that the file would be corrupted yet I know it isn't and is in fact a read-only file. Any Suggestions?

Error:

Traceback (most recent call last):
File "/tmp/usercode.py", line 45, in <module>
zip.extractall('/tmp',pwd = "piso")
File "/usr/lib64/python2.7/zipfile.py", line 1040, in extractall
self.extract(zipinfo, path, pwd)
File "/usr/lib64/python2.7/zipfile.py", line 1028, in extract
return self._extract_member(member, path, pwd)
File "/usr/lib64/python2.7/zipfile.py", line 1084, in _extract_member
shutil.copyfileobj(source, target)
File "/usr/lib64/python2.7/shutil.py", line 49, in copyfileobj
buf = fsrc.read(length)
File "/usr/lib64/python2.7/zipfile.py", line 632, in read
data = self.read1(n - len(buf))
File "/usr/lib64/python2.7/zipfile.py", line 672, in read1
self._update_crc(data, eof=(self._compress_left==0))
File "/usr/lib64/python2.7/zipfile.py", line 647, in _update_crc
raise BadZipfile("Bad CRC-32 for file %r" % self.name)
zipfile.BadZipfile: Bad CRC-32 for file 'alien-12.txt'

Code:

# importing required modules
from zipfile import ZipFile

# specifying the zip file name
file_name = "/tmp/alien-12.zip"

# opening the zip file in READ mode
with ZipFile(file_name, 'r') as zip:
    # printing all the contents of the zip file
    zip.printdir()

    # extracting all the files
    print('Extracting all the files now...')
    zip.extractall('/tmp',pwd = "piso")
    print('Done!')

If I change the line of:

    zip.extractall('/tmp',pwd = "piso")

then I get the error of:

IOError: [Errno 30] Read-only file system: 

Then go on to try and fix it first by trying to output what is in the zip file.

zipfile.testzip() returns which then errors

Error:

 RuntimeError: File alien-12.txt is encrypted, password required for extraction
Disputable answered 12/2, 2018 at 11:20 Comment(9)
Method calls on the zip object should take place in the with block. Outside of it, the file is already closed.Glynis
@Glynis Sorry indentation doesn't show that it is, I'll fix that!Disputable
I think you also forget to include your change to the line zip.extractall('/tmp',pwd = "piso") where you wrote "If I change the line of"Glynis
What does zip.testzip() return? Did you try to pass the password with the costructor?Glynis
@Glynis Throws error for password: RuntimeError: File alien-12.txt is encrypted, password required for extractionDisputable
I'm afraid the description of the error conditions is sitll a bit unclear. There is the IOError and the RuntimeError which are caused by two versions of your script (?), but your question only contains one. I assume that the IOError with the read-only file system is either because /tmp in indeed on a read-only files system or because the zip archive contains absolute paths that point to a read-only file system.Glynis
So whats the best way extract if it is an absolute path that is read-only then. Am I doing it wrong with the above way? @GlynisDisputable
With ZipFile.read() you can extract the file into memory and save anywhere you want. But it is still guesswork whether this will elimiate the CRC error. Do you mind to edit your question for a better explantion of what you did actually try?Glynis
Let us continue this discussion in chat.Disputable

© 2022 - 2024 — McMap. All rights reserved.