开发者

Python: binary file to gz file and then to jpg extension and finally return again to the original binary file

I want to do the following in Python:

  1. Take a binary (executable) file
  2. Turn it into a zip file with gzip (gz extension)
  3. Then put the jpg extension

Later again the desire to recover the original (without the gz extension or jpg).

The idea is to sen开发者_JAVA技巧d binary files through GMail SMTP to recover and then get them over IMAP and process them in ehtir original form (1).


The python gzip and shutil libraries can do what you need.

To gzip the executable.

import gzip, shutil

src = open('executable', 'rb')
dest = gzip.open('executable.gz.jpg', 'wb')

shutil.copyfileobj(src, dest)

src.close()
dest.close()

And then to get the original back.

import gzip. shutil

src = gzip.open('executable.gz.jpg', 'rb')
dest = gzip.open('executable', 'wb')

shutil.copyfileobj(src, dest)

src.close()
dest.close()

That being said, gmail's MIME filters look at content, not extension, so it may still block the new file.


You can use gzip to compress the files and os.rename to change file names. In your case you could just use gzip and save it with a .jpg extension in the first place.

import gzip

# write compressed file
with gzip.open('my_file.jpg', 'wb') as f:
    f.write(content)

# read it again
with gzip.open('my_file.jpg', 'rb') as f:
    content = f.read()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