Python 3x- Compression Makes File Bigger :(

2019-06-07 16:17发布

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条回答
【Aperson】
2楼-- · 2019-06-07 16:57

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.

查看更多
登录 后发表回答