This project has moved and is read-only. For the latest updates, please go here.

Memorystream don't work what I need :(

Apr 7, 2010 at 7:38 AM
Edited Apr 7, 2010 at 7:39 AM

Hy there, I'm newby in programming and I have big problems on uzpdating zip file. There is my code with little explenation what I want to do, but it's make error when I want open zip file

public String GetFile(string OdtZip, string Content)
            byte[] byteTempZip; 
            byte[] byteTempContent;


           //get byte massive from byte64 string. This is zip file where I need to update content.xml
            byteTempZip = Convert.FromBase64String(OdtZip.Replace(" ","+")); 

          //get byte massive from byte64 string. This is for content file.
            byteTempContent = Convert.FromBase64String(Content.Replace(" ","+"));

           //get both memory streams.
            MemoryStream StreamTempZip = new MemoryStream(byteTempZip);
            MemoryStream StreamTempContent = new MemoryStream(byteTempContent);


           //There I want to open the zip memorystream file and there I get an error
            ZipStorer zip = ZipStorer.Open(StreamTempZip, FileAccess.ReadWrite);
            zip.AddStream(ZipStorer.Compression.Store, "content.xml", StreamTempContent, DateTime.Now, "");

           //And there I'm other big problem. How can I get this uzpdate value into stream or byte massive, because I need to make string of byte64
            return null;


Tjere I get this error:

System.NotSupportedException: Memory stream is not expandable.
   at System.IO.__Error.MemoryStreamNotExpandable()
   at System.IO.MemoryStream.set_Capacity(Int32 value)
   at System.IO.MemoryStream.EnsureCapacity(Int32 value)
   at System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count)
   at System.IO.Compression.ZipStorer.WriteLocalHeader(ZipFileEntry& _zfe) in C:\WSforZIP\ForZIPTesting\ForZIPTesting\ZipStorer.cs:line 485
   at System.IO.Compression.ZipStorer.AddStream(Compression _method, String _filenameInZip, Stream _source, DateTime _modTime, String _comment) in C:\WSforZIP\ForZIPTesting\ForZIPTesting\ZipStorer.cs:line 225
   at ForZIPTesting.Service1.GetFile(String OdtZip, String Content) in C:\WSforZIP\ForZIPTesting\ForZIPTesting\Service1.asmx.cs:line 38


I hope you can help me. Thx for help :) I hope you understand me, because I have terrible english usage :)

Apr 8, 2010 at 3:03 AM

Hi there,

Your problem is that you have to create the memory stream with an empty constructor in order to be writable. So, instead of:

MemoryStream StreamTempZip = new MemoryStream(byteTempZip);

try with:

MemoryStream StreamTempZip = new MemoryStream();
StreamTempZip.Write(byteTempZip, 0, byteTempZip.Length);
StreamTempZip.Position = 0;