隐藏
#region Copyright 2009
/*
* Created: 2009.06.12
* Description: * Last Modified: 2009.06.12
* RemarK 序列化待实现 引入 trinet.dll Bug 休眠
* Version: 2.0
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Trinet.Core.IO.Ntfs;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
namespace ConsoleApplication17
{
class Program
{
public class ConStream
{
private static BinaryFormatter transfer = new BinaryFormatter();
/// <summary>
/// 隐藏文件内容到NTFS文件流
/// </summary>
/// <param name="args"></param>
public void FileToStream(string filePath)
{
// 获得文件全名 System.IO.Path.GetFileName(filePath);
// 获得文件名 System.IO.Path.GetFileNameWithoutExtension(filePath);
// 获得文件类型 System.IO.Path.(filePath);
string fileName =System.IO.Path.GetFileName(filePath);
string fileType = fileName.Substring(fileName.LastIndexOf(".") + 1);
string path = System.IO.Path.GetFileNameWithoutExtension(filePath);
string tempPath = path+"fanruyi" + fileType;
string streamName = fileName;
FileStream fs = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
if (!File.Exists(tempPath))
{
File.Create(tempPath);
GC.Collect();
}
FileInfo fileinfo = new FileInfo(tempPath);
AlternateDataStreamInfo adsi = fileinfo.GetAlternateDataStream(streamName, System.IO.FileMode.OpenOrCreate);
BinaryReader sr = new BinaryReader(fs, Encoding.Unicode);
byte[] b = new byte[(int)fs.Length];
b = ObjectToByte(sr);
BinaryWriter sw = new BinaryWriter(adsi.OpenWrite());
sr.Close();
sw.Write(b);
sw.Close();
fs.Close();
FileInfo fileDel = new FileInfo(filePath);
fileDel.Delete();
GC.Collect();
fileinfo.MoveTo(filePath); //不破坏流
GC.Collect();
}
/// <summary>
/// 还原NTFS文件流到文件内容
/// </summary>
/// <param name="args"></param>
public void StreamToFile(string filePath)
{
// 获得文件全名 System.IO.Path.GetFileName(filePath);
// 获得文件名 System.IO.Path.GetFileNameWithoutExtension(filePath);
// 获得文件类型 System.IO.Path.(filePath);
string fileName = System.IO.Path.GetFileName(filePath);
string fileType = fileName.Substring(fileName.LastIndexOf(".") + 1);
string path = System.IO.Path.GetFileNameWithoutExtension(filePath);
string tempPath = path + "fanruyi" + fileType;
string streamName = fileName;
FileInfo fileinfo = new FileInfo(filePath);
if (!File.Exists(filePath))
{
//Console.WriteLine("流文件未成功创建");
return ;
}
foreach (AlternateDataStreamInfo adsj in fileinfo.ListAlternateDataStreams())
{
FileStream fs = fileinfo.GetAlternateDataStream(adsj.Name).Open(FileMode.Open);
BinaryReader br = new BinaryReader(fs, Encoding.Unicode);
byte[] buffer = br.ReadBytes((int)adsj.Size);
if (!File.Exists(tempPath))
{
File.Create(tempPath);
GC.Collect();
}
FileInfo tempFileInfo = new FileInfo(tempPath);
BinaryWriter sw = new BinaryWriter(tempFileInfo.OpenWrite());
br.Close();
sw.Write(buffer);
sw.Close();
fs.Close();
}
FileInfo fileDel = new FileInfo(filePath);
fileDel.Delete();
//GC.Collect();
File.Move(tempPath, filePath);//移动(破坏流 实际已无流存在)
}
/// <summary>
/// 序列化,存储用
/// </summary>
/// <param name="args"></param>
private static byte[] ObjectToByte(object obj)
{
try
{
MemoryStream ms = new MemoryStream();
transfer.Serialize(ms,obj);
byte[] buffer = ms.GetBuffer();
return buffer;
}
catch(Exception err)
{
return null;
}
}
/// <summary>
/// 反序列化,读取用
/// </summary>
/// <param name="args"></param>
///
private static object ByteToObject(byte[] buffer)
{
try
{
MemoryStream ms = new MemoryStream(buffer, 0, buffer.Length, true, true);
object obj = transfer.Deserialize(ms);
return obj;
}
catch(Exception err)
{
return null;
}
}
}
//测试例程
static void Main(string[] args)
{
string testPath = @"......";
ConStream cs = new ConStream();
for (int i = 0; i < 200; i++)
{
cs.FileToStream(testPath);
cs.StreamToFile(testPath);
}
Console.ReadKey();
}
}
}