Python 3x- Compression Makes File Bigger :(


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!


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.


  • macOS 10.15.4 - external NFC Reader over USB (CCID & PC/SC compliant) for Host Card Emulation
  • Oracle's puzzling behaviour with NLS_SORT and a simple regexp_like
  • Dynamically determine existence of Ember component?
  • What happened to Microsoft.FSharp.Math.Matrix?
  • How to get image from drawable-xhdpi
  • wpf datagridcheckboxcolumn style
  • OCaml: get value's type name
  • Regular expression to match sets of numbers that are not equal nor reversed
  • How can I disable jquery validation on readonly fields?
  • How to check if “set” in c
  • Not logged in after manual login in CakePHP if redirected, logged in if no redirect
  • How to set the minimum SDK Build Tools version required in an Android Studio project?
  • Trouble using Runtime.getRuntime().exec
  • Unable to get the text inside the monaco editor using protractor
  • Where can I find tomesh.c for windows?
  • disable EJS caching in production
  • How to change textview height constraint within table view cell?
  • set clipboard content in any browser
  • Eloquent Javascript: Can't understand how the number value is determined in the sum function
  • How to manipulate content of a comment with Apache POI
  • Can someone explain how Yii minimizing assets is supposed to work on Heroku?
  • Directed graph linear algorithm
  • Opening tel: links from UIWebView
  • How to put an object in the air?
  • Sample deviceQuery cuda program
  • Android studio import problems. (Apktool)
  • how to add dashed border on highcharts “area” graph for every point
  • What does “T extends Junk” mean in a generic class in Java?
  • Building JavaFX 2.0 App on Mac, deploying on Windows
  • How to use Typescript with libraries like Ampersand.js that parse configs to build prototypes
  • How do I use libcurl to printf a remote FTP directory listing?
  • How to create subsets of a single set of elements with XSLT?
  • how to run a different select statement based on condition in Hive SQL
  • Conflicting declaration using constexpr and auto in C++11
  • How to include associated objects using gon in Rails/jQuery
  • How to clear a browser cache in Protractor
  • Background transfer download task failed when app was closed
  • ssh remote server login script
  • Using Service Component Runtime
  • ReferenceError: TextEncoder is not defined