os-bridge 0.1.4

A cross platform API bridge based on Rust language
Documentation
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
use std::fs::{self, File};
use std::io::{self, BufReader, Write};
use std::path::Path;
use walkdir::WalkDir;
use zip::read::ZipArchive;
use zip::result::ZipError;
use zip::unstable::write::FileOptionsExt;
use zip::{write::FileOptions, CompressionMethod, ZipWriter};

use crate::core::Zip;
use crate::errors::BridgeError;
use crate::BridgeResult;

pub struct Unzip {
  pub zip_pwd: Option<String>,
}

impl Unzip {
  pub fn new(zip_pwd: Option<String>) -> Self {
    Self { zip_pwd: zip_pwd }
  }
}

impl Zip for Unzip {
  /// @description:  Decompression extractor
  /// @parama zip_src_path: Decompression target path
  /// @parama zip_output_path: Output Path
  fn extract<P>(&self, zip_input_path: P, zip_output_path: P) -> BridgeResult<bool>
  where
    P: AsRef<Path>,
  {
    if !zip_input_path.as_ref().exists() {
      return Err(BridgeError::from(format!(
        "The file '{}' does not exist",
        zip_input_path.as_ref().display()
      )));
    }
    if let Some(parent) = zip_output_path.as_ref().parent() {
      if !parent.exists() {
        fs::create_dir_all(parent).map_err(|err| BridgeError::from(err.to_string()))?;
      }
    }
    let file = File::open(zip_input_path)?;
    let mut archive =
      ZipArchive::new(BufReader::new(file)).map_err(|err| BridgeError::from(err.to_string()))?;
    match &self.zip_pwd {
      Some(pwd) => {
        for i in 0..archive.len() {
          let mut file = match archive.by_index_decrypt(i, pwd.as_bytes()) {
            Ok(f) => f,
            Err(ZipError::InvalidPassword) => {
              return Err(BridgeError::from(
                "Password error, unable to decompress the file!",
              ));
            }
            Err(err) => return Err(BridgeError::from(err.to_string())),
          };
          let outpath = Path::new(zip_output_path.as_ref()).join(file.name());
          if file.is_dir() {
            std::fs::create_dir_all(&outpath).map_err(|err| BridgeError::from(err.to_string()))?;
          } else {
            if let Some(parent) = outpath.parent() {
              if !parent.exists() {
                std::fs::create_dir_all(parent)?;
              }
            }
            let mut outfile =
              File::create(&outpath).map_err(|err| BridgeError::from(err.to_string()))?;
            io::copy(&mut file, &mut outfile).map_err(|err| BridgeError::from(err.to_string()))?;
          }
        }
      }
      None => {
        for i in 0..archive.len() {
          let mut file = archive
            .by_index(i)
            .map_err(|err| BridgeError::from(err.to_string()))?;
          let outpath = Path::new(zip_output_path.as_ref()).join(file.name());
          if file.name().ends_with('/') {
            std::fs::create_dir_all(&outpath).map_err(|err| BridgeError::from(err.to_string()))?;
          } else {
            if let Some(parent) = outpath.parent() {
              if !parent.exists() {
                std::fs::create_dir_all(parent)
                  .map_err(|err| BridgeError::from(err.to_string()))?;
              }
            }
            let mut outfile =
              File::create(&outpath).map_err(|err| BridgeError::from(err.to_string()))?;
            io::copy(&mut file, &mut outfile).map_err(|err| BridgeError::from(err.to_string()))?;
          }
        }
      }
    }

    Ok(true)
  }

  /// @description:  Single file compression
  /// @parama zip_input_path: Compression target path
  /// @parama zip_output_path: Output Path
  fn file_compression<P>(&self, zip_input_path: P, zip_output_path: P) -> BridgeResult<bool>
  where
    P: AsRef<Path>,
  {
    if !zip_input_path.as_ref().is_file() {
      return Err(BridgeError::from("Please input the file path".to_string()));
    }
    if let Some(parent) = zip_output_path.as_ref().parent() {
      if !parent.exists() {
        fs::create_dir_all(parent).map_err(|err| BridgeError::from(err.to_string()))?;
      }
    }
    let file = fs::File::create(zip_output_path.as_ref())
      .map_err(|err| BridgeError::from(err.to_string()))?;
    let mut zip = ZipWriter::new(file);
    let mut options: FileOptions<'_, ()> = FileOptions::default()
      .compression_method(CompressionMethod::Deflated)
      .compression_level(Some(9))
      .large_file(true)
      .unix_permissions(0o755);
    if self.zip_pwd.is_some() {
      options = options.with_deprecated_encryption(
        &self
          .zip_pwd
          .clone()
          .unwrap_or(Default::default())
          .to_string()
          .as_bytes()
          .to_vec(),
      );
    }
    match zip_input_path.as_ref().file_name() {
      Some(file_name) => {
        if let Some(s) = file_name.to_os_string().to_str() {
          zip
            .start_file(s, options)
            .map_err(|e| BridgeError::from(format!("{}", e.to_string())))?;
          let src_file_content = fs::read(zip_input_path.as_ref())
            .map_err(|err| BridgeError::from(format!("File read error: {}", err.to_string())))?;
          zip.write_all(&src_file_content)?;
          zip
            .finish()
            .map_err(|err| BridgeError::from(err.to_string()))?;
        } else {
          return Err(BridgeError::from("The file does not exist"));
        }
      }
      None => return Err(BridgeError::from("The file does not exist")),
    }
    Ok(true)
  }

