ZipArchive gives Unexpected end of data corrupted error
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 totrue
. - 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
).
Central Directory corrupt error in ziparchive
You're creating the ZipArchive
without specifying a mode, which means it's trying to read from it first, but there's nothing to read. You can solve that by specifying ZipArchiveMode.Create
in the constructor call.
Another problem is that you're writing the MemoryStream
to the output before closing the ZipArchive
... which means that the ZipArchive
code hasn't had a chance to do any house-keeping. You need to move the writing part to after the nested using
statement - but note that you need to change how you're creating the ZipArchive
to leave the stream open:
using (MemoryStream ms = new MemoryStream())
{
// Create new ZIP archive within prepared MemoryStream
using (ZipArchive zip = new ZipArchive(ms, ZipArchiveMode.Create, true))
{
zip.CreateEntry(logoimage);
// ...
}
ms.WriteTo(HttpContext.Current.Response.OutputStream);
}
ZipArchive generated zip file not extracted
Using statement braces matter here.
You have to flush out any buffered data and finish the zip archive, by closing it, which is what writes the central directory record into the zip file, before you read back the bytes written to the MemoryStream.
Error when creating Zip file using DotNetZip: Unexpected end of archive
Do you need to Flush the stream after your are done writing to it?
HttpContext.Current.Response.Flush();
EDIT:
Call HttpContext.Current.ApplicationInstance.CompleteRequest()
error while downloading zip archive from server
The problem is actually the .zip file. I downloaded it and I cannot open the file with WinZip. Please try to make a new archive and upload it again on your server.
The code looks okay so I think it just the file that is the problem.
UPDATE #1:
The .zip file is now correct. Try to delete the files before you download and extract the new files. You can use unlink("uploads/update.zip") maybe you also need to clear the uploads/temp directory first.
UPDATE #2:
The download worked now for me. Try to add this header:
header("Content-Transfer-Encoding: Binary");
Also don't forget to close the ZipArchive after extracting:
$zip->close();
Downloading a zip archive from Express endpoint
The problem came from bad async patterns and chaining. The operations were not executing in the correct order, thus my archive was being sent before it was finalized.
Related Topics
How to Match Hyphens With Regular Expression
Fastest Way to Find Strings in a File
C# Replace Item in List<String> "/"
Angular Post Request Received in Net Core API as Null
Create a C# Method to Generate Auto Increment Id
C# Check the Empty or Null Value in All Datatable Values
Best Way to Check If a Data Table Has a Null Value in It
Regular Expression for Password Validation in C#
Get Strings Between Characters in Brackets in C#
How to Get Nested Json Values in C#
Pass Array into ASP.NET Core Route Query String
Windows Service Cannot Access Network Location (Unc) Path
Ziparchive Gives Unexpected End of Data Corrupted Error
Mongodb C# Exception Cannot Deserialize String from Bsontype Int32
Regular Expression for Valid Filename
How to Pass Jquery Variable Value to C# MVC
How to Implement Async Task to Fetch Data from Database Using Async and Await