Skip to content

Instantly share code, notes, and snippets.

/Runner.java Secret

Created September 19, 2014 22:56
get file from compiled rpm in java
package demo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.channels.Channels;
import java.util.zip.GZIPInputStream;
import org.apache.commons.compress.archivers.cpio.CpioArchiveEntry;
import org.apache.commons.compress.archivers.cpio.CpioArchiveInputStream;
import org.apache.commons.compress.compressors.xz.XZCompressorInputStream;
import org.apache.commons.io.IOUtils;
import org.redline_rpm.ReadableChannelWrapper;
import org.redline_rpm.Scanner;
import org.redline_rpm.header.AbstractHeader.Entry;
import org.redline_rpm.header.Format;
import org.redline_rpm.header.Header;
import org.redline_rpm.header.Header.HeaderTag;
import org.redline_rpm.header.PayloadCompressionType;
public class Runner {
private final static String defaultUrl = "my.rpm;"
/**
* @param args
*/
public static void main(String[] args) {
String url = defaultUrl;
if (args.length > 1) {
url = defaultUrl;
}
/**
* Checks if the File Exists, and manages the download if it does not
* alread exists
*/
System.out.println("= Download the RPM =");
File rpm = new File(generateRPMNameFromURL(url));
if (!rpm.exists()) {
boolean success = downloadFile(url);
if (!success) {
System.out
.println("WARNING: No Download Completed - Refer to StackTrace");
}
}
/**
*
*/
System.out.println("= Begin Dump =");
File cpio = new File(rpm.getPath() + ".cpio");
if (!cpio.exists()) // Else dump it
dumpToCpio(rpm);
listCpioFiles(cpio);
}
/**
* lists the files in the CPIO
*
* @param rpm
*/
public static void listCpioFiles(File cpio) {
try {
CpioArchiveInputStream cpioIn = new CpioArchiveInputStream(
new FileInputStream(cpio));
CpioArchiveEntry cpioEntry;
while ((cpioEntry = (CpioArchiveEntry) cpioIn.getNextEntry()) != null) {
outputEntry(cpioEntry.getName(), cpioEntry, cpioIn);
}
cpioIn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* outputs the file name if it's worth while to output
*
* @param fileName
* @throws IOException
*/
public static void outputEntry(String fileName, CpioArchiveEntry entry,
CpioArchiveInputStream cpioIn) throws IOException {
if (fileName.startsWith("target.xml") {
String outputName = fileName.substring(fileName.lastIndexOf("/"));
System.out.println("/" + outputName);
File cpioFile = new File("/" + outputName);
OutputStream fos = new FileOutputStream(cpioFile);
IOUtils.copy(cpioIn, fos);
System.out.println("DONE");
fos.close();
}
}
/**
* dumps the rpm to a cpio file
*
* @param rpm
*/
public static void dumpToCpio(File rpm) {
try {
InputStream fios = new FileInputStream(rpm.getPath());
ReadableChannelWrapper in = new ReadableChannelWrapper(
Channels.newChannel(fios));
Scanner scanner = new Scanner();
Format format = scanner.run(in);
Header rpmheader = format.getHeader();
InputStream uncompressed = Runner
.openPayloadStream(rpmheader, fios);
File cpio = new File(rpm.getPath() + ".cpio");
OutputStream fos = new FileOutputStream(cpio);
IOUtils.copy(uncompressed, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the proper stream wrapper to handling the rpmIS payload section
* based on the payload compression header tag.
*
* @param header
* the header
* @param rpmIS
* raw input stream of the rpm
* @return the "proper" input stream
* @throws IOException
* an IO error occurred
*/
public static InputStream openPayloadStream(Header header, InputStream rpmIS)
throws IOException {
Entry pcEntry = header.getEntry(HeaderTag.PAYLOADCOMPRESSOR);
String[] pc = (String[]) pcEntry.getValues();
PayloadCompressionType pcType = PayloadCompressionType.valueOf(pc[0]);
InputStream payloadIS = rpmIS;
switch (pcType) {
case none:
break;
case gzip:
payloadIS = new GZIPInputStream(rpmIS);
break;
case bzip2:
try {
payloadIS = new org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream(
rpmIS);
} catch (Exception e) {
throw new IOException(
"Failed to load BZIP2 compression stream", e);
}
break;
case xz:
try {
payloadIS = new XZCompressorInputStream(rpmIS);
} catch (Exception e) {
throw new IOException("Failed to load XZ compression stream", e);
}
break;
}
return payloadIS;
}
/**
* download the file at the given url only supports http and valid https
* certificates
*
* @param url
* @return
*/
public static boolean downloadFile(String u) {
boolean downloaded = false;
try {
URL url = new URL(u);
HttpURLConnection httpCon = (HttpURLConnection) url
.openConnection();
httpCon.setRequestMethod("GET");
InputStream is = httpCon.getInputStream();
System.out.println("Response Code " + httpCon.getResponseCode());
File rpm = new File(generateRPMNameFromURL(u));
OutputStream fos = new FileOutputStream(rpm);
IOUtils.copy(is, fos);
fos.close();
downloaded = true;
} catch (Exception e) {
e.printStackTrace();
}
return downloaded;
}
/**
* generates RPM Name from URL
*
* @param url
* @return
*/
private static String generateRPMNameFromURL(String url) {
return url.substring(url.lastIndexOf("/"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment