现在做的东西需要将文件以两进制的形式保存的数据库中去,又要方便的取到文件名,扩展名。于是想自己来构建一个二进制串,其中包含文件,又能包含文件信息,像文件名,扩展名等等吧
考虑到文件的信息长度不会太长,所以用Int16的变量来保存信息的长度。这样,一个Int16类型的可以用两位byte来表示,那构建出的字符串应该为"FILENAME: EXTENSION: BODY: 文件名扩展名文件"
当然,串中的FILENAME:等标识是没什么实际意义的,只是看着方便,而这些标识后面的那两位是记录相应信息的位置那两个byte位,前面存高8位,后面存低8位
读的时候把高8位的byte位左移8位,或上低8位的byte位,即是信息在串中的真实位置。(好像是在说计算机原理-_-)
就说这些了,看程序吧 ,一共写了五个方法,
/// <summary>
/// 将文件转换为二进制串
/// </summary>
/// <param name="filename">文件名</param>
/// <returns></returns>
public byte[] FileToBytes(string filename)
{
byte[] rv = null;
byte[] buf = null;
//以下两变量用于位置记录,用两个byte来保存一个16位的整形
//高8位在前,低8位在后
Int16 iPostion =30; //第一个保存的位置是跟在下面三句后面,所以位置是30
Int16 iBuf =0;
//空的两位即为保存位置的两个byte位
rv = new ASCIIEncoding().GetBytes("FILENAME: ");
rv = AppendBytes(rv,new ASCIIEncoding().GetBytes("EXTENSION: "));
rv = AppendBytes(rv,new ASCIIEncoding().GetBytes("BODY: "));
string fn = Path.GetFileName(filename);
string ex = Path.GetExtension(filename);
//添加文件名
iBuf = System.Convert.ToInt16(iPostion>>8);
rv[9] = (byte)iBuf;
iBuf = System.Convert.ToInt16((iPostion<<8)>>8);
rv[10] = (byte)iBuf;
buf = new UnicodeEncoding().GetBytes(fn);
iPostion += (Int16)buf.Length;
rv = AppendBytes(rv,buf);
//添加扩展名
iBuf = System.Convert.ToInt16(iPostion>>8);
rv[21] = (byte)iBuf;
iBuf = System.Convert.ToInt16((iPostion<<8)>>8);
rv[22] = (byte)iBuf;
buf = new UnicodeEncoding().GetBytes(ex);
iPostion += (Int16)buf.Length;
rv = AppendBytes(rv,buf);
//将文件转换为流添加
iBuf = System.Convert.ToInt16(iPostion>>8);
rv[28] = (byte)iBuf;
iBuf = System.Convert.ToInt16((iPostion<<8)>>8);
rv[29] = (byte)iBuf;
FileStream fs = File.Open(filename,FileMode.Open);
buf = new byte[fs.Length];
fs.Position =0;
fs.Read(buf,0,(int)fs.Length);
rv = AppendBytes(rv,buf);
return rv;
}
/// <summary>
/// 添加串
/// </summary>
/// <param name="oldValue">旧串</param>
/// <param name="newValue">新串</param>
/// <returns></returns>
public byte[] AppendBytes(byte[] oldValue,byte[] newValue)
{
int len = oldValue.Length + newValue.Length;
byte[] buf = new byte[len];
oldValue.CopyTo(buf,0);
for(int i=0;i<newValue.Length;i++)
{
buf[i+oldValue.Length] = newValue[i];
}
return buf;
}
//===================================
//上面是压缩,下面是获得
//===================================
/// <summary>
/// 获取文件名
/// </summary>
/// <param name="buf"></param>
/// <returns></returns>
public string GetFileName(byte[] buf)
{
int h=buf[21];
int l=buf[22];
h= h<<8;
int i = h | l;
return new UnicodeEncoding().GetString(buf,i,i-30);
}
/// <summary>
/// 获取扩展名
/// </summary>
/// <param name="buf"></param>
/// <returns></returns>
public string GetFileExtension(byte[] buf)
{
int h=buf[28];
int l=buf[29];
h = h<<8;
int i = h | l;
h = buf[21];
l = buf[22];
h = h<<8;
int j = h| l;
return new UnicodeEncoding().GetString(buf,j,i-j);
}
/// <summary>
/// 获取文件流
/// </summary>
/// <param name="buf"></param>
/// <returns></returns>
public static byte[] GetContent(byte[] buf)
{
int h= buf[28];
int l = buf[29];
h = h<<8;
int p= h | l;
byte[] content = new byte[buf.Length - p];
for( int i=0;i<buf.Length-p;i++)
{
content[i] = buf[i+p];
}
return content;
}
/// 将文件转换为二进制串
/// </summary>
/// <param name="filename">文件名</param>
/// <returns></returns>
public byte[] FileToBytes(string filename)
{
byte[] rv = null;
byte[] buf = null;
//以下两变量用于位置记录,用两个byte来保存一个16位的整形
//高8位在前,低8位在后
Int16 iPostion =30; //第一个保存的位置是跟在下面三句后面,所以位置是30
Int16 iBuf =0;
//空的两位即为保存位置的两个byte位
rv = new ASCIIEncoding().GetBytes("FILENAME: ");
rv = AppendBytes(rv,new ASCIIEncoding().GetBytes("EXTENSION: "));
rv = AppendBytes(rv,new ASCIIEncoding().GetBytes("BODY: "));
string fn = Path.GetFileName(filename);
string ex = Path.GetExtension(filename);
//添加文件名
iBuf = System.Convert.ToInt16(iPostion>>8);
rv[9] = (byte)iBuf;
iBuf = System.Convert.ToInt16((iPostion<<8)>>8);
rv[10] = (byte)iBuf;
buf = new UnicodeEncoding().GetBytes(fn);
iPostion += (Int16)buf.Length;
rv = AppendBytes(rv,buf);
//添加扩展名
iBuf = System.Convert.ToInt16(iPostion>>8);
rv[21] = (byte)iBuf;
iBuf = System.Convert.ToInt16((iPostion<<8)>>8);
rv[22] = (byte)iBuf;
buf = new UnicodeEncoding().GetBytes(ex);
iPostion += (Int16)buf.Length;
rv = AppendBytes(rv,buf);
//将文件转换为流添加
iBuf = System.Convert.ToInt16(iPostion>>8);
rv[28] = (byte)iBuf;
iBuf = System.Convert.ToInt16((iPostion<<8)>>8);
rv[29] = (byte)iBuf;
FileStream fs = File.Open(filename,FileMode.Open);
buf = new byte[fs.Length];
fs.Position =0;
fs.Read(buf,0,(int)fs.Length);
rv = AppendBytes(rv,buf);
return rv;
}
/// <summary>
/// 添加串
/// </summary>
/// <param name="oldValue">旧串</param>
/// <param name="newValue">新串</param>
/// <returns></returns>
public byte[] AppendBytes(byte[] oldValue,byte[] newValue)
{
int len = oldValue.Length + newValue.Length;
byte[] buf = new byte[len];
oldValue.CopyTo(buf,0);
for(int i=0;i<newValue.Length;i++)
{
buf[i+oldValue.Length] = newValue[i];
}
return buf;
}
//===================================
//上面是压缩,下面是获得
//===================================
/// <summary>
/// 获取文件名
/// </summary>
/// <param name="buf"></param>
/// <returns></returns>
public string GetFileName(byte[] buf)
{
int h=buf[21];
int l=buf[22];
h= h<<8;
int i = h | l;
return new UnicodeEncoding().GetString(buf,i,i-30);
}
/// <summary>
/// 获取扩展名
/// </summary>
/// <param name="buf"></param>
/// <returns></returns>
public string GetFileExtension(byte[] buf)
{
int h=buf[28];
int l=buf[29];
h = h<<8;
int i = h | l;
h = buf[21];
l = buf[22];
h = h<<8;
int j = h| l;
return new UnicodeEncoding().GetString(buf,j,i-j);
}
/// <summary>
/// 获取文件流
/// </summary>
/// <param name="buf"></param>
/// <returns></returns>
public static byte[] GetContent(byte[] buf)
{
int h= buf[28];
int l = buf[29];
h = h<<8;
int p= h | l;
byte[] content = new byte[buf.Length - p];
for( int i=0;i<buf.Length-p;i++)
{
content[i] = buf[i+p];
}
return content;
}
最后获取的文件也是个二进制串,可以用FileStream的Write方法写入文件即可了
浙公网安备 33010602011771号