有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

避免“ZLIB输入流意外结束”的java计时要求

我有一个日志分析工具,需要抓取*。从Linux服务器下载gz文件,并在Linux和Windows客户端上解压它们。在许多情况下,我都会遇到“ZLIB输入流的意外结束”,我认为Linux和Windows上的文件在细节上有所不同

下面是我的功能。这很基本。如何改进它以防止EOF错误

中的“”符号是一个FileInputStream,它是在构造该函数所属的类时创建的

public void unzip(File fileTo) throws IOException {
    OutputStream out = new FileOutputStream(fileTo);
    LOGGER.info("Setting up the file for outputstream : "+fileTo);
    try {
      in = new GZIPInputStream(in);
      byte[] buffer = new byte[65536];
      int noRead;
      while ((noRead = in.read(buffer)) != -1) {
          out.write(buffer, 0, noRead);
      }
    } finally {
        try { out.close(); } catch (Exception e) {}
    }
}

我从上面改成了这个,现在它可以工作了。似乎在加载输入流之前,它试图加载输出流

        public void unzip(File fileTo, String f) throws IOException,           
            EOFException, InterruptedException {
        LOGGER.info("Setting up the file for outputstream : "+fileTo);
        GZIPInputStream cIn = new GZIPInputStream(new FileInputStream(f));
        OutputStream out = new FileOutputStream(fileTo);
        fileTo.setReadable(true, false);
        fileTo.setWritable(true, false);
        byte[] buffer = new byte[65536];
        int noRead;
        for (int i = 10; i > 0 && cIn.available() == 1; i--) {
            Thread.sleep(1000);
        }
        try {
            while ((noRead = cIn.read(buffer)) != -1) {
                out.write(buffer, 0, noRead);
            }
        } finally {
            try { out.close();cIn.close();in.close(); } catch (Exception e) {}
        }
    }

共 (0) 个答案