无法从InputStream创建zip文件。

huangapple go评论72阅读模式
英文:

Unable to create zip file from InputStream

问题

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)
        {
            zipFileToSend.putNextEntry(new ZipEntry(info.getFileName()));
            InputStream in = info.getFileContentStream();
            // Removed getCheckSum() invocation
            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)
{
    // Removed checksum calculation code
}

// Rest of the code remains unchanged

Please note that I have removed the invocation of the getCheckSum() method within the compress() method. If you want to maintain the checksum functionality, you should review the calculateChecksum() and related code to ensure that it doesn't interfere with the zip file creation process.

英文:

I have a requirement to create a zip file from input stream data, and before writing to zip I need to find the checksum for the input stream.

To do that I am using below codes:

private String writeZipFileToFS(List&lt;ResponsePacks&gt; attachmentList) throws IOException
{
File fileToWrite = new File(getZipPath() + &quot;fileName.zip&quot;);
try
{
FileUtils.copyInputStreamToFile(compress(attachmentList), fileToWrite);
}
catch (IOException e)
{
throw e;
}
return fileName;
}
private InputStream compress(List&lt;ResponsePacks&gt; 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)) &gt;= 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(&quot;Byte Array Size {}&quot;, dataCopy.length);
String checkSum = calculateChecksum(dataCopy);
printLog(&quot;Checksum for file {} {}&quot;, fileName, checkSum);
outputStream.flush();
outputStream.close();
}
catch (Exception e)
{
printLog(&quot;Error on calculationg checksum {}&quot;, 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 &quot;&quot;;
}
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()));
}
}

But problem is if I am adding code to calculate the checksum then zip file creating with empty content and if I am removing the checksum calculation code then zip file creating with proper contents.

无法从InputStream创建zip文件。

And also when I check the log I found InputStream contents different contents but still I am getting the same checkSum (empty string) always as below:

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

I am unable to find where I am doing wrong, due to which zip file is creating with empty content, and checkSum also creating same always.

Requesting to help me to find where I am doing wrong.

答案1

得分: 2

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

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

第二次尝试读取时,已经没有可读取的内容,因此不会将任何内容写入zip条目。

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

英文:

You consume twice the same inputstream: first you read it to get the checksum and the you read it again to write the zip entry.

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

The second time you're trying to read, there's nothing to read anymore, so nothing gets written into the zip entry.

Some input streams can be reset and read multiple times, if that's not the case here you would need to save the data into a ByteArrayOutputStream (as you're already doing inside the getCheckSum() method), and then you could read that data multiple times.

huangapple
  • 本文由 发表于 2020年9月11日 00:16:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63833758.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定