无法从 InputStream 创建 zip 文件

Unable to create zip file from InputStream

我需要从输入流数据创建一个 zip 文件,在写入 zip 之前我需要找到输入流的校验和。

为此,我使用以下代码:

private String writeZipFileToFS(List<ResponsePacks> attachmentList) throws IOException
    {
        File fileToWrite = new File(getZipPath() + "fileName.zip");
        try
        {
            FileUtils.copyInputStreamToFile(compress(attachmentList), fileToWrite);
        }
        catch (IOException e)
        {
            throw e;
        }
        return fileName;
    }

private InputStream compress(List<ResponsePacks> attachmentList)
    {
        byte buffer[] = new byte[2048];
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        ZipOutputStream zipFileToSend = new ZipOutputStream(byteStream);
        try
        {
            for (ResponsePacks info : attachmentList)
            {
                // only for successful requests files would need to be added
                zipFileToSend.putNextEntry(new ZipEntry(info.getFileName()));
                InputStream in = info.getFileContentStream();
                getCheckSum(in, info.getFileName());
                int length;
                while ((length = in.read(buffer)) >= 0)
                {
                    zipFileToSend.write(buffer, 0, length);
                }
                zipFileToSend.closeEntry();
            }
            zipFileToSend.close();
        }
        catch (IOException e)
        {
            throw e;
        }
        return new ByteArrayInputStream(byteStream.toByteArray());
    }
    
    private static void getCheckSum(InputStream is, String fileName)
    {
        byte[] dataCopy = null;
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try
        {
            IOUtils.copy(is, outputStream);
            dataCopy = outputStream.toByteArray();
            printLog("Byte Array Size {}", dataCopy.length);
            String checkSum = calculateChecksum(dataCopy);
            printLog("Checksum for file {} {}", fileName, checkSum);
            outputStream.flush();
            outputStream.close();
        }
        catch (Exception e)
        {
            printLog("Error on calculationg checksum {}", e.getMessage());
        }
    }

    private static String calculateChecksum(byte[] dataCopy)
    {
        try (ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(dataCopy)))
        {
            ZipEntry zipEntry;
            MessageDigest digest = DigestUtils.getSha256Digest();
            DWriter writer = new DWriter(digest);
            while ((zipEntry = zipInputStream.getNextEntry()) != null)
            {
                byte[] entityData = IOUtils.toByteArray(zipInputStream);
                if (!zipEntry.isDirectory())
                {
                    writer.write(entityData);
                }
            }
            if (writer.getChecksum() != null)
            {
                return writer.getChecksum();
            }
        }
        catch (Exception e)
        {
             throw e;
        }
        return "";
    }

static class DWriter
    {
        private final MessageDigest myDigest;

        DWriter(MessageDigest digest)
        {
            myDigest = digest;
        }

        public void write(byte[] data)
        {
            myDigest.update(data);
        }

        public String getChecksum()
        {
            return new String(Hex.encodeHex(myDigest.digest()));
        }
    }

但问题是,如果我添加代码来计算校验和,然后创建内容为空的 zip 文件,如果我删除校验和计算代码,则创建内容正确的 zip 文件。

而且当我检查日志时,我发现 InputStream 内容不同,但我仍然得到相同的校验和(空字符串),如下所示:

Byte Array Size 20854
Checksum for file 20200910173919142.json e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
Byte Array Size 14383
Checksum for file 1599752440405.zip e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855

我找不到哪里做错了,因为创建的 zip 文件内容为空,而 checkSum 也总是创建相同的内容。

请求帮助我找出我做错的地方。

您使用了两次相同的输入流:首先读取它以获得校验和,然后再次读取它以写入 zip 条目。

            getCheckSum(in, info.getFileName());
            int length;
            while ((length = in.read(buffer)) >= 0)
            {
                zipFileToSend.write(buffer, 0, length);
            }

第二次尝试阅读时,没有任何内容可读,因此没有任何内容写入 zip 条目。

一些输入流可以被重置并多次读取,如果不是这种情况,您需要将数据保存到 ByteArrayOutputStream 中(就像您已经在 getCheckSum() 中所做的那样)方法),然后你可以多次读取该数据。