Correct way to make an obb file - java

I'm trying to make an obb file and I've been unsuccessful with the JOBB tool see here: JOBB DirectoryFullException: de.waldheinz.fs.fat.DirectoryFullException: directory is full and when I follow the suggestion found in this answer: https://stackoverflow.com/a/21982186/1489990 and I try to access the contents with ZipFile as
expansionFile = APKExpansionSupport.getAPKExpansionZipFile(context, 46, 1);
InputStream fileStream = expansionFile.getInputStream("/storage/emulated/0/Android/obb/com.nick.app/main.46.com.nick.app.obb/img1large.jpg");
This does not work and I get a log message that says V/zipro: Not a Zip archive.When I made this OBB file I just selected all of my images and used WinRar to add them to a "ZIP" archive and renamed it to ".OBB". What I'm wondering is to use the ZipResourceFile tool are you supposed to create a zip archive of your content, and then add that archive to another archive and rename it to ".OBB"? What is the correct way to accomplish this?

Easy Method : get it to ES file explorer(app in android) you will see it as .zip then rename it as .obb and also this renaming method is not possible in windows

Related

How to read rar file comment in java?

I have a directory containing both Zip & Rar archives.
I already have a way to get a zip file's comment -
if (f.getName().substring(f.getName().length() - 3).equals("zip")) {
ZipFile zip = new ZipFile(f);
zip.getComment();
}
Is there a way to do the same thing on a Rar file?
note that:
There are too many rar files for me to manually convert them to zip on some site (If there is some script to convert them, it could work).
Renaming a rar file's extension to .zip (file.rar -> file.zip) would still produce an exception when trying to create a new ZipFile object with it.
Thanks in advance!
I think in the end, you are looking for some sort of library to get that done for you. Like raroscope or java-unrar.
Alternatively, you could decide to re-invent that wheel yourself (not recommended).
Or you simply run the command line rar tool using ProcessBuilder (as a system command), like explained here.

How do you load an Image from a ZIP file contained in a .jar file?

We are programming a game, which shall be startable from a .jar file. First we created a Project in IntelliJ and loaded the Images from a ZIP with the following code:
ZipFile zf = null;
try {
zf = new ZipFile(zipPath);
Image Image = ImageIO.read(zf.getInputStream(zf.getEntry("Block/Air.png")));
} catch (IOException ignored) {}
Now the attempt without the ZIP (just from the .jar) is:
Image image=ImageIO.read(getClass().getResourceAsStream(path+ "Block/Air.png"));
It doesn't load any texture. Do you have a better way to do this in combination?
Edit:Seems not to be the Problem.
Since jars are zip files you could place them in the jar file and placing them the classpath.
Image image=ImageIO.read(getClass().getResourceAsStream(path+ "Block/Air.png");
Path must be a relative path from a source path root. E.g. I have a file in "src/main/resource/my/cool/game/" path is "/my/cool/game".
If you want to use a zip file, it must be outside of your jar file. To load the zip, you could use a relative file path, which is the same if you start your game from Intellij and from dekstop.
To change the working directory in intellij look here.
The best way would be to place the zip file alongside the jar so you can use "." as working directory to load the zip file.
Alternatively you could use a fixed directory, but then your game needs some sort of installation so it knows where to finde the zip file.
If you use ".", the zip file needs to be in the root of the project directory.
The Class.getResource and Class.getResourceAsStream methods take a URL (not a file path!) which is relative to the root of each classpath entry. For classpath entries which are .jar files, this means the path of a file packaged within the respective .jar file.
If your entire program is in one .jar file, the classpath consists of just one item: that .jar file. Therefore, there is only one classpath root, and the String you pass to getResourceAsStream is the URL of an entry within your .jar file. Do not include the path to the .jar file in that String.
If you are not sure what you should pass, examine your .jar file's contents. Every IDE (that I know of) provides a way to do this. You can also use any unzip utility to examine a .jar file, since every .jar file is actually a .zip file. (If you only have Windows, with no zip tools installed, make a copy of the .jar file and change the copy's extension to ".zip", then open it.)
Inside the .jar file are, of course, zip entries. The full path of the entry you want to load (without the path to the .jar file) is what you must pass to getResourceAsStream. getResourceAsStream accepts a URL, and URLs always use forward slashes (/) on all platforms, so do not use any backslashes. Also, the first character of the String must be /.
It is actually possible to specify a shorter path, depending on how your images are packaged in the .jar, but that is a separate topic. See the documentation for full details.
Side note: Never, ever write an empty catch block. Ever. That caught exception is by far the easiest way for you or anyone else to know when and why your program is not working. At the very least, put exc.printStackTrace(); in your catch block. More often, the correct course of action is to abort the program with something like throw new RuntimeException(exc);. After all, your program can't continue to function properly if it can't load that image, right?
Why do you need to store your images in a zip file? If you're doing it to reduce the file size, you gain absolutely nothing from zipping it first. JAR files are zipped files anyway (if you don't believe me, rename your .jar file to .zip, and try to open it). What you're basically doing is attempting to zip an already zipped file, which doesn't really do anything.
I would recommend you unzip your images and store them somewhere like < resources >/images
If you insist on leaving them zipped, you'll need to change it to something like this. Otherwise, it's looking for the zip file in the working directory (directory from which the jar was executed)
ZipFile zf = new ZipFile(getClass().getResourcesAsStream("path/to/zip"));
Disclaimer: I am not familiar with the ZipFile class, so I do not know if that constructor exists.

