如何在使用DotNetZip时解决此CRC32错误?

时间:2018-10-21 07:39:18

标签: c# .net asp.net-mvc dotnetzip

我正在使用DotNetZip库从要从SQL Server数据库检索的字节数组生成Zip文件。由于某些文件损坏了整个档案,我一直将CRC32设为0000000x0。

我已经尝试在DotNetZip How to fix 0000000 CRC32 issue?上实施解决方案,但是仍然无法解决问题。

这是我的Controller方法:

 using (var outputStream = new MemoryStream())
                //outputStream.Position = 0;
                using (var z = new ZipFile())
                {
                    while (j < a.Length)
                    {
                        //SQL Connection Code to retrieve base64 string called attachment
                        if (b.attachment != null)
                        {
                            string attachment = b.attachment;
                            byte[] byteArray = Convert.FromBase64String(attachment);
                            string contentType = b.filetype;

                            if (contentType.Equals("application/pdf"))
                            {
                                var newname = "D:\\Pdf_" + randomGenerator() + ".pdf";
                                z.AddEntry(newname, byteArray);
                            }
                            else if (contentType.Equals("text/plain"))
                            {
                                var newname = "D:\\Txt_" + randomGenerator() + ".txt";
                                z.AddEntry(newname, byteArray);
                            }
                            else if (contentType.Equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document") || contentType.Equals("application/msword"))
                            {
                                var newname = "D:\\Doc_" + randomGenerator() + ".docx";
                                z.AddEntry(newname, byteArray);
                            }
                            else if (contentType.Equals("application/vnd.openxmlformats-officedocument.spreadsheet.document") || contentType.Equals("application/msexcel") || contentType.Equals("application/vnd.ms-excel") || contentType.Equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
                            {
                                var newname = "D:\\Doc_" + randomGenerator() + ".xlsx";
                                z.AddEntry(newname, byteArray);
                            }
                            else if (contentType.Equals("image/jpeg") || contentType.Equals("image/png") || contentType.Equals("image/jpg"))
                            {
                                var newname = "D:\\Img_" + randomGenerator() + ".jpeg";
                                z.AddEntry(newname, byteArray);
                            }
                            else if (contentType.Equals("image/svg") || contentType.Equals("image/svg+xml"))
                            {
                                var newname = "D:\\SVG_" + randomGenerator() + ".svg";
                                z.AddEntry(newname, byteArray);
                            }
                            outputStream.Flush();
                            z.ParallelDeflateThreshold = -1;
                            z.Save(outputStream);

                            outputStream.Seek(0, SeekOrigin.Begin);
                            outputStream.Position = 0;

                            j++;
                        }


                        else
                        {
                            ViewBag.Error = "Attachment Data Not Found!";
                            return View();
                        }
                    }
                    //outputStream.Seek(0, 0);

                    Response.Clear();
                    Response.BufferOutput = false;
                    Response.ContentType = "application/zip";
                    Response.AddHeader("content-disposition", "attachment; filename=" + "ZipDownload_" + randomGenerator() + ".zip");
                    z.Save(Response.OutputStream);
                    Response.End();
                    return File(outputStream, "application/zip", "ZipDownload_" + randomGenerator() + ".zip");
}
}
return View();
}

我也曾尝试将ParallelDeflateThreshold设置为-1,但这并没有改变。

除此之外,我尝试使用System.IO.Compression生成ZipArchive,但CRC32问题仍然存在。

我可以单独下载文件,而且都没有损坏。

0 个答案:

没有答案