您的位置:首页 > 其它

论坛无法上传文件,就把它变成文本文件再发帖留言

2010-04-21 15:01 253 查看
在论坛想上传一个文件,不行!

直接粘贴文件,二进制文件无法粘贴!

怎么办,利用这个小软件,将二进制文件转换成文本文件,想贴就贴!

别人怎么看?该软件可进行逆向还原成原来的文件,想看接看!



]using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Compression;

namespace bin2Asc
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

// 选择要将其转换成文本文件的任何文件
private void btnBrowse_Click(object sender, EventArgs e)
{
OpenFileDialog d = new OpenFileDialog();
d.Filter = "任意类型的文件(*.*)|*.*";
d.CheckFileExists = true;
d.ShowDialog();
txtFile.Text = d.FileName;
}

// 将二进制文件转换成本文文件
// 论坛留言中不可能粘贴二进制数据,但可将其转换成文本文件,便于在论坛发帖。
private void btnExecute_Click(object sender, EventArgs e)
{
if (!File.Exists(txtFile.Text)) return;
rtxtResult.Clear();
int bytes, rarLength = 0;
try
{
bytes = int.Parse(txtBytes.Text);
}
catch
{
bytes = 48;
}
if (bytes < 16) bytes = 16;
FileStream fs = new FileStream(txtFile.Text, FileMode.Open, FileAccess.Read);
BinaryReader br = null;
MemoryStream ms = null;
int fileLength = (int)fs.Length;
StringBuilder allString = new StringBuilder();
StringBuilder sb = new StringBuilder(bytes * 2);
if (chkRar.Checked)
{
byte[] buffer = new byte[fileLength];
fs.Read(buffer, 0, fileLength);
ms = new MemoryStream();
GZipStream g = new GZipStream(ms, CompressionMode.Compress, true);
g.Write(buffer, 0, fileLength);
g.Close();
rarLength = (int)ms.Length;
br = new BinaryReader(ms);
ms.Position = 0;
}
else
{
br = new BinaryReader(fs);
}
string t = (rarLength > 0 ? "1" + rarLength.ToString() : "0" + fileLength.ToString()) + "O";
allString.Append(t);
int y = bytes * 2 - t.Length;
Random r = new Random();
while (y > 0)
{
int k = y;
if (k > 9) k = 9;
allString.Append(r.Next((int)Math.Pow(10, k - 1) + 1, (int)Math.Pow(10, k) - 1).ToString());
y -= k;
}
allString.Append("/n");
while (true)
{
byte[] buffer = br.ReadBytes(bytes);
if (buffer.Length == 0) break;
sb.Length = 0;
for (int i = 0; i < buffer.Length; ++i)
{
sb.Append(buffer[i].ToString("X2"));
}
allString.Append(sb.ToString() + "/n");
}
br.Close();
if (ms != null) ms.Close();
fs.Close();
rtxtResult.AppendText(allString.ToString());
lblMessages.Text = "源文件:" + fileLength + " Bytes" + (rarLength > 0 ? "  压缩后:" + rarLength + " Bytes" : "") + "  结果:" + rtxtResult.Text.Length + " Bytes";
Clipboard.SetText(rtxtResult.Text);
MessageBox.Show("转换成功,内容已复制到剪贴板!  ", "二进制文件转换成文本文件", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

// 还原成二进制文件。
// 复制论坛的数据后粘贴到文本框,可重新将文本文件还原成原来的二进制文件。
private void btnToBin_Click(object sender, EventArgs e)
{
SaveFileDialog fd = new SaveFileDialog();
fd.Title = "保存转换生成的(二进制)文件";
fd.Filter = "任意文件(*.*)|*.*";
fd.CheckPathExists = true;
if (fd.ShowDialog() == DialogResult.OK)
{
string[] lines = rtxtResult.Lines;
if (lines.Length < 1) return;
bool isRar = (lines[0][0] == '1');
int pos = lines[0].IndexOf('O');
if (pos < 0)
{
MessageBox.Show("数据有错,不能还原文件!");
return;
}
int len = int.Parse(lines[0].Substring(1, pos - 1));
byte[] buffer = new byte[len];
pos = 0;
for (int j = 1; j < lines.Length; ++j)
{
for (int i = 0; i < lines[j].Length; i += 2)
{
buffer[pos] = Convert.ToByte(lines[j].Substring(i, 2), 16);
++pos;
}
}
lines = null;
if (isRar)
{
int oldLength = BitConverter.ToInt32(buffer, buffer.Length - 4);
byte[]tmp = new byte[oldLength];
MemoryStream ms = new MemoryStream(buffer);
GZipStream gs = new GZipStream(ms, CompressionMode.Decompress);
gs.Read(tmp, 0, oldLength);
gs.Close();
ms.Close();
buffer = tmp;
}
FileStream fs = new FileStream(fd.FileName, FileMode.Create);
fs.Write(buffer, 0, buffer.Length);
fs.Close();
}
}
}
}

---------------------------------------------

下面这段代码也很爽:

static void Main()
{
FileStream fs = new FileStream("d://aaa.rar", FileMode.Open);
byte[] b = new byte[fs.Length];
fs.Read(b, 0, b.Length);
fs.Close();
string s = Regex.Replace(BitConverter.ToString(b).Replace("-", ""), "(//w{64})", "$1/n");
Console.WriteLine(s);
Console.WriteLine("/n/n程序执行结束,按任意键关闭窗口!");
Console.ReadKey();
}

运行结果:

526172211A0700CF907300000D00000000000000A5E17440902E00C902000073
0400000267C5CD929046B53A1D330900200000006572726F722E61737000F0D0
12130C1D510CCCBD1C11BB0247C1F6B1414809BDAE4A50B6C9116C92D2A50B61
6BA05E8E47B1C60DB8CD7B4B2023E3C1D6888842A81091520E02A05B447F0857
85E07004750EA224091B8DCDDD9224215391D778EE7F5ECF2F667C7B73733737
333D9ECC5FB12F62F25E5EF7C0E4C4E74FCBD0CED4CBCE9D290E96669CCD2CFA
0A20099A3367503C3C8918E84497C6E2DD45BD85A528361A8BA42EBEB09D225D
1B88EBE991F26D78A83F88468C5A5A602D3A09C0192A0.......省略啦。

程序执行结束,按任意键关闭窗口!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: