lith 1 gadu atpakaļ
vecāks
revīzija
1d7b3d0963

+ 158 - 0
FileZip/Cmd/Copy.cs

@@ -0,0 +1,158 @@
+using System;
+using System.IO;
+using System.Linq;
+
+using SharpCompress;
+
+using Vit.ConsoleUtil;
+
+namespace FileZip.Cmd
+{
+    public class Copy
+    {
+        #region copy
+        [Command("copy")]
+        [Remarks("复制文件(夹)")]
+        [Remarks("参数说明:")]
+        [Remarks("-i[--input] 源文件(夹),例如 \"/data/a.txt\" \"/data/files\"")]
+        [Remarks("-o[--output] 目标文件(夹)")]
+        [Remarks("-f[--file] 若指定,则输出每一个文件信息")]
+        [Remarks("-d[--dir] 若指定,则输出每一个文件夹信息")]
+        [Remarks("-r[--remove] 若指定,则在copy完后删除源文件(夹)")]
+        [Remarks("--overwrite 若指定,则强制覆盖已存在文件,默认:false")]
+        [Remarks("示例: copy -i \"/data/files\" -o \"/data/files2\" --file --dir --overwrite ")]
+        public static void copy(string[] args)
+        {
+            #region (x.1) get arg
+            string input = ConsoleHelp.GetArg(args, "-i") ?? ConsoleHelp.GetArg(args, "--input");
+            if (string.IsNullOrEmpty(input))
+            {
+                ConsoleHelp.Log("请指定源文件(夹)");
+                return;
+            }
+
+            string output = ConsoleHelp.GetArg(args, "-o") ?? ConsoleHelp.GetArg(args, "--output");
+
+            file = !(ConsoleHelp.GetArg(args, "-f") == null && ConsoleHelp.GetArg(args, "--file") == null);
+            dir = !(ConsoleHelp.GetArg(args, "-d") == null && ConsoleHelp.GetArg(args, "--dir") == null);
+            remove = !(ConsoleHelp.GetArg(args, "-r") == null && ConsoleHelp.GetArg(args, "--remove") == null);
+            overwrite = !(ConsoleHelp.GetArg(args, "--overwrite") == null);
+
+            #endregion
+
+
+            #region (x.2) 开始copy
+
+            ConsoleHelp.Log("开始copy");
+            ConsoleHelp.Log("源文件(夹):" + input);
+            ConsoleHelp.Log("目标文件(夹):" + output);
+
+            file_sumCount = 0;
+            file_curCount = 0;
+            dir_sumCount = 0;
+            dir_curCount = 0;
+
+
+            var sourceDir = new DirectoryInfo(input);
+            if (sourceDir.Exists)
+            {
+                if (file || dir)
+                {
+                    GetDirCount(sourceDir);
+                    ConsoleHelp.Log($"[{dir_curCount}/{dir_sumCount} d] [{file_curCount}/{file_sumCount} f]");
+                }
+                CopyDir(sourceDir, output);
+            }
+            else if (File.Exists(input))
+            {
+                file_curCount++;
+                CopyFile(input, output);
+            }
+            else
+            {
+                ConsoleHelp.Log("文件(夹)不存在");
+                return;
+            }
+
+            ConsoleHelp.Log($"[{dir_curCount}/{dir_sumCount} d] [{file_curCount}/{file_sumCount} f]");
+            ConsoleHelp.Log("文件copy成功!!!");
+
+            #endregion
+
+        }
+
+        static bool remove = false;
+
+        static bool file = false;
+        static int file_sumCount = 0;
+        static int file_curCount = 0;
+
+        static bool dir = false;
+        static int dir_sumCount = 0;
+        static int dir_curCount = 0;
+        static bool overwrite = true;
+        static void GetDirCount(DirectoryInfo sourceDir)
+        {
+            dir_sumCount++;
+            sourceDir.EnumerateDirectories().ForEach(GetDirCount);
+            if (file) file_sumCount += sourceDir.EnumerateFiles().Count();
+        }
+
+
+        static void OnFile(string fileName)
+        {
+            if (file)
+                ConsoleHelp.Log($"[{DateTime.Now.ToString("HH:mm:ss")}][{dir_curCount}/{dir_sumCount} d] [{file_curCount}/{file_sumCount} f] file   " + fileName);
+        }
+        static void OnDir(string fileName)
+        {
+            if (dir) ConsoleHelp.Log($"[{DateTime.Now.ToString("HH:mm:ss")}][{dir_curCount}/{dir_sumCount} d] [{file_curCount}/{file_sumCount} f] dir    " + fileName);
+        }
+        static void CopyFile(string source, string dest)
+        {
+            file_curCount++;
+            if (overwrite || !File.Exists(dest))
+            {
+                File.Copy(source, dest, overwrite);
+            }
+            if (remove) File.Delete(source);
+            OnFile(source);
+        }
+
+
+        static bool CopyDir(DirectoryInfo sourceDir, string destDir)
+        {
+            dir_curCount++;
+            if (!sourceDir.Exists) return false;
+
+            Directory.CreateDirectory(destDir);
+
+            var dirs = sourceDir.GetDirectories();
+            if (dirs.Any())
+            {
+                foreach (var child in dirs)
+                {
+                    CopyDir(child, Path.Combine(destDir, child.Name));
+                }
+            }
+
+            var files = sourceDir.GetFiles();
+            if (files.Any())
+            {
+                foreach (var child in files)
+                {
+                    CopyFile(child.FullName, Path.Combine(destDir, child.Name));        
+                }
+            }
+            OnDir(sourceDir.FullName);
+            if (remove) sourceDir.Delete();
+            return true;
+        }
+
+        #endregion
+
+
+
+
+    }
+}

