What is gZip compression?

2019-03-09 15:45发布

I have heard a lot that one should use gZip for their webpages for faster access and downloads, and that it also saves the bandwidth.

So what is this gZip compression and how does it make the downloads faster ? Does it compress the HTML and images or just the HTML.

If I am to start using gZip in my webapps what considerations do I need to make ? Are there any server or browser restrictions for it, or does it support a particular encoding only.

2条回答
干净又极端
2楼-- · 2019-03-09 16:14

GZip Compress the files like html , js and css files while serving the request to the browser. As size of the file is reduced it is served to the user in faster manner. Not all the browser support compression but now all the modern browser support. It is highly recommended , but only one part is that it increases the CPU usages of the server which may be concern sometime. Using Gzip with client side caching will help in increasing the performance.

查看更多
萌系小妹纸
3楼-- · 2019-03-09 16:17

GZip is a form of data compression -- ie it takes a chunk of data and makes it smaller. The original data can be restored by un-zipping the compressed file.

It is relevant to web apps and web sites because the HTTP protocol includes the ability to gzip data that is being sent.

This means that when it is in use, your bandwidth costs for serving the site will be lower because people visiting the site will be downloading smaller files.

There are a few caveats to using GZip, but overall it's usually better to use gzip than not to -- for example, it does take time and processor power to zip and unzip the files, but typically this is not a problem because the time it takes to do that is often less than the time that is saved by downloading a smaller file. Therefore the overall effect is a time saving, despite the browser having to unzip the file.

GZip can compress all files; it doesn't make any difference what the file type is or the encoding. Obviously some files can be compressed more effectively than others, so the bandwidth saving will vary - text files like HTML give the best results; images are not compressed so much by gzip because they already have some compression built-in. Some files (eg those that are already heavily compressed like .zip files) may actually get slightly bigger when gzipped, because they can't be compressed any futher but gzip still needs to add it's meta data to the file. But these are edge cases, and don't make much difference.

GZip across HTTP normally happens completely transparently. The end user should be completely unaware that it is happening; the browser would do it behind the scenes for them. And from the web server end it is simply a matter of turning on a config setting in your web server software. From your perspective, that's really all you need to know; just set the gzip setting on your server (or ask your ISP to do it). It's quite possible it may already be active on your site without you even knowing.

查看更多
登录 后发表回答