简体   繁体   English

以zip格式查看图像而不解压缩

[英]Viewing images in zip without unzipping

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. 我正在开发一个Java程序,该程序将允许我查看zip / rar文件中的图像,而无需将其解压缩到硬盘上的文件夹中。 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? 我相信ZipFile是我应该做更多研究的地方吗? 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. 我正在寻找是否走在正确的道路上,或者是否有任何良好的资源/特定的api我可以寻求帮助,因为我几个月来都没有做任何编码(保存浅色的php和html)或Java大约一年了,而我在后燃器上已经使用了足够长的时间。

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. 您使用ZipFile正确的轨道上,我认为您无需在查看图像之前提取到磁盘。

A ZipFile object will give you a list of its contents with entries() . 一个ZipFile对象将为您提供一个包含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. 您可以迭代ZipEntry对象的集合以显示要查看的文件的选择,当然,如果需要,也可以将其过滤为已知的扩展名。

Strangely enough it's the ZipFile object and not the the individual ZipEntry objects that will give you an InputStream for the given entry. 奇怪的是,它将为您提供给定条目的InputStreamZipFile对象,而不是单个ZipEntry对象。 You can read this object into a byte[] in memory and send it to the component that will be responsible for displaying the image. 您可以将该对象读取到内存中的byte[]中,并将其发送到负责显示图像的组件。

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. 需要注意的是,对于zip文件,要获取存储在zip中的最后一个文件,基本上必须解压缩整个存档,这可能很耗时。 So it may make sense to cache files on disk or an in-memory LRU cache depending on the usage pattern. 因此,根据使用模式,将文件缓存在磁盘或内存LRU缓存中可能很有意义。

You didn't mention if this is for a Swing application, but if it is this might be helpful for displaying the images: 您没有提到这是否适用于Swing应用程序,但如果这样,则对于显示图像可能会有所帮助:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM