Linux 下,使用unzip解压时,报错:
$ unzip abc.zip
Archive: abc.zip
End-of-central-directory signature not found. Either this file is not
a zipfile, or it constitutes one disk of a multi-part archive. In the
latter case the central directory and zipfile comment will be found on
the last disk(s) of this archive.
unzip: cannot find zipfile directory in one of abc.zip or
abc.zip.zip, and cannot find abc.zip.ZIP, period.
我开始的时候是把一个1.2G的文件分卷1M压缩成了1200多个子包,因为服务器限制上传文件的大小,只能这样干了。解压的时候需要cat abc.zip.* > abc.zip 合并为一个zip包, 再unzip abc.zip 解压zip包。合并包的时候没有问题,但解压包的时候问题就来了,报了上面的错误,然后查了一些方法,有的说使用jar解压的,有的说没上传完。使用jar照样出错,应该这种解决方法不行,检查一遍上传也上传完了。最后发现zip在分卷的时候命名有一些问题,比如xxx.zip.1070会在xxx.zip.107的前面,导致cat的时候发生乱序,所以解压的时候会出错。这时需要写一个程序把后缀107改成0107,这样就不会有上述问题了,成功解压~
附上改后缀的代码:
from glob import glob
path = '/users/xxx/xxx.zip.*' #子zip所在目录
for p in glob(path):
tail = p.split('.')[-1] #数字后缀
head = '.'.join(p.split('.')[:-1]) #数字后缀前的字符
if len(tail) < 4 and tail != 'zip': #我最多到千位,如果你的更多可以做一下修改
os.rename(p, head + '.0{}'.format(tail)) #改后缀