simple_zip/
zip.rs

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