~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tuned_gzip.py

  • Committer: Jonathan Lange
  • Date: 2009-06-26 08:46:52 UTC
  • mto: (4484.1.1 bring-1.16.1-back)
  • mto: This revision was merged to the branch mainline in revision 4485.
  • Revision ID: jml@canonical.com-20090626084652-x7wn8yimd3fj0j0y
Tweak NEWS slightly based on mbp's feedback.

Show diffs side-by-side

added added

removed removed

Lines of Context:
52
52
    width=-zlib.MAX_WBITS, mem=zlib.DEF_MEM_LEVEL,
53
53
    crc32=zlib.crc32):
54
54
    """Create a gzip file containing bytes and return its content."""
55
 
    return chunks_to_gzip([bytes])
56
 
 
57
 
 
58
 
def chunks_to_gzip(chunks, factory=zlib.compressobj,
59
 
    level=zlib.Z_DEFAULT_COMPRESSION, method=zlib.DEFLATED,
60
 
    width=-zlib.MAX_WBITS, mem=zlib.DEF_MEM_LEVEL,
61
 
    crc32=zlib.crc32):
62
 
    """Create a gzip file containing chunks and return its content.
63
 
 
64
 
    :param chunks: An iterable of strings. Each string can have arbitrary
65
 
        layout.
66
 
    """
67
55
    result = [
68
56
        '\037\213'  # self.fileobj.write('\037\213')  # magic header
69
57
        '\010'      # self.fileobj.write('\010')      # compression method
81
69
    # using a compressobj avoids a small header and trailer that the compress()
82
70
    # utility function adds.
83
71
    compress = factory(level, method, width, mem, 0)
84
 
    crc = 0
85
 
    total_len = 0
86
 
    for chunk in chunks:
87
 
        crc = crc32(chunk, crc)
88
 
        total_len += len(chunk)
89
 
        zbytes = compress.compress(chunk)
90
 
        if zbytes:
91
 
            result.append(zbytes)
 
72
    result.append(compress.compress(bytes))
92
73
    result.append(compress.flush())
 
74
    result.append(struct.pack("<L", LOWU32(crc32(bytes))))
93
75
    # size may exceed 2GB, or even 4GB
94
 
    result.append(struct.pack("<LL", LOWU32(crc), LOWU32(total_len)))
 
76
    result.append(struct.pack("<L", LOWU32(len(bytes))))
95
77
    return ''.join(result)
96
78
 
97
79