-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added support for compress files
- Loading branch information
Showing
12 changed files
with
155 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,3 +40,4 @@ open = "5.1.3" | |
lopdf = "0.32.0" | ||
sha2 = "0.10.8" | ||
zip = "2.1.0" | ||
walkdir = "2.5.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use walkdir::WalkDir; | ||
|
||
use zip::{ | ||
CompressionMethod, | ||
|
||
write::{ | ||
FileOptions, | ||
ExtendedFileOptions | ||
}, | ||
}; | ||
|
||
use std::{ | ||
fs::File, | ||
path::Path, | ||
|
||
io::{ | ||
self, | ||
Read, | ||
Write | ||
}, | ||
}; | ||
|
||
use crate::{ | ||
syntax::vars_block::VarsBlock, | ||
|
||
ui::{ | ||
ui_base::UI, | ||
compress_alerts::CompressAlerts, | ||
} | ||
}; | ||
|
||
pub struct PathCompress; | ||
|
||
impl PathCompress { | ||
|
||
pub fn compress(contents: &str) -> io::Result<()> { | ||
if let Some(zip_file) = VarsBlock::get_compress(contents) { | ||
UI::section_header("Compressing files"); | ||
|
||
let folder_path = VarsBlock::get_path(contents); | ||
|
||
let output_path = Path::new(&zip_file); | ||
let output_file = File::create(output_path)?; | ||
|
||
let mut zip = zip::ZipWriter::new(output_file); | ||
let options: FileOptions<ExtendedFileOptions> = FileOptions::default() | ||
.compression_method(CompressionMethod::Deflated) | ||
.unix_permissions(0o755); | ||
|
||
for entry in WalkDir::new(&folder_path) { | ||
let entry = entry?; | ||
let path = entry.path(); | ||
let name = path.strip_prefix(Path::new(&folder_path)).unwrap(); | ||
|
||
if path.is_file() { | ||
let file = name.to_str().unwrap(); | ||
CompressAlerts::added(&file, &zip_file); | ||
|
||
zip.start_file(name.to_str().unwrap(), options.clone())?; | ||
|
||
let mut f = File::open(path)?; | ||
let mut buffer = Vec::new(); | ||
|
||
f.read_to_end(&mut buffer)?; | ||
zip.write_all(&buffer)?; | ||
} | ||
} | ||
|
||
zip.finish()?; | ||
CompressAlerts::completed(&zip_file); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod compress; | ||
pub mod checksum; | ||
pub mod download; | ||
pub mod read_list; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
extern crate colored; | ||
|
||
use colored::*; | ||
|
||
use crate::{ | ||
ui::emojis::Emojis, | ||
system::system::System, | ||
}; | ||
|
||
pub struct CompressAlerts; | ||
|
||
impl CompressAlerts { | ||
|
||
pub fn added(file: &str, zip_file: &str) { | ||
let current_datetime = System::date_time(); | ||
|
||
println!( | ||
"[{}] -> Added {} in: {}. {}", | ||
current_datetime.green(), | ||
file.blue(), | ||
zip_file.cyan(), | ||
Emojis::ADD | ||
); | ||
} | ||
|
||
pub fn completed(zip_file: &str) { | ||
let current_datetime = System::date_time(); | ||
|
||
println!( | ||
"[{}] -> All files in the folder have been compressed into {}. {}", | ||
current_datetime.green(), | ||
zip_file.blue(), | ||
Emojis::COMPRESS | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters