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?
Related
I'm making a file import system, and I can't move files into the compiled .jar file the application is in.
Here's what I'm trying to do:
Path FROM = Paths.get(filePath.getText());
Path TO = Paths.get("C:\\Users\\" + System.getProperty("user.name") +
"\\AppData\\Roaming\\.minecraft\\mods\\music_crafter-1.0\\src\\main\\resources\\assets\\music_crafter\\sounds\\block\\music_player");
//jar file
Files.move(FROM, TO.resolve(FROM.getFileName()), StandardCopyOption.REPLACE_EXISTING);
You need to handle the jar file internally. A Jar is not a directory, it is a compressed container file (pretty much a ZIP file with a different extension).
To do this, given that you are on Java 6, you have 2 options:
Unzip the contents to a temporary working directory (there are built
in APIs for this, or use a library such as Apache Commons Compress)
do your work (copying, deleting, etc) and then re-zip.
Make external command line calls to the Jar utilities that come with
Java
Of those, only (1) makes any real sense.
A third option would be available if you could up your Java to 7+ which would be:
3. Use a Zip File System Provider to to treat it as a file system in code
All that said, however:
As per comments on your question, you really might want to look at if this something you need to do at all? Why do you need to insert into existing jars? If this is 'external' data, it would be much better in a separate resource location/container, not the application jar.
I have a text file ,i want to perform Automatic Term Recognition by using jajatr library. How can i set path of my file in this library. This library will process the text file and generate output file which will contain results.
I have downloaded this library from url provided below
< https://code.google.com/p/jajatr/downloads/list >
I have studied and found this thing helpful
After unzip the download folder . Traverse to the folder provided below
jajatr\jajatr\src\jatr\src
A Property File will be found named as jatr
Now set the text file path in the file named as jatr.
One of the statement in this file is :
jatr.system.refcorpus=/mnt/minerva1/nlp/corpora/monolingual/english/gigaword/gw.lemmas.unigram_counts
I think so that i have to set corpus path in this jatr.properties file
But I don't know how to set path of my corpus.
After running TestCValue.java class
Output
Usage: java TestTfIdf [path_to_corpus]
You do not need to change jatr.properties file inside the src directory of the jar file. You can rather supply the property you are interested to customize when you run the jar through the command line, something like:
java -jar jajatr.jar -Djatr.system.refcorpus=%YOUR-PATH%
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.
I have a Java code that transfers file from FTP on Mainframe to my local system . The problem is how do I tell if the given path is a file or folder ? I cannot use the check for extensions as i don't know what the possible extensions can be . Also the following code always returns false ,
fileSystem.isFile("fileName");
Being a mainframe filesystem , the path is seperated by . instead of / and hence a check for . at the end also doesnt work.
Again I stream data from the input path to output location using
url="connection url of the mainframe"
bufferedInputStream = new BufferedInputStream(url.getInputStream());
When I have files at the source path , it writes the content of the file to the destination , and when the source path has a directory it writes the the names and other properties of the files at the directory to the destination.
Sample output when the source is a directory is
Name VV.MM Created Changed Size Init Mod Id
QQQQ 01.00 2009/12/18 2009/12/18 12:15 18 18 0 XXXX
RRRR 01.00 2009/12/18 2009/12/18 12:16 19 19 0 XXXXX
How do I determine if the source path is a file or folder ?
I do not have an answer but it sounds like you are acessing Zos. For those who do not
know anything anything about Zos
Zos Does not have directories
What he is calling a directory is probably a PDS (see PDS in Dataset). For those not from a mainframe background, think of it as a type of archive (i.e. jar, Tar, zip etc). This is not a completely accurate description, PDS have some limitations & uses jar files do not, but a jar file is the closest analogy in the PC, *NIX world I can think of.
How do you detect them - not sure, there may be an file attribute you can access
I found a way to determine if the path on mainframe is a file or PDS (analogically a directory ) . I used org.apache.hadoop.fs.ftp.FTPFileSystem library and the following snippet would work by returning the list of files .
FTPFile[] files = ftp.listFiles();
Looping through the files and checking file1.isFile() would suffice.
Again remember to change to the current working directory before listing the files using ftp.cwd(inputPath);
For an explanation on Mainframe file system please refer to Bruce Martin's answer and the comments that follow. .
You don't say what version of java you are using or if you're using a special library for the host filesystem.
Can you use java.io.File#isDirectory() instead?
I know, I know, this has been asked before. But every resource I've looked at uses IconImages while I just have plain Images.
Is there a different solution? Please help as I've been stuck researching and trying to figure this out for days now with no progress.
Image Floor = Toolkit.getDefaultToolkit().getImage("Floor.PNG");
EDIT: If I was to make sure the jar wouldn't compress and I created a seperate directory in the jar for images and put the correct file path, would this code work?
Toolkit#getImage(String s) looks for a file and likely your image is in the Jar and is now a resource not a file. Look to using resources for this.
Note that ImageIO.read(...) can accept an InputStream parameter the Class class has a method, getResourceAsStream(...) which can put the resource into a Stream that ImageIO can read. Give that a try.
Also, are you getting any error messages when you try what you're doing?
Make sure you know what your current directory is, and how it relates to the position of the files in your jar.
Here's how I would handle it.
1) Require there to be a file called "images.txt" in the directory with your jar (or bundle it into the jar.)
2) Make a file called "images.txt" with a format like `FLOOR:C:\\images\\floor.png`
3) Load this file into memory on load.
4) Load your images based on the entries in the file
This will give you the advantage of changing your images without changing your code if it's defined outside the jar :)
It's not loading because you're not putting the path to the images in the declaration. It expects the images to be wherever the jar is (notice there's no directories there)
You need to offload the definition of the file names to a file, or at the very least guarantee the relative position of the files.
Another good option is to put the images in the jar itself, say in an img directory, and reference them there. But then changes to the images require a new jar, which may not be desired for development purposes.
The getImage call is looking in the file system working directory, not inside the Jar file. This is why the jar file loads the images successfully when they are placed in the same directory outside the jar file. If the images are bundled in the jar file, they are no longer file system files to be accessed, but rather Jar file resources. There is a different way to load these, but sorry, I don't know it off the top of my head.
Check the extension of files. I had this problem because the extension was "PNG", when I changed it to "png", everything was ok.
You can't expect a JAR file to magically know where your images are. If you put a JAR file alone on the desktop, it's going to look for the files on the desktop! The code
getImage("Floor.png")
searches the current directory of the JAR (or source project) by default and you'd expect that if the JAR was in the same directory, it would work. If the JAR is on the desktop how does it know where Floor.png is? Of course, you can specify a hard-coded path
getImage("C:\Some Folder Path\Floor.png")
but then Floor.png has to be in C:\Some Folder Path\ for the JAR to work properly.
It sounds like what you really want to do is keep the images in the JAR file (which acts like a ZIP file). The tutorial on doing that is here:
http://download.oracle.com/javase/tutorial/uiswing/components/icon.html#getresource
And I know for ImageIcon you use: new javax.swing.ImageIcon(getClass().getResource("myimage.jpeg") but I have not found anything similar for plain Image.
<head-desk /> You should really get into reading the JavaDocs. Otherwise you are 'coding by magic'. Which generally won't work.
URL urlToImage = getClass().getResource("myimage.jpeg");
// If you need to support Java 1.3
Image image = Toolkit.getDefaultToolKit().getImage(urlToImage);
// If your users have dragged their JRE into this millennium
BufferedImage bufferedImage = ImageIO.read(urlToImage);