Initial version of epub_compress in rust

This commit is contained in:
2025-07-14 22:13:22 +02:00
commit ec25edc81a
4 changed files with 1053 additions and 0 deletions

52
src/main.rs Normal file
View File

@ -0,0 +1,52 @@
use infer;
use magick_rust;
use std::env;
use std::ffi::OsStr;
use std::fs::File;
use std::io::prelude::*;
use std::io::Write;
use std::path::Path;
use zip;
use zip::write::SimpleFileOptions;
fn main() -> std::io::Result<()> {
let mut argv = env::args();
if argv.len() != 2 {
panic!("Not enough arguments");
}
let filename: String = argv.nth(1).unwrap();
let buf = File::open(&filename)?;
magick_rust::magick_wand_genesis();
let mut zip = zip::ZipArchive::new(buf)?;
let tmp_filename = filename.to_string() + ".tmp";
let mut tmp_file = File::create(tmp_filename)?;
let mut tmp_zip = zip::ZipWriter::new(&mut tmp_file);
let options = SimpleFileOptions::default();
for i in 0..zip.len() {
let mut file = zip.by_index(i)?;
let mut buf = Vec::new();
let n = file.read_to_end(&mut buf)?;
if infer::is_image(&buf[..n]) {
let extension = Path::new(file.name()).extension().and_then(OsStr::to_str).expect("String");
println!("{}", extension);
let wand = magick_rust::MagickWand::new();
wand.read_image_blob(buf).expect("Successful read");
wand.fit(1200, 1600);
wand.strip_image().expect("Strip");
let to_write = wand.write_image_blob(extension).expect("Write worked");
println!("data len: {}", to_write.len());
tmp_zip.start_file(file.name(), options)?;
tmp_zip.write_all(&to_write)?;
} else {
tmp_zip.start_file(file.name(), options)?;
tmp_zip.write_all(&buf)?;
}
}
tmp_zip.finish()?;
Ok(())
}