Beside, from your description, I suppose perhaps you want to upload the Zip file. If that is the case, I suggest you could try to use the FileUpload control and use the
following code.
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
My version takes a stream and compresses it into a zipped stream then returns it as a byte array:
using (var compressedFileStream = new MemoryStream())
{
//Create an archive and store the stream in memory.
using (ZipArchive zipArchive = new ZipArchive(compressedFileStream, ZipArchiveMode.Update, false))
{
foreach (string strFileName in arrayFileNames)
{
strErrorFileName = strFileName;
using (Stream msFileBody = new MemoryStream())
{
// this is custom code that copies a file location's data to a Stream (msFileBody); this is the Stream needing compression that prompted my initial question
// this is custom code that copies a file location's data to a Stream (msFileBody); this is the Stream needing compression that prompted my initial question
//Create a zip entry for each FileName in the FileName array
zipArchiveEntry = zipArchive.CreateEntry("myFileName");
using (Stream originalFileStream = msFileBody)
{
using (Stream zipEntryStream = zipArchiveEntry.Open())
{
//Copy the attachment stream to the zip entry stream
originalFileStream.CopyTo(zipEntryStream);
}
}
}
}
}
Member
22 Points
253 Posts
ZipArchive.CreateEntry for a Stream
Jan 22, 2015 04:45 PM|jjmonty|LINK
'Sup Nerds:
I want to know if there is anyway that I can create a ZipArchive with a Stream; then return that ZipArchive as a byte[]:
pseudocode:
Stream myStream;
myZipArchive.CreateEntry(myStream)
byte[] myByteArray = myZipArchive.ToByteArray()
Is this possible?
All-Star
45489 Points
7008 Posts
Microsoft
Re: ZipArchive.CreateEntry for a Stream
Jan 23, 2015 03:44 AM|Zhi Lv - MSFT|LINK
Hi jjmonty,
From my point of view, I don’t think you can do that. The parameters of the ZipArchive.CreateEntry method should be string type.
For more information, please refer to this article.
ZipArchive.CreateEntry Method: https://msdn.microsoft.com/en-us/library/system.io.compression.ziparchive.createentry(v=vs.110).aspx
Beside, from your description, I suppose perhaps you want to upload the Zip file. If that is the case, I suggest you could try to use the FileUpload control and use the following code.
More information, please see: http://www.aspsnippets.com/Articles/Save-Files-to-SQL-Server-Database-using-FileUpload-Control.aspx
Best Regards,
Dillion
Member
22 Points
253 Posts
Re: ZipArchive.CreateEntry for a Stream
Feb 09, 2015 10:16 AM|jjmonty|LINK
I found my solution by adapting this code:
http://stackoverflow.com/questions/17217077/create-zip-file-from-byte
My version takes a stream and compresses it into a zipped stream then returns it as a byte array:
using (var compressedFileStream = new MemoryStream())
{
//Create an archive and store the stream in memory.
using (ZipArchive zipArchive = new ZipArchive(compressedFileStream, ZipArchiveMode.Update, false))
{
foreach (string strFileName in arrayFileNames)
{
strErrorFileName = strFileName;
using (Stream msFileBody = new MemoryStream())
{
// this is custom code that copies a file location's data to a Stream (msFileBody); this is the Stream needing compression that prompted my initial question
objFormSystem.OpenFile(strFileName, "myFileName").CopyTo(msFileBody);
// this is custom code that copies a file location's data to a Stream (msFileBody); this is the Stream needing compression that prompted my initial question
//Create a zip entry for each FileName in the FileName array
zipArchiveEntry = zipArchive.CreateEntry("myFileName");
using (Stream originalFileStream = msFileBody)
{
using (Stream zipEntryStream = zipArchiveEntry.Open())
{
//Copy the attachment stream to the zip entry stream
originalFileStream.CopyTo(zipEntryStream);
}
}
}
}
}
arrayByteReturn = compressedFileStream.ToArray();
}