免费建网站 步骤,免费下载软件的网站有哪些,如何建造自己的网站,佛山做网站需要多少钱前言
flate2 是 Rust 中用于处理 gzip 和其他压缩格式的库。以下是 flate2 的主要 API 和用法说明。
依赖添加
在你的 Cargo.toml 中添加依赖#xff1a;
[dependencies]
flate2 1.0.34主要模块
flate2::write#xff1a;用于压缩数据的写入器。flate2::re…前言
flate2 是 Rust 中用于处理 gzip 和其他压缩格式的库。以下是 flate2 的主要 API 和用法说明。
依赖添加
在你的 Cargo.toml 中添加依赖
[dependencies]
flate2 1.0.34主要模块
flate2::write用于压缩数据的写入器。flate2::read用于解压缩数据的读取器。flate2::Compression指定压缩级别的枚举。
压缩数据
使用 GzEncoder
use flate2::write::GzEncoder;
use flate2::Compression;
use std::fs::File;
use std::io::{self, BufWriter, Write};fn compress_file(input: str, output: str) - io::Result() {let input_file File::open(input)?;let output_file File::create(output)?;let mut encoder GzEncoder::new(output_file, Compression::default());let mut buffer std::io::BufReader::new(input_file);std::io::copy(mut buffer, mut encoder)?;encoder.finish()?;Ok(())
}GzEncoder用于将数据压缩为 gzip 格式。Compression用于指定压缩级别Compression::default() 使用默认级别范围从 0 到 9。
解压缩数据
使用 GzDecoder
use flate2::read::GzDecoder;
use std::fs::File;
use std::io::{self, BufReader, Write};fn decompress_file(input: str, output: str) - io::Result() {let input_file File::open(input)?;let output_file File::create(output)?;let mut decoder GzDecoder::new(input_file);let mut buffer std::io::BufWriter::new(output_file);std::io::copy(mut decoder, mut buffer)?;Ok(())
}GzDecoder用于将 gzip 格式的数据解压缩。
在内存中进行压缩和解压缩
压缩数据到 Vec
use flate2::write::GzEncoder;
use flate2::Compression;fn compress_data(dataflate2 是 Rust 中用于处理 gzip 和其他压缩格式的库。以下是 flate2 的主要 API 和用法说明。### 1. 依赖添加在你的 Cargo.toml 中添加依赖toml
[dependencies]
flate2 1.0主要模块
flate2::write用于压缩数据的写入器。flate2::read用于解压缩数据的读取器。flate2::Compression指定压缩级别的枚举。
压缩数据
使用 GzEncoder
use flate2::write::GzEncoder;
use flate2::Compression;
use std::fs::File;
use std::io::{self, BufWriter, Write};fn compress_file(input: str, output: str) - io::Result() {let input_file File::open(input)?;let output_file File::create(output)?;let mut encoder GzEncoder::new(output_file, Compression::default());let mut buffer std::io::BufReader::new(input_file);std::io::copy(mut buffer, mut encoder)?;encoder.finish()?;Ok(())
}GzEncoder用于将数据压缩为 gzip 格式。Compression用于指定压缩级别Compression::default() 使用默认级别范围从 0 到 9。
解压缩数据
使用 GzDecoder
use flate2::read::GzDecoder;
use std::fs::File;
use std::io::{self, BufReader, Write};fn decompress_file(input: str, output: str) - io::Result() {let input_file File::open(input)?;let output_file File::create(output)?;let mut decoder GzDecoder::new(input_file);let mut buffer std::io::BufWriter::new(output_file);std::io::copy(mut decoder, mut buffer)?;Ok(())
}GzDecoder用于将 gzip 格式的数据解压缩。
在内存中进行压缩和解压缩
压缩数据到 Vec
use flate2::write::GzEncoder;
use flate2::Compression;fn compress_data( [u8]) - Vecu8 {let mut encoder GzEncoder::new(Vec::new(), Compression::default());encoder.write_all(data).expect(Failed to write data);encoder.finish().expect(Failed to finish compression)
}解压缩 Vec
use flate2::read::GzDecoder;fn decompress_data(dataflate2 是 Rust 中用于处理 gzip 和其他压缩格式的库。以下是 flate2 的主要 API 和用法说明。### 1. 依赖添加在你的 Cargo.toml 中添加依赖toml
[dependencies]
flate2 1.0主要模块
flate2::write用于压缩数据的写入器。flate2::read用于解压缩数据的读取器。flate2::Compression指定压缩级别的枚举。
压缩数据
使用 GzEncoder
use flate2::write::GzEncoder;
use flate2::Compression;
use std::fs::File;
use std::io::{self, BufWriter, Write};fn compress_file(input: str, output: str) - io::Result() {let input_file File::open(input)?;let output_file File::create(output)?;let mut encoder GzEncoder::new(output_file, Compression::default());let mut buffer std::io::BufReader::new(input_file);std::io::copy(mut buffer, mut encoder)?;encoder.finish()?;Ok(())
}GzEncoder用于将数据压缩为 gzip 格式。Compression用于指定压缩级别Compression::default() 使用默认级别范围从 0 到 9。
解压缩数据
使用 GzDecoder
use flate2::read::GzDecoder;
use std::fs::File;
use std::io::{self, BufReader, Write};fn decompress_file(input: str, output: str) - io::Result() {let input_file File::open(input)?;let output_file File::create(output)?;let mut decoder GzDecoder::new(input_file);let mut buffer std::io::BufWriter::new(output_file);std::io::copy(mut decoder, mut buffer)?;Ok(())
}GzDecoder用于将 gzip 格式的数据解压缩。
5. 在内存中进行压缩和解压缩
压缩数据到 Vec
use flate2::write::GzEncoder;
use flate2::Compression;fn compress_data( [u8]) - Vecu8 {let mut encoder GzEncoder::new(Vec::new(), Compression::default());encoder.write_all(data).expect(Failed to write data);encoder.finish().expect(Failed to finish compression)
}解压缩 Vec
use flate2::read::GzDecoder;fn decompress_data( [u8]) - Vecu8 {let mut decoder GzDecoder::new(data);let mut decompressed_data Vec::new();decoder.read_to_end(mut decompressed_data).expect(Failed to read decompressed data);decompressed_data
}API 概述
GzEncoder用于 gzip 压缩的结构体。GzDecoder用于 gzip 解压缩的结构体。Compression压缩级别的设置包含以下选项 Compression::none()Compression::fast()Compression::default()Compression::best()Compression::custom(level)自定义级别。
错误处理
所有 I/O 操作都返回 io::Result()在调用时可以使用 ? 运算符来处理错误。
示例
fn main() - io::Result() {let input example.txt;let compressed_output example.gz;let decompressed_output decompressed.txt;compress_file(input, compressed_output)?;println!(Compression complete);decompress_file(compressed_output, decompressed_output)?;println!(Decompression complete);Ok(())
}
文章转载自: http://www.morning.wpmqq.cn.gov.cn.wpmqq.cn http://www.morning.ryxyz.cn.gov.cn.ryxyz.cn http://www.morning.jqsyp.cn.gov.cn.jqsyp.cn http://www.morning.tzmjc.cn.gov.cn.tzmjc.cn http://www.morning.qphgp.cn.gov.cn.qphgp.cn http://www.morning.pxlsh.cn.gov.cn.pxlsh.cn http://www.morning.mcjrf.cn.gov.cn.mcjrf.cn http://www.morning.zrdhd.cn.gov.cn.zrdhd.cn http://www.morning.gbfuy28.cn.gov.cn.gbfuy28.cn http://www.morning.pwggd.cn.gov.cn.pwggd.cn http://www.morning.lhyhx.cn.gov.cn.lhyhx.cn http://www.morning.bqts.cn.gov.cn.bqts.cn http://www.morning.txgjx.cn.gov.cn.txgjx.cn http://www.morning.qhrlb.cn.gov.cn.qhrlb.cn http://www.morning.wsxxq.cn.gov.cn.wsxxq.cn http://www.morning.dcpbk.cn.gov.cn.dcpbk.cn http://www.morning.cklgf.cn.gov.cn.cklgf.cn http://www.morning.pqcbx.cn.gov.cn.pqcbx.cn http://www.morning.yxplz.cn.gov.cn.yxplz.cn http://www.morning.hqllx.cn.gov.cn.hqllx.cn http://www.morning.kjawz.cn.gov.cn.kjawz.cn http://www.morning.bzlfw.cn.gov.cn.bzlfw.cn http://www.morning.kfmnf.cn.gov.cn.kfmnf.cn http://www.morning.wzwyz.cn.gov.cn.wzwyz.cn http://www.morning.ktmnq.cn.gov.cn.ktmnq.cn http://www.morning.kwwkm.cn.gov.cn.kwwkm.cn http://www.morning.cxtbh.cn.gov.cn.cxtbh.cn http://www.morning.jkdtz.cn.gov.cn.jkdtz.cn http://www.morning.clfct.cn.gov.cn.clfct.cn http://www.morning.yrblz.cn.gov.cn.yrblz.cn http://www.morning.jljiangyan.com.gov.cn.jljiangyan.com http://www.morning.irqlul.cn.gov.cn.irqlul.cn http://www.morning.rgfx.cn.gov.cn.rgfx.cn http://www.morning.sxcwc.cn.gov.cn.sxcwc.cn http://www.morning.aowuu.com.gov.cn.aowuu.com http://www.morning.lfxcj.cn.gov.cn.lfxcj.cn http://www.morning.qkqgj.cn.gov.cn.qkqgj.cn http://www.morning.tkhyk.cn.gov.cn.tkhyk.cn http://www.morning.wnmdt.cn.gov.cn.wnmdt.cn http://www.morning.blqgc.cn.gov.cn.blqgc.cn http://www.morning.lpzyq.cn.gov.cn.lpzyq.cn http://www.morning.dmldp.cn.gov.cn.dmldp.cn http://www.morning.skrxp.cn.gov.cn.skrxp.cn http://www.morning.wkhfg.cn.gov.cn.wkhfg.cn http://www.morning.zzgtdz.cn.gov.cn.zzgtdz.cn http://www.morning.wwkdh.cn.gov.cn.wwkdh.cn http://www.morning.xnltz.cn.gov.cn.xnltz.cn http://www.morning.gbwfx.cn.gov.cn.gbwfx.cn http://www.morning.tjmfz.cn.gov.cn.tjmfz.cn http://www.morning.bqyb.cn.gov.cn.bqyb.cn http://www.morning.qrcxh.cn.gov.cn.qrcxh.cn http://www.morning.mpnff.cn.gov.cn.mpnff.cn http://www.morning.yxnfd.cn.gov.cn.yxnfd.cn http://www.morning.yzygj.cn.gov.cn.yzygj.cn http://www.morning.bpmnh.cn.gov.cn.bpmnh.cn http://www.morning.xgmf.cn.gov.cn.xgmf.cn http://www.morning.thwcg.cn.gov.cn.thwcg.cn http://www.morning.rxfbf.cn.gov.cn.rxfbf.cn http://www.morning.wbxrl.cn.gov.cn.wbxrl.cn http://www.morning.gqdsm.cn.gov.cn.gqdsm.cn http://www.morning.lsnnc.cn.gov.cn.lsnnc.cn http://www.morning.deupp.com.gov.cn.deupp.com http://www.morning.wklhn.cn.gov.cn.wklhn.cn http://www.morning.ghqyr.cn.gov.cn.ghqyr.cn http://www.morning.qjmnl.cn.gov.cn.qjmnl.cn http://www.morning.bloao.com.gov.cn.bloao.com http://www.morning.zrnph.cn.gov.cn.zrnph.cn http://www.morning.ylmxs.cn.gov.cn.ylmxs.cn http://www.morning.zcrjq.cn.gov.cn.zcrjq.cn http://www.morning.nytpt.cn.gov.cn.nytpt.cn http://www.morning.tkchg.cn.gov.cn.tkchg.cn http://www.morning.qbwtb.cn.gov.cn.qbwtb.cn http://www.morning.brps.cn.gov.cn.brps.cn http://www.morning.pycpt.cn.gov.cn.pycpt.cn http://www.morning.xpzgg.cn.gov.cn.xpzgg.cn http://www.morning.kclkb.cn.gov.cn.kclkb.cn http://www.morning.wlqll.cn.gov.cn.wlqll.cn http://www.morning.cxsdl.cn.gov.cn.cxsdl.cn http://www.morning.smkxm.cn.gov.cn.smkxm.cn http://www.morning.jggr.cn.gov.cn.jggr.cn