Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am taking backup of database on button click event. But for security purpose I want to create password protected zip file.Which will have that .bak file which recently created. And after creating zip delete that .bak file. I tried this code but it's constantly throwing exception that "unsuported file format"

What I have tried:

C#
private void btnSave_Click(object sender, EventArgs e)
{
    if (txtLocation.Text == string.Empty)
    {
        MessageBox.Show("Please select Backup File Location", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    else
    {
        try
        {
            string FileName = "'"+txtLocation.Text + "Backup";
            string BackupFileName = "" + FileName + ".bak'";
            string BackupZipFileName = "" + FileName + ".zip'";
            cn.Open();
            cmd.CommandText = @"BACKUP DATABASE POS TO DISK = " + BackupFileName;
            cmd.CommandType = CommandType.Text;
            cmd.Connection = cn;
            dr = cmd.ExecuteReader();
            dr.Close();
            cn.Close();
           // byte[] bt = File.ReadAllBytes(BackupFileName);
            FileStream fStream = File.Create(BackupZipFileName);
            GZipStream gZip = new GZipStream(fStream, CompressionMode.Compress);
            //zip process
            //delete backupfilename
            //string sZipFileName = BackupFileName.Replace(".bak", ".zip");
            //byte[] bt = File.ReadAllBytes(sZipFileName);
            //FileStream fStream = File.Create(sZipFileName, bt.Length, FileOptions.Encrypted);

            //    GZipStream gZip = new GZipStream(fStream, CompressionMode.Compress);

            //    gZip.Write(bt, 0, bt.Length);
            //    gZip.Close();
            //    gZip.Dispose();

            MessageBox.Show("Backup Successfull", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.Close();
        }
        catch (Exception ex)
        {
            cn.Close();
            MessageBox.Show(ex.Message);
        }
    }
Posted
Updated 15-Jun-20 0:46am

I'd suggest to use DotNetZip 1.13.8 (NuGet Gallery)[^]

Quote:
DotNetZip is a FAST, FREE class library and toolset for manipulating zip files. Use VB, C# or any .NET language to easily create, extract, or update zip files


Usage:
C#
using (ZipFile zip = new ZipFile())
{
  zip.Password= "123456!"; //password for all files
  zip.AddFile("ReadMe.txt");
  zip.AddFile("7440-N49th.png");
  zip.AddFile("2005_Annual_Report.pdf");
  zip.Save("Backup.zip");
}


Here is a complete documentation: About DotNetZip - DotNetZip Documentation[^]
 
Share this answer
 
Comments
Garth J Lancaster 15-Jun-20 9:03am    
+5 Maciej
Maciej Los 15-Jun-20 9:08am    
Thank you, Garth.
C#
FileStream fStream = File.Create(BackupZipFileName);
GZipStream gZip = new GZipStream(fStream, CompressionMode.Compress);

You are trying to zip the zip file that you have not yet created. You need to zip the backup file.
 
Share this answer
 
Comments
Member 14861986 14-Jun-20 4:01am    
Can you post the code how to do this?
Richard MacCutchan 14-Jun-20 4:20am    
What code? You just need to pass the correct filename to the FileStream constructor.
Member 14861986 14-Jun-20 4:33am    
FileStream constructor isn't creating BackupZipFileName?
Richard MacCutchan 14-Jun-20 4:49am    
Sorry I don't know. Check the documentation for the GZip classes you are trying to use. Where does the exception actually occur?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900