Skip to content

Commit c56e636

Browse files
author
lith
committed
auto commit 2.0
1 parent f6f788f commit c56e636

7 files changed

Lines changed: 222 additions & 27 deletions

File tree

FileZip/Cmd/Copy.cs

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
5+
using SharpCompress;
6+
7+
using Vit.ConsoleUtil;
8+
9+
namespace FileZip.Cmd
10+
{
11+
public class Copy
12+
{
13+
#region copy
14+
[Command("copy")]
15+
[Remarks("复制文件(夹)")]
16+
[Remarks("参数说明:")]
17+
[Remarks("-i[--input] 源文件(夹),例如 \"/data/a.txt\" \"/data/files\"")]
18+
[Remarks("-o[--output] 目标文件(夹)")]
19+
[Remarks("-f[--file] 若指定,则输出每一个文件信息")]
20+
[Remarks("-d[--dir] 若指定,则输出每一个文件夹信息")]
21+
[Remarks("-r[--remove] 若指定,则在copy完后删除源文件(夹)")]
22+
[Remarks("--overwrite 若指定,则强制覆盖已存在文件,默认:false")]
23+
[Remarks("示例: copy -i \"/data/files\" -o \"/data/files2\" --file --dir --overwrite ")]
24+
public static void copy(string[] args)
25+
{
26+
#region (x.1) get arg
27+
string input = ConsoleHelp.GetArg(args, "-i") ?? ConsoleHelp.GetArg(args, "--input");
28+
if (string.IsNullOrEmpty(input))
29+
{
30+
ConsoleHelp.Log("请指定源文件(夹)");
31+
return;
32+
}
33+
34+
string output = ConsoleHelp.GetArg(args, "-o") ?? ConsoleHelp.GetArg(args, "--output");
35+
36+
file = !(ConsoleHelp.GetArg(args, "-f") == null && ConsoleHelp.GetArg(args, "--file") == null);
37+
dir = !(ConsoleHelp.GetArg(args, "-d") == null && ConsoleHelp.GetArg(args, "--dir") == null);
38+
remove = !(ConsoleHelp.GetArg(args, "-r") == null && ConsoleHelp.GetArg(args, "--remove") == null);
39+
overwrite = !(ConsoleHelp.GetArg(args, "--overwrite") == null);
40+
41+
#endregion
42+
43+
44+
#region (x.2) 开始copy
45+
46+
ConsoleHelp.Log("开始copy");
47+
ConsoleHelp.Log("源文件(夹):" + input);
48+
ConsoleHelp.Log("目标文件(夹):" + output);
49+
50+
file_sumCount = 0;
51+
file_curCount = 0;
52+
dir_sumCount = 0;
53+
dir_curCount = 0;
54+
55+
56+
var sourceDir = new DirectoryInfo(input);
57+
if (sourceDir.Exists)
58+
{
59+
if (file || dir)
60+
{
61+
GetDirCount(sourceDir);
62+
ConsoleHelp.Log($"[{dir_curCount}/{dir_sumCount} d] [{file_curCount}/{file_sumCount} f]");
63+
}
64+
CopyDir(sourceDir, output);
65+
}
66+
else if (File.Exists(input))
67+
{
68+
file_curCount++;
69+
CopyFile(input, output);
70+
}
71+
else
72+
{
73+
ConsoleHelp.Log("文件(夹)不存在");
74+
return;
75+
}
76+
77+
ConsoleHelp.Log($"[{dir_curCount}/{dir_sumCount} d] [{file_curCount}/{file_sumCount} f]");
78+
ConsoleHelp.Log("文件copy成功!!!");
79+
80+
#endregion
81+
82+
}
83+
84+
static bool remove = false;
85+
86+
static bool file = false;
87+
static int file_sumCount = 0;
88+
static int file_curCount = 0;
89+
90+
static bool dir = false;
91+
static int dir_sumCount = 0;
92+
static int dir_curCount = 0;
93+
static bool overwrite = true;
94+
static void GetDirCount(DirectoryInfo sourceDir)
95+
{
96+
dir_sumCount++;
97+
sourceDir.EnumerateDirectories().ForEach(GetDirCount);
98+
if (file) file_sumCount += sourceDir.EnumerateFiles().Count();
99+
}
100+
101+
102+
static void OnFile(string fileName)
103+
{
104+
if (file)
105+
ConsoleHelp.Log($"[{DateTime.Now.ToString("HH:mm:ss")}][{dir_curCount}/{dir_sumCount} d] [{file_curCount}/{file_sumCount} f] file " + fileName);
106+
}
107+
static void OnDir(string fileName)
108+
{
109+
if (dir) ConsoleHelp.Log($"[{DateTime.Now.ToString("HH:mm:ss")}][{dir_curCount}/{dir_sumCount} d] [{file_curCount}/{file_sumCount} f] dir " + fileName);
110+
}
111+
static void CopyFile(string source, string dest)
112+
{
113+
file_curCount++;
114+
if (overwrite || !File.Exists(dest))
115+
{
116+
File.Copy(source, dest, overwrite);
117+
}
118+
if (remove) File.Delete(source);
119+
OnFile(source);
120+
}
121+
122+
123+
static bool CopyDir(DirectoryInfo sourceDir, string destDir)
124+
{
125+
dir_curCount++;
126+
if (!sourceDir.Exists) return false;
127+
128+
Directory.CreateDirectory(destDir);
129+
130+
var dirs = sourceDir.GetDirectories();
131+
if (dirs.Any())
132+
{
133+
foreach (var child in dirs)
134+
{
135+
CopyDir(child, Path.Combine(destDir, child.Name));
136+
}
137+
}
138+
139+
var files = sourceDir.GetFiles();
140+
if (files.Any())
141+
{
142+
foreach (var child in files)
143+
{
144+
CopyFile(child.FullName, Path.Combine(destDir, child.Name));
145+
}
146+
}
147+
OnDir(sourceDir.FullName);
148+
if (remove) sourceDir.Delete();
149+
return true;
150+
}
151+
152+
#endregion
153+
154+
155+
156+
157+
}
158+
}

