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