-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add BlobExtractor to extract SET, ST*, and FRS blobs from jarfiles
- Loading branch information
1 parent
94bb044
commit 0caba36
Showing
3 changed files
with
59 additions
and
17 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |