文件分塊與合並


1. 文件分塊

文件分塊的流程如下:

  1. 獲取源文件長度
  2. 根據設定的分塊文件的大小計算出塊數
  3. 從源文件讀數據依次向每一個塊文件寫數據。
//測試文件分塊方法
    @Test
    public void testChunk() throws IOException {
        File sourceFile = new File("D:/test/ffmpeg/test.mp4");
        String chunkPath = "D:/test/ffmpeg/file/";
        File chunkFolder = new File(chunkPath);
        if (!chunkFolder.exists()) {
            chunkFolder.mkdirs();
        }
        //分塊大小
        long chunkSize = 1024 * 1024 * 1;
        //分塊數量
        long chunkNum = (long) Math.ceil(sourceFile.length() * 1.0 / chunkSize);
        if (chunkNum <= 0) {
            chunkNum = 1;
        }
        //緩沖區大小
        byte[] b = new byte[1024];
        //使用RandomAccessFile訪問文件
        RandomAccessFile raf_read = new RandomAccessFile(sourceFile, "r");
        //分塊
        for (int i = 0; i < chunkNum; i++) {
            //創建分塊文件
            File file = new File(chunkPath + i);
            boolean newFile = file.createNewFile();
            if (newFile) {
                //向分塊文件中寫數據
                RandomAccessFile raf_write = new RandomAccessFile(file, "rw");
                int len = -1;
                while ((len = raf_read.read(b)) != -1) {
                    raf_write.write(b, 0, len);
                    if (file.length() > chunkSize) {
                        break;
                    }
                }
                raf_write.close();
            }
        }
        raf_read.close();
    }

2. 文件合並

文件合並流程:

  1. 找到要合並的文件並按文件合並的先后進行排序。
  2. 創建合並文件
  3. 依次從合並的文件中讀取數據向合並文件寫入數
//測試文件合並方法
    @Test
    public void testMerge() throws IOException {
        //塊文件目錄
        File chunkFolder = new File("D:/test/ffmpeg/file/");
        //合並文件
        File mergeFile = new File("D:/test/ffmpeg/test1.mp4");
        if (mergeFile.exists()) {
            mergeFile.delete();
        }
        //創建新的合並文件
        mergeFile.createNewFile();
        //用於寫文件
        RandomAccessFile raf_write = new RandomAccessFile(mergeFile, "rw");
        //指針指向文件頂端
        raf_write.seek(0);
        //緩沖區
        byte[] b = new byte[1024];
        //分塊列表
        File[] fileArray = chunkFolder.listFiles();
        //  轉成集合,便於排序
        List<File> fileList = new ArrayList<File>(Arrays.asList(fileArray));
        //  從小到大排序
        Collections.sort(fileList, new Comparator<File>() {
            @Override
            public int compare(File o1, File o2) {
                if (Integer.parseInt(o1.getName()) < Integer.parseInt(o2.getName())) {
                    return -1;
                }
                return 1;
            }
        });
        //合並文件
        for (File chunkFile : fileList) {
            RandomAccessFile raf_read = new RandomAccessFile(chunkFile, "rw");
            int len = -1;
            while ((len = raf_read.read(b)) != -1) {
                raf_write.write(b, 0, len);
            }
            raf_read.close();
        }
        raf_write.close();
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM