Decompress things to a string in a py2/3 safe fashion
    >>> json_str = '{"test": 1}'
    >>> blob = zlib_compress(json_str)
    >>> got_str = zlib_decompress_to_string(blob)
    >>> got_str == json_str
    True
161625
def zlib_decompress_to_string(blob):
    
    if PY3K:
        if isinstance(blob, bytes):
            decompressed = zlib.decompress(blob)
        else:
            decompressed = zlib.decompress(bytes(blob, 'utf-8'))
        return decompressed.decode('utf-8')
    return zlib.decompress(blob)
742547
def _expand_json(self, j):
        
        decompressed_json = copy.copy(j)
        decompressed_json.pop('blob', None)  # Remove BLOB portion of JSON

        # Decode and decompress JSON
        compressed_data = base64.b64decode(j['blob'])
        original_json = zlib.decompress(compressed_data).decode('utf-8')

        decompressed_json['users'] = json.loads(original_json)  # Insert users

        return decompressed_json
205709
def compress(data):
    

    if isinstance(data, bytes):
        source = data
    elif isinstance(data, str):
        source = bytes(data, encoding='utf-8')
    else:
        raise RuntimeError("Compression is only supported for strings and bytes")

    return base64.b64encode(gzip.compress(source))
989903
def bytes2zip(bytes):
    
    if hasattr(bytes, "read"):
        buff = TemporaryFile()
        archive = gzip.GzipFile(fileobj=buff, mode='w')
        for b in bytes:
            archive.write(b)
        archive.close()
        buff.seek(0)
        from pyLibrary.env.big_data import FileString, safe_size
        return FileString(buff)

    buff = BytesIO()
    archive = gzip.GzipFile(fileobj=buff, mode='w')
    archive.write(bytes)
    archive.close()
    return buff.getvalue()
1307858
def _compress_json(self, j):
        
        compressed_json = copy.copy(j)
        compressed_json.pop('users', None)

        compressed_data = zlib.compress(
            json.dumps(j['users']).encode('utf-8'),
            self.zlib_compression_strength
        )
        b64_data = base64.b64encode(compressed_data).decode('utf-8')

        compressed_json['blob'] = b64_data

        return compressed_json
1027284
def write_as_json(blob, dest, indent=0, sort_keys=True):
    
    opened_out = False
    if is_str_type(dest):
        out = codecs.open(dest, mode='w', encoding='utf-8')
        opened_out = True
    else:
        out = dest
    try:
        json.dump(blob, out, indent=indent, sort_keys=sort_keys)
        out.write('\n')
    finally:
        out.flush()
        if opened_out:
            out.close()
989627
@SneakyThrows
    public static String decompress(final String zippedBase64Str) {
        val bytes = EncodingUtils.decodeBase64(zippedBase64Str);
        try (val zi = new GZIPInputStream(new ByteArrayInputStream(bytes))) {
            return IOUtils.toString(zi, Charset.defaultCharset());
        }
    }
411174
def _safe_inflate data
      zi = Zlib::Inflate.new
      pos = 0; r = ''
      begin
        # save some memory by not using String#[] when not necessary
        r << zi.inflate(pos==0 ? data : data[pos..-1])
        if zi.total_in < data.size
          @extradata << data[zi.total_in..-1]
          puts "[?] #{@extradata.last.size} bytes of extra data after zlib stream".red if @verbose >= 1
        end
        # decompress OK
      rescue Zlib::BufError
        # tried to decompress, but got EOF - need more data
        puts "[!] #{$!.inspect}".red if @verbose >= -1
        # collect any remaining data in decompress buffer
        r << zi.flush_next_out
      rescue Zlib::DataError
        puts "[!] #{$!.inspect}".red if @verbose >= -1
        #p [pos, zi.total_in, zi.total_out, data.size, r.size]
        r << zi.flush_next_out
        # XXX TODO try to skip error and continue
#        printf "[d] pos=%d/%d t_in=%d t_out=%d bytes_ok=%d\n".gray, pos, data.size,
#          zi.total_in, zi.total_out, r.size
#        if pos < zi.total_in
#          pos = zi.total_in
#        else
#          pos += 1
#        end
#        pos = 0
#        retry if pos < data.size
      rescue Zlib::NeedDict
        puts "[!] #{$!.inspect}".red if @verbose >= -1
        # collect any remaining data in decompress buffer
        r << zi.flush_next_out
      end

      r == "" ? nil : r
    ensure
      zi.close if zi && !zi.closed?
    end
530580
def decompress(zdata):
    
    data = zlib.decompress(zdata)
    lc = struct.unpack('<L', data[0:4])[0]
    li = struct.unpack('<L', data[4:8])[0]
    c = json.loads(data[8:8 + lc].decode('utf-8'))
    i = np.fromstring(data[8 + lc:8 + lc + li], dtype=np.float32)
    v = np.fromstring(data[8 + lc + li:], dtype=np.float64)

    if len(c) == 1:
        return Trace(v, i, name=c[0])
    else:
        return Chromatogram(v.reshape(len(i), len(c)), i, c)
541176
def _compress(compress):
    '''
    
    '''
    if compress in ('bz2', 'bzip2', 'j'):
        compression = 'j'
        ext = 'bz2'
    elif compress in ('gz', 'gzip', 'z'):
        compression = 'z'
        ext = 'gz'
    elif compress in ('xz', 'a', 'J'):
        compression = 'J'
        ext = 'xz'

    return compression, ext
789959
def _decompress(self, cmd, s3_snap):
        
        compressor = COMPRESSORS.get(s3_snap.compressor)
        if compressor is None:
            return cmd
        decompress_cmd = compressor['decompress']
        return "{} | {}".format(decompress_cmd, cmd)
280767
def unescape_json_str(st):
    
    c = 0
    ln = len(st)
    escape_chmap = {
        b'b': b'\b',
        b'f': b'\f',
        b'n': b'\n',
        b'r': b'\r',
        b't': b'\t',
        b'\\': b'\\',
        b'"': b'\"',
        b'/': b'\/'
    }

    ret = []

    def at(i):
        return st[i:i+1]

    while c < ln:
        if at(c) == b'\\':
            if at(c+1) == b'u':
                ret.append(bytes([int(st[c+2:c+6], 16)]))
                # ret.append(st[c:c+6].decode('unicode_escape').encode('utf8'))
                c += 6

            else:
                ret.append(escape_chmap[at(c+1)])
                c += 2

        else:
            ret.append(at(c))
            c += 1

    df = (b''.join(ret))
    return df
1216480
def to_str(self, s):
        '''
        
        '''
        if s.__class__ != str:
            if not IS_PY3K:
                s = s.encode('utf-8')
            else:
                raise AssertionError('Expected to have str on Python 3. Found: %s (%s)' % (s, s.__class__))
        return s
129802
def uncompress_file(s3_object)
      result = ''

      if s3_object.exists?
        io = StringIO.new
        io.write(s3_object.read)
        io.rewind

        gz = Zlib::GzipReader.new(io)
        result = gz.read

        gz.close
      end

      result
    end
915499
def load(string)
      marshal, compress, value = decompose(string)

      marshal.load(inflate(value, compress))
    rescue TypeError, NoMethodError
      string
    end
1002143
def pypy_json_encode(value, pretty=False):
    
    global _dealing_with_problem
    if pretty:
        return pretty_json(value)

    try:
        _buffer = UnicodeBuilder(2048)
        _value2json(value, _buffer)
        output = _buffer.build()
        return output
    except Exception as e:
        # THE PRETTY JSON WILL PROVIDE MORE DETAIL ABOUT THE SERIALIZATION CONCERNS
        from mo_logs import Log

        if _dealing_with_problem:
            Log.error("Serialization of JSON problems", e)
        else:
            Log.warning("Serialization of JSON problems", e)
        _dealing_with_problem = True
        try:
            return pretty_json(value)
        except Exception as f:
            Log.error("problem serializing object", f)
        finally:
            _dealing_with_problem = False
1215562
def deflate_and_base64_encode(string_val):
    
    if not isinstance(string_val, six.binary_type):
        string_val = string_val.encode('utf-8')
    return base64.b64encode(zlib.compress(string_val)[2:-4])