Created
January 11, 2013 15:00
-
-
Save hamnis/4511284 to your computer and use it in GitHub Desktop.
Extract RPM Files with redline
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
package rpm; | |
import org.freecompany.redline.ReadableChannelWrapper; | |
import org.freecompany.redline.Scanner; | |
import org.freecompany.redline.header.Format; | |
import org.freecompany.redline.payload.CpioHeader; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.InputStream; | |
import java.nio.channels.Channels; | |
import java.nio.channels.WritableByteChannel; | |
import java.util.zip.GZIPInputStream; | |
public class Extractor { | |
public static File FILE = new File("test1.rpm"); | |
public static File target = new File("target", "test1"); | |
public static void main(String[] args) throws Exception { | |
makeDir(target); | |
System.out.println("FILE = " + FILE); | |
InputStream fios = new FileInputStream( FILE.toString() ); | |
ReadableChannelWrapper in = new ReadableChannelWrapper( Channels.newChannel(fios)); | |
Format format = new Scanner().run( in); | |
System.out.println( format); | |
InputStream uncompressed = new GZIPInputStream( fios ); | |
in = new ReadableChannelWrapper( Channels.newChannel( uncompressed)); | |
CpioHeader header; | |
int total = 0; | |
do { | |
header = new CpioHeader(); | |
total = header.read( in, total); | |
if (header.getType() == CpioHeader.DIR) { | |
makeDir(new File(target, header.getName())); | |
} | |
else if (header.getType() == CpioHeader.FILE) { | |
File file = new File(target, header.getName()); | |
File parent = file.getAbsoluteFile().getParentFile(); | |
if (!parent.exists()) { | |
makeDir(parent); | |
} | |
FileOutputStream str = new FileOutputStream(file); | |
WritableByteChannel writableByteChannel = Channels.newChannel(str); | |
try { | |
header.write(writableByteChannel, header.getFileSize()); | |
} finally { | |
writableByteChannel.close(); | |
} | |
} | |
System.out.println( header); | |
final int skip = header.getFileSize(); | |
if ( uncompressed.skip( skip) != skip) throw new RuntimeException( "Skip failed."); | |
total += header.getFileSize(); | |
} while ( !header.isLast()); | |
} | |
private static void makeDir(File file) { | |
if (!file.exists() && !file.mkdirs()) { | |
throw new IllegalStateException("Failed to create directory"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I tried above code, it extract the rpm file but it is creating 1KB files and content of the file is like below
070701004dc0ac000081ed0000000000000000000000015b1786de00037600000000fd0000000000000000000000000000000e00000000./