#!/usr/bin/python -Qwarnall """ZipSyncer, a tool to keep zip files synchronized with unzipped directories Usage examples: zipsyncer listzipped [directory] Scan directory recursively look for zipped files that are supported by zipsyncer. zipsyncer createunzipped [directory] Scan directory recursively, look for zipped files that are supported and unzip them. zipsyncer createzipped [directory] Scan directory recursively, look for unzipped directories that are have the right naming and zip them. zipsyncer sync [directory] Scan directory recursively, look for zipped-unzipped pairs and synchronize them if one has been changed. zipsyncer removezipped [directory] Scan directory recursively, look for zipped-unzipped pairs and delete the zipped part if they are in sync. zipsyncer removeunzipped [directory] Scan directory recursively, look for zipped-unzipped pairs and delete the unzipped part if they are in sync. """ import struct, zlib, os, base64, time, shutil """ Deflate a data blob and remove the pre- and post-fix. This is how files are compressed by the DEFLATE method in zip files. """ def deflate(data, level): return zlib.compress(data, level)[2:-4] """ Find the compression level that compresses to a given size. To recreate a zipfile from the unzipped files, the file has to be compressed to the same size as specified in the original zip file. Depending on the used DEFLATE algorithm, this may or may nor succeed. """ def compressToTargetSize(data, targetsize): for level in [6, 9, 5, 4, 3, 2, 1, 7, 8, 0, -1]: d = deflate(data, level) if len(d) == targetsize: return d return None def getCRC(data): return zlib.crc32(data) & 0xFFFFFFFF def dos2unixtime(dostime, dosdate): """ Convert date/time code to (year, month, day, hour, min, sec) """ return ( (dosdata>>9)+1980, (dosdate>>5)&0xF, dosdate&0x1F, dostime>>11, (dostime>>5)&0x3F, (dostime&0x1F) * 2 ) def unixtime2dos(secondssinceepoch): t = time.gmtime(secondssinceepoch) dosdate = (t[0] - 1980) << 9 | t[1] << 5 | t[2] dostime = t[3] << 11 | t[4] << 5 | t[5] >> 1 return (dostime, dosdate) class ZipEntry: sigstruct = struct.Struct(" 4: sig = ZipEntry.sigstruct.unpack_from(data, offset) if sig[0] == 0x08074b50: self.signature = 0x08074b50 offset += 4 if flag & 8 or self.signature: d = ZipEntry.DataDescriptor.struct.unpack_from(data, offset) offset += 12 else: d = (0, 0, 0) (self.crc32, self.compressedSize, self.uncompressedSize) = d return offset def pack(self): if self.signature: return ZipEntry.sigstruct.pack(self.signature) \ + ZipEntry.DataDescriptor.struct.pack(self.crc32, self.compressedSize, self.uncompressedSize) if self.crc32 or self.compressedSize \ or self.uncompressedSize: return ZipEntry.DataDescriptor.struct.pack( self.crc32, self.compressedSize, self.uncompressedSize) return '' def getFields(self): return map(str, [self.signature, self.crc32, self.compressedSize, self.uncompressedSize]) def setFields(self, fields): (self.signature, self.crc32, self.compressedSize, self.uncompressedSize) = map(int, fields) def getSize(self): if self.signature: return 16 if self.crc32: return 12 return 0 class CentralDirectoryData(Common): struct1 = struct.Struct(" len(data): self.error = "premature end of zipfile" return dirend = struct.unpack_from("