在老的FAT32文件系统中,最大的单个文件大小必须保存在4G内,对于常常看电影的我这个是不能容许的。不过如今Windows有NTFS文件系统,Linux大部分发行版为Ext4文件系统,最大单个文件大小能大于4G。不过这两者并不能兼容。。格式化NTFS的U盘Linux不能识别,格式化Ext4的U盘Windows不能识别,只能用老的FAT32兼容两者。因此将文件分割,再进行拼接就很重要,文件通过分割了在网络上传输就十分方便,也能开多线程对每部分进行HASH提升处理效率。网络
首先:对文件进行分割须要肯定每一部分的大小,假设上面的Fury.mkv文件大小为280M,分割每一块设置默认大小为64M,因此: 多线程
public class FileSlice {
/**
* 分割文件
* @param filePath 文件路径
* @param filePieceSize 文件每块大小,单位为字节,为-1则默认为每块64M
* @return 成功返回True,出错则返回False
*/
public static boolean slice(Path filePath, int filePieceSize){
return true;
}
/**
* 将分割好的文件从新连接
* @param filePath 被分割好的其中之一文件路径,默认其余块与其在同一目录下
* @param howManyParts 一共有多少块
* @return 成功返回True,出错则返回False
*/
public static boolean glue(Path filePath, int howManyParts){
return true;
}
}
复制代码
接下来实现单线程的分割方法: 用图解的话应该是这样: ide
if (!Files.exists(filePath)){
return false;
}
复制代码
接下来判断每块大小是否使用默认值:函数
if(filePieceSize == -1){
filePieceSize = 1024*1024*64;
}
复制代码
将路径转换为文件对象,再计算将分割多少块:测试
File file = filePath.toFile();
int howManyParts = (int) Math.ceil(file.length() / (double)filePieceSize);
复制代码
初始化输入输出流,出错输出错误信息,返回false,得到当前目录:spa
DataInputStream fileReader = null;
try {
fileReader = new DataInputStream(new FileInputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("文件找不到!");
return false;
}
DataOutputStream fileWriter;
Path dir = filePath.getParent();
复制代码
接下来读取文件,而且分别输出到各个part文件中:线程
int readLength = -1;
long total = 0;
try {
for (int i = 1; i <= howManyParts ; i++){
//新建文件part i
Path temp = Files.createFile(dir.resolve(filePath.getFileName() + ".part" + i));
//搭建输出流
fileWriter = new DataOutputStream(new FileOutputStream(temp.toFile()));
//读取文件并输出
while ( (readLength = fileReader.read(buffer)) != -1){
fileWriter.write(buffer,0,readLength);
fileWriter.flush();
total += readLength;
if (total == filePieceSize){
total = 0;
break;
}
}
//part i的文件已经输出完毕,关闭流
fileWriter.close();
}
//读取完毕,关闭输入流
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("IO错误!");
return false;
}
复制代码
该函数已经实现完毕,接下来测试(因为电影Fury有14G。。太大了。。仍是换个吧): code
public static void main(String[] args) throws IOException {
double before = System.currentTimeMillis();
Path bigboss = Paths.get("D:\\Video\\我是大哥大\\我是大哥大.Kyou.kara.Ore.wa.Ep05.Chi_Jap.HDTVrip.1280X720.mp4");
FileSlice.slice(bigboss,-1);
double after = System.currentTimeMillis();
System.out.println("分割文件我是大哥大.Kyou.kara.Ore.wa.Ep05.Chi_Jap.HDTVrip.1280X720.mp4," + Files.size(bigboss) + "字节,总用时" + (after - before) + "ms" );
}
复制代码
运行结果:cdn
分割文件我是大哥大.Kyou.kara.Ore.wa.Ep05.Chi_Jap.HDTVrip.1280X720.mp4,765321889字节,总用时16335.0ms
复制代码
这个就很简单了,和分割相反就OK。 直接上完整代码:对象
public static boolean glue(Path filePath, int howManyParts){
if (!Files.exists(filePath)){
return false;
}
//获取原始文件名
String filename = getOriginalFileName(filePath.getFileName().toString());
if (filename == null){
System.out.println("传入part文件名解析出错!");
return false;
}
//初始化缓冲区
byte [] buffer = new byte[1024 * 8];
//获取文件存储的路径
Path dir = filePath.getParent();
try {
DataInputStream fileReader = null;
//建立原始文件
Files.createFile(dir.resolve(filename));
//搭建原始文件输出流
DataOutputStream fileWriter = new DataOutputStream(new FileOutputStream(dir.resolve(filename).toFile()));
int readLength = -1;
for (int i = 1; i <= howManyParts ; i++){
//获得part i文件路径
Path temp = dir.resolve(filename + ".part" + i);
//搭建输入流
fileReader = new DataInputStream(new FileInputStream(temp.toFile()));
//读取文件并输出
while ( (readLength = fileReader.read(buffer)) != -1){
fileWriter.write(buffer,0,readLength);
fileWriter.flush();
}
//part i的文件已经读入完毕,关闭流
fileReader.close();
}
//写入完毕,关闭输出流
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("IO错误!");
return false;
}
return true;
}
复制代码
再测试刚刚分割好的我是大哥大第5集
public static void main(String[] args) throws IOException {
double before = System.currentTimeMillis();
Path bigboss = Paths.get("D:\\Video\\我是大哥大\\我是大哥大.Kyou.kara.Ore.wa.Ep05.Chi_Jap.HDTVrip.1280X720.mp4.part1");
FileSlice.glue(bigboss,12);
double after = System.currentTimeMillis();
System.out.println("拼接12个part,用时" + (after - before) + "ms");
}
复制代码
结果输出,用12s左右,还行。
拼接12个part,用时12147.0ms
复制代码
打开播放毫无问题,最后截张图。