2010-11-11 23:11:36 +01:00
|
|
|
#!/usr/bin/env python
|
2012-12-19 14:48:11 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2010-11-11 23:11:36 +01:00
|
|
|
|
|
|
|
import sys
|
|
|
|
import zlib
|
2012-11-07 14:14:25 +01:00
|
|
|
import zipfilerugged
|
2010-11-11 23:11:36 +01:00
|
|
|
import os
|
|
|
|
import os.path
|
|
|
|
import getopt
|
|
|
|
from struct import unpack
|
|
|
|
|
|
|
|
|
|
|
|
_FILENAME_LEN_OFFSET = 26
|
|
|
|
_EXTRA_LEN_OFFSET = 28
|
|
|
|
_FILENAME_OFFSET = 30
|
|
|
|
_MAX_SIZE = 64 * 1024
|
2011-02-08 18:21:51 +01:00
|
|
|
_MIMETYPE = 'application/epub+zip'
|
|
|
|
|
2012-11-07 14:14:25 +01:00
|
|
|
class ZipInfo(zipfilerugged.ZipInfo):
|
2011-02-08 18:21:51 +01:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
if 'compress_type' in kwargs:
|
|
|
|
compress_type = kwargs.pop('compress_type')
|
|
|
|
super(ZipInfo, self).__init__(*args, **kwargs)
|
|
|
|
self.compress_type = compress_type
|
2010-11-11 23:11:36 +01:00
|
|
|
|
|
|
|
class fixZip:
|
|
|
|
def __init__(self, zinput, zoutput):
|
2011-02-08 18:21:51 +01:00
|
|
|
self.ztype = 'zip'
|
|
|
|
if zinput.lower().find('.epub') >= 0 :
|
|
|
|
self.ztype = 'epub'
|
2012-11-07 14:14:25 +01:00
|
|
|
self.inzip = zipfilerugged.ZipFile(zinput,'r')
|
|
|
|
self.outzip = zipfilerugged.ZipFile(zoutput,'w')
|
2010-11-11 23:11:36 +01:00
|
|
|
# open the input zip for reading only as a raw file
|
2012-11-07 14:14:25 +01:00
|
|
|
self.bzf = file(zinput,'rb')
|
|
|
|
|
2010-11-11 23:11:36 +01:00
|
|
|
def getlocalname(self, zi):
|
|
|
|
local_header_offset = zi.header_offset
|
|
|
|
self.bzf.seek(local_header_offset + _FILENAME_LEN_OFFSET)
|
|
|
|
leninfo = self.bzf.read(2)
|
|
|
|
local_name_length, = unpack('<H', leninfo)
|
|
|
|
self.bzf.seek(local_header_offset + _FILENAME_OFFSET)
|
|
|
|
local_name = self.bzf.read(local_name_length)
|
|
|
|
return local_name
|
|
|
|
|
|
|
|
def uncompress(self, cmpdata):
|
|
|
|
dc = zlib.decompressobj(-15)
|
|
|
|
data = ''
|
|
|
|
while len(cmpdata) > 0:
|
|
|
|
if len(cmpdata) > _MAX_SIZE :
|
|
|
|
newdata = cmpdata[0:_MAX_SIZE]
|
|
|
|
cmpdata = cmpdata[_MAX_SIZE:]
|
|
|
|
else:
|
|
|
|
newdata = cmpdata
|
|
|
|
cmpdata = ''
|
|
|
|
newdata = dc.decompress(newdata)
|
|
|
|
unprocessed = dc.unconsumed_tail
|
|
|
|
if len(unprocessed) == 0:
|
|
|
|
newdata += dc.flush()
|
|
|
|
data += newdata
|
|
|
|
cmpdata += unprocessed
|
|
|
|
unprocessed = ''
|
|
|
|
return data
|
|
|
|
|
|
|
|
def getfiledata(self, zi):
|
|
|
|
# get file name length and exta data length to find start of file data
|
|
|
|
local_header_offset = zi.header_offset
|
|
|
|
|
|
|
|
self.bzf.seek(local_header_offset + _FILENAME_LEN_OFFSET)
|
|
|
|
leninfo = self.bzf.read(2)
|
|
|
|
local_name_length, = unpack('<H', leninfo)
|
|
|
|
|
|
|
|
self.bzf.seek(local_header_offset + _EXTRA_LEN_OFFSET)
|
|
|
|
exinfo = self.bzf.read(2)
|
|
|
|
extra_field_length, = unpack('<H', exinfo)
|
|
|
|
|
|
|
|
self.bzf.seek(local_header_offset + _FILENAME_OFFSET + local_name_length + extra_field_length)
|
|
|
|
data = None
|
|
|
|
|
|
|
|
# if not compressed we are good to go
|
2012-11-07 14:14:25 +01:00
|
|
|
if zi.compress_type == zipfilerugged.ZIP_STORED:
|
2010-11-11 23:11:36 +01:00
|
|
|
data = self.bzf.read(zi.file_size)
|
|
|
|
|
|
|
|
# if compressed we must decompress it using zlib
|
2012-11-07 14:14:25 +01:00
|
|
|
if zi.compress_type == zipfilerugged.ZIP_DEFLATED:
|
2010-11-11 23:11:36 +01:00
|
|
|
cmpdata = self.bzf.read(zi.compress_size)
|
|
|
|
data = self.uncompress(cmpdata)
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
2012-11-07 14:14:25 +01:00
|
|
|
|
2010-11-11 23:11:36 +01:00
|
|
|
|
|
|
|
def fix(self):
|
|
|
|
# get the zipinfo for each member of the input archive
|
|
|
|
# and copy member over to output archive
|
|
|
|
# if problems exist with local vs central filename, fix them
|
2011-02-02 15:41:15 +01:00
|
|
|
|
2011-02-08 18:21:51 +01:00
|
|
|
# if epub write mimetype file first, with no compression
|
|
|
|
if self.ztype == 'epub':
|
2012-11-07 14:14:25 +01:00
|
|
|
nzinfo = ZipInfo('mimetype', compress_type=zipfilerugged.ZIP_STORED)
|
2011-02-08 18:21:51 +01:00
|
|
|
self.outzip.writestr(nzinfo, _MIMETYPE)
|
2011-02-02 15:41:15 +01:00
|
|
|
|
|
|
|
# write the rest of the files
|
|
|
|
for zinfo in self.inzip.infolist():
|
2011-02-08 18:21:51 +01:00
|
|
|
if zinfo.filename != "mimetype" or self.ztype == '.zip':
|
2011-02-02 15:41:15 +01:00
|
|
|
data = None
|
|
|
|
nzinfo = zinfo
|
2012-11-07 14:14:25 +01:00
|
|
|
try:
|
2011-02-02 15:41:15 +01:00
|
|
|
data = self.inzip.read(zinfo.filename)
|
2012-11-07 14:14:25 +01:00
|
|
|
except zipfilerugged.BadZipfile or zipfilerugged.error:
|
2011-02-02 15:41:15 +01:00
|
|
|
local_name = self.getlocalname(zinfo)
|
|
|
|
data = self.getfiledata(zinfo)
|
|
|
|
nzinfo.filename = local_name
|
|
|
|
|
|
|
|
nzinfo.date_time = zinfo.date_time
|
|
|
|
nzinfo.compress_type = zinfo.compress_type
|
|
|
|
nzinfo.flag_bits = 0
|
|
|
|
nzinfo.internal_attr = 0
|
|
|
|
self.outzip.writestr(nzinfo,data)
|
2010-11-11 23:11:36 +01:00
|
|
|
|
|
|
|
self.bzf.close()
|
|
|
|
self.inzip.close()
|
|
|
|
self.outzip.close()
|
|
|
|
|
|
|
|
|
|
|
|
def usage():
|
|
|
|
print """usage: zipfix.py inputzip outputzip
|
|
|
|
inputzip is the source zipfile to fix
|
|
|
|
outputzip is the fixed zip archive
|
|
|
|
"""
|
2012-11-07 14:14:25 +01:00
|
|
|
|
2010-11-11 23:11:36 +01:00
|
|
|
|
2011-02-02 15:41:15 +01:00
|
|
|
def repairBook(infile, outfile):
|
2010-11-11 23:11:36 +01:00
|
|
|
if not os.path.exists(infile):
|
|
|
|
print "Error: Input Zip File does not exist"
|
|
|
|
return 1
|
|
|
|
try:
|
|
|
|
fr = fixZip(infile, outfile)
|
|
|
|
fr.fix()
|
|
|
|
return 0
|
|
|
|
except Exception, e:
|
|
|
|
print "Error Occurred ", e
|
|
|
|
return 2
|
|
|
|
|
2011-02-02 15:41:15 +01:00
|
|
|
|
|
|
|
def main(argv=sys.argv):
|
|
|
|
if len(argv)!=3:
|
|
|
|
usage()
|
|
|
|
return 1
|
|
|
|
infile = argv[1]
|
|
|
|
outfile = argv[2]
|
|
|
|
return repairBook(infile, outfile)
|
|
|
|
|
|
|
|
|
2010-11-11 23:11:36 +01:00
|
|
|
if __name__ == '__main__' :
|
|
|
|
sys.exit(main())
|