FileZip/Cmd/Zip.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class Zip
1414
[Remarks("支持格式: .zip, .gz, .tar")]
1515
[Remarks("参数说明:")]
1616
[Remarks("-i[--input] 待压缩文件(夹),例如 \"/data/a.txt\" \"/data/files\"")]
17-
[Remarks("-o[--output] 压缩输出文件m,使用后缀指定格式压缩,(若不指定,输出到待压缩文件所在目录)")]
17+
[Remarks("-o[--output] 压缩输出文件,使用后缀指定格式压缩,(若不指定,输出到待压缩文件所在目录)")]
1818
[Remarks("-p[--progress] 进度信息间隔。如0.1代表进度每增加10%,提示一次。默认0.1")]
1919
[Remarks("-f[--file] 若指定,则输出每一个文件信息")]
2020
[Remarks("示例: zip -i \"/data/files\" -o \"/data/files.zip\" -p 0.1 -f")]

FileZip/ConsoleUtil/ConsoleHelp.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public static void Exec(string[] args)
6262
//arg.AddRange(new[] { "-o", "T:\\temp\\un7z" });
6363
//args = arg.ToArray();
6464

65-
#region (x.1)通过反射获取所有命令
65+
#region (x.1)通过反射获取所有命令
6666
var cmdMap =
6767
//获取所有type
6868
Assembly.GetEntryAssembly().GetTypes()
@@ -78,7 +78,7 @@ public static void Exec(string[] args)
7878
#endregion
7979

8080

