本文探讨的是關于C#TXT檔案的壓縮、加密以及解壓問題,采用的是密鑰方式,可以先進行加密再進行壓縮包導入到桌面。
界面如下:
源檔案:想要壓縮的TXT檔案,裡邊必須有東西,不然代碼會報錯
壓縮檔案:要壓縮到桌面的路徑
代碼如下:
public partial class 壓縮檔案 : Form
{
public 壓縮檔案()
{
InitializeComponent();
}
string str = "";
string a = "";
private void btnYasuo_Click(object sender, EventArgs e)
{
if(String.IsNullOrEmpty(textBox1.Text))
{
MessageBox.Show("請選擇源檔案","資訊提示");
return;
}
if (String.IsNullOrEmpty(textBox2.Text))
{
MessageBox.Show("請輸入壓縮檔案名", "資訊提示");
return;
}
string str1 = textBox1.Text;
string str2 = textBox2.Text.Trim() + ".zip";
str = str2;
byte[] myByte = null;
FileStream myStream = null;
FileStream myDesStream = null;
GZipStream myComStream = null;
try
{
myStream = new FileStream(str1, FileMode.Open, FileAccess.Read, FileShare.Read);
myByte = new byte[myStream.Length];
myStream.Read(myByte, 0, myByte.Length);
myDesStream = new FileStream(str2, FileMode.OpenOrCreate, FileAccess.Write);
myComStream = new GZipStream(myDesStream, CompressionMode.Compress, true);
myComStream.Write(myByte, 0, myByte.Length);
MessageBox.Show("壓縮檔案完成");
}
catch(Exception )
{
}
finally
{
myStream.Close();
myComStream.Close();
myDesStream.Close();
}
}
private void btnJiami_Click(object sender, EventArgs e)
{
if(textBox1 .Text =="")
{
MessageBox.Show("請選擇要加密的檔案");
}
else
{
try
{
string strPath = textBox1.Text;
int intLent = strPath.LastIndexOf("\\") + 1;
int intLong = strPath.Length;
string strName = strPath.Substring(intLent, intLong - intLent);
int intTxt = strName.LastIndexOf(".");
int intTextLeng = strName.Length;
string strTxt = strName.Substring(intTxt, intTextLeng - intTxt);
strName = strName.Substring(0, intTxt);
string strOutName = strPath.Substring(0, strPath.LastIndexOf("\\") + 1) + strName +"Out"+ strTxt;
a = strOutName;
str = strPath.Substring(0, strPath.LastIndexOf("\\") + 1) + strName + "Out";
byte[] key = { 24, 55, 102, 24, 98, 26, 67, 29, 84, 19, 37, 118, 104, 85, 121, 27, 93, 86, 24, 55, 102, 27, 98, 26, 67, 29, 9, 2, 49, 69, 73, 92 };
byte[] IV = { 22, 56, 82, 77, 84, 31, 74, 24, 55, 102, 27, 98, 26, 67, 29, 99 };
RijndaelManaged myRijndael = new RijndaelManaged();
FileStream fsOut = File.Open(strOutName, FileMode.Create, FileAccess.Write);
FileStream fsIn = File.Open(strPath, FileMode.Open, FileAccess.Read);
CryptoStream csDexrypt = new CryptoStream(fsOut, myRijndael.CreateEncryptor(key, IV), CryptoStreamMode.Write);
BinaryReader br = new BinaryReader(fsIn);
csDexrypt.Write(br.ReadBytes((int)fsIn.Length), 0, (int)fsIn.Length);
csDexrypt.FlushFinalBlock();
csDexrypt.Close();
fsIn.Close();
fsOut.Close();
if(MessageBox.Show ("加密成功!加密後的檔案名及路徑為:\n"+strOutName +",是否删除源檔案","資訊提示",MessageBoxButtons.YesNo)==DialogResult .Yes )
{
File.Delete(strPath);
//textBox1.Text = "";
}
else
{
//textBox1.Text = "";
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
yasuo();
}
}
}
private void btnJiemi_Click(object sender, EventArgs e)
{
if(textBox1 .Text =="")
{
MessageBox.Show("請選擇解密的檔案路徑");
}
else
{
string strPath = textBox1.Text;
int intLent = strPath.LastIndexOf("\\") + 1;
int intLong = strPath.Length;
string strName = strPath.Substring(intLent, intLong - intLent);
int intTxt = strName.LastIndexOf(".");
int intTextLeng = strName.Length;
string strTxt = strName.Substring(intTxt, intTextLeng - intTxt);
strName = strName.Substring(0, intTxt);
if(strName .LastIndexOf ("Out")!=-1)
{
strName = strName.Substring(0, strName.LastIndexOf("Out"));
}
else
{
strName = strName + "In";
}
string strInName = strPath.Substring(0, strPath.LastIndexOf("\\") + 1) + strName + ".txt";
byte[] key = { 24, 55, 102, 24, 98, 26, 67, 29, 84, 19, 37, 118, 104, 85, 121, 27, 93, 86, 24, 55, 102, 27, 98, 26, 67, 29, 9, 2, 49, 69, 73, 92 };
byte[] IV = { 22, 56, 82, 77, 84, 31, 74, 24, 55, 102, 27, 98, 26, 67, 29, 99 };
RijndaelManaged myRijndael = new RijndaelManaged();
FileStream fsOut = File.Open(strPath, FileMode.Open, FileAccess.Read);
CryptoStream csDexrypt = new CryptoStream(fsOut, myRijndael.CreateEncryptor(key, IV), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(csDexrypt);
StreamWriter sw = new StreamWriter(strInName);
sw.Write(sr.ReadToEnd());
sw.Flush();
sw.Close();
sr.Close();
fsOut.Close();
if (MessageBox.Show("解密成功!解密後的檔案名及路徑為:\n" + strInName + ",是否删除源檔案", "資訊提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
File.Delete(strPath);
textBox1.Text = "";
}
else
{
textBox1.Text = "";
}
}
}
private void yasuo()
{
if (String.IsNullOrEmpty(textBox1.Text))
{
MessageBox.Show("請選擇源檔案", "資訊提示");
return;
}
if (String.IsNullOrEmpty(textBox2.Text))
{
MessageBox.Show("請輸入壓縮檔案名", "資訊提示");
return;
}
string str1 = a;
string str2 = str+ ".zip";
byte[] myByte = null;
FileStream myStream = null;
FileStream myDesStream = null;
GZipStream myComStream = null;
try
{
myStream = new FileStream(str1, FileMode.Open, FileAccess.Read, FileShare.Read);
myByte = new byte[myStream.Length];
myStream.Read(myByte, 0, myByte.Length);
myDesStream = new FileStream(str2, FileMode.OpenOrCreate, FileAccess.Write);
myComStream = new GZipStream(myDesStream, CompressionMode.Compress, true);
myComStream.Write(myByte, 0, myByte.Length);
MessageBox.Show("壓縮檔案完成");
}
catch (Exception)
{
}
finally
{
myStream.Close();
myComStream.Close();
myDesStream.Close();
}
}
本文代碼中可能對于上述按鈕實作的功能不太對應,可能點選壓縮或者加密按鈕,直接就會實作兩個功能,因為兩個方法之間會有調用關系,我寫的是加密完成之後直接進行壓縮到桌面,而不是說壓縮、加密分開實作,當然你也可以把方法中調用的另一個方法剝離開即可實作按鈕對應的功能。
謝謝大家!