Imports System.Net.Mail
Imports System.IO
16
10
位
元
16
10
位
元
16
10
位
元
16
10
位
元
16
10
位
元
16
10
位
元
16
10
位
元
16
10
位
元
0
0
10
16
20
32
30
48
0
40
64
@
50
80
P
60
96
`
70
112
p
1
1
11
17
21
33
!
31
49
1
41
65
A
51
81
Q
61
97
a
71
113
q
2
2
12
18
22
34
"
32
50
2
42
66
B
52
82
R
62
98
b
72
114
r
3
3
13
19
23
35
#
33
51
3
43
67
C
53
83
S
63
99
c
73
115
s
4
4
14
20
24
36
$
34
52
4
44
68
D
54
84
T
64
100
d
74
116
t
5
5
15
21
25
37
%
35
53
5
45
69
E
55
85
U
65
101
e
75
117
u
6
6
16
22
26
38
&
36
54
6
46
70
F
56
86
V
66
102
f
76
118
v
7
7
17
23
27
39
'
37
55
7
47
71
G
57
87
W
67
103
g
77
119
w
8
8
18
24
28
40
(
38
56
8
48
72
H
58
88
X
68
104
h
78
120
x
9
9
..
19
25
29
41
)
39
57
9
49
73
I
59
89
Y
69
105
i
79
121
y
A
10
..
1A
26
..
2A
42
*
3A
58
:
4A
74
J
5A
90
Z
6A
106
j
7A
122
z
B
11
1B
27
2B
43
+
3B
59
;
4B
75
K
5B
91
[
6B
107
k
7B
123
{
C
12
1C
28
2C
44
,
3C
60
<
4C
76
L
5C
92
\
6C
108
l
7C
124
|
D
13
..
1D
29
2D
45
-
3D
61
=
4D
77
M
5D
93
]
6D
109
m
7D
125
}
E
14
1E
30
‑
2E
46
.
3E
62
>
4E
78
N
5E
94
^
6E
110
n
7E
126
~
F
15
1F
31
2F
47
/
3F
63
?
4F
79
O
5F
95
_
6F
111
o
7F
127
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.IO
Imports ICSharpCode.SharpZipLib.Zip
Public Class Zip
''' <summary>
''' 壓縮整個資料夾
''' </summary>
''' <param name="SourceDir">要壓縮的資料夾(完整路徑) </param>
''' <param name="TargetFile">要產生的Zip檔案(完整路徑檔名)</param>
''' <param name="Password">密碼</param>
''' <param name="BackupOldFile">是否備份舊檔案</param>
Public Sub ZipDir(ByVal SourceDir As String, ByVal TargetFile As String, ByVal Password As String, ByVal BackupOldFile As Boolean)
Dim oZipDir As New ICSharpCode.SharpZipLib.Zip.FastZip()
Try
If Not Directory.Exists(SourceDir) Then
Throw New Exception("資料夾不存在!")
End If
If BackupOldFile = True Then
'判斷要產生的ZIP檔案是否存在
If File.Exists(TargetFile) = True Then
'原本的檔案存在,把他ReName
File.Copy(TargetFile, TargetFile & "-" & DateTime.Now.ToString("yyyyMMddHHmmss") & ".back")
File.Delete(TargetFile)
End If
End If
If Password <> "" Then
oZipDir.Password = Password
End If
oZipDir.CreateZip(TargetFile, SourceDir, True, "")
Catch
Throw
End Try
End Sub
''' <summary>
'''壓縮整個資料夾
''' </summary>
''' <param name="SourceDir">要壓縮的資料夾(完整路徑) </param>
''' <param name="TargetFile">要產生的Zip檔案(完整路徑檔名)</param>
''' <param name="Password">密碼</param>
Public Sub ZipDir(ByVal SourceDir As String, ByVal TargetFile As String, ByVal Password As String)
Me.ZipDir(SourceDir, TargetFile, Password, True)
End Sub
''' <summary>
'''壓縮整個資料夾
''' </summary>
''' <param name="SourceDir">要壓縮的資料夾(完整路徑) </param>
''' <param name="TargetFile">要產生的Zip檔案(完整路徑檔名)</param>
Public Sub ZipDir(ByVal SourceDir As String, ByVal TargetFile As String)
Me.ZipDir(SourceDir, TargetFile, "", True)
End Sub
''' <summary>
''' 壓縮檔案
''' </summary>
''' <param name="SourceFiles">要壓縮的檔案(完整路徑)</param>
''' <param name="TargetFile">要產生的Zip檔案(完整路徑檔名)</param>
''' <param name="Password">密碼</param>
''' <param name="BackupOldFile">是否備份舊檔案</param>
Public Sub ZipFiles(ByVal SourceFiles As String(), ByVal TargetFile As String, ByVal Password As String, ByVal BackupOldFile As Boolean)
Try
If SourceFiles Is Nothing OrElse SourceFiles.Length <= 0 Then
Throw New Exception("並未傳入檔案完整路徑")
End If
For i As Integer = 0 To SourceFiles.Length - 1
If File.Exists(SourceFiles(i)) = False Then
Throw New Exception("要壓縮的檔案【" & SourceFiles(i) & "】不存在")
End If
Next
If BackupOldFile = True Then
'判斷要產生的ZIP檔案是否存在
If File.Exists(TargetFile) = True Then
'原本的檔案存在,把他ReName
File.Copy(TargetFile, TargetFile & "-" & DateTime.Now.ToString("yyyyMMddHHmmss") & ".back")
File.Delete(TargetFile)
End If
End If
Dim zs As New ZipOutputStream(File.Create(TargetFile))
zs.SetLevel(9)
'壓縮比
If Password <> "" Then
zs.Password = Password
End If
For i As Integer = 0 To SourceFiles.Length - 1
Dim s As FileStream = File.OpenRead(SourceFiles(i))
Dim buffer As Byte() = New Byte(s.Length - 1) {}
s.Read(buffer, 0, buffer.Length)
Dim Entry As New ZipEntry(Path.GetFileName(SourceFiles(i)))
Entry.DateTime = DateTime.Now
Entry.Size = s.Length
s.Close()
zs.PutNextEntry(Entry)
zs.Write(buffer, 0, buffer.Length)
Next
zs.Finish()
zs.Close()
Catch
Throw
End Try
End Sub
''' <summary>
''' 壓縮檔案
''' </summary>
''' <param name="SourceFiles">要壓縮的檔案(完整路徑)</param>
''' <param name="TargetFile">要產生的Zip檔案(完整路徑檔名)</param>
''' <param name="Password">密碼</param>
Public Sub ZipFiles(ByVal SourceFiles As String(), ByVal TargetFile As String, ByVal Password As String)
Me.ZipFiles(SourceFiles, TargetFile, Password, True)
End Sub
''' <summary>
''' 壓縮檔案
''' </summary>
''' <param name="SourceFiles">要壓縮的檔案(完整路徑)</param>
''' <param name="TargetFile">要產生的Zip檔案(完整路徑檔名)</param>
Public Sub ZipFiles(ByVal SourceFiles As String(), ByVal TargetFile As String)
Me.ZipFiles(SourceFiles, TargetFile, "", True)
End Sub
''' <summary>
''' 壓縮單一檔案
''' </summary>
''' <param name="SourceFiles">要壓縮的檔案(完整路徑)</param>
''' <param name="TargetFile">要產生的Zip檔案(完整路徑檔名)</param>
''' <param name="Password">密碼</param>
''' <param name="BackupOldFile">是否備份舊檔案</param>
Public Sub ZipFile(ByVal SourceFile As String, ByVal TargetFile As String, ByVal Password As String, ByVal BackupOldFile As Boolean)
Me.ZipFiles(New String() {SourceFile}, TargetFile, Password, BackupOldFile)
End Sub
''' <summary>
''' 壓縮單一檔案
''' </summary>
''' <param name="SourceFiles">要壓縮的檔案(完整路徑)</param>
''' <param name="TargetFile">要產生的Zip檔案(完整路徑檔名)</param>
''' <param name="Password">密碼</param>
Public Sub ZipFile(ByVal SourceFile As String, ByVal TargetFile As String, ByVal Password As String)
Me.ZipFile(SourceFile, TargetFile, Password, True)
End Sub
''' <summary>
''' 壓縮單一檔案
''' </summary>
''' <param name="SourceFiles">要壓縮的檔案(完整路徑)</param>
''' <param name="TargetFile">要產生的Zip檔案(完整路徑檔名)</param>
''' <returns></returns>
Public Sub ZipFile(ByVal SourceFile As String, ByVal TargetFile As String)
Me.ZipFile(SourceFile, TargetFile, "", True)
End Sub
''' <summary>
''' 解壓縮
''' </summary>
''' <param name="SourceFile">要解壓縮的Zip檔案(完整路徑檔名) </param>
''' <param name="TargetDir">解壓縮後存放的資料夾路徑(完整路徑) </param>
''' <param name="Password">密碼</param>
Public Sub ExtractZip(ByVal SourceFile As String, ByVal TargetDir As String, ByVal Password As String)
Dim oZip As New ICSharpCode.SharpZipLib.Zip.FastZip()
Try
'判斷要產生的ZIP檔案是否存在
If File.Exists(SourceFile) = False Then
Throw New Exception("要解壓縮的檔案【" & SourceFile & "】不存在,無法執行")
End If
If Password <> "" Then
oZip.Password = Password
End If
oZip.ExtractZip(SourceFile, TargetDir, "")
Catch
Throw
End Try
End Sub
''' <summary>
''' 解壓縮
''' </summary>
''' <param name="SourceFile">要解壓縮的Zip檔案(完整路徑檔名) </param>
''' <param name="TargetDir">解壓縮後存放的資料夾路徑(完整路徑) </param>
Public Sub ExtractZip(ByVal SourceFile As String, ByVal TargetDir As String)
Me.ExtractZip(SourceFile, TargetDir, "")
End Sub
End Class
Imports System.Text
'全型轉半型
Public Function ToNchr(ByRef data As String) As String
Dim sb As New StringBuilder
Dim ascii As Integer = 0
For Each c As Char In data.Replace("〔", "[").Replace("〕", "]").Replace("'", "'").ToCharArray()
ascii = System.Convert.ToInt32(c)
If ascii = 12288 Then
sb.Append(System.Convert.ToChar(32))
Else
If ascii > 65280 And ascii < 65375 Then
sb.Append(System.Convert.ToChar(ascii - 65248))
Else
sb.Append(System.Convert.ToChar(ascii))
End If
End If
Next
Return sb.ToString
End Function
Imports System.IO
Imports System.Text
'檔案Anis轉成Unicode格式
Dim reader1 As New StreamReader("D:\TEST.DAT", Encoding.GetEncoding(0))
Dim writer1 As New StreamWriter("D:\TEST_Un.TXT", True, Encoding.Unicode)
writer1.Write(reader1.ReadToEnd)
writer1.Flush()
writer1.Close()
reader1.Close()
Imports System.IO
Imports System.Text
'StreamWriter匯出文字檔
Dim sL_Path As String = "D:\AAA.csv"
Dim sw As StreamWriter = New StreamWriter(sL_Path, True, Encoding.Unicode)
If File.Exists(sL_Path) Then
File.Delete(sL_Path)
End If
sw.WriteLine("開始")
sw.WriteLine("內容1")
sw.WriteLine("內容2")
sw.WriteLine("結束")
sw.Close()