using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Data; using System.Threading; namespace Merge { public class Program { static void Main(string[] args)//////TESTAR TESTAR TESTAR TAMANHO MAXIMO!!!!!!!!!!!!!!!!!!!!!!! { FileHandling obj = new FileHandling(); int opcao; do { Console.WriteLine("1 - Dividir"); Console.WriteLine("2 - Juntar"); opcao = int.Parse(Console.ReadLine()); Console.WriteLine("Digite o caminho do arquivo"); String arquivo = Console.ReadLine(); if (opcao == 1) { Console.WriteLine("Digite o tamanho de cada pedaco"); int tamanhoDoPedaco = int.Parse(Console.ReadLine()); obj.SplitUp(arquivo, tamanhoDoPedaco); } else if (opcao == 2) { obj.MargeUp(arquivo); } } while (opcao != 1 && opcao != 2); } } //Operacao principal de arquivo eh feita aqui public class FileHandling { string caminho = string.Empty; int sliceLen = 512; //512 bytes cada fatia int counter = 0; int limitesuperior = 0; float temp; public void SplitUp(string filename, int fileSizeInBytes) { byte[] buffer = new byte[sliceLen]; //string curFileName; caminho = filename; caminho = caminho.Replace("\\", "\\\\"); if (fileSizeInBytes < 1) { Console.WriteLine("You can't put less than 1 Byte"); return; } else sliceLen = fileSizeInBytes; if (filename.Length < 1) return; counter = 0; BinaryReader br = new BinaryReader(new FileStream(caminho, FileMode.Open)); //Verifica se a fatia eh maior que o tamanho do aquivo if (br.BaseStream.Length < sliceLen) sliceLen = (int)br.BaseStream.Length; //Comeca a partir for (int iteracao = 1; iteracao <= 5; iteracao++) { limitesuperior += (((int)br.BaseStream.Length / sliceLen) / 5 ); if (iteracao == 5) { temp = (float)br.BaseStream.Length / sliceLen; if ((float)(br.BaseStream.Length % sliceLen) != 0) temp = (int)temp + 1; temp = temp % 5; limitesuperior = limitesuperior + (int)temp; //Console.WriteLine("aaaaaaaaaaaaaaa: " + temp); //Console.WriteLine("limite: " + limitesuperior); //Console.WriteLine("tmanho: " + ((br.BaseStream.Length ))); //Console.WriteLine("pedaço: " + ((sliceLen))); } Worker workerObject = new Worker(counter, sliceLen, br, limitesuperior, filename); Thread workerThread = new Thread(workerObject.DoWork); workerThread.Start(); //Console.WriteLine("limite superior: " + limitesuperior); counter = counter + ((int)br.BaseStream.Length / (sliceLen* 5)); } } public void MargeUp(string firstFileName) { if (firstFileName.Length < 1) return; string endPart = firstFileName; string orgFile = ""; orgFile = endPart.Substring(0, endPart.LastIndexOf(".")); endPart = endPart.Substring(endPart.LastIndexOf(".") + 1); if (endPart == "E")//Se so houver um pedaco { orgFile = orgFile.Substring(0, orgFile.LastIndexOf(".")); endPart = "0"; } if (File.Exists(orgFile)) { Console.WriteLine("Arquivo ja existe, quer deletar e fazer um novo?"); Console.WriteLine("1 - Sim"); Console.WriteLine("2 - Nao"); int opcao = int.Parse(Console.ReadLine()); do { if (opcao == 1) File.Delete(orgFile); else if (opcao == 2) { Console.WriteLine("Arquivo nao foi juntado. Operacao cancelada pelo usuario"); return; } } while (opcao != 1 && opcao != 2); } //Junta a partir daqui BinaryWriter bw = new BinaryWriter(File.Open(orgFile, FileMode.Append)); string nextFileName = ""; byte[] buffer = new byte[bw.BaseStream.Length]; int counter = int.Parse(endPart); while (true) { nextFileName = orgFile + "." + counter.ToString(); if (File.Exists(nextFileName + ".E")) { //Ultima fatia buffer = File.ReadAllBytes(nextFileName + ".E"); bw.Write(buffer); break; } else { buffer = File.ReadAllBytes(nextFileName); bw.Write(buffer); } counter++; } bw.Close(); Console.WriteLine("File assembled successfully"); } } public class Worker { int counter; int sliceLen; BinaryReader br; int limitesuperior; string filename; public Worker(int counter, int sliceLen, BinaryReader br, int limitesuperior, string filename) { this.counter = counter; this.sliceLen = sliceLen; this.br = br; this.limitesuperior = limitesuperior; this.filename = filename; } // This method will be called when the thread is started. public void DoWork() { byte[] buffer = new byte[sliceLen]; string curFileName; while (counter != limitesuperior) { //Console.WriteLine(counter); if (br.BaseStream.Length > sliceLen * (counter + 1)) { br.BaseStream.Read(buffer, 0, sliceLen); curFileName = filename + "." + counter.ToString(); } else { Console.WriteLine("Pego papai"); int remainLen = (int)br.BaseStream.Length - (sliceLen * counter); Console.WriteLine(); buffer = new byte[remainLen]; br.BaseStream.Read(buffer, 0, remainLen); curFileName = filename + "." + counter.ToString() + ".E"; } if (File.Exists(curFileName)) File.Delete(curFileName); File.WriteAllBytes(curFileName, buffer); counter++; } //br.Close(); Console.WriteLine("File splitted successfully"); Console.Read(); } } }