public static void JoinFile(string inputFile, string outputFile, KsSplitJoinHandler handler) { long rwIncrament=500000;//updateInterval=400000, updateIndex=0; FileInfo fInfo=new FileInfo(inputFile); long iLen=fInfo.Length; FileStream ifs=new FileStream(inputFile, FileMode.Open); BinaryReader reader=new BinaryReader(ifs); //bool cont; FileInfo fi=new FileInfo(outputFile); FileMode fm=FileMode.Append; if(!fi.Exists) fm=FileMode.CreateNew; FileStream ofs=new FileStream(outputFile, fm); BinaryWriter writer=new BinaryWriter(ofs); long i=0, cnt; while(i<iLen) { cnt=rwIncrament; if((i+cnt)>=iLen) cnt=iLen-i; //byte val[cnt]; writer.Write(reader.ReadBytes((int)cnt)); i+=cnt; if(!handler.OnUpdate(inputFile, outputFile, i, iLen)) { ifs.Close(); reader.Close(); ofs.Close(); writer.Close(); handler.OnCanceled(inputFile, outputFile, i, iLen); return; } } ifs.Close(); reader.Close(); ofs.Close(); writer.Close(); handler.OnFinished(inputFile, outputFile, i, iLen); }
public static void SplitFile(string inputFile, string outputDir, string outputPrefix, long chunkSizeInBytes, KsSplitJoinHandler handler) { string outputFile=""; if(!outputDir.EndsWith("\\")) outputDir+="\\"; FileInfo fInfo=new FileInfo(inputFile); long i, iLen=fInfo.Length, pos=0, len, cnt=0, cnt2, rwIncrament=500000; FileStream ifs=new FileStream(inputFile, FileMode.Open, FileAccess.Read); BinaryReader reader=new BinaryReader(ifs); while (pos<iLen) { cnt++; outputFile=outputDir+outputPrefix+".ksChunk"+cnt.ToString().PadLeft(3, '0'); FileStream ofs=new FileStream(outputFile, FileMode.Create); BinaryWriter writer=new BinaryWriter(ofs); len=chunkSizeInBytes; if(pos<1) len-=200; if((pos+len)>iLen) len=(iLen-pos); i=0; while(i<len) { cnt2=rwIncrament; if((i+cnt2)>=len) cnt2=len-i; writer.Write(reader.ReadBytes((int)cnt2)); i+=cnt2; pos+=cnt2; if(!handler.OnUpdate(inputFile, outputFile, pos, iLen)) { reader.Close(); ifs.Close(); writer.Close(); ofs.Close(); handler.OnCanceled(inputFile, outputFile, pos, iLen); return; } } writer.Close(); ofs.Close(); if(!handler.OnUpdate(inputFile, outputFile, pos, iLen)) { reader.Close(); ifs.Close(); handler.OnCanceled(inputFile, outputFile, pos, iLen); return; } } reader.Close(); ifs.Close(); SaveAutoJoinFile(outputDir+outputPrefix+".ksAutoJoin", (int)cnt, (int)iLen); handler.OnFinished(inputFile, outputFile, pos, iLen); }