I have been trying to unpack a .pkg file, which contains two folders and a .json file, into a directory using Java. I have used the apache.commons.compress library in the past, but unable to use it for the .pkg file. Please let me know how i can do this.
Many thanks
Managed to unpack. The .pkg file was actually a .cpio payload file with some header and tail bytes.
Related
How to convert text file into jar file.Actually In my java project I read and write data from a text file but when i convert my project into jar executable file then reading and writing from/into text file not working what should I do to make this work please help me.
A jar file is immutable, for security reasons. You cannot save data back to a file stored in a jar file.
Data saved from a Java program must be stored outside the classpath. Where that is, is up to you.
If you program must ship with a sample/default file, to get started, you code can try reading the file from the external location. If not found there, read from the sample/default file in the jar file. After updating content in memory, save to external location, where it will be read from on next execution.
You can make use of Classloader to read the file present in jar, but ofcourse you will not be able to write to it.
To be able to read and write both file should be present in filesystem, you may want to have a look at Read and Write to Java file via Resource
I'm learning java and am currently trying to develop a simple application. My question is can you store data about settings, etc in a text file internal to a .jar? If so how would you go about accessing this within java? Sorry if this is a really stupid idea.
InputStream is = this.getClass().getResourceAsStream("/data/file.txt");
The resources you are getting need to be on the classpath
Yes you can, and it's not a stupid question we all need to start somewhere.
There are two parts to your question:
Adding a data/text file to a .jar - (using ant to jar it:) add "fileset dir=..." to the jar target, where dir is set equal to the directory that has the data/text file. Refer to How can I include data text files in a jar using Ant?
Accessing that data/text file from within the java code - you need to use a ClassLoader and getResourceAsStream. Refer to Loading files in JAR in Tomcat using getResourceAsStream
Also, please take a look at https://github.com/gitjonathan/turbo-sansa, I have a working version up on it.
Can you store data inside a .jar?
Read-only data can be stored inside a JAR file. You can read such data using getResourceAsStream(...) if the JAR is on the classpath, or by using the standard JAR file API class if it is not on tle classpath.
But storing update-able data in a JAR file is problematic:
In a lot of circumstances it is impossible; e.g. because the JAR file is read-only or was downloaded on the fly.
In all other cases it would be very awkward, because the standard JAR file API class does not support update in place. (You would need to create a new ZIP file, copy across the old content apart from the file you are updating, add that file, and then rename the resulting file.)
I need to extract all the multi-part ZIP and RAR archives in a given path? Does Java have any inbuilt methods for doing this? If not, would someone happen to know of a free library that does this?
Thanks.
Loop all files in the directory - new File(dir).listFiles(), possibly using a FilenameFilter to retain only those whose extension is .zip
You can use the java.util.zip package, or commons-compress. But my experience shows that using the ant Zip task programatically is way easier. (You'd need the ant jar on the classpath). This is for .zip files. For RAR you'd need another utility. Take a look at this question. The URL in the answer does not open, but the name of the project is valid - google it.
I was wondering if is possible to find the content in an XML file placed in a jar thath is placed in a ear too. It would help me find the properties of java beans.
Up into the ear I can iterate through documents and see what's inside, but if it is a jar I can't iterate documents inside that.
Someone can give me some advice?
From the ear file you should be able to extract the jar file. Then you can use WinZip, 7 Zip, etc to do explore the jar file contents the GUI. Or you can run the jar tf command to extract the content of the jar file in command line. If you don't have any of these tools and using windows, then you can rename the jar file to a .zip and windows should be able to explore it (most of the cases it works).
Edits - I am not sure if you wanted to do it using Java. In that case you are looking for JarFile. I found an example of it here for exploring Jar contents programatically.
so i just tested the thing you want to do - and as long as the JAR lies in the classpath of your EAR, then you can access any file within it. basically the try to look up the file from the context-root of your application.
for example if in your JAR the file abc.xml resides under the package a.b.resources, then from say a servlet in your EAR you can access it using :
InputStream is = this.getClass().getClassLoader().getResourceAsStream("a/b/resources/abc.properties");
Yes, you can read any file that is packed into zip file. It does not matter how many nested zip file you have to open on your way. Use ZipInputStream, get needed ZipEntry, read it content. If it is still zip, open it and do it again and again until you access the required resource.
I am trying to reverse engineer an existing android app and understand how a particular UI is constructed. I've found that I can rename the apk to zip and view some of the compiled source. The res/layout directory is populated with all the xml files that define the UI, but while they do have the xml extension, they are not in XML format. Is there anyway to convert these files back to text based markup?
I think you can use android-apktool. The XML files look very well.
Create a new folder and put the .apk file which you want to decode into that.
Download the latest version of from here.
Open cmd then navigate to the root directory of APKtool and type the following command:
apktool d myApp.apk
(where myApp.apk is the filename that you want to decode)
now you get a file folder in that folder and can easily read the apk's xml files.
Here is the link for more detail install instructions
For those Who are shortcut lovers:
and using ubunto,
go to cmd : ctl+alt+t
type:
sudo apt install apktool
press enter
then go to your apk(to decompile) folder. by using
cd ~/folder_name
then type:
apktool d yourAppName.apk
and boom , you are done.
First covert your .apk to .zip. Inside that you will get a compiled-bundled version of java class files called .dex. apply the below trick to convert that dex to class files
There's a way to do this. A slight atypical way is to download this opensource tool dextojar http://code.google.com/p/dex2jar/
This will convert your dex files to Jar file. Unjar these files to get java class files.
Then reverse engineer the java class files to get the java code. Don't use it to break some commercial projects.
Thanks
if you just want to know the UI construction ,you can use Hierarchy Viewer