ZipArchive gives Unexpected end of data corrupted error
Asked Answered
L

7

38

I'm trying to create a zip stream on the fly with some byte array data and make it download via my MVC action.

But the downloaded file always gives the following corrupted error when opened in windows.

enter image description here

And this error when I try to xtract from 7z

enter image description here

But note that the files extracted from the 7z is not corrupted.

I'm using ZipArchive and the below is 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 highly appreciated as I'm struggling with this for a whole day.

Thanks in advance

Landscape answered 8/12, 2017 at 4:44 Comment(0)
R
55

Move zipStream.ToArray() outside of the zipArchive using.

The reason for your problem is that the stream is buffered. There's a few ways to deal wtih it:

  • You can set the stream's AutoFlush property to true.
  • You can manually call .Flush() on the stream.

Or, since it's MemoryStream and you're using .ToArray(), you can simply allow the stream to be Closed/Disposed first (which can be done by moving return zipStream.ToArray(); outside the using of zipArchive).

Rasure answered 8/12, 2017 at 4:57 Comment(3)
Would you mind explaining why buffering is an issue?Tocantins
@CalculusWhiz imagine that ZipArchive writes in 4 kilobyte chunks. You write 19 kilobytes to it. It has flushed 16 bytes of data and is waiting for that 1 extra byte to flush the last chunk to the underlying stream. If you try and access the full contents of the underlying stream before this flush occurs, you will only get 16 kilobytes of data, not the full 19. You can force a flush using one of the methods I suggested above.Rasure
What if the code terminates before finishing (like az function) and I have flush few entries. Is it possible to bypass this issue and still read (like 7z is doing).Nadiya
D
13

I Dispose ZipArchive And error solved

 public static byte[] GetZipFile(Dictionary<string, List<FileInformation>> allFileInformations)
    {

        MemoryStream compressedFileStream = new MemoryStream();
        //Create an archive and store the stream in memory.
        using (var zipArchive = new ZipArchive(compressedFileStream, ZipArchiveMode.Create, true))
        {
            foreach (var fInformation in allFileInformations)
            {
                var files = allFileInformations.Where(x => x.Key == fInformation.Key).SelectMany(x => x.Value).ToList();
                for (var i = 0; i < files.Count; i++)
                {
                    ZipArchiveEntry zipEntry = zipArchive.CreateEntry(fInformation.Key + "/" + files[i].FileName);

                    var caseAttachmentModel = Encoding.UTF8.GetBytes(files[i].Content);

                    //Get the stream of the attachment
                    using (var originalFileStream = new MemoryStream(caseAttachmentModel))
                    using (var zipEntryStream = zipEntry.Open())
                    {
                        //Copy the attachment stream to the zip entry stream
                        originalFileStream.CopyTo(zipEntryStream);
                    }
                }
            }
            //i added this line 
            zipArchive.Dispose();

            return compressedFileStream.ToArray();
        }
    }

public void SaveZipFile(){
        var zipFileArray = Global.GetZipFile(allFileInformations);
        var zipFile = new MemoryStream(zipFileArray);
        FileStream fs = new FileStream(path + "\\111.zip", 
        FileMode.Create,FileAccess.Write);
        zipFile.CopyTo(fs);
        zipFile.Flush();
        fs.Close();
        zipFile.Close();
}
Department answered 28/10, 2019 at 10:57 Comment(1)
Same for me: Using "using" strictly, doing all the flushes and closes and the archive keeps being corrupted. I added this one magic line, and it works perfectly. I am wondering now why this is.Porkpie
F
0

I was also having problems with this and I found my issue was not the generation of the archive itself but rather how I was handing my GET request in AngularJS.

This post helped me: how to download a zip file using angular

The key was adding responseType: 'arraybuffer' to my $http call.

factory.serverConfigExportZIP = function () {
    return $http({
        url: dataServiceBase + 'serverConfigExport',
        method: "GET",
        responseType: 'arraybuffer'
    })
};
Firn answered 10/8, 2018 at 19:6 Comment(0)
R
0

you can remove "using" and use Dispose and Close methods it's work for me

...
zip.Dispose();
zipStream.Close();
return zipStream.ToArray();
Ratib answered 4/6, 2020 at 8:27 Comment(0)
T
0

I know this is a C# question but for managed C++, delete the ZipArchive^ after you're done with it to fix the error.

ZipArchive^ zar = ZipFile::Open(starget, ZipArchiveMode::Create);
ZipFileExtensions::CreateEntryFromFile(zar, sfile1, "file.txt");
ZipFileExtensions::CreateEntryFromFile(zar, sfile2, "file2.txt");
delete zar;
Treacle answered 10/12, 2020 at 21:51 Comment(0)
O
0

when i wanted to create zip file directly from MemoryStream which i used for ZipArchive i was getting error ( "unexpected end of data" or zero length file )

there are three points to get ride of this error

  1. set the last parameter of ZipArchive constructor to true ( it leaves to leave stream open after ZipArchive disposed )

  2. call dispose() on ZipArchive and dispose it manually.

  3. create another MemoryStream based on which you set in ZipArchive constructor, by calling ToArray() method.

here is sample code :

using (var memoryStream = new MemoryStream())
            {
                using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create,))
                {
                    foreach (var s3Object in objectList.S3Objects)
                    {
                        var entry = archive.CreateEntry(s3Object.Key, CompressionLevel.NoCompression);

                        using (var entryStream = entry.Open())
                        {
                            var request = new GetObjectRequest { BucketName = command.BucketName, Key = s3Object.Key };
                            using (var getObjectResponse = await client.GetObjectAsync(request))
                            {
                                await getObjectResponse.ResponseStream.CopyToAsync(entryStream);
                            }
                        }
                    }

                    archive.Dispose();
                    using (var fileStream = new FileStream(outputFileName, FileMode.Create, FileAccess.Write))
                    {
                        var zipFileMemoryStream = new MemoryStream(memoryStream.ToArray());
                        zipFileMemoryStream.CopyTo(fileStream);
                        zipFileMemoryStream.Flush();
                        fileStream.Close();
                        zipFileMemoryStream.Close();

                    }
                }
            }
Oberheim answered 30/6, 2022 at 10:40 Comment(0)
H
0

I had the same problem... In this case I just needed to move the ToArray() (byte[]) from MemoryStream outside the using (var zipArchive = new ZipArchive...

I think it is necessary for using related to ZipArchive to completely close and dispose of the file before converting it into a byte array

Homologous answered 19/9, 2022 at 15:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.