Coverage for girder/utility/ziputil : 80%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
#!/usr/bin/env python # -*- coding: utf-8 -*-
############################################################################### # Copyright 2013 Kitware Inc. # # Licensed under the Apache License, Version 2.0 ( the "License" ); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. ###############################################################################
This module is essentially a subset of the python zipfile module that has been modified to allow it to read arbitrary streams (using generators) as input, instead of only accepting files. It also streams the output using generators.
Example of creating and consuming a streaming zip:
zip = ziputil.ZipGenerator('TopLevelFolder')
for data in zip.addFile(lambda: 'hello world', 'hello.txt'): yield data
yield zip.footer() """
except ImportError: # pragma: no cover zlib = None
'filename', 'timestamp', 'compressType', 'createSystem', 'createVersion', 'extractVersion', 'externalAttr', 'headerOffset', 'crc', 'compressSize', 'fileSize' )
# Terminate the file name at the first null byte. Null bytes in file # names are used as tricks by viruses in archives. filename = filename[0:nullByte] filename = filename.replace(os.sep, '/')
self.createSystem = 0 # pragma: no cover else:
fmt = '<4slQQ' else: fmt, 'PK\x07\x08', self.crc, self.compressSize, self.fileSize)
""" Return the per-file header as a string. """
'<4s2B4HlLL2H', 'PK\003\004', self.extractVersion, 0, 0x8, self.compressType, dostime, dosdate, 0, 0, 0, len(self.filename), 0)
""" This class can be used to create a streaming zip file that consumes from one generator and writes to another. """ """ :param rootPath: The root path for all files within this archive. :type rootPath: str :param compression: Whether files in this archive should be compressed.
:type """ raise RuntimeError('Missing zlib module') # pragma: no cover
""" Call this whenever data is added to the archive to keep track of the offset of the data. """
""" Generates data to add a file at the given path in the archive. :param generator: Generator function that will yield the file contents. :type generator: function :param path: The path within the archive for this entry. :type path: str """ time.localtime()[0:6])
compressor = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION, zlib.DEFLATED, -15) else:
break buf = compressor.compress(buf) compressSize += len(buf)
buf = compressor.flush() compressSize += len(buf) yield self._advanceOffset(buf) header.compressSize = compressSize else:
""" Once all zip files have been added with addFile, you must call this to get the footer of the archive. """ extra.append(header.fileSize) extra.append(header.compressSize) fileSize = compressSize = 0xffffffff else:
extra.append(header.headerOffset) headerOffset = -1 else:
extraData = struct.pack( '<hh' + 'q'*len(extra), 1, 8*len(extra), *extra) extractVersion = max(45, header.extractVersion) createVersion = max(45, header.createVersion) else:
'<4s4B4HlLL5HLl', 'PK\001\002', createVersion, header.createSystem, extractVersion, 0, 0x8, header.compressType, dostime, dosdate, header.crc, compressSize, fileSize, len(header.filename), len(extraData), 0, 0, 0, header.externalAttr, headerOffset)
zip64endrec = struct.pack( '<4sqhhllqqqq', 'PK\x06\x06', 44, 45, 45, 0, 0, count, count, pos2 - pos1, pos1) data.append(self._advanceOffset(zip64endrec))
zip64locrec = struct.pack('<4slql', 'PK\x06\x07', 0, pos2, 1) data.append(self._advanceOffset(zip64locrec))
offsetVal = -1
pos2 - pos1, offsetVal, 0)
|