java大文件分块处理

发布时间:2024年01月12日
package com.ri;
import java.io.*;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;
public class FileChunkingAndMergingTest {

    private static final int CHUNK_SIZE = 1024 * 1024; // 假设每个块大小为1MB

    public static void main(String[] args) throws IOException {
        String sourceFilePath = "D:\\apache-tomcat-8.5.75.zip"; // 大文件路径
        String tempDirectoryPath = "C:\\abc"; // 临时目录用于存放分块
        String targetFilePath = "C:\\abc\\merged-tomcat.zip"; // 合并后的文件路径
        // 分割文件到临时目录
        List<String> chunkFiles = splitFile(sourceFilePath, tempDirectoryPath, CHUNK_SIZE);
        // 合并分块文件到目标文件
        mergeChunks(chunkFiles, targetFilePath);
        System.out.println("File has been successfully split and merged.");
    }

    /**
     * 将大文件分割成多个小文件,并将各个分块文件路径存入列表返回。
     */
    public static List<String> splitFile(String sourceFilePath, String tempDirectoryPath, int chunkSize) throws IOException {
        File sourceFile = new File(sourceFilePath);
        File tempDir = new File(tempDirectoryPath);
        if (!tempDir.exists() && !tempDir.mkdirs()) {
            throw new IOException("Failed to create temporary directory: " + tempDirectoryPath);
        }

        List<String> chunkFilePaths = new ArrayList<>();
        try (FileInputStream fis = new FileInputStream(sourceFile);
             FileChannel fileChannel = fis.getChannel()) {

            long fileSize = fileChannel.size();
            for (long position = 0; position < fileSize; position += chunkSize) {
                long remainingFileSize = fileSize - position;
                long chunkEnd = Math.min(position + chunkSize - 1, fileSize - 1);

                String chunkFileName = "chunk-" + position + ".part";
                File chunkFile = new File(tempDirectoryPath, chunkFileName);
                try (FileOutputStream fos = new FileOutputStream(chunkFile);
                     FileChannel outChannel = fos.getChannel()) {

                    fileChannel.transferTo(position, Math.min(chunkSize, remainingFileSize), outChannel);
                }

                chunkFilePaths.add(chunkFile.getAbsolutePath());
            }
        }

        return chunkFilePaths;
    }

    /**
     * 将多个分块文件按顺序合并到目标文件。
     */
    public static void mergeChunks(List<String> chunkFilePaths, String targetFilePath) throws IOException {
        File targetFile = new File(targetFilePath);
        try (FileOutputStream fos = new FileOutputStream(targetFile)) {
            FileChannel outChannel = fos.getChannel();

            for (String chunkFilePath : chunkFilePaths) {
                File chunkFile = new File(chunkFilePath);
                try (FileInputStream fis = new FileInputStream(chunkFile);
                     FileChannel inChannel = fis.getChannel()) {

                    inChannel.transferTo(0, inChannel.size(), outChannel);
                }finally {
                    // 在每次成功转移数据后删除分块文件
                    if (!chunkFile.delete()) {
                        System.err.println("Failed to delete the chunk file: " + chunkFilePath);
                    }
                }

            }
        }
        
        // 确保所有分块都已成功合并并删除,这里可以进行额外的清理操作
//        for (String chunkFilePath : chunkFilePaths) {
//            File chunkFile = new File(chunkFilePath);
//            if (chunkFile.exists() && !chunkFile.delete()) { // 如果由于某种原因之前未删除,再次尝试删除
//            	FileUtils.deleteQuietly(chunkFile);
//                System.err.println("Final attempt to delete the chunk file failed: " + chunkFilePath);
//            }
//        }

    }
}

文章来源:https://blog.csdn.net/qq_37792401/article/details/135521905
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。