ZipArchive gives an unexpected end to a corrupted error

I am trying to create a zip stream on the fly with some byte array data and make it load using my MVC action.

But the downloaded file always gives the following damaged error when opening in Windows.

enter image description here

And this error when I try to extract from 7z

enter image description here

But note that files extracted from 7z are not corrupted.

I use ZipArchiveand below my code.

    private byte[] GetZippedPods(IEnumerable<POD> pods, long consignmentID)
    {
        using (var zipStream = new MemoryStream())
        {
            //Create an archive and store the stream in memory.                
            using (var zipArchive = new ZipArchive(zipStream, ZipArchiveMode.Create, true))
            {
                int index = 1;
                foreach (var pod in pods)
                {                        
                    var zipEntry = zipArchive.CreateEntry($"POD{consignmentID}{index++}.png", CompressionLevel.NoCompression);                       
                    using (var originalFileStream = new MemoryStream(pod.ByteData))
                    {
                        using (var zipEntryStream = zipEntry.Open())
                        {
                            originalFileStream.CopyTo(zipEntryStream);
                        }
                    }
                }
                return zipStream.ToArray();
            }
        }
    }

    public ActionResult DownloadPOD(long consignmentID)
    {
        var pods = _consignmentService.GetPODs(consignmentID);
        var fileBytes = GetZippedPods(pods, consignmentID);
        return File(fileBytes, MediaTypeNames.Application.Octet, $"PODS{consignmentID}.zip");
    }

What am I doing wrong here.

Any help would be greatly appreciated since I have been struggling with this all day.

thanks in advance

+9
source share
3 answers

Move zipStream.ToArray()out zipArchiveusing.

, . :

  • stream AutoFlush true.
  • .Flush() .

, MemoryStream, .ToArray(), / ( , using).

+13

, , , , GET AngularJS.

: zip angular

responseType: 'arraybuffer' $ http.

factory.serverConfigExportZIP = function () {
    return $http({
        url: dataServiceBase + 'serverConfigExport',
        method: "GET",
        responseType: 'arraybuffer'
    })
};
0

The proposed solution does not work. The error on the Windows machine never disappeared. You can create an archive from memory on Windows and be able to open it normally. However, the devil is in the details, as soon as you try to unzip it in Linux, it does not work. You get the signature End-of-central-directory not found. This solution does not resolve the original problem.

0
source

Source: https://habr.com/ru/post/1690416/


All Articles