103 lines
2.7 KiB
Java
103 lines
2.7 KiB
Java
package goblincave.gitea.nes;
|
|
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
|
|
import org.apache.commons.io.FilenameUtils;
|
|
|
|
import io.github.jacksonbrienen.jwfd.JWindowsFileDialog;
|
|
|
|
public class AT3merge {
|
|
|
|
public static void main(String[] args) throws IOException {
|
|
|
|
String[] selection = JWindowsFileDialog.showMultiDialog(null, "Select AT3 files to merge");
|
|
boolean[] merged = new boolean[selection.length];
|
|
|
|
// identify AT3 part files: name_01.at3, name_02.at3
|
|
for(int a = 0; a < selection.length; a++) {
|
|
|
|
if(merged[a])
|
|
continue;
|
|
|
|
File fileA = new File(selection[a]);
|
|
String filenameA = FilenameUtils.removeExtension(fileA.getName());
|
|
String partnameA = filenameA.substring(filenameA.length() - 3);
|
|
filenameA = filenameA.substring(0, filenameA.length() - 3);
|
|
|
|
if(partnameA.equals("_01")) {
|
|
|
|
for(int b = 0; b < selection.length; b++) {
|
|
|
|
if(merged[b] || selection[a].equals(selection[b]))
|
|
continue;
|
|
|
|
File fileB = new File(selection[b]);
|
|
|
|
String filenameB = FilenameUtils.removeExtension(fileB.getName());
|
|
String partnameB = filenameB.substring(filenameB.length() - 3);
|
|
filenameB = filenameB.substring(0, filenameB.length() - 3);
|
|
|
|
if(filenameA.equals(filenameB) && partnameB.equals("_02")) {
|
|
|
|
// MERGE parts into one file
|
|
System.out.println("Merging " + fileA.getName() + " and " + fileB.getName());
|
|
|
|
merge(fileA, fileB);
|
|
|
|
merged[b] = true;
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// REMOVE suffix from processed file
|
|
System.out.println("Renaming to " + filenameA + ".at3");
|
|
|
|
if(fileA.renameTo(new File(fileA.getParent(), filenameA + ".at3")));
|
|
else System.out.println("Failed to rename " + fileA);
|
|
|
|
merged[a] = true;
|
|
|
|
} else continue;
|
|
|
|
}
|
|
|
|
// merge matching parts into a single file
|
|
// remove part suffixes from files
|
|
|
|
}
|
|
|
|
/**
|
|
* Appends fileB to the end of fileA. FileB is then deleted.
|
|
*
|
|
* @param fileA
|
|
* @param fileB
|
|
* @throws IOException
|
|
*/
|
|
private static void merge(File fileA, File fileB) throws IOException {
|
|
|
|
try (
|
|
FileInputStream input = new FileInputStream(fileB);
|
|
FileOutputStream output = new FileOutputStream(fileA, true);
|
|
) {
|
|
// append fileB
|
|
byte[] buffer = new byte[1024];
|
|
int bytesRead;
|
|
while((bytesRead = input.read(buffer)) != -1) {
|
|
output.write(buffer, 0, bytesRead);
|
|
}
|
|
|
|
}
|
|
|
|
// delete fileB
|
|
if(fileB.delete());
|
|
else System.out.println("Failed to delete " + fileB);
|
|
|
|
}
|
|
|
|
}
|