我正在尝试从 ZipInputStream 中提取 xml 文件和其他内容,并从解析 zipinputstream 的 xml 创建我的对象。但是,当我没有 while 循环读取 inputStream 时,我得到了文件提前结束异常 - 对于以下代码或 Stream Closed。据我了解, ZipInputStream.getNextEntry 获取下一个条目输入流。
另外 - 当我通过创建一个实际的临时文件并传递输入流(如注释代码中)来运行它时 - 它处理得很好 - 但在我的情况下,我将无法写入磁盘 - 所以这一切都必须发生在-记忆。有人能告诉我我的代码哪里出错了,我能做些什么来修复它?
ZipEntry entry;
Map<String, byte[]> otherElements = new HashMap<String, byte[]>();
entry =((ZipInputStream)inputStream).getNextEntry();
while (entry != null) {
logger.debug("entry: " + entry.getName() + ", " + entry.getSize());
System.out.println(entry.getName() + " - " + entry.getSize());
if (entry.getName().equalsIgnoreCase("Document.xml")) {
/*File file = new File("C:\\tmp.xml");
FileOutputStream fos = new FileOutputStream();
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
fos.write(bytes, 0, read);
}
InputStream fis = new FileInputStream();*/
while(inputStream.available()>0){
inputStream.read();
}
myOutput = buildMyOutput((ZipInputStream)inputStream);
//fos.close();
//fis.close();
// method that takes the input and creates the java object
private MyObject buildMyOutput(InputStream xmlStream) throws Exception {
// build my objects
XStream xstream = ConvertUtil.getXStream();
xstream.processAnnotations(MyObject.class);
MyObject myOutput = (MyObject) xstream.fromXML(xmlStream);
return myOutput;
}