Trouble using python's gzip/"How do I know what compression is being used?"
Question: Ok, so I've got an Open Source Java client/server program that uses packets to
communicate. I'm trying to write a python client for said program, but the
contents of the packet seem to be compressed. A quick perusal through the
source code suggested gzip as the compression schema (since that was the only
compression module imported in the code that I could find), but when I saved
the data from one of the packets out of wireshark and tried to do

    
    
    import gzip
    f = gzip.open('compressed_file')
    f.read()
    

It told me that this wasn't a gzip file because the header was wrong. Can
someone advise me what I've done wrong here? Did I change or mess up the
format when I saved it out? Do I need to strip away some of the extraneous
data from the packet before I try running this block on it?

    
    
        if (zipped) {
    
            // XML encode the data and GZIP it.
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            Writer zipOut = new BufferedWriter(new OutputStreamWriter(
                    new GZIPOutputStream(baos)));
            PacketEncoder.encodeData(packet, zipOut);
            zipOut.close();
    
            // Base64 encode the commpressed data.
            // Please note, I couldn't get anything other than a
            // straight stream-to-stream encoding to work.
            byte[] zipData = baos.toByteArray();
            ByteArrayOutputStream base64 = new ByteArrayOutputStream(
                    (4 * zipData.length + 2) / 3);
            Base64.encode(new ByteArrayInputStream(zipData), base64, false);
    

EDIT: Ok, sorry I have the information requested here. This was gathered using
Wireshark to listen in on communication between two running copies of the
original program on different computers. To get the hex stream below, I used
the "Copy -> Hex (Byte Stream)" option in Wireshark.

001321cdc68ff4ce46e4f00d0800450000832a85400080061e51ac102cceac102cb004f8092a9909b32c10e81cb25018f734823e00000100000000000000521f8b08000000000000005bf39681b59c85818121a0b4884138da272bb12c512f27312f5dcf3f292b35b9c47ac2b988f902c59a394c0c0c150540758c250c5c2ea5b9b9950a2e89258900aa4c201a3f000000

I know this will contain the string "Dummy Data" in it. I believe it should
also contain "Jonathanb" (the player name I used to send the message) and the
integer 80 (80 is the command # for "Chat" as far as I can gather from the
code).


Answer: You could try using standard library module
[zlib](http://docs.python.org/library/zlib.html?highlight=zlib#module-zlib)
directly -- that's what
[gzip](http://docs.python.org/library/gzip.html?highlight=gzip#module-gzip)
uses for the compress/decompress part. If the whole packet isn't liked by the
[decompress](http://docs.python.org/library/zlib.html?highlight=zlib#zlib.decompress)
function, you can try using different values of `wbits` and/or slicing off a
few bytes off the packet's front (if you could "reverse engineer" exactly
**how** the Java code is compressing that packet -- even just understand how
many `wbits` is using, or whether it's putting out any prefix before the
compressed data -- that would help immensely, of course).

The only likely "damage" you might have done to the file itself would be, on
windows, if you had written it without specifying `'wb'` to use binary mode --
writing it in "text mode" on windows would make the file unusable. Just
saying...!-)

