Compressing and decompressing data
In today's age of bloated websites and daily new web frameworks, many sites feel way more sluggish than they used (and ought) to. One way to mitigate this is by compressing your resources before sending them and then decompressing them when received. This has become the (often ignored) standard on the web. For this purpose, this recipe will teach you how to compress and decompress any kind of data with different algorithms.
How to do it...
Follow these steps:
Open the
Cargo.toml
file that was generated earlier for you.Under
[dependencies]
, add the following line:flate2 = "0.2.20"
If you want, you can go to
flate2
's crates.io page (https://crates.io/crates/flate2) to check for the newest version and use that one instead.In the
bin
folder, create a file calledcompression.rs
.Add the following code and run it with
cargo run --bin compression
:
1 extern crate flate2; 2 3 use std::io::{self, SeekFrom}; 4 use std::io::prelude::*; 5 6 use flate2::{Compression...