469,360 Members | 1,608 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,360 developers. It's quick & easy.

Incremental Compression

Hey.

I have a problem in some network code. I want to send my packets compressed,
but I don't want to compress each packet separately (via .encode('zlib') or
such) but rather I'd like to compress it with regard to the history of the
compression stream. If I use zlib.compressobj and flush it to get the
packet data, I cannot continue to compress with that stream.

I cannot wait until the end of the stream and then flush, because I need to
flush after every packet.

Another capability I require is to be able to copy the compression stream.
i.e: To be able to create multiple continuations of the same compression
stream. Something like:

a = compressobj()
pre_x = a.copy()
x = a.compress('my_packet1')
# send x
# x was not acked yet, so we must send another packet via the pre_x
compressor
y = pre_x.compress('my_packet2')

Is there a compression object that can do all this?
Mar 25 '06 #1
2 2088
On Sat, 2006-03-25 at 03:08 +0200, Eyal Lotem wrote:
Hey.

I have a problem in some network code. I want to send my packets compressed,
but I don't want to compress each packet separately (via .encode('zlib') or
such) but rather I'd like to compress it with regard to the history of the
compression stream. If I use zlib.compressobj and flush it to get the
packet data, I cannot continue to compress with that stream.
Yes, you can.

Help on built-in function flush:

flush(...)
flush( [mode] ) -- Return a string containing any remaining
compressed data.
mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH,
Z_FINISH; the
default value used when mode is not specified is Z_FINISH.
If mode == Z_FINISH, the compressor object can no longer be used
after
calling the flush() method. Otherwise, more data can still be
compressed.

you want to call

mycompressor.flush( zlib.Z_SYNC_FLUSH )

The difference between the flushes is this:

1. Z_SYNC_FLUSH. This basically send enough data so that the receiver
will get everything you put in. This does decerase your compression
ratio (however, in weird case when I last played with it, it helped.)

2. Z_FULL_FLUSH. This sends enough data so that the receiver will get
everything you put in. This also wipes the compressors statistics, so
the when you pick up where you left of, the compressor will compress
about as well as if you had just started, you are wiping its memory of
what it saw in the past.

3. Z_FINISH. This is the default action, this is what is killing you.

Good luck - Adam DePrince

I cannot wait until the end of the stream and then flush, because I need to
flush after every packet.

Another capability I require is to be able to copy the compression stream.
i.e: To be able to create multiple continuations of the same compression
stream. Something like:

a = compressobj()
pre_x = a.copy()
x = a.compress('my_packet1')
# send x
# x was not acked yet, so we must send another packet via the pre_x
compressor
y = pre_x.compress('my_packet2')

Is there a compression object that can do all this?

Ahh, you are trying to "pretune" the compressor before sending a little
bit ... I think C-zlib does this, but I don't know for sure.

- Adam DePrince

Mar 25 '06 #2
Adam DePrince wrote:
On Sat, 2006-03-25 at 03:08 +0200, Eyal Lotem wrote:
Hey.

I have a problem in some network code. I want to send my packets
compressed, but I don't want to compress each packet separately (via
.encode('zlib') or such) but rather I'd like to compress it with regard
to the history of the
compression stream. If I use zlib.compressobj and flush it to get the
packet data, I cannot continue to compress with that stream.
Yes, you can.

Help on built-in function flush:

flush(...)
flush( [mode] ) -- Return a string containing any remaining
compressed data.
mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH,
Z_FINISH; the
default value used when mode is not specified is Z_FINISH.
If mode == Z_FINISH, the compressor object can no longer be used
after
calling the flush() method. Otherwise, more data can still be
compressed.

you want to call

mycompressor.flush( zlib.Z_SYNC_FLUSH )

The difference between the flushes is this:

1. Z_SYNC_FLUSH. This basically send enough data so that the receiver
will get everything you put in. This does decerase your compression
ratio (however, in weird case when I last played with it, it helped.)

2. Z_FULL_FLUSH. This sends enough data so that the receiver will get
everything you put in. This also wipes the compressors statistics, so
the when you pick up where you left of, the compressor will compress
about as well as if you had just started, you are wiping its memory of
what it saw in the past.

3. Z_FINISH. This is the default action, this is what is killing you.

Good luck - Adam DePrince


Thanks! That really helps.

I cannot wait until the end of the stream and then flush, because I need
to flush after every packet.

Another capability I require is to be able to copy the compression
stream. i.e: To be able to create multiple continuations of the same
compression stream. Something like:

a = compressobj()
pre_x = a.copy()
x = a.compress('my_packet1')
# send x
# x was not acked yet, so we must send another packet via the pre_x
compressor
y = pre_x.compress('my_packet2')

Is there a compression object that can do all this?

Ahh, you are trying to "pretune" the compressor before sending a little
bit ... I think C-zlib does this, but I don't know for sure.


Yeah, but I don't need a powerful tuning, just a means to copy the
compressor's state. I guess I'll need to write some C for this.

Thanks again!

- Adam DePrince


Mar 25 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by jane | last post: by
3 posts views Thread by apple | last post: by
6 posts views Thread by Rudy Ray Moore | last post: by
3 posts views Thread by Alex Shturm | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.