EldoS | Feel safer!

Software components for data protection, secure storage and transfer

ZIP from MemoryStream

Also by EldoS: CallbackProcess
A component to control process creation and termination in Windows and .NET applications.
#17550
Posted: 09/19/2011 07:33:56
by Vsevolod Ievgiienko (EldoS Corp.)

All memory streams that you create must exist when you call Compress method. So you should remove 'using (Stream buff = new MemoryStream()) ' construction.
#17552
Posted: 09/19/2011 08:25:20
by Aaron Cowie (Basic support level)
Joined: 09/19/2011
Posts: 11

Can you give me a clue as to how I would achieve this in a loop.

I cannot use a single Stream.
I cannot use multiple streams of the same name.
#17553
Posted: 09/19/2011 08:32:06
by Vsevolod Ievgiienko (EldoS Corp.)

Do not use 'using' construction. zipWriter.Add method saves a reference to a stream. So you can simply replace 'using (Stream buff = new MemoryStream())' with 'Stream buff = new MemoryStream()'.
#17554
Posted: 09/19/2011 08:32:29
by Aaron Cowie (Basic support level)
Joined: 09/19/2011
Posts: 11

Yes worked. Thx!!

final code that seems to work (test example)




byte[] output;

// Set the Eldos.com SecureBlackBox Licence Key
HIPT.Provisioning.Sbb.Licensing.SecureBlackBoxLicense.SetLicence();


SBArcZip.TElZipWriter zipWriter = new SBArcZip.TElZipWriter();
zipWriter.CompressionAlgorithm = SBZipUtils.Unit.ZIP_COMPRESSION_DEFLATE;
zipWriter.CreateArchive();

using (Stream zipArchiveStream = new MemoryStream())
{
int i = 0;
foreach (dynamic att in attachments)
{
Stream buff = new MemoryStream();

i++;
byte[] byteArray = Encoding.UTF8.GetBytes(att.FileContent);
buff.Write(byteArray, 0, byteArray.Length);
buff.Position = 0;
zipWriter.Add(buff, "File" + i.ToString() + att.FileName);

if (i > 2)
{
break;
}
}

zipWriter.Compress(zipArchiveStream);
output = new byte[zipArchiveStream.Length];
zipArchiveStream.Position = 0;
zipArchiveStream.Read(output, 0, (int)zipArchiveStream.Length);
}


return output;

Reply

Statistics

Topic viewed 3146 times

Number of guests: 1, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!