locked
ZipArchive.CreateEntry for a Stream RRS feed

  • Question

  • User1000638852 posted

    '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?

    Thursday, January 22, 2015 4:45 PM

Answers

  • User1000638852 posted

    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();
                    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 9, 2015 10:16 AM

All replies

  • User-271186128 posted

    Hi jjmonty,<!--?xml:namespace prefix = "o" ns = "urn:schemas-microsoft-com:office:office" /--><o:p></o:p>

    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.<o:p></o:p>

    ZipArchive.CreateEntry Method: https://msdn.microsoft.com/en-us/library/system.io.compression.ziparchive.createentry(v=vs.110).aspx <o:p></o:p>

    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.

                    Stream fs = FileUpload1.PostedFile.InputStream;
                    BinaryReader br = new BinaryReader(fs);
                    Byte[] bytes = br.ReadBytes((Int32)fs.Length);

    More information, please see: http://www.aspsnippets.com/Articles/Save-Files-to-SQL-Server-Database-using-FileUpload-Control.aspx

    Best Regards,
    Dillion

    Friday, January 23, 2015 3:44 AM
  • User1000638852 posted

    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();
                    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 9, 2015 10:16 AM