Skip to content

Example of Using in MemoryStream, Zip and ASP.NET

Adrian Voo edited this page Jun 17, 2018 · 3 revisions

Possible Routes

  1. Export, Save as File, Transmit for Download
  2. Export, Save as File, Zip as another File, Transmit for Download
  3. Export to MemoryStream (without saving the file) and Transmit It for Download
  4. Export to MemoryStream, Zip It (without saving files) Transmit for Download
  5. Upload, Save as File, Import
  6. Upload, Save as File, Unzip/Extract as Another File, Import
  7. Upload to MemoryStream and Import
  8. Upload to MemoryStream, Unzip/Extract (without saving the file) and Import

About Zip

ZipStorer is used in these examples to zip and unzip files. It is a C# Class and is completely written in C#. No 3rd party DLL is needed in order for ZipStorer to work. Download ZipStorer at: https://github.com/jaime-olivares/zipstorer

1. Export, Save as File, Transmit for Download

// Example File Path on Web: www.mywebsite.com/backup.sql

// Get physical file path
string filePhysicalPath = HttpContext.Current.Server.MapPath("~/backup.sql");

using (MySqlConnection conn = new MySqlConnection(connectionString))
{
    using (MySqlCommand cmd = new MySqlCommand())
    {
        using (MySqlBackup mb = new MySqlBackup(cmd))
        {
            cmd.Connection = conn;
            conn.Open();
            mb.ExportToFile(filePhysicalPath);
        }
    }
}

System.IO.FileInfo fi = new System.IO.FileInfo(filePhysicalPath);

Response.ContentType = "text/plain";
Response.AppendHeader("Content-Disposition", "attachment; filename=backup.sql");
Response.AppendHeader("Content-Length", fi.Length.ToString());
Response.TransmitFile(filePhysicalPath);
Response.End();

2. Export, Save as File, Zip as another File, Transmit for Download

using System.IO.Compression;
string filePhysicalPath = HttpContext.Current.Server.MapPath("~/backup.sql");

using (MySqlConnection conn = new MySqlConnection(connectionString))
{
    using (MySqlCommand cmd = new MySqlCommand())
    {
        using (MySqlBackup mb = new MySqlBackup(cmd))
        {
            cmd.Connection = conn;
            conn.Open();
            mb.ExportToFile(filePhysicalPath);
        }
    }
}

string fileZip = HttpContext.Current.Server.MapPath("~/backup.zip");
using (ZipStorer zip = ZipStorer.Create(fileZip, "MySQL Backup"))
{
    zip.AddFile(ZipStorer.Compression.Deflate, filePhysicalPath, "backup.sql", "MySQL Backup");
}

System.IO.FileInfo fi = new System.IO.FileInfo(fileZip);

Response.ContentType = "application/zip";
Response.AppendHeader("Content-Disposition", "attachment; filename=backup.zip");
Response.AppendHeader("Content-Length", fi.Length.ToString());
Response.TransmitFile(fileZip);
Response.End();

3. Export to MemoryStream (without saving the file) and Transmit It for Download

using System.IO;
string connstr = "server=localhost;user=root;pwd=1234;database=test;";
MemoryStream ms = new MemoryStream();
using (MySqlConnection conn = new MySqlConnection(connstr))
{
    MySqlCommand cmd = new MySqlCommand();
    MySqlBackup mb = new MySqlBackup(cmd);
    cmd.Connection = conn;
    conn.Open();
    mb.ExportToMemoryStream(ms);
}
Response.ContentType = "text/plain";
Response.AppendHeader("Content-Disposition", "attachment; filename=backup.sql");
Response.AppendHeader("Content-Length", ms.Length.ToString());
Response.BinaryWrite(ms.ToArray());
Response.End();

4. Export to MemoryStream, Zip It (without saving files) Transmit for Download

