Python 3x- Compression Makes File Bigger :(

回眸只為那壹抹淺笑 提交于 2019-12-08 04:24:19

问题


Ok. Recently I was testing out a piece of code for a small project. It required me to compress some files, and it actually makes the file size bigger, unless there is a problem in what it prints. Here's my code:

def Compress(z):
    #Line Spacing May Be Off A Little Because I'm New to Stack Overflow
    import zlib, sys, time, base64
    text = open(z, "rb").read()
    print ("Raw Size:", sys.getsizeof(text))
    compressed = zlib.compress(text, 9)
    print ("Compressed Size:", sys.getsizeof(compressed))
    ratio = sys.getsizeof(text) / sys.getsizeof(compressed)
    print ("Compression Ratio:", ratio)

EDIT: Hey, thanks for answering, you guys were a lot of help!


回答1:


Not all inputs can be compressed. Your input might be too short to compress, or it might simply have no patterns or skewed statistics for the compressor to work with. Compression requires some form of redundancy in the input in order to compress.



来源:https://stackoverflow.com/questions/34443793/python-3x-compression-makes-file-bigger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!