0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

分割圧縮ファイルの解凍

Last updated at Posted at 2023-12-01

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class FileProcessor {

public static void main(String[] args) {
    splitAndCombineFiles("/input/sample.docx", "/split", "/output/sample.docx");
}

private static void splitAndCombineFiles(String inputPath, String splitFolderPath, String outputPath) {
    try {
        // Split the original file
        splitFile(inputPath, splitFolderPath);

        // Combine the split files
        combineFiles(splitFolderPath, outputPath);

    } catch (IOException e) {
        e.printStackTrace();
    }
}

private static void splitFile(String inputPath, String splitFolderPath) throws IOException {
    File inputFile = new File(inputPath);
    byte[] fileContent = readBytesFromFile(inputFile);

    int sections = 3;
    int bytesPerSection = fileContent.length / sections;

    for (int i = 0; i < sections; i++) {
        int startIndex = i * bytesPerSection;
        int endIndex = (i == sections - 1) ? fileContent.length : (i + 1) * bytesPerSection;

        byte[] sectionContent = new byte[endIndex - startIndex];
        System.arraycopy(fileContent, startIndex, sectionContent, 0, sectionContent.length);

        writeBytesToFile(splitFolderPath + "/split" + (i + 1) + ".bin", sectionContent);
    }
}

private static void combineFiles(String splitFolderPath, String outputPath) throws IOException {
    List<byte[]> splitContents = new ArrayList<>();

    // Load split files
    for (int i = 1; i <= 3; i++) {
        byte[] content = readBytesFromFile(new File(splitFolderPath + "/split" + i + ".bin"));
        splitContents.add(content);
    }

    // Combine split contents
    byte[] combinedContent = concatenateByteArrays(splitContents);

    // Save combined content to output file
    writeBytesToFile(outputPath, combinedContent);
}

private static byte[] readBytesFromFile(File file) throws IOException {
    try (FileInputStream fis = new FileInputStream(file);
         ByteArrayOutputStream bos = new ByteArrayOutputStream()) {

        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = fis.read(buffer)) != -1) {
            bos.write(buffer, 0, bytesRead);
        }

        return bos.toByteArray();
    }
}

private static void writeBytesToFile(String filePath, byte[] content) throws IOException {
    try (FileOutputStream fos = new FileOutputStream(filePath)) {
        fos.write(content);
    }
}

private static byte[] concatenateByteArrays(List<byte[]> byteArrays) {
    int totalLength = 0;
    for (byte[] array : byteArrays) {
        totalLength += array.length;
    }

    byte[] result = new byte[totalLength];
    int currentIndex = 0;

    for (byte[] array : byteArrays) {
        System.arraycopy(array, 0, result, currentIndex, array.length);
        currentIndex += array.length;
    }

    return result;
}

}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?