BVB Source Codes

DeDRM_tools Show zipfix.py Source code

Return Download DeDRM_tools: download zipfix.py Source code - Download DeDRM_tools Source code - Type:.py
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. # zipfix.py, version 1.1
  5. # Copyright 漏 2010-2013 by some_updates, DiapDealer and Apprentice Alf
  6.  
  7. # Released under the terms of the GNU General Public Licence, version 3
  8. # <http://www.gnu.org/licenses/>
  9.  
  10. # Revision history:
  11. #   1.0 - Initial release
  12. #   1.1 - Updated to handle zip file metadata correctly
  13.  
  14. """
  15. Re-write zip (or ePub) fixing problems with file names (and mimetype entry).
  16. """
  17.  
  18. __license__ = 'GPL v3'
  19. __version__ = "1.1"
  20.  
  21. import sys
  22. import zlib
  23. import zipfilerugged
  24. import os
  25. import os.path
  26. import getopt
  27. from struct import unpack
  28.  
  29.  
  30. _FILENAME_LEN_OFFSET = 26
  31. _EXTRA_LEN_OFFSET = 28
  32. _FILENAME_OFFSET = 30
  33. _MAX_SIZE = 64 * 1024
  34. _MIMETYPE = 'application/epub+zip'
  35.  
  36. class ZipInfo(zipfilerugged.ZipInfo):
  37.     def __init__(self, *args, **kwargs):
  38.         if 'compress_type' in kwargs:
  39.             compress_type = kwargs.pop('compress_type')
  40.         super(ZipInfo, self).__init__(*args, **kwargs)
  41.         self.compress_type = compress_type
  42.  
  43. class fixZip:
  44.     def __init__(self, zinput, zoutput):
  45.         self.ztype = 'zip'
  46.         if zinput.lower().find('.epub') >= 0 :
  47.             self.ztype = 'epub'
  48.         self.inzip = zipfilerugged.ZipFile(zinput,'r')
  49.         self.outzip = zipfilerugged.ZipFile(zoutput,'w')
  50.         # open the input zip for reading only as a raw file
  51.         self.bzf = file(zinput,'rb')
  52.  
  53.     def getlocalname(self, zi):
  54.         local_header_offset = zi.header_offset
  55.         self.bzf.seek(local_header_offset + _FILENAME_LEN_OFFSET)
  56.         leninfo = self.bzf.read(2)
  57.         local_name_length, = unpack('<H', leninfo)
  58.         self.bzf.seek(local_header_offset + _FILENAME_OFFSET)
  59.         local_name = self.bzf.read(local_name_length)
  60.         return local_name
  61.  
  62.     def uncompress(self, cmpdata):
  63.         dc = zlib.decompressobj(-15)
  64.         data = ''
  65.         while len(cmpdata) > 0:
  66.             if len(cmpdata) > _MAX_SIZE :
  67.                 newdata = cmpdata[0:_MAX_SIZE]
  68.                 cmpdata = cmpdata[_MAX_SIZE:]
  69.             else:
  70.                 newdata = cmpdata
  71.                 cmpdata = ''
  72.             newdata = dc.decompress(newdata)
  73.             unprocessed = dc.unconsumed_tail
  74.             if len(unprocessed) == 0:
  75.                 newdata += dc.flush()
  76.             data += newdata
  77.             cmpdata += unprocessed
  78.             unprocessed = ''
  79.         return data
  80.  
  81.     def getfiledata(self, zi):
  82.         # get file name length and exta data length to find start of file data
  83.         local_header_offset = zi.header_offset
  84.  
  85.         self.bzf.seek(local_header_offset + _FILENAME_LEN_OFFSET)
  86.         leninfo = self.bzf.read(2)
  87.         local_name_length, = unpack('<H', leninfo)
  88.  
  89.         self.bzf.seek(local_header_offset + _EXTRA_LEN_OFFSET)
  90.         exinfo = self.bzf.read(2)
  91.         extra_field_length, = unpack('<H', exinfo)
  92.  
  93.         self.bzf.seek(local_header_offset + _FILENAME_OFFSET + local_name_length + extra_field_length)
  94.         data = None
  95.  
  96.         # if not compressed we are good to go
  97.         if zi.compress_type == zipfilerugged.ZIP_STORED:
  98.             data = self.bzf.read(zi.file_size)
  99.  
  100.         # if compressed we must decompress it using zlib
  101.         if zi.compress_type == zipfilerugged.ZIP_DEFLATED:
  102.             cmpdata = self.bzf.read(zi.compress_size)
  103.             data = self.uncompress(cmpdata)
  104.  
  105.         return data
  106.  
  107.  
  108.  
  109.     def fix(self):
  110.         # get the zipinfo for each member of the input archive
  111.         # and copy member over to output archive
  112.         # if problems exist with local vs central filename, fix them
  113.  
  114.         # if epub write mimetype file first, with no compression
  115.         if self.ztype == 'epub':
  116.             # first get a ZipInfo with current time and no compression
  117.             mimeinfo = ZipInfo('mimetype',compress_type=zipfilerugged.ZIP_STORED)
  118.             mimeinfo.internal_attr = 1 # text file
  119.             try:
  120.                 # if the mimetype is present, get its info, including time-stamp
  121.                 oldmimeinfo = self.inzip.getinfo('mimetype')
  122.                 # copy across useful fields
  123.                 mimeinfo.date_time = oldmimeinfo.date_time
  124.                 mimeinfo.comment = oldmimeinfo.comment
  125.                 mimeinfo.extra = oldmimeinfo.extra
  126.                 mimeinfo.internal_attr = oldmimeinfo.internal_attr
  127.                 mimeinfo.external_attr = oldmimeinfo.external_attr
  128.                 mimeinfo.create_system = oldmimeinfo.create_system
  129.             except:
  130.                 pass
  131.             self.outzip.writestr(mimeinfo, _MIMETYPE)
  132.  
  133.         # write the rest of the files
  134.         for zinfo in self.inzip.infolist():
  135.             if zinfo.filename != "mimetype" or self.ztype != 'epub':
  136.                 data = None
  137.                 try:
  138.                     data = self.inzip.read(zinfo.filename)
  139.                 except zipfilerugged.BadZipfile or zipfilerugged.error:
  140.                     local_name = self.getlocalname(zinfo)
  141.                     data = self.getfiledata(zinfo)
  142.                     zinfo.filename = local_name
  143.  
  144.                 # create new ZipInfo with only the useful attributes from the old info
  145.                 nzinfo = ZipInfo(zinfo.filename, zinfo.date_time, compress_type=zinfo.compress_type)
  146.                 nzinfo.comment=zinfo.comment
  147.                 nzinfo.extra=zinfo.extra
  148.                 nzinfo.internal_attr=zinfo.internal_attr
  149.                 nzinfo.external_attr=zinfo.external_attr
  150.                 nzinfo.create_system=zinfo.create_system
  151.                 self.outzip.writestr(nzinfo,data)
  152.  
  153.         self.bzf.close()
  154.         self.inzip.close()
  155.         self.outzip.close()
  156.  
  157.  
  158. def usage():
  159.     print """usage: zipfix.py inputzip outputzip
  160.     inputzip is the source zipfile to fix
  161.     outputzip is the fixed zip archive
  162.    """
  163.  
  164.  
  165. def repairBook(infile, outfile):
  166.     if not os.path.exists(infile):
  167.         print "Error: Input Zip File does not exist"
  168.         return 1
  169.     try:
  170.         fr = fixZip(infile, outfile)
  171.         fr.fix()
  172.         return 0
  173.     except Exception, e:
  174.         print "Error Occurred ", e
  175.         return 2
  176.  
  177.  
  178. def main(argv=sys.argv):
  179.     if len(argv)!=3:
  180.         usage()
  181.         return 1
  182.     infile = argv[1]
  183.     outfile = argv[2]
  184.     return repairBook(infile, outfile)
  185.  
  186.  
  187. if __name__ == '__main__' :
  188.     sys.exit(main())
  189.  
downloadzipfix.py Source code - Download DeDRM_tools Source code
Related Source Codes/Software:
kcp - KCP - A Fast and Reliable ARQ Protocol 2017-04-08
cloc - cloc counts blank lines, comment lines, and physic... 2017-04-08
nvidia-docker - Build and run Docker containers leveraging NVIDIA ... 2017-04-08
quickstart - Angular 2 QuickStart - source from the documentati... 2017-04-08
jQuery-Autocomplete - Ajax Autocomplete for jQuery allows you to easily ... 2017-04-08
fuckitjs - The Original Javascript Error Steamroller 2017-04-09
eslint-plugin-react - React specific linting rules for ESLint 2017-04-09
husky - 2017-04-09
electron-builder - A complete solution to package and build a ready f... 2017-04-09
jsfeat - JavaScript Computer Vision library. 2017-04-16
flakes - Flakes is an Admin Template Framework. A combinati... 2017-04-15
capstone - Capstone disassembly/disassembler framework: Core ... 2017-04-15
nginx-resources - A collection of resources covering Nginx, Nginx + ... 2017-04-15
utron - A lightweight MVC framework for Go(Golang) 2017-04-15
cfssl - CFSSL: Cloudflare's PKI and TLS toolkit ... 2017-04-15
HandBrake - HandBrake's main development repository ... 2017-04-15
Awesome-Networking - A curated list of awesome networking libraries, re... 2017-04-15
react-sortable-hoc - A set of higher-order components to turn any list ... 2017-04-15
jugglingdb - Multi-database ORM for nodejs: redis, mongodb, mys... 2017-04-15

 Back to top