1 use std::{io::{Cursor, Write}, path::PathBuf};
6 compressors::Compressor,
7 error::{Error, OuchResult},
11 use super::compressor::Entry;
13 pub struct ZipCompressor {}
16 // TODO: this function does not seem to be working correctly ;/
17 fn make_archive_from_memory(input: File) -> OuchResult<Vec<u8>> {
19 let mut writer = zip::ZipWriter::new(std::io::Cursor::new(buffer));
21 let inner_file_path: Box<str> = input
25 // TODO: Is this reachable?
32 zip::write::FileOptions::default().compression_method(zip::CompressionMethod::Deflated);
34 writer.start_file(inner_file_path, options)?;
36 let input_bytes = match input.contents_in_memory {
39 // TODO: error description, although this block should not be
41 return Err(Error::InvalidInput);
45 writer.write(&*input_bytes)?;
49 let bytes = writer.finish().unwrap();
51 Ok(bytes.into_inner())
54 fn make_archive_from_files(input_filenames: Vec<PathBuf>) -> OuchResult<Vec<u8>> {
56 let mut writer = zip::ZipWriter::new(Cursor::new(buffer));
59 zip::write::FileOptions::default().compression_method(zip::CompressionMethod::Deflated);
61 for filename in input_filenames {
62 for entry in WalkDir::new(filename) {
64 let entry_path = &entry.path();
65 if entry_path.is_dir() {
70 entry_path.to_string_lossy(),
73 let file_bytes = std::fs::read(entry.path())?;
74 writer.write(&*file_bytes)?;
79 let bytes = writer.finish().unwrap();
81 Ok(bytes.into_inner())
85 impl Compressor for ZipCompressor {
86 fn compress(&self, from: Entry) -> OuchResult<Vec<u8>> {
88 Entry::Files(filenames) => Ok(Self::make_archive_from_files(filenames)?),
89 Entry::InMemory(file) => Ok(Self::make_archive_from_memory(file)?),