c# - Byte array getting corrupted when passed to another method -


i have bunch of jpg images in byte array form. want add these zip file, turn zip file byte array, , pass somewhere else. in method, have code:

var response = //some response object hold byte array      using (var ms = new memorystream())     {         using (var ziparchive = new ziparchive(ms, ziparchivemode.create, true))         {             var = 1;             foreach (var image in images) // collection holds byte arrays.             {                 var entry = ziparchive.createentry(i + ".jpg");                  using (var entrystream = entry.open())                 using (var compressstream = new memorystream(photo.imageoriginal))                 {                     compressstream.copyto(entrystream);                 }                     i++;             }             response.zipfile = ms.toarray();         }      using (var fs = new filestream(@"c:\users\myname\desktop\image.zip", filemode.create))         {             ms.position = 0;             ms.copyto(fs);         }     }      return response; 

now, i've added filestream near bottom write zipfile right away testing purposes. works, zip file 1 or more images in on desktop. however: response.zipfile can not made valid zipfile in same way. have tried this:

using (var ms2 = new memorystream(response.zipfile)) using (var fs = new filestream(@"c:\users\bara\desktop\image.zip", filemode.create)) {     ms2.position = 0;     ms2.copyto(fs); } 

but creates zipfile can not opened.

what i'm trying do: turn response.zipfileinto array can turned working zipfile again. doing wrong in code?

how know ziparchive's dispose doesn't write more underlying stream?

you should move line after disposing ziparchive:

response.zipfile = ms.toarray(); 

full code:

var response = //some response object hold byte array  using (var ms = new memorystream()) {     using (var ziparchive = new ziparchive(ms, ziparchivemode.create, true))     {         var = 1;         foreach (var image in images) // collection holds byte arrays.         {             var entry = ziparchive.createentry(i + ".jpg");              using (var entrystream = entry.open())             using (var compressstream = new memorystream(photo.imageoriginal))             {                 compressstream.copyto(entrystream);             }             i++;         }     }     response.zipfile = ms.toarray(); }  return response; 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -