-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPasswordZip.cs
189 lines (169 loc) · 4.98 KB
/
PasswordZip.cs
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
//
// <PasswordZip>
//
// このプログラムはDotNetZipライブラリを使用して作成たオープンソースライセンスです。。
// ライセンスはDotNetZip dllのライセンスに準拠します(Microsoft Public License (Ms-PL))。
// (ライブラリの取得元:http://dotnetzip.codeplex.com/)
// ライセンスの詳細はLisence.txtをご覧ください。
//
// プログラムの著作権はライセンスとは関係なくK.Tsunezumiに属します。
// このプログラムを使用したことによるあらゆる損害に対しての責任は負いかねます。
// 個人の責任においてご使用をお願いします。
//
using System;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using Ionic.Zip;
namespace cockatiel.cage.net
{
public class PasswordZip
{
//用例
private static void Usage()
{
Console.WriteLine("usage:\n PasswordZip <output> <input_file/dir> <input_file/dir2>... ");
Environment.Exit(1);
}
//ファイルorディレクトリ存在フラグ
private enum FATTR {
NOT_EXIST = 0,
FILE,
DIRECTORY
};
//ファイル存在チェック
private static FATTR IsExist(String item)
{
FATTR ret = FATTR.NOT_EXIST;
if (System.IO.Directory.Exists(item))
{
ret = FATTR.DIRECTORY;
}
else if (System.IO.File.Exists(item))
{
ret = FATTR.FILE;
}
return (ret);
}
//展開
private static void addItemsToZip(ref ZipFile zip, String[] args)
{
//入力ファイルを一つ一つ検査
foreach (String item in args)
{
FATTR fattr;
//存在しないファイルorディレクトリだったら次のアイテムへ
if ((fattr = IsExist(item)) == FATTR.NOT_EXIST)
{
continue;
}
if (fattr == FATTR.FILE)
{
//ファイルの場合はそのまま追加
Console.WriteLine("\tadding... [file] {0}", item);
zip.AddFile(item, "");
}
else if (fattr == FATTR.DIRECTORY)
{
//ディレクトリの場合は階層構造を保った上で追加
Console.WriteLine("\tadding... [dir] {0}", item);
System.IO.FileInfo info = new System.IO.FileInfo(item);
zip.AddDirectory(item, info.Name);
}
}
}
//zipファイル名取得
private static String GetZipFilename(String src)
{
Regex re = new Regex("\\.\\w+$");
String dst = re.Replace(src,".zip");
if (dst == src)
{
dst = src + ".zip";
}
return dst;
}
//パスワード入力
private static String GetPassword(String prompt)
{
Console.Write(prompt + ": ");
String password = "";
while (true)
{
ConsoleKeyInfo input = Console.ReadKey(true);
if (input.Key == ConsoleKey.Enter)
{
Console.WriteLine();
break;
}
else if (input.Key == ConsoleKey.Backspace)
{
if (password.Length > 0)
{
password.Substring(0, password.Length - 1);
}
}
else
{
password += input.KeyChar;
}
}
return password;
}
//main
public static void Main(String[] args)
{
//入力ファイルがひとつもなかったら終了
if (args.Length == 0) Usage();
//出力ファイル名を取得
String dst = GetZipFilename(args[0]);
try
{
ZipFile zip = new ZipFile();
//日本語ファイル名対策(for win)
zip.ProvisionalAlternateEncoding = System.Text.Encoding.GetEncoding("shift_jis");
//圧縮レベルを変更
zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
//必要な時はZIP64で圧縮する。デフォルトはNever。
zip.UseZip64WhenSaving = Ionic.Zip.Zip64Option.AsNecessary;
//エラーが出てもスキップする。デフォルトはThrow。
zip.ZipErrorAction = Ionic.Zip.ZipErrorAction.Skip;
//コメント
zip.Comment = "(´-`).。oO(This application is using DotNetZip library)\n";
zip.Comment += "(´-`).。oO(...producted by K.Tsunezumi)\n";
Console.Write(zip.Comment);
//パスワード入力
int retry;
String pass = "", pass2;
for (retry = 0; retry < 3; retry++)
{
pass = GetPassword("Enter Password");
pass2 = GetPassword("Retry Password");
//3回間違えたらプログラムを終了
if (pass == pass2) break;
}
//パスワードをかけて暗号化(パスワードが空文字列の場合は暗号化しない)
if (pass != "")
{
zip.Password = pass;
zip.Encryption = Ionic.Zip.EncryptionAlgorithm.PkzipWeak;
Console.Write("(・∀・)PW encrypt is enabled!!\n");
}
//入力ファイル/ディレクトリを圧縮
addItemsToZip(ref zip, args);
//zipファイル保存
zip.Save(dst);
//オブジェクト削除
zip.Dispose();
Console.WriteLine("\toutput to >> " + dst);
Console.Write("(・∀・)zip compressing is succeeded!!\n");
}
catch (System.Exception ex1)
{
//エラー
System.Console.Error.WriteLine("(´・ω・`)error: " + ex1);
}
Console.Write("(・∀・)press any key");
Console.ReadKey(true);
}
}
}