简体   繁体   中英

ZipArchive gives Unexpected end of data corrupted error

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.

在此处输入图像描述

And this error when I try to xtract from 7z

在此处输入图像描述

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

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 we've done by moving it outside the using ).

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();
}

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'
    })
};

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

...
zip.Dispose();
zipStream.Close();
return zipStream.ToArray();

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;

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();

                    }
                }
            }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM