I've been troubled for the past 3 days now by an annoying problem, I do not have the faintest clue on how i could possibly transfer files from a zip to a jar. I'm not sure whether or not i need to supply a buffer to said process, and whether i use the JarInput/OutputStream or i instead use Enumerations, I've done some research on how to do it and i cannot find anything apart from a big paste of code, as you can tell i would much prefer an explanation of what i need to do as a pose to how to do it.
Basically i would just like a nice simple, yes informative tutorial on how i could transfer the contents of lets so "foo.zip" into "bar.jar". ("bar.jar" already contains files which i do not want to delete)
To transfer all files from zip to jar you just have to rename zip to jar because jar is a zip format.
EDIT:
If you want to copy some entries from zip/jar to other zip/jar or to copy entries to existing zip/jar you have to iterate over entries in input file using ZipInputStream, read the entries and write them to output ZipOutputStream. Take a look on this tutorial: http://java.sun.com/developer/technicalArticles/Programming/compression/. It explains everything in details and contains code samples.
You can actually just rename the file from foo.zip to foo.jar. I'm not kidding!
You can read and write zip and jar files with java.util.zip, jar files are really just zip files.
Related
I wrote a little Java app for analyzing .csv files. Now I want to keep reading from and writing to a .txt file, which acts similar to a mini-database. For this purpose I simply added the .txt in my project and used the Files.readString(Path) and Files.write(Path path, byte[] bytes) methods.
When I do this in IntelliJ I have no problems but as soon as I build/export the file with Maven and started with the created launcher the app didn't work because the project structure / file organization isn't the same anymore.
I also tried to just add the .txt file to the exported folder afterwards but even then I couldn't manage to implement a relative path to the file.
I'm still relatively new to programming and it's just a small app so I don't think mySQL would fit my needs. I've also read that I could store the data in a property file but I don't know if that would be the right way to archive what I want. Certainly it must be possible to somehow keep a .txt for reading and writing in your exported project. Does someone have an idea?
If you use a ยด*.txt` file for storing, that file cannot be inside the jar because the jar cannot change its contents while running.
You need to put the file somewhere else, either at some dedicated location (let the user choose/configure one), or next to the jar. To figure out your own execution path, you can use
How to get the path of a running JAR file?
Maven is one tricky tool. You need to go to the pom file and add the resource.
Unable to add resources to final jar for maven project in Intellij.
-I hope this helps
Trader
I have three java jar files which can be run independently. Each file contains images resource. I want each of the programs to be able to read resource from each other program. Let's assume they are in the same directory, when the program "A" is run I want to be able to read resources file inside file "B" or "C".
There may be a more elegant way of doing this with classloaders/etc.
However, JAR files are simply ZIP files with a different name. Assuming you know in advance the location of the folder in the JAR (probably src/main/resources), you can simply unzip and read from there.
The class ZipEntry will allow you to do this, without needing any extra libraries: https://docs.oracle.com/javase/8/docs/api/java/util/zip/ZipEntry.html
Like I say, however, there is likely a better way of doing this.
How to Zip multiple files from two different folders and store it in any of these two folder using java
This person had a similar question, check it out: Zipping a folder which contains subfolders
The answer to their question (Posted by dogbane) could answer yours, it is as follows:
You need to check if the file is a directory because you can't pass
directories to the zip method.
Take a look at this page which shows how you can recursively zip
a given directory.
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.