O
orekinbck
Hi There
Is there any easy way to use the System.Compression tools in .NET 2.0
to compress an entire directory ?
All my source code is kept in a single directory so I have written a
utility that recursively backs up the directory and compresses each
file as it goes. The utility has no GUI.
I have attached the program here, I would be interested to get any
feedback as I am still learning C#.
TIA
Bill
using System;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Globalization;
namespace FileCopier
{
sealed public class FileCopierMain
{
public enum CompressionActions { Nothing, Compress, Decompress };
static string CompressSuffix = ".BWZIP";
static string ToFolder = @"C:\Backups\";
//***************************************************
//TO BACKUP AND COMPRESS, COMMENT THIS IN
//AND SET THE FROM FOLDERS
static CompressionActions CompressionAction =
CompressionActions.Compress;
static string FromFolder = @"C:\Dev\";
//***************************************************
//***************************************************
//TO DECOMPRESS A BACKED UP FOLDER, COMMENT THIS IN
//AND SET THE FROM FOLDER EQUAL TO THE BACKED UP FOLDER
//static CompressionActions CompressionAction =
CompressionActions.Decompress;
//static string FromFolder = @"C:\Backups\Dev_30Jul05_3";
//***************************************************
private FileCopierMain() { }
static void Main()
{
if (FromFolder.Substring(FromFolder.Length - 1, 1) == @"\")
FromFolder = FromFolder.Substring(0, FromFolder.Length - 1);
if (ToFolder.Substring(ToFolder.Length - 1, 1) == @"\")
ToFolder = ToFolder.Substring(0, ToFolder.Length - 1);
//Work out the folder which we are going to copy to. Do not overwrite
old backups
int LastBackSlashPos = FromFolder.LastIndexOf(@"\");
string NewFolderName = FromFolder.Substring(LastBackSlashPos + 1,
FromFolder.Length - LastBackSlashPos - 1);
NewFolderName += "_" + System.DateTime.Now.Date.ToString("ddMMMyy",
CultureInfo.CurrentCulture);
if (Directory.Exists(ToFolder + @"\" + NewFolderName))
{
int newFolderAppend = 1;
while (Directory.Exists(ToFolder + @"\" + NewFolderName + "_" +
newFolderAppend.ToString(CultureInfo.CurrentCulture)))
{
newFolderAppend++;
}
NewFolderName += "_" +
newFolderAppend.ToString(CultureInfo.CurrentCulture);
}
//Do the copy
CopyDirectory(FromFolder, ToFolder + @"\" + NewFolderName, true);
}
public static void CopyDirectory(string sourcePath, string
destinationPath, bool recurse)
{
String[] files;
if (destinationPath[destinationPath.Length - 1] !=
Path.DirectorySeparatorChar)
destinationPath += Path.DirectorySeparatorChar;
if (!Directory.Exists(destinationPath))
Directory.CreateDirectory(destinationPath);
files = Directory.GetFileSystemEntries(sourcePath);
foreach (string element in files)
{
if (recurse)
{
// copy sub directories (recursively)
if (Directory.Exists(element))
CopyDirectory(element, destinationPath + Path.GetFileName(element),
recurse);
// copy files in directory
else
{
if (CompressionAction == CompressionActions.Compress)
BWCompressionUtility.CopyAndCompressFile(element, destinationPath +
Path.GetFileName(element) + CompressSuffix);
else if (CompressionAction == CompressionActions.Decompress)
BWCompressionUtility.CopyAndDecompressFile(element, destinationPath +
Path.GetFileName(element).Replace(CompressSuffix,string.Empty));
else
File.Copy(element, destinationPath + Path.GetFileName(element), true);
}
}
else
{
// only copy files in directory
if (!Directory.Exists(element))
{
if (CompressionAction == CompressionActions.Compress)
BWCompressionUtility.CopyAndCompressFile(element, destinationPath +
Path.GetFileName(element) + CompressSuffix);
else if (CompressionAction == CompressionActions.Decompress)
BWCompressionUtility.CopyAndDecompressFile(element, destinationPath +
Path.GetFileName(element).Replace(CompressSuffix, string.Empty));
else
File.Copy(element, destinationPath + Path.GetFileName(element), true);
}
}
}
}
}
public class BWCompressionUtility
{
//References
//http://www.codeguru.com/columns/DotNet/article.php/c9931
//http://groups.google.com.au/group/m...pression+Folder&rnum=1&hl=en#bb15f1069eb8e175
//http://msdn2.microsoft.com/library/as1ff51s(en-us,vs.80).aspx
/// <summary>
/// Private because all members are static
/// </summary>
private BWCompressionUtility() { }
public static void CopyAndCompressFile(string SourceFullPath, string
DestFullPath)
{
FileStream fs = null;
GZipStream compressedZipStream = null;
// Open and read the contents of the file
fs = new FileStream(SourceFullPath, FileMode.Open, FileAccess.Read,
FileShare.Read);
byte[] buffer = new byte[fs.Length];
int count = fs.Read(buffer, 0, buffer.Length);
if (count != buffer.Length)
throw new Exception("Unable to read data from file");
fs.Close();
// Write buffer to file
fs = new FileStream(DestFullPath, FileMode.Create);
compressedZipStream = new GZipStream(fs, CompressionMode.Compress,
true);
compressedZipStream.Write(buffer, 0, buffer.Length);
compressedZipStream.Close();
fs.Close();
return;
}
public static void CopyAndDecompressFile(string SourceFullPath, string
DestFullPath)
{
FileStream fsREAD = null;
FileStream fsWRITE = null;
GZipStream compressedZipStream = null;
//Compressed File to Read and Associated Stream
fsREAD = System.IO.File.OpenRead(SourceFullPath);
compressedZipStream = new GZipStream(fsREAD,
CompressionMode.Decompress);
//Decompressed File
fsWRITE = new FileStream(DestFullPath, FileMode.Create);
//Read through the stream and write it out
int bytesRead = 0;
int bytesToDecompress = 1000;
byte[] holdingBay = new byte[bytesToDecompress];
while ((bytesRead = compressedZipStream.Read(holdingBay, 0,
holdingBay.Length)) > 0)
fsWRITE.Write(holdingBay, 0, bytesRead);
//Close all streams
compressedZipStream.Close();
fsWRITE.Close();
fsREAD.Close();
}
}
}
Is there any easy way to use the System.Compression tools in .NET 2.0
to compress an entire directory ?
All my source code is kept in a single directory so I have written a
utility that recursively backs up the directory and compresses each
file as it goes. The utility has no GUI.
I have attached the program here, I would be interested to get any
feedback as I am still learning C#.
TIA
Bill
using System;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Globalization;
namespace FileCopier
{
sealed public class FileCopierMain
{
public enum CompressionActions { Nothing, Compress, Decompress };
static string CompressSuffix = ".BWZIP";
static string ToFolder = @"C:\Backups\";
//***************************************************
//TO BACKUP AND COMPRESS, COMMENT THIS IN
//AND SET THE FROM FOLDERS
static CompressionActions CompressionAction =
CompressionActions.Compress;
static string FromFolder = @"C:\Dev\";
//***************************************************
//***************************************************
//TO DECOMPRESS A BACKED UP FOLDER, COMMENT THIS IN
//AND SET THE FROM FOLDER EQUAL TO THE BACKED UP FOLDER
//static CompressionActions CompressionAction =
CompressionActions.Decompress;
//static string FromFolder = @"C:\Backups\Dev_30Jul05_3";
//***************************************************
private FileCopierMain() { }
static void Main()
{
if (FromFolder.Substring(FromFolder.Length - 1, 1) == @"\")
FromFolder = FromFolder.Substring(0, FromFolder.Length - 1);
if (ToFolder.Substring(ToFolder.Length - 1, 1) == @"\")
ToFolder = ToFolder.Substring(0, ToFolder.Length - 1);
//Work out the folder which we are going to copy to. Do not overwrite
old backups
int LastBackSlashPos = FromFolder.LastIndexOf(@"\");
string NewFolderName = FromFolder.Substring(LastBackSlashPos + 1,
FromFolder.Length - LastBackSlashPos - 1);
NewFolderName += "_" + System.DateTime.Now.Date.ToString("ddMMMyy",
CultureInfo.CurrentCulture);
if (Directory.Exists(ToFolder + @"\" + NewFolderName))
{
int newFolderAppend = 1;
while (Directory.Exists(ToFolder + @"\" + NewFolderName + "_" +
newFolderAppend.ToString(CultureInfo.CurrentCulture)))
{
newFolderAppend++;
}
NewFolderName += "_" +
newFolderAppend.ToString(CultureInfo.CurrentCulture);
}
//Do the copy
CopyDirectory(FromFolder, ToFolder + @"\" + NewFolderName, true);
}
public static void CopyDirectory(string sourcePath, string
destinationPath, bool recurse)
{
String[] files;
if (destinationPath[destinationPath.Length - 1] !=
Path.DirectorySeparatorChar)
destinationPath += Path.DirectorySeparatorChar;
if (!Directory.Exists(destinationPath))
Directory.CreateDirectory(destinationPath);
files = Directory.GetFileSystemEntries(sourcePath);
foreach (string element in files)
{
if (recurse)
{
// copy sub directories (recursively)
if (Directory.Exists(element))
CopyDirectory(element, destinationPath + Path.GetFileName(element),
recurse);
// copy files in directory
else
{
if (CompressionAction == CompressionActions.Compress)
BWCompressionUtility.CopyAndCompressFile(element, destinationPath +
Path.GetFileName(element) + CompressSuffix);
else if (CompressionAction == CompressionActions.Decompress)
BWCompressionUtility.CopyAndDecompressFile(element, destinationPath +
Path.GetFileName(element).Replace(CompressSuffix,string.Empty));
else
File.Copy(element, destinationPath + Path.GetFileName(element), true);
}
}
else
{
// only copy files in directory
if (!Directory.Exists(element))
{
if (CompressionAction == CompressionActions.Compress)
BWCompressionUtility.CopyAndCompressFile(element, destinationPath +
Path.GetFileName(element) + CompressSuffix);
else if (CompressionAction == CompressionActions.Decompress)
BWCompressionUtility.CopyAndDecompressFile(element, destinationPath +
Path.GetFileName(element).Replace(CompressSuffix, string.Empty));
else
File.Copy(element, destinationPath + Path.GetFileName(element), true);
}
}
}
}
}
public class BWCompressionUtility
{
//References
//http://www.codeguru.com/columns/DotNet/article.php/c9931
//http://groups.google.com.au/group/m...pression+Folder&rnum=1&hl=en#bb15f1069eb8e175
//http://msdn2.microsoft.com/library/as1ff51s(en-us,vs.80).aspx
/// <summary>
/// Private because all members are static
/// </summary>
private BWCompressionUtility() { }
public static void CopyAndCompressFile(string SourceFullPath, string
DestFullPath)
{
FileStream fs = null;
GZipStream compressedZipStream = null;
// Open and read the contents of the file
fs = new FileStream(SourceFullPath, FileMode.Open, FileAccess.Read,
FileShare.Read);
byte[] buffer = new byte[fs.Length];
int count = fs.Read(buffer, 0, buffer.Length);
if (count != buffer.Length)
throw new Exception("Unable to read data from file");
fs.Close();
// Write buffer to file
fs = new FileStream(DestFullPath, FileMode.Create);
compressedZipStream = new GZipStream(fs, CompressionMode.Compress,
true);
compressedZipStream.Write(buffer, 0, buffer.Length);
compressedZipStream.Close();
fs.Close();
return;
}
public static void CopyAndDecompressFile(string SourceFullPath, string
DestFullPath)
{
FileStream fsREAD = null;
FileStream fsWRITE = null;
GZipStream compressedZipStream = null;
//Compressed File to Read and Associated Stream
fsREAD = System.IO.File.OpenRead(SourceFullPath);
compressedZipStream = new GZipStream(fsREAD,
CompressionMode.Decompress);
//Decompressed File
fsWRITE = new FileStream(DestFullPath, FileMode.Create);
//Read through the stream and write it out
int bytesRead = 0;
int bytesToDecompress = 1000;
byte[] holdingBay = new byte[bytesToDecompress];
while ((bytesRead = compressedZipStream.Read(holdingBay, 0,
holdingBay.Length)) > 0)
fsWRITE.Write(holdingBay, 0, bytesRead);
//Close all streams
compressedZipStream.Close();
fsWRITE.Close();
fsREAD.Close();
}
}
}