+ 1 - 1
FileZip/Cmd/Zip.cs

@@ -14,7 +14,7 @@ namespace FileZip.Cmd
         [Remarks("支持格式: .zip, .gz, .tar")]
         [Remarks("参数说明:")]
         [Remarks("-i[--input] 待压缩文件(夹),例如 \"/data/a.txt\" \"/data/files\"")]
-        [Remarks("-o[--output] 压缩输出文件m,使用后缀指定格式压缩,(若不指定,输出到待压缩文件所在目录)")]
+        [Remarks("-o[--output] 压缩输出文件,使用后缀指定格式压缩,(若不指定,输出到待压缩文件所在目录)")]
         [Remarks("-p[--progress] 进度信息间隔。如0.1代表进度每增加10%,提示一次。默认0.1")]
         [Remarks("-f[--file] 若指定,则输出每一个文件信息")]
         [Remarks("示例: zip -i \"/data/files\" -o \"/data/files.zip\" -p 0.1 -f")]

+ 6 - 6
FileZip/ConsoleUtil/ConsoleHelp.cs

@@ -62,7 +62,7 @@ namespace Vit.ConsoleUtil
             //arg.AddRange(new[] { "-o", "T:\\temp\\un7z" });
             //args = arg.ToArray();
 
-            #region (x.1)通过反射获取所有命令            
+            #region (x.1)通过反射获取所有命令
             var cmdMap =
                 //获取所有type
                 Assembly.GetEntryAssembly().GetTypes()
@@ -78,7 +78,7 @@ namespace Vit.ConsoleUtil
             #endregion
 
 