using System.IO.Compression;
using System.IO;
string connstr = "server=localhost;user=root;pwd=1234;database=test;";
MemoryStream ms = new MemoryStream();
using (MySqlConnection conn = new MySqlConnection(connstr))
{
    MySqlCommand cmd = new MySqlCommand();
    MySqlBackup mb = new MySqlBackup(cmd);
    cmd.Connection = conn;
    conn.Open();
    mb.ExportToMemoryStream(ms);
}

ms.Position = 0;
MemoryStream msZip = new MemoryStream();
ZipStorer zip = ZipStorer.Create(msZip, "MySQL Backup");
zip.AddStream(ZipStorer.Compression.Deflate, "Backup.sql", ms, DateTime.Now, "MySQL Backup");
zip.Close();

Response.ContentType = "application/zip";
Response.AppendHeader("Content-Disposition", "attachment; filename=backup.zip");
Response.AppendHeader("Content-Length", msZip.Length.ToString());
Response.BinaryWrite(msZip.ToArray());
Response.End();

5. Upload, Save as File, Import

string filePhysicalPath = HttpContext.Current.Server.MapPath("~/upload.sql");

FileUpload1.SaveAs(filePhysicalPath);

using (MySqlConnection conn = new MySqlConnection(connectionString))
{
    using (MySqlCommand cmd = new MySqlCommand())
    {
        using (MySqlBackup mb = new MySqlBackup(cmd))
        {
            cmd.Connection = conn;
            conn.Open();
            mb.ImportFromFile(filePhysicalPath);
        }
    }
}
Response.Write("<script type=\"text/javascript\">alert('Import Completed')</script>");

6. Upload, Save as File, Unzip/Extract as Another File, Import

using System.IO.Compression;
using System.IO;
using System.Text;
string filezip = HttpContext.Current.Server.MapPath("~/upload.zip");
string fileSql = HttpContext.Current.Server.MapPath("~/backup.sql");

FileUpload1.SaveAs(filezip);

using (ZipStorer zip = ZipStorer.Open(filezip, FileAccess.Read))
{
    List<ZipStorer.ZipFileEntry> dir = zip.ReadCentralDir();
    zip.ExtractFile(dir[0], fileSql);
}

using (MySqlConnection conn = new MySqlConnection(connectionString))
{
    using (MySqlCommand cmd = new MySqlCommand())
    {
        using (MySqlBackup mb = new MySqlBackup(cmd))
        {
            cmd.Connection = conn;
            conn.Open();
            mb.ImportFromFile(fileSql);
        }
    }
}
Response.Write("<script type=\"text/javascript\">alert('Import Completed')</script>");

7. Upload to MemoryStream and Import

using System.IO;
string connstr = "server=localhost;user=root;pwd=1234;database=test;";
byte[] ba = FileUpload1.FileBytes;
MemoryStream ms = new MemoryStream(ba);
using (MySqlConnection conn = new MySqlConnection(connstr))
{
    MySqlCommand cmd = new MySqlCommand();
    MySqlBackup mb = new MySqlBackup(cmd);
    cmd.Connection = conn;
    conn.Open();
    mb.ImportFromMemoryStream(ms);
}

8. Upload to MemoryStream, Unzip/Extract (without saving the file) and Import

using System.IO.Compression;
using System.IO;
MemoryStream ms = new MemoryStream();
string connstr = "server=localhost;user=root;pwd=1234;database=test;";

byte[] ba = FileUpload1.FileBytes;
MemoryStream msZip = new MemoryStream(ba);
ZipStorer zip = ZipStorer.Open(msZip, FileAccess.Read);
var dir = zip.ReadCentralDir();
zip.ExtractFile(dir[0], ms);

ms.Position = 0;

using (MySqlConnection conn = new MySqlConnection(connstr))
{
    MySqlCommand cmd = new MySqlCommand();
    MySqlBackup mb = new MySqlBackup(cmd);
    cmd.Connection = conn;
    conn.Open();
    mb.ImportFromMemoryStream(ms);
}