Algorithm that makes maximum compression of completly diffused data.

Tim Chase python.list at tim.thechases.com
Wed Oct 30 19:01:39 EDT 2013


On 2013-10-30 21:30, Joshua Landau wrote:
> started talking about compressing *random data* 

If it's truly random bytes, as long as you don't need *the same*
random data, you can compress it quite easily.  Lossy compression is
acceptable for images, so why not random files?  :-)

  import os
  inname = "random.txt"
  namez = inname + '.rnz'
  # compress the file
  with open(outnamez, 'w') as f:
    f.write(os.stat(inname).st_size)

  # uncompress the file
  with open(namez) as f:
    size = int(f.read())
  with open('/dev/random', 'rb') as rnd, open(inname, 'wb') as out:
    for i in range(size):
      out.write(rnd.read(1))


There are optimizations that can be made, and I didn't make it run
on Win32, but I leave those as exercises for the reader.  That
said, this compresses *remarkably* well for large files ;-)

-tkc



 



More information about the Python-list mailing list