3

I am using ZipArchive with in a handler to serve to a user using memory stream and a web handler. Locally this was working until I uploaded the application to a live site.

Here is my code.

using (ZipArchive newArchive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
    newArchive.CreateEntryFromFile(fileName, Path.GetFileName(fileName));
    if (File.Exists(acRefFile))
    {
        newArchive.CreateEntryFromFile(acRefFile,
            newACRefName + Path.GetExtension(acRefFile));
    }
    else
    {
        SystemLogManager sysLogMgr = new SystemLogManager();
        sysLogMgr.AddErrorMessage(acRefFile, "File not found");
    }
    if (File.Exists(exRefFile))
    {
        newArchive.CreateEntryFromFile(exRefFile,
            newExRefName + Path.GetExtension(exRefFile));
    }
    else
    {
        SystemLogManager sysLogMgr = new SystemLogManager();
        sysLogMgr.AddErrorMessage(exRefFile, "File Not Found");
    }
    if (File.Exists(exRef2File))
    {
        newArchive.CreateEntryFromFile(exRef2File,
            newExRef2Name + Path.GetExtension(exRef2File));
    }
}
memoryStream.Position = 0;
byte[] bytes = memoryStream.GetBuffer();
context.Response.Buffer = true;
context.Response.Clear();
context.Response.ContentType = "application/zip";
context.Response.AddHeader("content-disposition",
    string.Format("attachment; filename =app_{0}_{1}.zip", appForm.Cand_sno,
        appForm.App_year));
context.Response.BinaryWrite(bytes.ToArray());
context.Response.Flush();

And the following image shows the downloaded zip file and the error generated. Zip error

So is there anything in code that could be wrong or something I could try server side?

Update 1: Based on the comments received I tried adding the zip file directly onto the server. Same issue occurs as in the zip is 'corrupted'.

Update 2: Further investigations I have now discovered that the zip file opens up when using 7zip but not standard windows extract. When right click extract all the message states the zip is empty.

Thanks

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Dashwall
  • 171
  • 16
  • Is your development machine and the server a windows based machine? And have you checked the servers event / application logs? – Paul Zahra Sep 23 '15 at 08:24
  • Both Development and server are windows based both running the correct version of .net as well. I have looked in application logs and noting is standing out as a possible cause. – Dashwall Sep 23 '15 at 08:27
  • Is it 'corrupted' every time or 9 times out of 10 ? – Paul Zahra Sep 23 '15 at 08:35
  • Yeah 'corrupted' every time. – Dashwall Sep 23 '15 at 08:43
  • Have you read http://stackoverflow.com/questions/12347775/ziparchive-creates-invalid-zip-file/12350106#12350106 and http://stackoverflow.com/questions/17232414/creating-a-zip-archive-in-memory-using-system-io-compression both seem to have a 'working' version... gotta be worth a try. – Paul Zahra Sep 23 '15 at 08:44
  • I wonder if your issue arises from not disposing of the memorystream... try a using statement for it. – Paul Zahra Sep 23 '15 at 08:45
  • Hi Paul. I have read both questions the only real difference is that the are storing it locally before using file stream to serve the file. I'll try disposing the stream as well. – Dashwall Sep 23 '15 at 08:47
  • I wonder if the streaming is screwing it or the file is created in a corrupt format... it's a quick alteration to save the requested archive to the server... then check it on the server. – Paul Zahra Sep 23 '15 at 08:59
  • Try to semplify the problem. Instead of doing file transfer, save it on the file system and try to open it.. is it corrupted? – Emanuele Greco Sep 24 '15 at 07:55
  • Yes I have tried writing the zip directly to the file system. Same issue opens with 7zip but not any other extractor. – Dashwall Sep 24 '15 at 07:58

2 Answers2

4

So the fix for this question was simply to change the byte[] bytes = MemoryStream.GetBuffer(); to byte[] bytes = MemoryStream.ToArray(); What this does is only get the used bytes not the extra bytes the buffer adds.

Dashwall
  • 171
  • 16
0

I use ZipFile class and the result is never corrupted. Can you try this?

ZipFile.CreateFromDirectory("C:\somefolder", "C:\someotherfolder\somefile.zip");
Emanuele Greco
  • 12,551
  • 7
  • 51
  • 70
  • ZipFile looks as if it creates a zip file direct from a directories contents. I wan't a zip file created from using a group of specified files from different locations. – Dashwall Sep 24 '15 at 08:21
  • This is right, but if ZipFile works right, you can create a method that 1)Copies all files into a temporary directory 2)Zips the directory into a temp zip file 3)Deletes temporary directory 4)Returns the zip file – Emanuele Greco Sep 24 '15 at 08:48