ソースを参照

feat: commit `unzip_file` function

mrxiaozhuox 2 年 前
コミット
b28043a944
2 ファイル変更12 行追加1 行削除
  1. 11 0
      src/plugin/interface/fs.rs
  2. 1 1
      src/tools.rs

+ 11 - 0
src/plugin/interface/fs.rs

@@ -5,6 +5,8 @@ use std::{
 
 use mlua::UserData;
 
+use crate::tools::extract_zip;
+
 pub struct PluginFileSystem;
 impl UserData for PluginFileSystem {
     fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
@@ -54,5 +56,14 @@ impl UserData for PluginFileSystem {
             file.write_all(content.as_bytes())?;
             Ok(())
         });
+        methods.add_function("unzip_file", |_, args: (String, String)| {
+            let file = PathBuf::from(args.0);
+            let target = PathBuf::from(args.1);
+            let res = extract_zip(&file, &target);
+            if let Err(e) = res {
+                return Err(mlua::Error::RuntimeError(e.to_string()));
+            }
+            Ok(())
+        });
     }
 }

+ 1 - 1
src/tools.rs

@@ -313,7 +313,7 @@ impl Tool {
     }
 }
 
-fn extract_zip(file: &Path, target: &Path) -> anyhow::Result<()> {
+pub fn extract_zip(file: &Path, target: &Path) -> anyhow::Result<()> {
     let zip_file = std::fs::File::open(&file)?;
     let mut zip = zip::ZipArchive::new(zip_file)?;