java.io.IOException:流关闭ZipInputStream

时间:2018-12-09 09:08:55

标签: java zipfile zipinputstream

下面是代码段。

    FileInputStream fin = new FileInputStream(zipFile);
    ZipInputStream zin = new ZipInputStream(fin);
    ZipEntry entry = null;
    String routerListUCM = "";

    try {
        entries:

        while ((entry = zin.getNextEntry()) != null) {
            if (entry.getName().startsWith("routes")) {

                BufferedReader in = new BufferedReader(new InputStreamReader(zin, "UTF-8"));

                if (true) {
                    //parse the xml of the route...

                    DOMParser dp = new DOMParser();

                    dp.parse(in);
                    Element e = (Element) dp.getDocument().getFirstChild();

                    String transferid = e.getElementsByTagName("transferId").item(0).getTextContent();
                    System.out.println("transferId=" + transferid);
                    int fileid = Integer.parseInt(transferid.split("-")[1]);
                    System.out.println("fileid=" + transferid);
                    String userList = e.getElementsByTagName("userList").item(0).getTextContent();
                    System.out.println("userList=" + userList);
                    String routeList = e.getElementsByTagName("routeList").item(0).getTextContent();
                    System.out.println("routeList=" + routeList);
                    routerListUCM = routeList;

                    if (routeList.toLowerCase().indexOf(myname().toLowerCase()) == -1) {
                        //my server is not in the current route...
                        //so skip this route table.
                        continue entries;

                    }
                }
            }
        }
    } catch (Exception e) {
        System.err.println(e.getMessage());
        e.printStackTrace();

    }

在某些情况下,“继续输入”之后;并尝试下一个循环,我看到“ stream close Exception”:/

error:Stream closed

堆栈跟踪:

java.io.IOException: Stream closed
at java.util.zip.ZipInputStream.ensureOpen(ZipInputStream.java:67)
at java.util.zip.ZipInputStream.getNextEntry(ZipInputStream.java:116)
at org.parsisys.test.mina.view.SimpleFtplet$beaVersion0_1155.isTransferFinished(SimpleFtplet.java:299)
at org.parsisys.test.mina.view.SimpleFtplet.isTransferFinished(SimpleFtplet.java)
at org.parsisys.test.mina.view.SimpleFtplet.beaAccessisTransferFinished(SimpleFtplet.java)
at org.parsisys.test.mina.view.SimpleFtplet$beaVersion0_1155.onUploadEnd(SimpleFtplet.java:208)
at org.parsisys.test.mina.view.SimpleFtplet.onUploadEnd(SimpleFtplet.java)
at org.apache.ftpserver.ftplet.DefaultFtplet.afterCommand(DefaultFtplet.java:89)
at org.parsisys.test.mina.view.SimpleFtplet.afterCommand(SimpleFtplet.java)
at org.apache.ftpserver.ftpletcontainer.impl.DefaultFtpletContainer.afterCommand(DefaultFtpletContainer.java:144)
at org.apache.ftpserver.impl.DefaultFtpHandler.messageReceived(DefaultFtpHandler.java:220)
at org.apache.ftpserver.listener.nio.FtpHandlerAdapter.messageReceived(FtpHandlerAdapter.java:61)
at org.apache.mina.core.filterchain.DefaultIoFilterChain$TailFilter.messageReceived(DefaultIoFilterChain.java:716)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.callNextMessageReceived(DefaultIoFilterChain.java:434)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.access$1200(DefaultIoFilterChain.java:46)
at org.apache.mina.core.filterchain.DefaultIoFilterChain$EntryImpl$1.messageReceived(DefaultIoFilterChain.java:796)
at org.apache.ftpserver.listener.nio.FtpLoggingFilter.messageReceived(FtpLoggingFilter.java:85)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.callNextMessageReceived(DefaultIoFilterChain.java:434)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.access$1200(DefaultIoFilterChain.java:46)
at org.apache.mina.core.filterchain.DefaultIoFilterChain$EntryImpl$1.messageReceived(DefaultIoFilterChain.java:796)
at org.apache.mina.core.filterchain.IoFilterEvent.fire(IoFilterEvent.java:75)
at org.apache.mina.filter.logging.MdcInjectionFilter.filter(MdcInjectionFilter.java:136)
at org.apache.mina.filter.util.CommonEventFilter.messageReceived(CommonEventFilter.java:70)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.callNextMessageReceived(DefaultIoFilterChain.java:434)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.access$1200(DefaultIoFilterChain.java:46)
at org.apache.mina.core.filterchain.DefaultIoFilterChain$EntryImpl$1.messageReceived(DefaultIoFilterChain.java:796)
at org.apache.mina.filter.codec.ProtocolCodecFilter$ProtocolDecoderOutputImpl.flush(ProtocolCodecFilter.java:427)
at org.apache.mina.filter.codec.ProtocolCodecFilter.messageReceived(ProtocolCodecFilter.java:245)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.callNextMessageReceived(DefaultIoFilterChain.java:434)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.access$1200(DefaultIoFilterChain.java:46)
at org.apache.mina.core.filterchain.DefaultIoFilterChain$EntryImpl$1.messageReceived(DefaultIoFilterChain.java:796)
at org.apache.mina.core.filterchain.IoFilterEvent.fire(IoFilterEvent.java:75)
at org.apache.mina.core.session.IoEvent.run(IoEvent.java:63)
at org.apache.mina.filter.executor.OrderedThreadPoolExecutor$Worker.runTask(OrderedThreadPoolExecutor.java:780)
at org.apache.mina.filter.executor.OrderedThreadPoolExecutor$Worker.runTasks(OrderedThreadPoolExecutor.java:772)
at org.apache.mina.filter.executor.OrderedThreadPoolExecutor$Worker.run(OrderedThreadPoolExecutor.java:714)
at java.lang.Thread.run(Thread.java:748)

请帮助我........................................... ................................................... ................................................... ................................................... ...................................................

2 个答案:

答案 0 :(得分:0)

似乎最外面的BufferedReader对象关闭了嵌套的流(尤其是ZipInputStream)。尝试将BufferedReader初始化代码移到更高的循环逻辑上。

此外,该主题可能会有所帮助:closing nested streams

更新: 好的,现在一切都清楚了。 DOMParser类的实现代码清楚地表明,parse方法关闭了基础InputStream的源代码(仅是finally块的摘录):

} finally {
    this.parser.reader.close();
}

在这种情况下可以采取的措施是入侵BufferedReader对象,该对象将传递给DOMParser对象。这是一个示例:

public class HackedReader extends BufferedReader {

    public HackedReader(InputStreamReader inputStreamReader) {
        super(inputStreamReader);
    }

    @Override
    public void close() {
        // Close method doesn't do anything, that's the main sense of overriding.
    }

    // But you know exact method which will close your underlying stream.
    public void hackedClose() throws IOException {
        super.close();
    }
}

答案 1 :(得分:0)

我发现使用org.apache.poi.util.CloseIgnoringInputStream对我有用。我能够将要传递的ZipInputStream包装到另一个方法中。

例如:

ExcelUtility.getLineCount(new CloseIgnoringInputStream(zipStream)