Skip to content

Instantly share code, notes, and snippets.

@hamnis
Created January 11, 2013 15:00
Show Gist options
  • Save hamnis/4511284 to your computer and use it in GitHub Desktop.
Save hamnis/4511284 to your computer and use it in GitHub Desktop.
Extract RPM Files with redline
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");
}
}
}
@dizzzz
Copy link

dizzzz commented Jul 13, 2015

The actual extraction of data is not done in this example; line 53 can be written like

// Determine size of file
final int filesize = header.getFileSize();

// Read file data into byte array
byte[] content = org.apache.commons.io.IOUtils.toByteArray(uncompressed, filesize);

@tanweer76
Copy link

tanweer76 commented Sep 23, 2017

I have tried the code for extracting my custom created .rpm file, although it's extracted but the size is not correct
Appreciate a lot if any guidance here!

Can you please share the exact code to extract the complete rpm file.

Tanweer

@RahulShah9191
Copy link

I tried above code, it extract the rpm file but it is creating 1KB files and content of the file is like below

070701004dc0ac000081ed0000000000000000000000015b1786de00037600000000fd0000000000000000000000000000000e00000000./

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment