class compressobj: # all jython uses wbits for is deciding whether to skip the header if it's negative def __init__(self, level=6, method=DEFLATED, wbits=MAX_WBITS, memLevel=0, strategy=0): if abs(wbits) > MAX_WBITS or abs(wbits) < 8: raise ValueError, "Invalid initialization option" self.deflater = Deflater(level, wbits < 0) self.deflater.setStrategy(strategy) if wbits < 0: _get_deflate_data(self.deflater) self._ended = False def compress(self, string): if self._ended: raise error("compressobj may not be used after flush(Z_FINISH)") string = _to_input(string) self.deflater.setInput(string, 0, len(string)) return _get_deflate_data(self.deflater) def flush(self, mode=Z_FINISH): if self._ended: raise error("compressobj may not be used after flush(Z_FINISH)") if mode not in _valid_flush_modes: raise ValueError, "Invalid flush option" self.deflater.finish() last = _get_deflate_data(self.deflater) if mode == Z_FINISH: self.deflater.end() self._ended = True return last
def write(self, f, encoding=None, method="xml", pretty_print=False, xml_declaration=None, with_tail=True, standalone=None, compression=0, exclusive=False, with_comments=True, inclusive_ns_prefixes=None): if encoding is not None or method != "xml" or pretty_print is not False or xml_declaration is not None or with_tail is not True or standalone is not None or exclusive is not False or with_comments is not True or inclusive_ns_prefixes is not None: raise NotImplementedError if compression == 0 and isinstance(f, (basestring, file, File)): # direct source = DOMSource(self._document.getDocumentElement()) result = StreamResult(f) identityTransformation.transform(source, result) else: # first to a BAOS f2 = ByteArrayOutputStream() source = DOMSource(self._document.getDocumentElement()) result = StreamResult(f2) identityTransformation.transform(source, result) if compression > 0: bytes = f2.toByteArray() deflater = Deflater(compression) deflater.setInput(bytes) deflater.finish() output = jarray.zeros(2 * len(bytes), "b") length = deflater.deflate(output) output = output[:length] else: output = f2.toByteArray() if isinstance(f, basestring): open(f, "wb").write(output.tostring()) else: f.write(output.tostring())
def compress(string, level=6): if level < Z_BEST_SPEED or level > Z_BEST_COMPRESSION: raise error, "Bad compression level" deflater = Deflater(level, 0) deflater.setInput(string, 0, len(string)) deflater.finish() return _get_deflate_data(deflater)
class compressobj(object): # All jython uses wbits for is in deciding whether to skip the # header if it's negative or to set gzip. But we still raise # ValueError to get full test compliance. GZIP_HEADER = "\x1f\x8b\x08\x00\x00\x00\x00\x00\x04\x03" # NB: this format is little-endian, not big-endian as we might # expect for network oriented protocols. Both are 4 bytes unsigned # modulus 2^32 per RFC-1952. CRC32.getValue() returns an unsigned # int as a long, so cope accordingly. GZIP_TRAILER_FORMAT = struct.Struct("<II") # crc32, size def __init__(self, level=6, method=DEFLATED, wbits=MAX_WBITS, memLevel=0, strategy=0): if abs(wbits) & 16: if wbits > 0: wbits -= 16 else: wbits += 16 self._gzip = True else: self._gzip = False if abs(wbits) > MAX_WBITS or abs(wbits) < 8: raise ValueError, "Invalid initialization option: %s" % (wbits,) self.deflater = Deflater(level, wbits < 0 or self._gzip) self.deflater.setStrategy(strategy) self._ended = False self._size = 0 self._crc32 = CRC32() def compress(self, string): if self._ended: raise error("compressobj may not be used after flush(Z_FINISH)") string = _to_input(string) self.deflater.setInput(string, 0, len(string)) deflated = _get_deflate_data(self.deflater) self._size += len(string) self._crc32.update(string) if self._gzip: return self.GZIP_HEADER + deflated else: return deflated def flush(self, mode=Z_FINISH): if self._ended: raise error("compressobj may not be used after flush(Z_FINISH)") if mode not in _valid_flush_modes: raise ValueError, "Invalid flush option" if mode == Z_FINISH: self.deflater.finish() last = _get_deflate_data(self.deflater, mode) if mode == Z_FINISH: if self._gzip: last += self.GZIP_TRAILER_FORMAT.pack( self._crc32.getValue(), self._size & _MASK32) self.deflater.end() self._ended = True return last
class compressobj(object): # All jython uses wbits for is in deciding whether to skip the # header if it's negative or to set gzip. But we still raise # ValueError to get full test compliance. GZIP_HEADER = "\x1f\x8b\x08\x00\x00\x00\x00\x00\x04\x03" # NB: this format is little-endian, not big-endian as we might # expect for network oriented protocols, as specified by RFCs; # CRC32.getValue() returns an unsigned int as a long, so cope # accordingly GZIP_TRAILER_FORMAT = struct.Struct("<Ii") # crc32, size def __init__(self, level=6, method=DEFLATED, wbits=MAX_WBITS, memLevel=0, strategy=0): if abs(wbits) & 16: if wbits > 0: wbits -= 16 else: wbits += 16 self._gzip = True else: self._gzip = False if abs(wbits) > MAX_WBITS or abs(wbits) < 8: raise ValueError, "Invalid initialization option: %s" % (wbits,) self.deflater = Deflater(level, wbits < 0 or self._gzip) self.deflater.setStrategy(strategy) self._ended = False self._size = 0 self._crc32 = CRC32() def compress(self, string): if self._ended: raise error("compressobj may not be used after flush(Z_FINISH)") string = _to_input(string) self.deflater.setInput(string, 0, len(string)) deflated = _get_deflate_data(self.deflater) self._size += len(string) self._crc32.update(string) if self._gzip: return self.GZIP_HEADER + deflated else: return deflated def flush(self, mode=Z_FINISH): if self._ended: raise error("compressobj may not be used after flush(Z_FINISH)") if mode not in _valid_flush_modes: raise ValueError, "Invalid flush option" if mode == Z_FINISH: self.deflater.finish() last = _get_deflate_data(self.deflater, mode) if mode == Z_FINISH: if self._gzip: last += self.GZIP_TRAILER_FORMAT.pack( self._crc32.getValue(), self._size % sys.maxint) self.deflater.end() self._ended = True return last
def compress(string, level=6): if level < Z_BEST_SPEED or level > Z_BEST_COMPRESSION: raise error("Bad compression level") deflater = Deflater(level, 0) try: string = _to_input(string) deflater.setInput(string, 0, len(string)) deflater.finish() return _get_deflate_data(deflater) finally: deflater.end()