Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Asp.Net Create text file, zip it, and save to Blob - Without writing anything to disk

A complicated one here, well for me anyway :)

Basically what i would like to achieve is to generate some text, zip this text file within two directories and then upload it to a MySQL blob field - all without writing anything to the disk. I am relatively new to all this so any pointers are greatly appreciated. Heres what i have sort of put together so far, it obviously crashes and burns but hopefully gives a better idea of what id like to do. Oh and im using DotNetZip currently :)

public void broadcastItem()
        {
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            System.IO.StreamWriter sw = new System.IO.StreamWriter(ms);
            System.IO.MemoryStream ms2 = new System.IO.MemoryStream();
            sw.Write("Some Text generated and placed in a file");
            sw.Close(); //Text File Now Created

            using (ZipFile zip = new ZipFile())
            {

                zip.AddDirectory(@"Directory1\Directory2");
                //Zipping within two directories
                ZipEntry e = zip.AddEntry("Test", ms);
                e.
                e.Comment = "The content for entry in the zip file was obtained from a stream";
                zip.Comment = "This zip was created at " + System.DateTime.Now.ToString("G");
                zip.Save(ms2); //Trying to save to memory stream
            }

            try
            {
                OdbcConnection Server = new OdbcConnection("DSN=CentralServer");
                Server.Open();
                OdbcCommand DbCommand = Server.CreateCommand();
                DbCommand.CommandText = "INSERT INTO blobtest(blobfield) VALUES(?)";
                OdbcParameter param = new OdbcParameter("@file", SqlDbType.Binary);
                param.Value = ms2;
                DbCommand.Parameters.Add(param);
                DbCommand.ExecuteNonQuery();
                //Trying to save zip file from memory stream to blob field


            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

** EDIT - Moving Closer ***

I now can create a text file and zip it in memory, problem is the text doesnt display in the file - ie its blank i now have the file within two directories :)

amended code below

public void test3()
        {
            MemoryStream ms = new MemoryStream();
            StreamWriter sw = new StreamWriter(ms);

            sw.WriteLine("HELLO!");
            sw.WriteLine("I WANT TO SAVE THIS FILE AS A .TXT FILE WITHIN TWO FOLDERS");
            ms.Position = 0;

            // create the ZipEntry archive from the xml doc store in memory stream ms
            MemoryStream outputMS = new System.IO.MemoryStream();

            ZipOutputStream zipOutput = new ZipOutputStream(outputMS);
            ZipEntry ze = new ZipEntry(@"Directory1\Directory2\example.txt");
            zipOutput.PutNextEntry(ze);
            zipOutput.Write(ms.ToArray(), 0, Convert.ToInt32(ms.Length));
            zipOutput.Finish();
            zipOutput.Close();

            byte[] byteArrayOut = outputMS.ToArray();
            outputMS.Close();
            ms.Close();
            try
            {
                OdbcConnection rstServer = new OdbcConnection("DSN=CentralServer");
                Server.Open();
                OdbcCommand DbCommand = Server.CreateCommand();
                DbCommand.CommandText = "INSERT INTO blobtest(blobfield) VALUES(?)";
                OdbcParameter param = new OdbcParameter("@file", SqlDbType.Binary);
                param.Value = byteArrayOut;
                DbCommand.Parameters.Add(param);
                DbCommand.ExecuteNonQuery();
                Response.Write(byteArrayOut.ToString());


            }

            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }
like image 778
JazziJeff Avatar asked Aug 07 '12 08:08

JazziJeff


1 Answers

You can use the open source c# compression library SharpZipLib to create an in-memory zip file, as explained here: In Memory compression using SharpZipLib

// zip XElement xdoc and add to requests MTOM value
using (MemoryStream ms = new System.IO.MemoryStream())
{   
   xdoc.Save(ms);  
   ms.Position = 0;

   // create the ZipEntry archive from the xml doc store in memory stream ms
   using (MemoryStream outputMS = new System.IO.MemoryStream())
   {
      using (ZipOutputStream zipOutput = new ZipOutputStream(outputMS))
      {
          ZipEntry ze = new ZipEntry("example.xml");
          zipOutput.PutNextEntry(ze);
          zipOutput.Write(ms.ToArray(), 0, Convert.ToInt32(ms.Length));
          zipOutput.Finish();
          zipOutput.Close();

          // add the zip archive to the request
          SubmissionReceiptListAttachmentMTOM = new base64Binary();
          SubmissionReceiptListAttachmentMTOM.Value = outputMS.ToArray();
      }

      outputMS.Close();
   }

   ms.Close();
}

Now you just need to convert the memory stream to a byte array and save it in the database.

like image 81
victorvartan Avatar answered Sep 21 '22 03:09

victorvartan