一、关于Aquilia
因为之前遇到过一个问题:就是有一个二进制文件,要把它保存到一个只支持ASCII字符的文本文件中,并且要做到可恢复成与之前一模一样的二进制文件。今天正好有时间,就用VB.NET写了一个程序,取名叫Aquilia,用来实现这个功能。
这个程序的源码已经上传到GitAtOSC上,地址为:http://git.oschina.net/Tsybius2014/Aquilia_Transcoder
二、制作一个样本文件HelloWorld.exe
建立一个C#控制台应用程序,手动添加引用“System.Windows.Forms”,然后输入代码如下:
namespace HelloWorld { class Program { static void Main(string[] args) { System.Windows.Forms.MessageBox.Show("Hello World!"); } } }
编译好的exe文件即为HelloWorld.exe,这个exe在运行后会弹出一个MessageBox上面写着“Hello World!”
三、Aquilia代码
1)ModuleMain.vb:主函数
Imports System.IO Module ModuleMain ''' <summary> ''' 主函数 ''' </summary> ''' <remarks></remarks> Sub Main() Dim Args As String() = System.Environment.GetCommandLineArgs() If Args.Count <> 4 Then PrintUsage() Pause() Exit Sub End If If (Args(1) <> "-e" And Args(1) <> "--encode" And _ Args(1) <> "-d" And Args(1) <> "--decode") Then PrintUsage() Pause() Exit Sub End If If Not File.Exists(Args(2)) Then Console.WriteLine("文件:{0} 不存在",Args(2)) End If If File.Exists(Args(3)) Then Console.Write("文件:{0} 已存在,要覆盖吗 (y/n)",Args(3)) While True Dim temp As String = Console.ReadLine If temp = "Y" Or temp = "y" Then Exit While ElseIf temp = "N" Or temp = "n" Then Exit Sub Else Console.Write("文件:{0} 已存在,要覆盖吗 (y/n)",Args(3)) Continue While End If End While End If If Args(1) = "-e" Or Args(1) = "--encode" Then FileConverHelper.Encode(Args(2),Args(3)) ElseIf Args(1) = "-d" Or Args(1) = "--decode" Then FileConverHelper.Decode(Args(2),Args(3)) End If Console.WriteLine("转换完毕!") Console.WriteLine("新生成的文件已被保存到: {0}",Args(3)) Pause() End Sub ''' <summary> ''' 打印程序用法 ''' </summary> ''' <remarks></remarks> Sub PrintUsage() Dim Usage(6) As String Usage(0) = "Aquilia程序使用方法" Usage(1) = "Aquilia [-e|--encode|-d|--decode] 文件1 文件2" Usage(2) = "第二个参数为-e或--encode时,Aquilia将文件1加密后写入到文件2中" Usage(3) = "第二个参数为-d或--decode时,Aquilia将文件1解密后写入到文件2中" Usage(4) = "示例参数1: -e HelloWorld.exe output.txt" Usage(5) = "示例参数2: -d output.txt HelloWorld2.exe" Console.WriteLine(Join(Usage,vbCrLf)) End Sub ''' <summary> ''' 按任意键继续 ''' </summary> ''' <remarks></remarks> Sub Pause() Console.WriteLine("按任意键继续") System.Console.ReadKey() System.Console.Write(Chr(8) + " ") '删除按下的“任意键”字符 End Sub End Module
2)FileConverHelper.vb:
Imports System.IO Imports System.Text Public Class FileConverHelper ''' <summary> ''' 将file1内容的以文本的形式输出到file2中 ''' </summary> ''' <param name="file1"></param> ''' <param name="file2"></param> ''' <remarks></remarks> Public Shared Sub Encode(file1 As String,file2 As String) Dim fi As FileInfo = New FileInfo(file1) Dim fs1 As New FileStream(file1,FileMode.Open,FileAccess.Read,FileShare.Read) Dim buffer(fi.Length) As Byte fs1.Read(buffer,CInt(fi.Length)) fs1.Close() Using sw As New StreamWriter(file2) For i As Integer = 0 To buffer.Length - 1 If i <> buffer.Length - 1 Then sw.Write(buffer(i) & ",") Else sw.Write(buffer(i)) End If Next End Using 'Dim sb As New StringBuilder 'For Each byt In buffer ' sb.Append(byt + ",") 'Next End Sub ''' <summary> ''' 将file1文本以二进制的形式输出到file2中 ''' </summary> ''' <param name="file1"></param> ''' <param name="file2"></param> ''' <remarks></remarks> Public Shared Sub Decode(file1 As String,file2 As String) Dim s() As String Using sr As New StreamReader(file1) s = sr.ReadToEnd().Split(",") End Using Dim buffer(s.Length - 1) As Byte For i As Integer = 0 To buffer.Length - 1 buffer(i) = CInt(s(i)) Next Dim fs2 As New FileStream(file2,FileMode.Create,FileAccess.Write,FileShare.None) fs2.Write(buffer,buffer.Length) fs2.Close() End Sub End Class
四、运行结果
将HelloWorld.exe复制到Aquilia.exe同一个目录下,然后输入下面两条命令
Aquilia.exe -e HelloWorld.exe output.txt Aquilia.exe -d output.txt HelloWorld2.exe
先将HelloWorld.exe转码后输出到output.txt下,再把output.txt中的内容重写到HelloWorld2.exe中
重写成的HelloWorld2.exe,双击后,也会输出内容为“Hello World!”的MessageBox
附:VB程序集中的“启动选项→命令行参数”,保存在文件Aquilia.vbproj.user中(Aquilia为本程序集的名称)
<?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|Anycpu'"> <StartArguments>-e HelloWorld.exe output.txt</StartArguments> </PropertyGroup> </Project>
提交Git的时候不要忘了这个文件
END
原文链接:https://www.f2er.com/vb/257622.html