RandomAccessFile to read files in a Jar file

I am having problem to use JWNL wordnet in a Jar file.
JWNL uses RandomAccessFile to read wordnet dictionary files. In order to create a Jar file, wordnet dictionary files are put in resources/wordnet folder. As resources is in my Build Path, I have no problem to run the application I created in Eclipse. However, when I use another application to run the created jar file, I get the following error:
java.io.FileNotFoundException: resources/wordnet/data.noun (No such file or directory)
from the following code:
RandomAccess _file = new RandomAccessFile(path, _permissions);
I use the following code to check the current working directory:
URL location = PrincetonRandomAccessDictionaryFile.class.getProtectionDomain().getCodeSource().getLocation();
System.out.println(location.getFile());
It seems both situation have the same location: /project/bin/
How should I fix the problem? Thank you
The key information you seem to be missing is that Jar files are compressed, and you can't "seek" because of the compression (which is I believe the DEFLATE algorithm).
However, you could extract the file(s) into temp file(s) on start and then use that. Temp files would be removed on application exit, and are the best answer I can think of.
RandomAccessFile to read files in a Jar file
There are no files in a JAR file. There are JAR entries. You can't read them with FileInputStreams, RandomAccessFiles, or FileReaders.You need to use a JarInputStream or its friends.

JAR - Listing files into a folder

I would like to get a list of file contained in a directory which is in a jar package.
I have an "images" folder, within it I have an Images class that should load all images from that directory.
In the past i used the MyClass.class.getResourceAsStream("filename"); to read files, but how do I read a directory?
This is what I tried:
System.out.println(Images.class.getResource("").getPath());
System.out.println(new File(Images.class.getResource("").getPath()).listFiles());
I tried with Images.class.getResource because I have to work with File and there isn't a constructor that accepts an InputStream.
The code produces
file:/home/k55/Java/MyApp/dist/Package.jar!/MyApp/images/
null
So it is finding the folder which I want to list files from, but it is not able to list files.
I've read on other forums that in fact you can't use this method for folders in a jar archive, so how can I accomplish this?
Update: if possible, i would like to read files without having to use the ZipInputStream
You can't do that easily.
What you need to do:
Get the path of the jar file.
Images.class.getResource("/something/that/exists").getPath()
Strip "!/something/that/exists".
Use Zip File System to browse the Jar file.
It's a little bit of hacking.

Java ZipOutputStream extra file created when builiding Zip

I'm creating EPUB files from an application, using ZipOutputStream. When I look at the resulting zip file with Winzip, it seems to be OK, except that in addition to the files and folders I expect, there is an additional folder which should not be there and does not appear in the log output by the code.
My folder structure to be zipped is the same as the IBM Developerworks "Getting Started with EPUB" tutorial. Before I started all this, I created an epub according to the tutorial instructions using WinZip and it extracts correctly, and works correctly in Adobe Digital Editions 2.0.
c:
/myepub
/epub
mimetype
/META-INF
container.xml
/OEBPS
content.html
content.opf
stylesheet.css
title.html
toc.ncx
/images
cover.png
My Java class (194 lines) creates the zip using the relative part of each name, and the result contains everything that the WinZip-created version has, i.e. it looks like the structure above below the /epub folder.
My code log:
mimetype
META-INF/container.xml
OEBPS/content.html
OEBPS/content.opf
OEBPS/images/cover.png
OEBPS/stylesheet.css
OEBPS/title.html
OEBPS/toc.ncx
But in addition there is:
/myepub
/epub
/OEBPS
images
Note that images here is not a folder, it is a zero-length file.
When I try to extract this with WinZip, it fails with:
Extracting to "C:\myepub\extract\"
Use Path: yes Overlay Files: no
Extracting OEBPS
C:\myepub\extract\OEBPS exists but is not directory
unable to process C:\myepub\extract\OEBPS\images.
I must be missing something very fundamental here. Can anyone point me in the right direction?

Categories

Resources