EldoS | Feel safer!

Software components for data protection, secure storage and transfer

ZIP from MemoryStream

Also by EldoS: Rethync
The cross-platform framework that simplifies synchronizing data between mobile and desktop applications and servers and cloud storages
#17539
Posted: 09/19/2011 05:24:43
by Aaron Cowie (Basic support level)
Joined: 09/19/2011
Posts: 11

I have been attempting to create a zip file using

public TElZipArchiveDirectoryEntry Add(Stream Stream, string FileName);

I have been unsuccessful. I either get an exception, or a zip file of empty files.

I do not see an example in your sample code that uses a Stream. Are you able to send me a working example

e.g. Have a collection of strings. Iterate through this collection and add each string to the zip file as if it were a file by converting it to a byte[] and then writing this to a Memory stream.

Finally get the zipfile contents via
public void Compress(Stream Destination);
using a memoryStream and read this to a byte[]. Close all of the streams and then write the byte[] to a file.
#17540
Posted: 09/19/2011 05:30:31
by Vsevolod Ievgiienko (EldoS Corp.)

Thank you for contacting us.

Please follow this how-to: http://www.eldos.com/documentation/sb...press.html
#17541
Posted: 09/19/2011 05:43:59
by Aaron Cowie (Basic support level)
Joined: 09/19/2011
Posts: 11

Thanks for the link, but this just gives standard info on methods available. I cannot see an example that uses the

Add(Stream Stream, string FileName);
#17542
Posted: 09/19/2011 05:49:32
by Vsevolod Ievgiienko (EldoS Corp.)

We do not have such sample but it's simple to use this method:

Code
MemoryStream ms = new MemoryStream();
ms.Write(...);
ZipWriter.Add(ms, "New_file.txt");
#17543
Posted: 09/19/2011 05:51:09
by Mykola Olshevsky (Basic support level)
Joined: 07/07/2005
Posts: 450

Hi. Sorry, misread your question. See Vsevolod's answer.
#17544
Posted: 09/19/2011 05:53:24
by Aaron Cowie (Basic support level)
Joined: 09/19/2011
Posts: 11

This is what I am doing, but the resultant zip file contains empty files when I open it. This is why I would like a worked example to see one that works and compare to mine.
#17545
Posted: 09/19/2011 05:57:20
by Mykola Olshevsky (Basic support level)
Joined: 07/07/2005
Posts: 450

Do you call all methods, described in how-to? I.e. .CreateArchive, .Add, .Compress? Please show the code snippet you wrote to see what is wrong.
#17546
Posted: 09/19/2011 06:51:14
by Aaron Cowie (Basic support level)
Joined: 09/19/2011
Posts: 11

NB. My attachments use a dynamic class that contains Filename and FileContent (string).



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 buff = new MemoryStream())
{
using (Stream zipArchiveStream = new MemoryStream())
{
int i = 0;
foreach (dynamic att in attachments)
{
i++;
byte[] byteArray = Encoding.UTF8.GetBytes(att.FileContent);
buff.Write(byteArray, 0, byteArray.Length);
zipWriter.Add(buff, "File" + i.ToString() + att.FileName);
break;
}

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

return output;
#17547
Posted: 09/19/2011 07:04:55
by Vsevolod Ievgiienko (EldoS Corp.)

You should add buff.Position = 0 before zipWriter.Add is called.
#17548
Posted: 09/19/2011 07:28:41
by Aaron Cowie (Basic support level)
Joined: 09/19/2011
Posts: 11

That partially worked. My last file would be correct with all earlier files being empty.

I then changed the code to have the buffer stream created for each file, but then I get an exception when I do the Compress ("cannot access a closed stream")

example below.

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)
{
using (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;
Also by EldoS: Rethync
The cross-platform framework that simplifies synchronizing data between mobile and desktop applications and servers and cloud storages

Reply

Statistics

Topic viewed 3147 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!