📄 processfile.cs
字号:
using System;
using System.IO;
using System.Windows.Forms;
namespace Festel
{
public class ProcessFile
{
private static FileStream fsi, fso;
private static int size;
private static long inLength;
private static byte[] buffForRead = new byte[(int)Math.Pow(10, 7)];
private static byte[] buffForWrite;
private static ulong[] block;
//Noi cac so ulong thang mot mang byte[]
public static byte[] AppendBuffers(params ulong[] block)
{
int i, size, length;
size = block.Length;
byte [][] subbuff = new byte[size][];
for (i=0;i<size;i++)
{
subbuff[i] = BitConverter.GetBytes(block[i]);
}
length = subbuff[0].Length * size;
byte [] buff = new byte[length];
for (i=0;i<size;i++)
{
Array.Copy(subbuff[i],0,buff,i*subbuff[i].Length,subbuff[i].Length);
}
return buff;
}
// tra ve mang ulong[] tu mot phan cua mang byte[]
public static ulong[] ReadArrayIntegerFromArrayByte(byte[] buffer,int offset, int size)
{
ulong[] block = new ulong[size];
for (int j = 0; j < size; j++)
{
int ind = offset + 8 * j;
block[j] = BitConverter.ToUInt64(buffer, ind);
}
return block;
}
//================================================================
//Mo file chuan bi cho viec doc, tra ve bo dem file va
//do dai tinh theo byte cua file
//================================================================
public static byte[] ReturnBuffer(string filename, int s)
{
long length;
try
{
fsi = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite);
size = s;
length = fsi.Length;
if (length % size != 0)
{
int r = size - (int)length % size;
inLength = length + r;
fsi.SetLength(inLength);
}
else
inLength = length;
buffForRead = new byte[inLength];
fsi.Read(buffForRead, 0, (int)inLength);
fsi.Close();
return buffForRead;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
}
//================================================================
//Xu li file
//================================================================
public static void Encode(string filename,string outfile,int flag)
{
int i;
try
{
fso = new FileStream(outfile, FileMode.Create);
if (flag == 0)
{
size = 16; //
buffForRead = ReturnBuffer(filename, size);
for (i = 0; i < (int)inLength; i += size)
{
block = new ulong[2];
block = ProcessFile.ReadArrayIntegerFromArrayByte(buffForRead, i, 2);
FestelAlg.FestelClassis(block);
buffForWrite = new byte[size];
buffForWrite = ProcessFile.AppendBuffers(block);
fso.Write(buffForWrite, 0, size);
//
}
}
else
{
// fso = new FileStream(filename, FileMode.Create);
size = 32;
//
buffForRead = ReturnBuffer(filename, size);// ProcessFile.ReadFile(inName, size, out buffForRead);//
//
for (i = 0; i < inLength; i += size)
{
block = new ulong[4];
block = ProcessFile.ReadArrayIntegerFromArrayByte(buffForRead, i, 4);
FestelAlg.FestelModif(block, flag);
buffForWrite = new byte[size];
buffForWrite = ProcessFile.AppendBuffers(block);
fso.Write(buffForWrite, 0, size);
}
}
fso.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -