{"id":375,"date":"2009-10-08T16:24:07","date_gmt":"2009-10-08T22:24:07","guid":{"rendered":"http:\/\/www.ourada.org\/blog\/?p=375"},"modified":"2009-10-08T16:24:07","modified_gmt":"2009-10-08T22:24:07","slug":"asc-gzip-xfd-compression","status":"publish","type":"post","link":"https:\/\/www.ourada.org\/blog\/archives\/375","title":{"rendered":"asc-gzip\/.xfd compression"},"content":{"rendered":"<p>If you should ever find yourself in the position of having to figure out how to compress XFDL files with the asc-gzip encoding, and I don&#8217;t wish it on you, here&#8217;s Python code to do it. Obviously, you&#8217;ll need some imports and error handling and optimization.<\/p>\n<p><a href=\"http:\/\/groups.google.com\/group\/comp.mail.mime\/browse_thread\/thread\/ecc256a241afe78a\/c36e51d1f5641a32?lnk=st&#038;q=%22content-encoding%22+%22asc-gzip%22&#038;rnum=1#c36e51d1f5641a32\">This thread<\/a> gave me pointers to figure this out; Bryan was just a little off because he was using a gzip library rather than a zlib one.<\/p>\n<pre>\n# compress according to wacky XFDL compression scheme\ndef compress(fc):\n    CHUNK_SIZE = 60000\n    out = ''\n    for i in range(0, len(fc), CHUNK_SIZE):\n        chunk = fc[i:i + CHUNK_SIZE]\n        chunklen = len(chunk)\n        compressedchunk = zlib.compress(chunk)\n        compressedchunklen = len(compressedchunk)\n        out += chr(compressedchunklen \/ 256)\n        out += chr(compressedchunklen % 256)\n        out += chr(chunklen \/ 256)\n        out += chr(chunklen % 256)\n        out += compressedchunk\n        \n    f = StringIO.StringIO()\n    f.write('application\/x-xfdl;content-encoding=\"asc-gzip\"n')\n    b64 = base64.standard_b64encode(out)\n    \n    for i in range(0, len(b64), 76):\n        f.write(b64[i:i+76])\n        f.write('rn')\n    ret = f.getvalue()\n    f.close()\n    return ret\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you should ever find yourself in the position of having to figure out how to compress XFDL files with the asc-gzip encoding, and I don&#8217;t wish it on you, here&#8217;s Python code to do it. Obviously, you&#8217;ll need some imports and error handling and optimization. This thread gave me pointers to figure this out; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-375","post","type-post","status-publish","format-standard","hentry","category-general","author-admin"],"_links":{"self":[{"href":"https:\/\/www.ourada.org\/blog\/wp-json\/wp\/v2\/posts\/375","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ourada.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ourada.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ourada.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ourada.org\/blog\/wp-json\/wp\/v2\/comments?post=375"}],"version-history":[{"count":0,"href":"https:\/\/www.ourada.org\/blog\/wp-json\/wp\/v2\/posts\/375\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.ourada.org\/blog\/wp-json\/wp\/v2\/media?parent=375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ourada.org\/blog\/wp-json\/wp\/v2\/categories?post=375"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ourada.org\/blog\/wp-json\/wp\/v2\/tags?post=375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}