import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;


public class SplitNJoinBySize {
	
	public static void CatFile(String FileNguon, int DungLuong) throws IOException{
		
		BufferedInputStream FNguon = new BufferedInputStream(new FileInputStream(FileNguon));	
		int Amount = DungLuong * 1024 * 1024;
		byte[] Mang = new byte[Amount];
		int a = 0;
		int b = 0;
		int c = 1;		
			
		long TimeA = System.currentTimeMillis();
		System.out.println("Đang Cắt Tệp Tin theo Kích Thước " + DungLuong + "Mb...");
		
		while ((Amount = FNguon.read(Mang)) != -1){	
		if (c > 9){ c = 0; b++;}
		if (b > 9){ b = 0; a++;}
		String FName = ""+a+b+c;
		
		BufferedOutputStream FDuocCat = new BufferedOutputStream(new FileOutputStream(FileNguon+"."+FName));
		FDuocCat.write(Mang, 0, Amount);	
		FDuocCat.close();
		c++;
		}
		
		long TimeZ = System.currentTimeMillis();
		System.out.println("Cắt Tệp Tin thành công. Mất: " + (TimeZ - TimeA) + " ms");
		
		FNguon.close();	
		
		
		// Tìm Kiếm Số Lượng File 00x đã được cắt
		File TepTin = new File(FileNguon);
		String FNameDich = TepTin.getName();	
		
		
		int SoFile = 0;
		String Root = FileNguon.substring(0, (FileNguon.length() - FNameDich.length()));
		File Dir = new File(Root);
		File[] NoiDung = Dir.listFiles();
		String FNameTimKiem = FNameDich.substring(0, (FNameDich.length() - 3));
		
		for (int i = 0; i < NoiDung.length; i++){
			if (NoiDung[i].getName().contains(FNameTimKiem)){			
			SoFile++;
			}
		}
		// Kết Thúc Tìm Kiếm
		System.out.println("Số Tệp Tin sau khi cắt là " + (SoFile - 1));	
		}
	
	public static void GhepFile(String FileNguon) throws IOException{
		
		BufferedOutputStream FDich = new BufferedOutputStream(new FileOutputStream(FileNguon.substring(0, (FileNguon.length() - 4))));
		
		int Amount;
		byte[] Mang = new byte[1024];
		int a = 0;
		int b = 0;
		int c = 1;	
		
		File TepTin = new File(FileNguon);
		String FNameDich = TepTin.getName();	
		
		// Tìm Kiếm Số Lượng File 00x để Ghép
		int SoFile = 0;
		String Root = FileNguon.substring(0, (FileNguon.length() - FNameDich.length()));
		File Dir = new File(Root);
		File[] NoiDung = Dir.listFiles();
		String FNameTimKiem = FNameDich.substring(0, (FNameDich.length() - 3));
		
		for (int i = 0; i < NoiDung.length; i++){
			if (NoiDung[i].getName().contains(FNameTimKiem)){			
			SoFile++;
			}
		}
		// Kết Thúc Tìm Kiếm
				
		String FNameNguon = Root +  FNameTimKiem;
		long TimeA = System.currentTimeMillis();
		System.out.println("Đang ghép các Tệp Tin...");
		for (int i = 0; i < SoFile; i++ ){
			if (c > 9){ c = 0; b++;}
			if (b > 9){ b = 0; a++;}
		BufferedInputStream FNguon = new BufferedInputStream(new FileInputStream(FNameNguon + ""+a+b+c));
		while ((Amount =  FNguon.read(Mang)) != -1){
			FDich.write(Mang, 0, Amount);
		}
		FNguon.close();
		c++;
	}
		FDich.close();
		long TimeZ = System.currentTimeMillis();
		System.out.println("Ghép các Tệp Tin thành công. Mất " + (TimeZ - TimeA) + " ms");
	}
	
public static void main(String[] args) throws IOException {
	
	String FileCat = "D:\\Archive.zip";
	String FileGhep  = "D:\\Archive.zip.001";

	
//	CatFile(FileCat, 6); // Nhập Số Mb
	GhepFile(FileGhep);

}
}
