文件分割代
FileInfo fInfo = new FileInfo("c:\\xx.rar");
int splitSize = 1024 * 400;
int splitCount = (int)fInfo.Length % splitSize==0 ? (int)fInfo.Length / splitSize : (int)fInfo.Length / splitSize + 1;
FileStream fileStream = new FileStream(fInfo.FullName,FileMode.Open,FileAccess.Read);
BinaryReader reader = new BinaryReader(fileStream);
for (int i = 0; i < splitCount; i++)
{
string tempName = "temp_" + i.ToString();
FileStream fs = new FileStream("c:\\tempfile" + tempName,FileMode.CreateNew,FileAccess.Write);
BinaryWriter tempWriter = new BinaryWriter(fs);
byte []tempData= reader.ReadBytes(splitSize);
if (tempData.Length == 0)
{
break;
}
tempWriter.Write(tempData, 0, tempData.Length);
tempWriter.Flush();
tempWriter.Close();
fs.Close();
}
reader.Close();
fileStream.Close();
文件合并代码:
DirectoryInfo dir = new DirectoryInfo("D:\\tempfile");
FileInfo[] fs = dir.GetFiles();
var sorts = from singlefile in fs
orderby singlefile.Name ascending
select singlefile;
string fullname = "D:\\xx.rar"
FileStream fileStream = new FileStream(fullname,FileMode.CreateNew,FileAccess.Write);
BinaryWriter writer = new BinaryWriter(fileStream);
foreach (FileInfo f in sorts)
{
FileStream tempStream = new FileStream(f.FullName, FileMode.Open, FileAccess.Read);
BinaryReader tempReader = new BinaryReader(tempStream);
byte[] data = tempReader.ReadBytes(1024 * 400);
writer.Write(data, 0, data.Length);
writer.Flush();
tempReader.Close();
tempStream.Close();
}
writer.Flush();
writer.Close();
fileStream.Close();
对exe文件和其他类型文件都没问题 对压缩文件(rar)分割后合并 出现 不可预料的压缩文件末端
不知道是什么原因?