溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Java實(shí)現(xiàn)文件切割拼接的實(shí)現(xiàn)代碼

發(fā)布時(shí)間:2020-10-18 02:11:41 來(lái)源:腳本之家 閱讀:168 作者:LudwigWuuu 欄目:編程語(yǔ)言

單線程實(shí)現(xiàn)

文件分割

在老的FAT32文件系統(tǒng)中,最大的單個(gè)文件大小必須保存在4G內(nèi),對(duì)于經(jīng)??措娪暗奈疫@個(gè)是不能允許的。不過(guò)現(xiàn)在Windows有NTFS文件系統(tǒng),Linux大部分發(fā)行版為Ext4文件系統(tǒng),最大單個(gè)文件大小能大于4G。不過(guò)這二者并不能兼容。。格式化NTFS的U盤(pán)Linux不能識(shí)別,格式化Ext4的U盤(pán)Windows不能識(shí)別,只能用老的FAT32兼容二者。所以將文件分割,再進(jìn)行拼接就很重要,文件經(jīng)過(guò)分割了在網(wǎng)絡(luò)上傳輸就十分方便,也能開(kāi)多線程對(duì)每部分進(jìn)行HASH提高處理效率。

Java實(shí)現(xiàn)文件切割拼接的實(shí)現(xiàn)代碼

最近看的BradPitt的《狂怒》

首先:對(duì)文件進(jìn)行分割需要確定每一部分的大小,假設(shè)上面的 Fury.mkv 文件大小為 280M ,分割每一塊設(shè)置默認(rèn)大小為 64M ,所以:

Java實(shí)現(xiàn)文件切割拼接的實(shí)現(xiàn)代碼

對(duì)于最后一塊,一般小于等于設(shè)定好的每塊默認(rèn)大小。 每塊大小設(shè)置好了,接下來(lái),就需要將文件的路徑獲取,代碼中搭建輸入流,將文件讀入內(nèi)存緩沖區(qū)中,再搭建輸出流,將緩沖區(qū)輸出到新的分割文件中。 再接下來(lái)實(shí)現(xiàn)就很簡(jiǎn)單了。 新建一個(gè) FileSlice

類(lèi):有切割方法,拼接方法。

public class FileSlice {
 /**
  * 分割文件
  * @param filePath 文件路徑
  * @param filePieceSize 文件每塊大小,單位為字節(jié),為-1則默認(rèn)為每塊64M
  * @return 成功返回True,出錯(cuò)則返回False
  */
 public static boolean slice(Path filePath, int filePieceSize){
  return true;
 }

 /**
  * 將分割好的文件重新鏈接
  * @param filePath 被分割好的其中之一文件路徑,默認(rèn)其他塊與其在同一目錄下
  * @param howManyParts 一共有多少塊
  * @return 成功返回True,出錯(cuò)則返回False
  */
 public static boolean glue(Path filePath, int howManyParts){
  return true;
 }
}

接下來(lái)實(shí)現(xiàn)單線程的分割方法: 用圖解的話應(yīng)該是這樣:

Java實(shí)現(xiàn)文件切割拼接的實(shí)現(xiàn)代碼

代碼實(shí)現(xiàn): 進(jìn)入函數(shù)首先判斷文件是否存在:

if (!Files.exists(filePath)){
 return false;
}

接下來(lái)判斷每塊大小是否使用默認(rèn)值:

if(filePieceSize == -1){
 filePieceSize = 1024*1024*64;
}

將路徑轉(zhuǎn)換為文件對(duì)象,再計(jì)算將分割多少塊:

File file = filePath.toFile();
int howManyParts = (int) Math.ceil(file.length() / (double)filePieceSize);

初始化輸入輸出流,出錯(cuò)輸出錯(cuò)誤信息,返回false,獲得當(dāng)前目錄:

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();

接下來(lái)讀取文件,并且分別輸出到各個(gè)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的文件已經(jīng)輸出完畢,關(guān)閉流
  fileWriter.close();
 }
 //讀取完畢,關(guān)閉輸入流
 fileReader.close();
} catch (IOException e) {
 e.printStackTrace();
 System.out.println("IO錯(cuò)誤!");
 return false;
}

該函數(shù)已經(jīng)實(shí)現(xiàn)完畢,接下來(lái)測(cè)試(由于電影Fury有14G。。太大了。。還是換個(gè)吧):

Java實(shí)現(xiàn)文件切割拼接的實(shí)現(xiàn)代碼

我是大哥大第5集,有729M,大概能分個(gè)12個(gè)part吧。

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) + "字節(jié),總用時(shí)" + (after - before) + "ms" );

}

運(yùn)行結(jié)果:

分割文件我是大哥大.Kyou.kara.Ore.wa.Ep05.Chi_Jap.HDTVrip.1280X720.mp4,765321889字節(jié),總用時(shí)16335.0ms

Java實(shí)現(xiàn)文件切割拼接的實(shí)現(xiàn)代碼

速度還是挺慢的。。 下次還是換成多線程來(lái)實(shí)現(xiàn),再來(lái)測(cè)試下速度。在單線程情況下一個(gè)普通的40分鐘日劇都要15-30s左右,要是mkv格式的電影都要好久了。。不過(guò)其實(shí)極限應(yīng)該不在CPU中執(zhí)行的速度,而是在硬盤(pán)IO中,如果是普通硬盤(pán)那么就算是多線程也應(yīng)該提速不了多少。。

文件拼接

這個(gè)就很簡(jiǎn)單了,和分割相反就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文件名解析出錯(cuò)!");
  return false;
 }
 //初始化緩沖區(qū)
 byte [] buffer = new byte[1024 * 8];
 //獲取文件存儲(chǔ)的路徑
 Path dir = filePath.getParent();

 try {
  DataInputStream fileReader = null;
  //創(chuàng)建原始文件
  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的文件已經(jīng)讀入完畢,關(guān)閉流
   fileReader.close();
  }
  //寫(xiě)入完畢,關(guān)閉輸出流
  fileWriter.close();
 } catch (IOException e) {
  e.printStackTrace();
  System.out.println("IO錯(cuò)誤!");
  return false;
 }
 return true;
}

再測(cè)試剛剛分割好的我是大哥大第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個(gè)part,用時(shí)" + (after - before) + "ms");

}

結(jié)果輸出,用12s左右,還行。

拼接12個(gè)part,用時(shí)12147.0ms

打開(kāi)播放毫無(wú)問(wèn)題,最后截張圖。

Java實(shí)現(xiàn)文件切割拼接的實(shí)現(xiàn)代碼

未完待續(xù)。。下次來(lái)使用多線程進(jìn)行實(shí)現(xiàn)。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI