mirror of
https://github.com/svenstaro/miniserve
synced 2024-07-08 20:05:56 +00:00
Working example of tar.gz
This commit is contained in:
parent
9857f26bdd
commit
e7c269b12e
12
Cargo.lock
generated
12
Cargo.lock
generated
|
@ -660,6 +660,16 @@ name = "libc"
|
|||
version = "0.2.49"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "libflate"
|
||||
version = "0.1.20"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"adler32 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"crc32fast 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "linked-hash-map"
|
||||
version = "0.4.2"
|
||||
|
@ -769,6 +779,7 @@ dependencies = [
|
|||
"clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"htmlescape 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libflate 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"maud 0.20.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"nanoid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
@ -1956,6 +1967,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
"checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14"
|
||||
"checksum lazycell 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b294d6fa9ee409a054354afc4352b0b9ef7ca222c69b8812cbea9e7d2bf3783f"
|
||||
"checksum libc 0.2.49 (registry+https://github.com/rust-lang/crates.io-index)" = "413f3dfc802c5dc91dc570b05125b6cda9855edfaa9825c9849807876376e70e"
|
||||
"checksum libflate 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)" = "54d1ddf9c52870243c5689d7638d888331c1116aa5b398f3ba1acfa7d8758ca1"
|
||||
"checksum linked-hash-map 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7860ec297f7008ff7a1e3382d7f7e1dcd69efc94751a2284bafc3d013c2aa939"
|
||||
"checksum literalext 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2f42dd699527975a1e0d722e0707998671188a0125f2051d2d192fc201184a81"
|
||||
"checksum lock_api 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "62ebf1391f6acad60e5c8b43706dde4582df75c06698ab44511d15016bc2442c"
|
||||
|
|
|
@ -40,4 +40,5 @@ serde = { version = "1.0.89", features = ["derive"] }
|
|||
tar = "0.4"
|
||||
tempfile = "3.0.7"
|
||||
bytes = "0.4.12"
|
||||
futures = "0.1.25"
|
||||
futures = "0.1.25"
|
||||
libflate = "0.1.20"
|
|
@ -1,7 +1,8 @@
|
|||
use bytes::Bytes;
|
||||
use libflate::gzip::Encoder;
|
||||
use serde::Deserialize;
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::io::Read;
|
||||
use std::io::{self, Read};
|
||||
use std::path::PathBuf;
|
||||
use tar::Builder;
|
||||
use tempfile::tempdir;
|
||||
|
@ -61,6 +62,7 @@ fn tgz_compress(dir: &PathBuf) -> Result<(String, usize, Bytes), CompressionErro
|
|||
let src_dir = dir.display().to_string();
|
||||
let inner_folder = dir.file_name()?.to_str()?;
|
||||
let dst_filename = format!("{}.tar", inner_folder);
|
||||
let dst_tgz_filename = format!("{}.gz", dst_filename);
|
||||
let tmp_dir = tempdir()?;
|
||||
|
||||
let dst_filepath = tmp_dir.path().join(dst_filename.clone());
|
||||
|
@ -70,11 +72,21 @@ fn tgz_compress(dir: &PathBuf) -> Result<(String, usize, Bytes), CompressionErro
|
|||
tar_builder.finish()?;
|
||||
|
||||
let mut tar_file = OpenOptions::new().read(true).open(&dst_filepath)?;
|
||||
let mut contents = Vec::new();
|
||||
let content_length = tar_file.read_to_end(&mut contents).unwrap();
|
||||
let mut tar_content = Vec::new();
|
||||
let content_length = tar_file.read_to_end(&mut tar_content).unwrap();
|
||||
|
||||
let gz_data = gzip(&mut tar_content)?;
|
||||
|
||||
let mut data = Bytes::new();
|
||||
data.extend_from_slice(&contents);
|
||||
data.extend_from_slice(&gz_data);
|
||||
|
||||
Ok((dst_filename, content_length, data))
|
||||
Ok((dst_tgz_filename, content_length, data))
|
||||
}
|
||||
|
||||
fn gzip(mut data: &[u8]) -> Result<Vec<u8>, CompressionError> {
|
||||
let mut encoder = Encoder::new(Vec::new())?;
|
||||
io::copy(&mut data, &mut encoder)?;
|
||||
let data = encoder.finish().into_result()?;
|
||||
|
||||
Ok(data)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user