-
Notifications
You must be signed in to change notification settings - Fork 106
Example of Using in MemoryStream, Zip and ASP.NET
Adrian Voo edited this page Jun 17, 2018
·
3 revisions
Possible Routes
- Export, Save as File, Transmit for Download
- Export, Save as File, Zip as another File, Transmit for Download
- Export to MemoryStream (without saving the file) and Transmit It for Download
- Export to MemoryStream, Zip It (without saving files) Transmit for Download
- Upload, Save as File, Import
- Upload, Save as File, Unzip/Extract as Another File, Import
- Upload to MemoryStream and Import
- Upload to MemoryStream, Unzip/Extract (without saving the file) and Import
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
// 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();
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();
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();
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();
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>");
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>");
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);
}
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);
}