I'm trying to extract the content of a zip file contained into my jarfile (in fact it contains a few files that can be edited and need to be extracted at the first use, even if there is no internet connection). I can extract a single file from my jar if I know its name using MyMainClass.getResource("myfile.zip") but I would like to extract its content without having to extract myfile.zip and delete it after. Is it possible with java 8?
Related
I developed a small program that takes as input some configuration parameters from a .cfg file and produces an output .txt file based on the values taken from the .cfg file.
While the program runs perfectly in eclipse, I receive a NullPointerException error when I create a JAR file of this program and try to run it. From my understanding I have to make the JAR access its internal files or try to receive the needed information (in this case the .cfg file) externally, e.g. create a resource folder next to the JAR file.
I have searched many related questions asked here but I got even more confused whether there is an optimal way to produce a JAR file that can access input files and produces output files. Should I modify my code to achieve this or there is another way?
For the record, I use FileReader and FileWriter to access and produce the files.
If your .cfg file is outside of the JAR file, it should work just as in Eclipse.
If you want to access it from inside of the JAR archive, then you should use a class loader to load it, instead of FileReader...
In java most of the zipping apis zips the single file by creating a folder.
For example in windows if we choose to right click-->Winzip-->Add to selected_file_name.zip it creates selected_file_name.zip and if we extract we directly get file rather than a folder and within folder a file.
After extracting by selecting a extract to here only file should come rather than a folder and then a file within that.
Can we do the same using any java api ?
I am trying to compress a folder to zip and extract the same folder using java. When i zipped or unzipped the folder the files which are in the folder compressed and extracted successfully. But a Thumbs.db file is creating with in the folder.
I want to delete/avoid the Thumbs.db files before/after compressing to zip or extracting from zip programmatically.
Is it possible in java ?
Thumbs.db is usually a file with the system and hidden attributes (see attrib.exe ) and therefore invisible in the Explorer (unless you set the Explorer to show all files including system files).
When you extract the file you should set it's attributes accordingly.
How to set a file attribute using Java is already described here: https://stackoverflow.com/a/36465283/150978
I am creating a zip file using ZipOutputStream. There will also be a manifest file (a csv file) which will have links to the entries in the Zip file. How do I programmatically create links for the zip entries ?
If you keep track of all the entries while you write them, you should be able to add another entry containing the "links" (but how should a csv link to a file? Please specify what you try to achieve).
If you intend to use the file under windows, you could create .lnk files programmatically; but this only works for one file per link. On unices, ZipOutputStream cannot create symlinks, but ZipFileSystem can.
I'm trying to load words into an arrayList from a txt file in the assets folder in my android program, and looking at the api, it says that AssetManager.list(String path) will "Return a String array of all the assets at the given path."
Does that mean that if I have a text file, words.txt, and I put in "words.txt" as the parameter for AssetManager.list(String), that it will return a String array of all the words in the txt file?
If not, how would I go about reading a txt file into an array in my android program?
I couldn't find a definition for "asset"
I'm using eclipse.
EDIT:
I'm not just asking what an asset is, I'm also asking about a specific method in the api and how to load strings into an arrayList
Android offers one more directory where you can keep files which also will be included in package. This directory called /assets. The difference between /res and /assets is that Android doesn’t generate IDs for assets content. You need to specify relative path and name for files inside /assets.
assets/
This is empty. You can use it to store raw asset files. Files that you save here are compiled into an .apk file as-is, and the original filename is preserved. You can navigate this directory in the same way as a typical file system using URIs and read files as a stream of bytes using the AssetManager. For example, this is a good location for textures and game data.
Source
Asset Usage Example
assets is place where you can normal files which you want to read in application as some point whenever needed. You save your files/directory's. And read in your application from input streams but you cannot write.
As far res concerned, they resource files which you can refer directly in your code. like strings, array of strings,images (drawables),style(android specific like css in html) and there is raw folder which stores all files binary and generates id for it.