CRC test of a zipped directory says it's corrupted but i can open and read it

957 Views Asked by At

I'm building a zipped archive with some data inside filtering the files inside a directory. The file are taken and removed from the directory after the zip archive is made. I've been asked to check the archive with a crc algorithm so i used this:

test=zf.testzip()

Te test fails and the "test" variable contains the first of the files which have to be filtered and compressed by the script. So i assume the other ones are all corrupted even. The problem is i can read the data inside the arechive, they are perfectly duplicated by extracting the archive so, where is the problem? The code to make the archive is the following:

import zipfile
import os
[...]
if dozip==True:
zf = zipfile.ZipFile(zipname, "w", comprez)
for dirname, subdirs, files in os.walk(dir):
    for filename in files:
        fl=filename.split("-")
        fdate= datetime.datetime.strptime(fl[0], "%Y%m%d")
        if start <= fdate <= end:
            if fl[1] == client_name+".stat":
                zf.write(os.path.join(dirname, filename))
                if docancel==True:
                    os.remove(os.path.join(dirname, filename))
test=zf.testzip()
if test == None:
    zf.close()
else:
    print test
    zf.close()

Where is my mistake? How can I solve this problem?

1

There are 1 best solutions below

0
On

You need to close the zipped file before checking for integrity, and open it in reading after:

zf = zipfile.ZipFile(zipname, "w", comprez)
[...]
zf.close()
zf2 = zipfile.ZipFile(zipname, "r")
test=zf2.testzip()