我正在尝试从 a 中读取单个文件 java.util.zip.ZipInputStream, ,并将其复制到 java.io.ByteArrayOutputStream (这样我就可以创建一个 java.io.ByteArrayInputStream 并将其交给第三方库,该库最终将关闭流,我不希望我的 ZipInputStream 即将关闭)。

我可能在这里遗漏了一些基本的东西,但我从未在这里输入 while 循环:

ByteArrayOutputStream streamBuilder = new ByteArrayOutputStream();
int bytesRead;
byte[] tempBuffer = new byte[8192*2];
try {
    while ((bytesRead = zipStream.read(tempBuffer)) != -1) {
        streamBuilder.write(tempBuffer, 0, bytesRead);
    }
} catch (IOException e) {
    // ...
}

我缺少什么可以让我复制流?

编辑:

我应该早点提到这一点 ZipInputStream 不是来自文件,所以我认为我不能使用 ZipFile. 。它来自通过 servlet 上传的文件。

另外,我已经打电话了 getNextEntry()ZipInputStream 在开始这段代码之前。如果我不尝试将文件复制到另一个文件中 InputStream (通过 OutputStream 上面提到的),然后通过 ZipInputStream 对于我的第 3 方库,该库关闭了流,我无法执行任何其他操作,例如处理流中的剩余文件。

有帮助吗?

解决方案

您的循环看起来有效 - 以下代码(仅就其本身而言)返回什么?

zipStream.read(tempBuffer)

如果它返回 -1,那么 zipStream 在您获取它之前就已关闭,并且所有的赌注都将被取消。是时候使用调试器并确保传递给您的内容实际上是有效的。

当您调用 getNextEntry() 时,它是否返回一个值,以及条目中的数据是否有意义(即getCompressedSize() 是否返回有效值)?如果您只是读取未嵌入预读 zip 条目的 Zip 文件,那么 ZipInputStream 将不适合您。

关于 Zip 格式的一些有用的花絮:

zip 文件中嵌入的每个文件都有一个标头。该标头可以包含有用的信息(例如流的压缩长度、文件中的偏移量、CRC) - 或者它可以包含一些神奇的值,这些值基本上表示“该信息不在流标头中,您必须检查” Zip 后缓步'。

每个 zip 文件都有一个附加在文件末尾的表,其中包含所有 zip 条目以及真实数据。最后的表格是强制性的,其中的值必须正确。相反,不必提供嵌入在流中的值。

如果您使用 ZipFile,它会读取 zip 末尾的表。如果您使用 ZipInputStream,我怀疑 getNextEntry() 尝试使用嵌入在流中的条目。如果未指定这些值,则 ZipInputStream 不知道流可能有多长。inflate 算法是自行终止的(实际上您不需要知道输出流的未压缩长度即可完全恢复输出),但该阅读器的 Java 版本可能不能很好地处理这种情况。

我想说,让 servlet 返回 ZipInputStream 是相当不寻常的(如果您要接收压缩内容,则接收 infatorInputStream 更为常见。

其他提示

您可能尝试过从 FileInputStream 像这样:

ZipInputStream in = new ZipInputStream(new FileInputStream(...));

惯于 因为 zip 存档可以包含多个文件,并且您需要指定要读取的文件,所以可以工作。

你可以使用 java.util.zip.ZipFile 和一个图书馆,例如 来自 Apache Commons IO 的 IOUtils 或者 来自 Guava 的字节流 帮助您复制流。

例子:

ByteArrayOutputStream out = new ByteArrayOutputStream();
try (ZipFile zipFile = new ZipFile("foo.zip")) {
    ZipEntry zipEntry = zipFile.getEntry("fileInTheZip.txt");

    try (InputStream in = zipFile.getInputStream(zipEntry)) {
        IOUtils.copy(in, out);
    }
}

我会用 IOUtils 来自 commons io 项目。

IOUtils.copy(zipStream, byteArrayOutputStream);

您未接来电

ZipEntry 条目 = (ZipEntry) zipStream.getNextEntry();

定位第一个条目解压缩的第一个字节。

 ByteArrayOutputStream streamBuilder = new ByteArrayOutputStream();
 int bytesRead;
 byte[] tempBuffer = new byte[8192*2];
 ZipEntry entry = (ZipEntry) zipStream.getNextEntry();
 try {
     while ( (bytesRead = zipStream.read(tempBuffer)) != -1 ){
        streamBuilder.write(tempBuffer, 0, bytesRead);
     }
 } catch (IOException e) {
      ...
 }

您可以围绕 ZipInputStream 实现自己的包装器,忽略 close() 并将其交给第三方库。

thirdPartyLib.handleZipData(new CloseIgnoringInputStream(zipStream));


class CloseIgnoringInputStream extends InputStream
{
    private ZipInputStream stream;

    public CloseIgnoringInputStream(ZipInputStream inStream)
    {
        stream = inStream;
    }

    public int read() throws IOException {
        return stream.read();
    }

    public void close()
    {
        //ignore
    }

    public void reallyClose() throws IOException
    {
        stream.close();
    }
}

我会在 ZipInputStream 上调用 getNextEntry() ,直到它到达您想要的条目(使用 ZipEntry.getName() 等)。调用 getNextEntry() 会将“光标”前进到它返回的条目的开头。然后,使用 ZipEntry.getSize() 确定应使用 zipInputStream.read() 读取多少字节。

目前还不清楚你是如何获得 zipStream 的。当你得到这样的结果时它应该可以工作:

  zipStream = zipFile.getInputStream(zipEntry)

不清楚你是如何获得 zipStream 的。当你得到这样的结果时它应该可以工作:

  zipStream = zipFile.getInputStream(zipEntry)

如果您从 ZipFile 获取 ZipInputStream,则可以为 3d 方库获取一个流,让其使用它,然后使用之前的代码获取另一个输入流。

请记住,输入流是一个游标。如果您拥有完整的数据(例如 ZipFile),您可以要求在其上放置 N 个光标。

一种不同的情况是,如果您只有一个“GZip”输入流,只有一个压缩字节流。在这种情况下,ByteArrayOutputStream 缓冲区就有意义了。

请尝试以下代码

private static byte[] getZipArchiveContent(File zipName) throws WorkflowServiceBusinessException {

  BufferedInputStream buffer = null;
  FileInputStream fileStream = null;
  ByteArrayOutputStream byteOut = null;
  byte data[] = new byte[BUFFER];

  try {
   try {
    fileStream = new FileInputStream(zipName);
    buffer = new BufferedInputStream(fileStream);
    byteOut = new ByteArrayOutputStream();

    int count;
    while((count = buffer.read(data, 0, BUFFER)) != -1) {
     byteOut.write(data, 0, count);
    }
   } catch(Exception e) {
    throw new WorkflowServiceBusinessException(e.getMessage(), e);
   } finally {
    if(null != fileStream) {
     fileStream.close();
    }
    if(null != buffer) {
     buffer.close();
    }
    if(null != byteOut) {
     byteOut.close();
    }
   }
  } catch(Exception e) {
   throw new WorkflowServiceBusinessException(e.getMessage(), e);
  }
  return byteOut.toByteArray();

 }

检查输入流是否位于请求中。

否则,作为实现:我认为您在阅读时不需要写入结果流,除非您在另一个线程中处理这个确切的流。

只需创建一个字节数组,读取输入流,然后创建输出流。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top