質問

I'm working on a Java program that will allow me to view images in a zip/rar file without unzipping it to a folder on my hdd. I'd like to be able to flip through them like on a normal image viewer, possibly able to zoom in/out if needed.

From what I've looked into, it'll have to be extracted even if it's just to a temporary folder, which I'm fine with as long as the program can delete it on its own after. I believe ZipFile would be something I should do more research in? Most of what I've seen deal with text documents rather than image files, so I'm not sure how to proceed in my research.

I'm looking to see if I'm on the right track or if there are any good resources/specific apis I could look into to help as I haven't done any coding in months (save for light php and html) or any java in about a year, and I've had this on the back-burner for long enough.

Thanks in advance. :)

役に立ちましたか?

解決

You're on the right track with ZipFile, and I don't think you need to extract to disk before viewing the images.

A ZipFile object will give you a list of its contents with entries(). You can iterate this collection of ZipEntry objects to present a choice of which file to view, and of course filter it to known extensions if you desire.

Strangely enough it's the ZipFile object and not the the individual ZipEntry objects that will give you an InputStream for the given entry. You can read this object into a byte[] in memory and send it to the component that will be responsible for displaying the image.

One caveat is that with zip files, in order to get to the last file stored in the zip it will basically have to decompress the whole archive which can be time consuming. So it may make sense to cache files on disk or an in-memory LRU cache depending on the usage pattern.

You didn't mention if this is for a Swing application, but if it is this might be helpful for displaying the images:

http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top