Attention: We are retiring the ASP.NET Community Blogs. Learn more >

Zip up those bak files

This is something I have come across during our SQL migration which I didn't even think about. You can zip up your bak files and save considerable hard drive space. We have a 14 gig database which happily compresses down to 2 gig. With hard drives coming down in price, you may think why bother. Well in our migration we have two separate domains for testing purposes which have to stay separate at all costs. Compressing our bak files we can burn it to DVD and move it to the test domain regularly for testing our in house applications.

Right I want this compression automated and complete before I come into the office, so I wrote this small utility.
If you find it at all helpful, feel free to use it. It uses the ICSharpCode.SharpZipLib.Zip library which can be downloaded from here.

 

using System;
using System.Collections;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;


static void Main()
     {
         ArrayList alFiles = new ArrayList();
 
         DirectoryInfo dir = new DirectoryInfo(".");
         foreach (FileInfo f in dir.GetFiles("*.bak"))
         {
             alFiles.Add(f.Name);
         }
 
         for (int i = 0; i < alFiles.Count; i++)
         {
             ZipNamedFile(alFiles[i].ToString());
         }
 
     }
 
private static void ZipNamedFile(string input)
{
         string fileName;
         fileName = input.ToLower();
         fileName = fileName.Replace(".bak", ".zip");
 
  using (ZipOutputStream s = new ZipOutputStream(File.Create(fileName)))
  {
 
        s.SetLevel(5); // 0-9, 9 being the highest compression
 
        byte[] buffer = new byte[4096];
 
        ZipEntry entry = new ZipEntry(Path.GetFileName(input));
 
        entry.DateTime = DateTime.Now;
 
        s.PutNextEntry(entry);
 
           using (FileStream fs = File.OpenRead(input))
             {
 
               int sourceBytes;
 
               do
               {
 
                sourceBytes = fs.Read(buffer, 0, buffer.Length);
 
                s.Write(buffer, 0, sourceBytes);
 
                 }
 
                 while (sourceBytes > 0);
 
             }
 
 
             s.Finish();
 
             s.Close();
 
   }
}

8 Comments

  • I think .NET 3.5 can create Zip files without a third party library. Check out System.IO.Packaging

  • Nice example. Thanks!

  • I have automatic .Bak file compression set up with a batch file and WinRar.exe from www.RarLabs.com

    Copy the lines below to a .Bat or a .Cmd file and schedule it with Windows' Scheduled Tasks:

    ECHO Start compressing .BAK files
    "C:\Program Files\WinRAR\winrar" a -ag_yyyymmddBCK -ibck -inul -hpOPTIONALPASSWORD -y SQLBUP.rar *.bak
    REM DELETE ALL .bak files after compression
    del *.bak

    (By the way I also used SharpZipLib for uploading and automatically decompressing image-folders. It's a very nice 3rd party component!) Good to hear that 3.5 has native support for file (de)compression!

  • I was just thinking about this a few days ago and I had a question, I'm no SQL Admin... Can you select the "Compress files" option on the backup folder's properties? Are there any issues with this?

  • @rrobins - Yes it is good that compression is now in the framework, it is something I will be looking into, but as yet we haven't done any development above 2.0. However as of yesterday we now have a nice and shiny MSDN sub so we can start using VS2008.

    @Denny - As far as I know there shouldn't be any issues with setting this on the folder. However every time the OS accesses this file you are going to hammer the processor, the bigger the file the more processing is going on. Also with zip technologies such as winzip, you can span disks for large files, you cannot do this with a windows compressed folder.

  • Another problem with compressed folders is that if you need to copy/move the backup to another location, it re-expands to full size for travel on the network. This can be a real problem if you have to go out on a slow WAN.

  • Also see DotNetZip - it's simpler to do this kind of thing.
    http://dotnetzip.codeplex.com

  • i tried using the above i needed it in vb.net but nt able to convert it usng the converter

    any help with vb.net code & seocnd where do i get namespace
    ICSharpCode.SharpZipLib.Zip fr downloading
    is it a DLL? or a sourec code
    i had clicke dth elink given above but

Comments have been disabled for this content.