-            #region (x.2)若未指定命令名称,则输出帮助文档            
+            #region (x.2)若未指定命令名称,则输出帮助文档
             if (args == null || args.Length == 0 || string.IsNullOrEmpty(args[0]))
             {
                 #region 输出命令帮助文档
@@ -98,18 +98,18 @@ namespace Vit.ConsoleUtil
             #endregion
 
 
-            #region (x.3)通过第一个参数查找命令并调用            
+            #region (x.3)通过第一个参数查找命令并调用
             try
             {
                 cmdMap.TryGetValue(args[0], out var method);
 
                 if (method == null)
                 {
-                    ConsoleHelp.Log($"出错:命令 { args[0] } 不存在!");
+                    ConsoleHelp.Log($"出错:命令 {args[0]} 不存在!");
                     return;
                 }
-                ConsoleHelp.Log("------------------------------");        
-                ConsoleHelp.Log($"开始执行命令 { args[0] } ...");
+                ConsoleHelp.Log("------------------------------");
+                ConsoleHelp.Log($"开始执行命令 {args[0]} ...");
                 ConsoleHelp.Log("---------------");
 
                 method.Invoke(null, new object[] { args });

+ 17 - 17
FileZip/FileZip.csproj

@@ -1,26 +1,26 @@
 <Project Sdk="Microsoft.NET.Sdk">
 
-	<PropertyGroup>
-		<publish>FileZip</publish>
-		<docker>filezip</docker>
-	</PropertyGroup>
+    <PropertyGroup>
+        <publish>FileZip</publish>
+        <docker>filezip</docker>
+    </PropertyGroup>
 
-	<PropertyGroup>
-		<OutputType>Exe</OutputType>
-		<TargetFramework>netcoreapp2.1</TargetFramework>
-		<Version>1.6</Version>
-	</PropertyGroup>
+    <PropertyGroup>
+        <OutputType>Exe</OutputType>
+        <TargetFramework>net6.0</TargetFramework>
+        <Version>2.0</Version>
+    </PropertyGroup>
 
-	<PropertyGroup>
-		<Authors>Lith</Authors>
-		<Description>文件加解压工具</Description>
-		<PackageProjectUrl>https://github.com/serset/FileZip</PackageProjectUrl>
-	</PropertyGroup>
+    <PropertyGroup>
+        <Authors>Lith</Authors>
+        <Description>zip/unzip/marge/copy tool for file and directory</Description>
+        <PackageProjectUrl>https://github.com/serset/FileZip</PackageProjectUrl>
+    </PropertyGroup>
 
 
-	<ItemGroup>
-		<PackageReference Include="sharpcompress" Version="0.28.3" />
-	</ItemGroup>
+    <ItemGroup>
+        <PackageReference Include="sharpcompress" Version="0.33.0" />
+    </ItemGroup>
 
 
 </Project>

+ 9 - 2
FileZip/Program.cs

@@ -12,8 +12,8 @@ namespace Main
         public static void Main(string[] args)
         {
 
-            //var arg = new List<string>() { "help" };
-            //args = arg.ToArray();
+            var arg = new List<string>() { "help" };
+            args = arg.ToArray();
 
 
             //var arg = new List<string>() { "unzip" };
@@ -26,6 +26,13 @@ namespace Main
             ////arg.AddRange(new[] { "-o", "T:\\temp\\tileset.zip" });
             //args = arg.ToArray();
 
+
+            // copy -i \"/data/files\" -o \"/data/files2\" --file --dir
+            //var arg = new List<string>() { "copy", "--file", "--dir" };
+            //arg.AddRange(new[] { "-i", "L:\\file" });
+            //arg.AddRange(new[] { "-o", "T:\\file" });
+            //args = arg.ToArray();
+
             Vit.ConsoleUtil.ConsoleHelp.Log("[FileZip] version: " + System.Diagnostics.FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetEntryAssembly().Location).FileVersion);
 
             ConsoleHelp.Exec(args);

+ 1 - 1
Publish/ReleaseFile/docker-image/filezip/Dockerfile

@@ -1,4 +1,4 @@
-FROM serset/dotnet:2.1
+FROM serset/dotnet:runtime-6.0
 
 
 #(x.1)install zip unzip

+ 30 - 0
README.md

@@ -27,6 +27,25 @@ docker run --rm -it -v /root/docker/file:/root/file serset/filezip dotnet FileZi
 
 docker run --rm -it -v /root/docker/file:/root/file serset/filezip filezip unzip -i /root/file/m.7z -o /root/file/m
 
+
+docker run --rm -it \
+-v /srv/dl:/root/file/in \
+-v /srv/temp/:/root/file/out \
+serset/filezip dotnet FileZip.dll unzip -i /root/file/in/file.zip -o /root/file/out/file -p 0.01 \
+
+nohup docker run --rm -t \
+-v /srv/dl:/root/file/in \
+-v /srv/temp:/root/file/out \
+serset/filezip dotnet FileZip.dll unzip -i /root/file/in/file.zip -o /root/file/out/file -p 0.01 \
+> /srv/temp/unzip.log 2>&1 &
+
+
+# 查看进程
+ps -ef | grep 'FileZip'
+
+# 杀死进程 
+kill -s 9 12311
+
 ```
 
 ## 3. 命令说明
@@ -37,6 +56,17 @@ help
 -c[--command] 要查询的命令。若不指定则返回所有命令的文档。如 help
 示例: help -c help
 ---------------
+copy
+复制文件(夹)
+参数说明:
+-i[--input] 源文件(夹),例如 "/data/a.txt" "/data/files"
+-o[--output] 目标文件(夹)
+-f[--file] 若指定,则输出每一个文件信息
+-d[--dir] 若指定,则输出每一个文件夹信息
+-r[--remove] 若指定,则在copy完后删除源文件(夹)
+--overwrite 若指定,则强制覆盖已存在文件,默认:false
+示例: copy -i "/data/files" -o "/data/files2" --file --dir --overwrite
+---------------
 marge
 合并文件。参数说明:
 -i[--input] 待合并的文件夹 或 文件查询字符串。 如 /data/a/1.7z.*