簡體   English   中英

C#Asp.Net創建文本文件,壓縮並保存到Blob - 無需向磁盤寫入任何內容

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

這里很復雜,反正對我來說:)

基本上我想要實現的是生成一些文本,將這個文本文件壓縮到兩個目錄中,然后將其上傳到MySQL blob字段 - 所有這些都沒有向磁盤寫入任何東西。 我對這一切都比較新,所以任何指針都非常感激。 到目前為止,我有點放在一起,它顯然崩潰和燒傷,但希望能更好地了解我喜歡做什么。 哦,我現在使用DotNetZip :)

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

** 編輯 - 移動更近 ** *

我現在可以創建一個文本文件並將其壓縮到內存中,問題是文本沒有顯示在文件中 - 即它的空白我現在有兩個目錄中的文件:)

修改后的代碼

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

您可以使用開源c#壓縮庫SharpZipLib創建內存中的zip文件,如下所述: 使用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();
}

現在您只需要將內存流轉換為字節數組並將其保存在數據庫中。

基本上這是我想要實現的整個匯編,所以我認為id將這些放在一起給任何需要類似東西的人 - 這是一段工作的代碼

public void diskLess()
        {
            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");
            sw.Flush(); //This is required or you get a blank text file :)
            ms.Position = 0;

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

            ZipOutputStream zipOutput = new ZipOutputStream(outputMS);
            ZipEntry ze = new ZipEntry(@"dir1\dir2\whatever.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());
            }

        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM