Hello everyone,
I would like to show you how to find/read a file that comes from a jar(compressed file) and provide the content of this.
Structure of folders and files I have:
File.jar
-->under File.jar there is a folder called zipThiagoFolder that has the xml file named xmlFileNameInJarFile.xml
Source code:
/**
*
*/
package thiago.leoncio;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.apache.commons.io.IOUtils;
/**
* @author Thiago Leoncio
* @date 08062016
* Description: provide the details on how to read a XML file from jar or compressed file in Java
*
*/
public class FindXmlContentInCompressedFile {
/**
* @param args
*/
public static void main(String args[]) {
//relativePath is the file that you are searching inside of the Jar/zip file.
String relativeFilePath = "zipThiagoFolder/xmlFileNameInJarFile.xml";
//The zip path is where the compressed files is.
String zipFilePath = "C:\\codes\\TesterProject\\src\\thiago\\leoncio\\File.jar";
String contents = readZipFile(zipFilePath,relativeFilePath);
System.out.println(contents);
}
public static String readZipFile(String zipFilePath, String relativeFilePath) {
try {
ZipFile zipFile = new ZipFile(zipFilePath);
Enumeration<? extends ZipEntry> e = zipFile.entries();
while (e.hasMoreElements()) {
System.out.println("[LOG] While it's getting the elements on XML content");
ZipEntry entry = (ZipEntry) e.nextElement();
// if the entry is not directory and matches relative file then extract it
if (!entry.isDirectory() && entry.getName().equals(relativeFilePath)) {
System.out.println("[LOG] Relative Path:"+relativeFilePath);
BufferedInputStream bis = new BufferedInputStream(
zipFile.getInputStream(entry));
// Read the file
// With Apache Commons I/O
System.out.println("[LOG] Converting in String file using Apache lib[commons-io-2.5.jar] for fileContentsStr var");
String fileContentsStr = IOUtils.toString(bis, "UTF-8");
// With Guava
//String fileContentsStr = new String(ByteStreams.toByteArray(bis),Charsets.UTF_8);
// close the input stream.
bis.close();
return fileContentsStr;
} else {
continue;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
Expected output:
[LOG] While it's getting the elements on XML content
[LOG] While it's getting the elements on XML content
[LOG] Relative Path:zipThiagoFolder/xmlFileNameInJarFile.xml
[LOG] Converting in String file using Apache lib[commons-io-2.5.jar] for fileContentsStr var
<References xmlns="http://xmlns.oracle.com/adf/jndi">
<Reference name="thiago" className="oracle.jdeveloper.db.adapter.DatabaseProvider" credentialStoreKey="thiago"
xmlns="">
<Factory className="oracle.jdevimpl.db.adapter.DatabaseProviderFactory1212"/>
<RefAddresses>
<StringRefAddr addrType="oraDriverType">
<Contents>thin</Contents>
</StringRefAddr>
<StringRefAddr addrType="hostname">
<Contents>clusterd-RacThiagoleoncio</Contents>
</StringRefAddr>
<SecureRefAddr addrType="password"/>
<StringRefAddr addrType="subtype">
<Contents>oraJDBC</Contents>
</StringRefAddr>
<StringRefAddr addrType="port">
<Contents>3099</Contents>
</StringRefAddr>
<StringRefAddr addrType="serviceName">
<Contents>SIDTESTThiagoLeoncioEnvironment</Contents>
</StringRefAddr>
<StringRefAddr addrType="user">
<Contents>ThiagoLeoncioUser</Contents>
</StringRefAddr>
</RefAddresses>
</Reference>
</References>
Happy coding,
Thiago Leoncio.
No comments:
Post a Comment