I asked a question earlier about unzip/zip using windows cmd command in Java. That turn out to be not good for a program and one would suggest using the java.util.zip to unzip/zip. After looking through some tutorial, I found out usually they just loop through the content of the zip file. If I know the name of the file I want, can I extract it explicitly without looping through the whole zip file?? Here in my case, my zip file have 2300+ entries, and I would only want one of them. Do you guys happen to know some tricks? Thank you very much!
You will find hot to decompress files at: http://java.sun.com/developer/technicalArticles/Programming/compression/
Instead of looping and extracting all files just locate file you are interested in with ZipFile.getEntry() method.
You can get a ZipEntry -object for the specific file from ZipFile-object via the getEntry-method. To access the file contents, use ZipFile's getInputStream with the ZipEntry-object.
If this is for yourself and not your program, have you considered using the jar command in the JDK for this?
Related
Summary:
I have a program I want to ship as a single jar file.
It depends on three big resource files (700MB each) in a binary format. The file content can easily be accessed via indexing, my parser therefore reads these files as RandomAccessFile-objects.
So my goal is to access resource files from a jar via File objects.
My problem:
When accessing the resource files from my file system, there is no issue, but I aim to pack them into the jar file of the program, so the user does not need to handle these files themselves.
The only way I found so far to access a file packed in a jar is via InputStream (generated by class.getResourceAsStream()), which is totally useless for my application as it would be much too slow reading these files from start to end instead of using RandomAccessFile.
Copying the file content into a file, reading it and deleting it in runtime is no option eigher for the same reason.
Can someone confirm that there is no way to achieve my goal or provide a solution (or a hint so I can work it out myself)?
What I found so far:
I found this answer and if I understand the answer it says that there is no way to solve my problem:
Resources in a .jar file are not files in the sense that the OS can access them directly via normal file access APIs.
And since java.io.File represents exactly that kind of file (i.e. a thing that looks like a file to the OS), it can't be used to refer to anything in a .jar file.
A possible workaround is to extract the resource to a temporary file and refer to that with a File.
I think I can follow the reasoning behind it, but it is over eight years old now and while I am not very educated when it comes to file systems and archives, I know that the Java language has evolved quite much since then, so maybe there is hope? :)
Probably useless background information:
The files are genomes in the 2bit format and I use the TwoBitParser from biojava via the wrapper class TwoBitFacade?. The Javadocs can be found here and here.
Resources are not files, and they live in a JAR file, which is not a random access medium.
Sup!
I'm trying to understand the concepts and differences between them to load and save files, and trying to figure out the best way to load files with a program I'm making (a database). I'm using Java, but try to help me in a more general way, if possible. Whatever help I get is a lot welcome and I thank you already for it.
I know there is "File Reader" in Java and a lot of programs use files in .txt format. I tried to open a .doc file with Notepad, just to see what would happen, and NOTHING was a text. I expected that, not everything though.
I just don't know how to make a file that is not a .txt. How that happens?
This is a big one. When I load a file, should I load all its content and let it ready for use, or should I just keep the file path and read from it as I use, using offsets and pointers starting from the file path? How should I do this either way?
In this database program of mine, some things will be images (for example), and if I were to use a .txt, I would give each image a label that the program would read and then interpret it like the label of the image, and then get the image. How could I do that?
I was sure I had more questions. If I do I ask again.
Once more, thanks in advance for the attention and help.
When it comes to the .txt bit, all "plain text" will work. Just change the file extension to .ini or .sql (if sql is installed) and change it in your filepath too.
I'm in doubt about something. I'm developing a Java Desktop application and I have a problem.
I need to get the current local folder that my application (jar file) is in user system. And after this, to search inside of this same folder for some files (like all .txt file, for example). And finally, get the name of only one of this files and converts to string.
Someone can help me?
Antecipate thanks.
May be the getCanonicalPath() and getProperty() functions could help you, since you don't show any code I can't make you any example, but may be looking this could help you a little.
And also there is a similar question here
I have a jar file which have 15 classes. I have to call some methods of this jar from web application. Some of the methods read .plist file.
In the jar code I have coded in such a way that all the plist files are readed once and uses multiple times(I have used some static map which hold all the properties of .plist files), but this is not happening.
Every time there is a call it is reading all .plist files again. And because of this there is "Too Many Open File Exception."
Is there any way to stop the file reading again and again. Please help me.
Can you post your code or stack trace. it looks like a logical problem . If you want to read file once just have a static flag and after reading it set it true and before reading it just check....If this is not the desired answer just let me know .
I am a little new to programming so please bear with me. I create a lot of self-extractors that I upload to a ftp site which my clients then download (with the self-extractor putting the files I zipped up to the right spot). I am trying to automate this process on my end since it is rather repetitive.
I think I can figure out how to create the zip and how to upload the file once created but I cannot figure out how to create use winzip se to create a self-extractor from the created zip file. WinZip has a command line interface but WinZIp SE apparently does not.
Any help would be appreciated...I am planning on writing this code in java btw.
Thanks.
According to What are the differences between the full WinZip Self-Extractor and the Personal Edition version?
Self-extracting files can be created from the command line.
That being said, I'm not sure I would use Java to automate the tasks you are describing, it's not the best tool for that IMHO. I'd rather use shell scripting.
Update: (answering a comment from the OP) I'm really not a Windows specialist but I think that Windows PowerShell might be appropriate for the job (and it can be used to query a MS Access database).
If you need to compress and decompress zip archives, just use any other command line utility, which you will call from your Java app. For example zip, and unzip: http://www.info-zip.org/
7-zip is free and can do this on the command line, meaning you can make a batch script. 7za a -sfx MySelfExtractingzip.exe MyFolderContainingFiles/