Example of Using in MemoryStream, Zip and ASP.NET


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: http://zipstorer.codeplex.com

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

Response.ContentType = "text/plain";
Response.AppendHeader("Content-Disposition", "attachment; filename=backup.sql");
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");
}

Response.ContentType = "application/zip";
Response.AppendHeader("Content-Disposition", "attachment; filename=backup.zip");
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.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.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);
}

Last edited Oct 20, 2014 at 4:04 PM by adriancs, version 5