81-
#region (x.2)若未指定命令名称,则输出帮助文档
81+
#region (x.2)若未指定命令名称,则输出帮助文档
8282
if (args == null || args.Length == 0 || string.IsNullOrEmpty(args[0]))
8383
{
8484
#region 输出命令帮助文档
@@ -98,18 +98,18 @@ public static void Exec(string[] args)
9898
#endregion
9999

100100

101-
#region (x.3)通过第一个参数查找命令并调用
101+
#region (x.3)通过第一个参数查找命令并调用
102102
try
103103
{
104104
cmdMap.TryGetValue(args[0], out var method);
105105

106106
if (method == null)
107107
{
108-
ConsoleHelp.Log($"出错:命令 { args[0] } 不存在!");
108+
ConsoleHelp.Log($"出错:命令 {args[0]} 不存在!");
109109
return;
110110
}
111-
ConsoleHelp.Log("------------------------------");
112-
ConsoleHelp.Log($"开始执行命令 { args[0] } ...");
111+
ConsoleHelp.Log("------------------------------");
112+
ConsoleHelp.Log($"开始执行命令 {args[0]} ...");
113113
ConsoleHelp.Log("---------------");
114114

115115
method.Invoke(null, new object[] { args });

FileZip/FileZip.csproj

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

3-
<PropertyGroup>
4-
<publish>FileZip</publish>
5-
<docker>filezip</docker>
6-
</PropertyGroup>
3+
<PropertyGroup>
4+
<publish>FileZip</publish>
5+
<docker>filezip</docker>
6+
</PropertyGroup>
77

8-
<PropertyGroup>
9-
<OutputType>Exe</OutputType>
10-
<TargetFramework>netcoreapp2.1</TargetFramework>
11-
<Version>1.6</Version>
12-
</PropertyGroup>
8+
<PropertyGroup>
9+
<OutputType>Exe</OutputType>
10+
<TargetFramework>net6.0</TargetFramework>
11+
<Version>2.0</Version>
12+
</PropertyGroup>
1313

14-
<PropertyGroup>
15-
<Authors>Lith</Authors>
16-
<Description>文件加解压工具</Description>
17-
<PackageProjectUrl>https://github.com/serset/FileZip</PackageProjectUrl>
18-
</PropertyGroup>
14+
<PropertyGroup>
15+
<Authors>Lith</Authors>
16+
<Description>zip/unzip/marge/copy tool for file and directory</Description>
17+
<PackageProjectUrl>https://github.com/serset/FileZip</PackageProjectUrl>
18+
</PropertyGroup>
1919

2020

21-
<ItemGroup>
22-
<PackageReference Include="sharpcompress" Version="0.28.3" />
23-
</ItemGroup>
21+
<ItemGroup>
22+
<PackageReference Include="sharpcompress" Version="0.33.0" />
23+
</ItemGroup>
2424

2525

2626
</Project>

FileZip/Program.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ public class Program
1212
public static void Main(string[] args)
1313
{
1414

15-
//var arg = new List<string>() { "help" };
16-
//args = arg.ToArray();
15+
var arg = new List<string>() { "help" };
16+
args = arg.ToArray();
1717

1818

1919
//var arg = new List<string>() { "unzip" };
@@ -26,6 +26,13 @@ public static void Main(string[] args)
2626
////arg.AddRange(new[] { "-o", "T:\\temp\\tileset.zip" });
2727
//args = arg.ToArray();
2828

29+
30+
// copy -i \"/data/files\" -o \"/data/files2\" --file --dir
31+
//var arg = new List<string>() { "copy", "--file", "--dir" };
32+
//arg.AddRange(new[] { "-i", "L:\\file" });
33+
//arg.AddRange(new[] { "-o", "T:\\file" });
34+
//args = arg.ToArray();
35+
2936
Vit.ConsoleUtil.ConsoleHelp.Log("[FileZip] version: " + System.Diagnostics.FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetEntryAssembly().Location).FileVersion);
3037

3138
ConsoleHelp.Exec(args);

Publish/ReleaseFile/docker-image/filezip/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM serset/dotnet:2.1
1+
FROM serset/dotnet:runtime-6.0
22

33

44
#(x.1)install zip unzip

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,25 @@ docker run --rm -it -v /root/docker/file:/root/file serset/filezip dotnet FileZi
2727

2828
docker run --rm -it -v /root/docker/file:/root/file serset/filezip filezip unzip -i /root/file/m.7z -o /root/file/m
2929

30+
31+
docker run --rm -it \
32+
-v /srv/dl:/root/file/in \
33+
-v /srv/temp/:/root/file/out \
34+
serset/filezip dotnet FileZip.dll unzip -i /root/file/in/file.zip -o /root/file/out/file -p 0.01 \
35+
36+
nohup docker run --rm -t \
37+
-v /srv/dl:/root/file/in \
38+
-v /srv/temp:/root/file/out \
39+
serset/filezip dotnet FileZip.dll unzip -i /root/file/in/file.zip -o /root/file/out/file -p 0.01 \
40+
> /srv/temp/unzip.log 2>&1 &
41+
42+
43+
# 查看进程
44+
ps -ef | grep 'FileZip'
45+
46+
# 杀死进程
47+
kill -s 9 12311
48+
3049
```
3150

3251
## 3. 命令说明
@@ -37,6 +56,17 @@ help
3756
-c[--command] 要查询的命令。若不指定则返回所有命令的文档。如 help
3857
示例: help -c help
3958
---------------
59+
copy
60+
复制文件(夹)
61+
参数说明:
62+
-i[--input] 源文件(夹),例如 "/data/a.txt" "/data/files"
63+
-o[--output] 目标文件(夹)
64+
-f[--file] 若指定,则输出每一个文件信息
65+
-d[--dir] 若指定,则输出每一个文件夹信息
66+
-r[--remove] 若指定,则在copy完后删除源文件(夹)
67+
--overwrite 若指定,则强制覆盖已存在文件,默认:false
68+
示例: copy -i "/data/files" -o "/data/files2" --file --dir --overwrite
69+
---------------
4070
marge
4171
合并文件。参数说明:
4272
-i[--input] 待合并的文件夹 或 文件查询字符串。 如 /data/a/1.7z.*

0 commit comments

Comments
 (0)