Skip to content

Commit

Permalink
Add BlobExtractor to extract SET, ST*, and FRS blobs from jarfiles
Browse files Browse the repository at this point in the history
  • Loading branch information
worldwise001 committed Aug 17, 2024
1 parent 94bb044 commit 0caba36
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 17 deletions.
17 changes: 0 additions & 17 deletions src/main/java/sh/shh/Main.java

This file was deleted.

15 changes: 15 additions & 0 deletions src/main/java/sh/shh/midi/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package sh.shh.midi;

import sh.shh.midi.roland.BlobExtractor;

import java.io.IOException;

public class Main {
public static void main(String[] args) {
try {
BlobExtractor.extractAll("lib/test.jar", "out");
} catch (IOException e) {
e.printStackTrace();
}
}
}
44 changes: 44 additions & 0 deletions src/main/java/sh/shh/midi/roland/BlobExtractor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package sh.shh.midi.roland;

import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BlobExtractor {
private static final Pattern REGEX = Pattern.compile("^*/?([^/.]+\\.(SET|ST\\d|FRS))$", Pattern.CASE_INSENSITIVE);

public static void extractAll(String jarFileName, String outputDir) throws IOException {
System.out.println("Extracting " + jarFileName);
JarFile jarFile = new JarFile(jarFileName);
jarFile.stream()
.filter(jarEntry -> REGEX.matcher(jarEntry.getName()).find())
.forEach(jarEntry -> extract(jarFile, jarEntry, outputDir));
}

static void extract(JarFile jarFile, JarEntry jarEntry, String outputDir) {
try {
Matcher matcher = REGEX.matcher(jarEntry.getName());
matcher.find();
String extractedFileName = matcher.group(1);
File outputFile = new File(outputDir, extractedFileName);
System.out.println("Extracting " + jarEntry.getName() + " -> " + outputFile.getPath());
if (!outputFile.exists()) {
outputFile.createNewFile();
}
FileOutputStream os = new FileOutputStream(outputFile);
InputStream is = jarFile.getInputStream(jarEntry);
IOUtils.copy(is, os);
is.close();
os.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

0 comments on commit 0caba36

Please sign in to comment.