~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tuned_gzip.py

  • Committer: Andrew Bennetts
  • Date: 2009-07-27 05:35:00 UTC
  • mfrom: (4570 +trunk)
  • mto: (4634.6.29 2.0)
  • mto: This revision was merged to the branch mainline in revision 4680.
  • Revision ID: andrew.bennetts@canonical.com-20090727053500-q76zsn2dx33jhmj5
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
#
14
14
# You should have received a copy of the GNU General Public License
15
15
# along with this program; if not, write to the Free Software
16
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
 
18
18
"""Bzrlib specific gzip tunings. We plan to feed these to the upstream gzip."""
19
19
 
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
    """
55
67
    result = [
56
68
        '\037\213'  # self.fileobj.write('\037\213')  # magic header
57
69
        '\010'      # self.fileobj.write('\010')      # compression method
69
81
    # using a compressobj avoids a small header and trailer that the compress()
70
82
    # utility function adds.
71
83
    compress = factory(level, method, width, mem, 0)
72
 
    result.append(compress.compress(bytes))
 
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)
73
92
    result.append(compress.flush())
74
 
    result.append(struct.pack("<L", LOWU32(crc32(bytes))))
75
93
    # size may exceed 2GB, or even 4GB
76
 
    result.append(struct.pack("<L", LOWU32(len(bytes))))
 
94
    result.append(struct.pack("<LL", LOWU32(crc), LOWU32(total_len)))
77
95
    return ''.join(result)
78
96
 
79
97