1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
use std::fs::File;
use std::path::{Path, PathBuf};
use std::{fs, io};
use zip::ZipArchive;
pub struct Unzip;
impl Unzip {
///
/// unzip file from a file path in the format of &str
///
/// # Example
///
/// ```
///use simple_zip::unzip::Unzip;
///
///let path = "./a.zip";
///Unzip::local_str(&path);
/// ```
///
pub fn local_str(filepath: &str) {
let filename = Path::new(filepath);
let file = File::open(&filename).unwrap();
let mut archive = ZipArchive::new(file).unwrap();
for i in 0..archive.len() {
let mut file = archive.by_index(i).unwrap();
let outpath = match file.enclosed_name() {
Some(path) => path.to_owned(),
None => continue,
};
{
let comment = file.comment();
if !comment.is_empty() {
println!("File: {}, comment: {}", i, comment);
}
}
if (*file.name()).ends_with("/") {
fs::create_dir_all(&outpath).unwrap();
} else {
if let Some(p) = outpath.parent() {
if !p.exists() {
fs::create_dir_all(&p).unwrap();
}
}
let mut outfile = File::create(&outpath).unwrap();
io::copy(&mut file, &mut outfile).unwrap();
}
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
if let Some(mode) = file.unix_mode() {
fs::set_permissions(&outpath, fs::Permissions::from_mode(mode)).unwrap();
}
}
}
}
///
/// unzip file from a PathBuffer
///
/// # Example
///
/// ```
///use std::path::Path;
/// use simple_zip::unzip::Unzip;
///
///let path = "./a.zip";
///let pathbuf = Path::new(&path);
///Unzip::local_buffer(&pathbuf);
/// ```
///
pub fn local_buffer(filepath: &Path) {
let file = File::open(&filepath).unwrap();
let mut archive = ZipArchive::new(file).unwrap();
for i in 0..archive.len() {
let mut file = archive.by_index(i).unwrap();
let outpath = match file.enclosed_name() {
Some(path) => path.to_owned(),
None => continue,
};
{
let comment = file.comment();
if !comment.is_empty() {
println!("File: {}, comment: {}", i, comment);
}
}
if (*file.name()).ends_with("/") {
fs::create_dir_all(&outpath).unwrap();
} else {
if let Some(p) = outpath.parent() {
if !p.exists() {
fs::create_dir_all(&p).unwrap();
}
}
let mut outfile = File::create(&outpath).unwrap();
io::copy(&mut file, &mut outfile).unwrap();
}
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
if let Some(mode) = file.unix_mode() {
fs::set_permissions(&outpath, fs::Permissions::from_mode(mode)).unwrap();
}
}
}
}
}