jython打开一个zip文件并阅读其内容

时间:2012-06-19 21:10:28

标签: file-io jython

我正在尝试使用FileInputStream和ZipInputStream使用jython打开一个zip文件。但是,当调用FileInputStream时,我很奇怪地得到一个FileNotFoundException。

这是我的代码:

from java.lang import System
from java.io import ObjectInputStream, FileInputStream, BufferedInputStream
from java.util.zip import ZipInputStream, ZipEntry


file_input_stream = FileInputStream('C:\\Documents and Settings\\usr\\My Documents\\Downloads\\test.zip')
zip_input_stream = ZipInputStream(BufferedInputStream(file_input_stream))

entry = zip_input_stream.getNextEntry()
entry = zip_input_stream.getNextEntry()

object_input_stream = ObjectInputStream(zip_input_stream)
graph.model = object_input_stream.readObject()

object_input_stream.close()
zip_input_stream.close()
file_input_stream.close()

我的错误是:

file_input_stream = FileInputStream('C:\\Documents and Settings\\usr\\My Documents\\Downloads\\test.zip')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
FileNotFoundException: java.io.FileNotFoundException: C:\Documents and Settings\usr\My Documents\Downloads\test.zip (The system cannot find the file specified)

我确定该文件位于正确的目录中,如果我使用非zip文件调用FileInputStream,它可以正常工作。我在这里做错了什么?

感谢

2 个答案:

答案 0 :(得分:1)

在Windows资源管理器中打开任何文件夹。如果您使用的是Windows 7或Vista,请按alt以查看菜单栏(在XP上它应该已经可见)。选择工具 - &gt;文件夹选项。在视图选项卡中,在框中搜索标有“隐藏已知文件类型的扩展名”的复选框,然后取消选中它。

就此而言,该文件可能被称为“test.zip.zip”,这就是找不到“test.zip”的原因。

答案 1 :(得分:0)

您知道可以在Jython代码中使用Python zipfile库。