  /// @description:  Single folder compression
  /// @parama zip_input_path: Compression target path
  /// @parama zip_output_path: Output Path
  fn single_folder_compression<P: AsRef<Path>>(
    &self,
    zip_input_path: P,
    zip_output_path: P,
  ) -> BridgeResult<bool> {
    if !zip_input_path.as_ref().exists() {
      return Err(BridgeError::from(format!(
        "The folder path '{}' does not exist",
        zip_input_path.as_ref().display()
      )));
    }
    if let Some(parent) = zip_output_path.as_ref().parent() {
      if !parent.exists() {
        fs::create_dir_all(parent).map_err(|err| BridgeError::from(err.to_string()))?;
      }
    }
    let zip_file = File::create(zip_output_path)?;
    let mut zip_writer = ZipWriter::new(zip_file);
    let base_dir = zip_input_path.as_ref().parent().unwrap_or(Path::new(""));
    self
      .add_dir(&mut zip_writer, base_dir, zip_input_path.as_ref())
      .map_err(|err| BridgeError::WithMsg(err.to_string()))?;
    zip_writer
      .finish()
      .map_err(|err| BridgeError::WithMsg(err.to_string()))?;
    Ok(true)
  }

  /// batch compression
  /// Please provide a list path
  /// @parama zip_input_paths: [zip_input_path1, zip_input_path2, ...]
  /// @parama zip_output_path: Output Path
  fn batch_compression<P>(&self, zip_input_paths: Vec<P>, zip_output_path: P) -> BridgeResult<bool>
  where
    P: AsRef<Path>,
  {
    if zip_input_paths.is_empty() {
      return Err(BridgeError::from("The file path cannot be empty"));
    }
    if let Some(parent) = zip_output_path.as_ref().parent() {
      if !parent.exists() {
        std::fs::create_dir_all(parent).map_err(|err| BridgeError::WithMsg(err.to_string()))?;
      }
    }
    for p in &zip_input_paths {
      if !p.as_ref().exists() {
        return Err(BridgeError::from(format!(
          "The path '{}' does not exist",
          p.as_ref().display()
        )));
      }
    }
    let zip_file = File::create(zip_output_path)?;
    let mut zip_writer = ZipWriter::new(zip_file);
    for path in zip_input_paths {
      self
        .add_path(&mut zip_writer, &path)
        .map_err(|err| BridgeError::from(err.to_string()))?;
    }
    zip_writer
      .finish()
      .map_err(|err| BridgeError::from(err.to_string()))?;
    Ok(true)
  }

  /// @description:  Add directory
  /// @parama zip_writer: ZipWriter
  /// @parama base_dir: Directory path
  /// @parama current_dir: Base directory
  fn add_dir<P: AsRef<Path>>(
    &self,
    zip_writer: &mut ZipWriter<File>,
    base_dir: P,
    current_dir: P,
  ) -> BridgeResult<bool> {
    if !current_dir.as_ref().exists() {
      return Err(BridgeError::from(format!(
        "The path '{}' does not exist",
        current_dir.as_ref().display()
      )));
    }
    for entry in fs::read_dir(current_dir).map_err(|err| BridgeError::from(err.to_string()))? {
      let entry = entry.map_err(|err| BridgeError::from(err.to_string()))?;
      let entry_path = entry.path();
      let relative_path = entry_path
        .strip_prefix(base_dir.as_ref())
        .map_err(|err| BridgeError::from(err.to_string()))?;
      let zip_path = format!("{}", relative_path.display()).replace("\\", "/");
      if entry_path.is_dir() {
        let mut options: FileOptions<'_, ()> = FileOptions::default()
          .compression_method(CompressionMethod::Deflated)
          .large_file(true)
          .unix_permissions(0o755);
        if self.zip_pwd.is_some() {
          options = options.with_deprecated_encryption(
            &self
              .zip_pwd
              .clone()
              .unwrap_or(Default::default())
              .to_string()
              .as_bytes()
              .to_vec(),
          );
        }
        zip_writer
          .add_directory(zip_path, options)
          .map_err(|err| BridgeError::from(err.to_string()))?;
        self
          .add_dir(zip_writer, base_dir.as_ref(), &entry_path)
          .map_err(|err| BridgeError::from(err.to_string()))?;
      } else {
        // 添加文件
        let mut options: FileOptions<'_, ()> = FileOptions::default()
          .compression_method(CompressionMethod::Deflated)
          .large_file(true)
          .unix_permissions(0o755);
        if self.zip_pwd.is_some() {
          options = options.with_deprecated_encryption(
            &self
              .zip_pwd
              .clone()
              .unwrap_or(Default::default())
              .to_string()
              .as_bytes()
              .to_vec(),
          );
        }
        let mut file = File::open(&entry_path)?;
        zip_writer
          .start_file(zip_path, options)
          .map_err(|err| BridgeError::from(err.to_string()))?;
        io::copy(&mut file, zip_writer)?;
      }
    }
    Ok(true)
  }

  /// @description: Add a path to the zip file
  /// @parama zip_writer: zip_writer
  /// @parama path: Path
  fn add_path<P: AsRef<Path>>(
    &self,
    zip_writer: &mut ZipWriter<File>,
    path: P,
  ) -> BridgeResult<bool> {
    if path.as_ref().is_file() {
      let file_name = path
        .as_ref()
        .file_name()
        .and_then(|n| n.to_str())
        .unwrap_or(Default::default());
      let mut options: FileOptions<'_, ()> = FileOptions::default()
        .compression_method(CompressionMethod::Deflated)
        .large_file(true)
        .unix_permissions(0o755);
      if self.zip_pwd.is_some() {
        options = options.with_deprecated_encryption(
          &self
            .zip_pwd
            .clone()
            .unwrap_or(Default::default())
            .to_string()
            .as_bytes()
            .to_vec(),
        );
      }
      zip_writer
        .start_file(file_name, options)
        .map_err(|err| BridgeError::new(&err.to_string()))?;
      let mut file = fs::File::open(path).map_err(|err| BridgeError::new(&err.to_string()))?;
      io::copy(&mut file, zip_writer).map_err(|err| BridgeError::new(&err.to_string()))?;
    } else if path.as_ref().is_dir() {
      let path_ref = path.as_ref();
      if self
        .check_folder_is_empty(path_ref)
        .map_err(|err| BridgeError::new(&err.to_string()))?
      {
        let create_path = path_ref
          .strip_prefix(path.as_ref().parent().unwrap_or(Path::new("")))
          .unwrap_or(Path::new(""))
          .to_str()
          .unwrap_or_default()
          .replace("\\", "/");
        let mut options: FileOptions<'_, ()> = FileOptions::default()
          .compression_method(CompressionMethod::Deflated)
          .large_file(true)
          .unix_permissions(0o755);
        if self.zip_pwd.is_some() {
          options = options.with_deprecated_encryption(
            &self
              .zip_pwd
              .clone()
              .unwrap_or(Default::default())
              .to_string()
              .as_bytes()
              .to_vec(),
          );
        }
        let out_path = create_path.replace("\\", "/");
        zip_writer
          .add_directory(out_path, options)
          .map_err(|err| BridgeError::new(&err.to_string()))?;
      } else {
        let prefix = path_ref.parent().unwrap_or(Path::new(""));
        self.add_dir(zip_writer, prefix, path.as_ref())?;
      }
    }
    Ok(true)
  }

  /// @description Check whether the folder is empty
  /// @parama path: Path
  fn check_folder_is_empty<P: AsRef<Path>>(&self, path: P) -> BridgeResult<bool> {
    let path = path.as_ref();
    if path.is_dir() {
      let entries = fs::read_dir(path)?;
      for entry in entries {
        let entry = entry?;
        if entry.file_type()?.is_file() || entry.file_type()?.is_dir() {
          return Ok(false);
        }
      }
      Ok(true)
    } else {
      return Err(BridgeError::new("Path is not a directory"));
    }
  }

  fn calculate_size<P>(&self, path: P) -> BridgeResult<f64>
  where
    P: AsRef<Path>,
  {
    let mut paths = vec![path.as_ref().to_path_buf()];
    let mut res_size = 0u64;
    while let Some(path) = paths.pop() {
      let meta =
        std::fs::symlink_metadata(&path).map_err(|err| BridgeError::from(err.to_string()))?;
      let file_type = meta.file_type();
      if file_type.is_dir() {
        let entries = std::fs::read_dir(path).map_err(|err| BridgeError::from(err.to_string()))?;
        for entry in entries {
          let entry = entry.map_err(|err| BridgeError::from(err.to_string()))?;
          let path = entry.path();
          paths.push(path);
        }
      }
      if file_type.is_file() {
        res_size += meta.len();
      }
    }
    Ok(res_size as f64)
  }

  fn get_size<P>(&self, path: P) -> u64
  where
    P: AsRef<Path>,
  {
    let total_size = WalkDir::new(path)
      .min_depth(1)
      .max_depth(3)
      .into_iter()
      .filter_map(|entry| entry.ok())
      .filter_map(|entry| entry.metadata().ok())
      .filter(|metadata| metadata.is_file())
      .fold(0, |acc, m| acc + m.len());
    total_size
  }
}