Synchronize Now!
by Eric Bergman-Terrell

Listing One
 ...
class FTPDataTransfer : DataTransfer
{
    ...
    private FtpWebRequest GetFtpWebRequest(string FTPAddress, string Method)
    {
        FtpWebRequest ftpWebRequest = null;
        try
        {
            ftpWebRequest = (FtpWebRequest)WebRequest.Create(FTPAddress);
            // Start a new FTP session.
            ftpWebRequest.KeepAlive   = false;
            // Data will be transferred in binary format.
            ftpWebRequest.UseBinary   = true;
            ftpWebRequest.Credentials = 
                           new NetworkCredential(userID, password);
            ftpWebRequest.Method      = Method;
        }
        catch (UriFormatException ex)
        {
            // Display error message if FTP address is invalid.
            MessageBox.Show(ex.Message, Application.ProductName, 
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        return ftpWebRequest;
    }
    ...
    protected override byte[] DownloadBuffer(string BucketName, 
                                             string FileName)
    {
        WebResponse response = null;
        // Convert bucket and filename into legal FTP folder and filenames.
        BucketName = EncodeName(BucketName);
        FileName   = EncodeName(FileName);

        byte[] result = null;

        try
        {
            string ftpAddress = ftpSite + "/" + ftpHomeDirectory + 
                                "/" + BucketName + "/" + FileName;
            FtpWebRequest 
                ftpWebRequest = GetFtpWebRequest(ftpAddress, 
                        WebRequestMethods.Ftp.DownloadFile);
            response = ftpWebRequest.GetResponse();
            // Prepare to read the file from the response stream.
            Stream responseStream = response.GetResponseStream();
            BinaryReader binaryReader = new BinaryReader(responseStream);
            const int bufferSize = 1024;
            byte[] buffer;
            List<byte> bufferList = new List<byte>();
            // Read the file, one buffer at a time.
            do
            {
                buffer = binaryReader.ReadBytes(bufferSize);

                if (buffer.Length > 0)
                {
                    bufferList.AddRange(buffer);
                }
            } while (buffer.Length == bufferSize);
            result = bufferList.ToArray();
            binaryReader.Close();
        }
        catch (WebException)
        {
            // A WebException will be thrown if file does not exist on server.
        }
        finally
        {
            if (response != null)
            {
                response.Close();
            }
        }

        return result;
    }
    ...
}


Listing Two

 ...

public static class Serialization
{
    public static string ObjectToBase64String(object ObjectValue)
    {
        string objectString = null;
        using (MemoryStream memoryStream = new MemoryStream())
        using (StreamReader streamReader = new StreamReader(memoryStream))
        {
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            // Serialize the object into the memory stream.
            binaryFormatter.Serialize(memoryStream, ObjectValue);
            // Rewind the memory stream.
            memoryStream.Position = 0;
            // Extract the object's data into a byte array.
            byte[] buffer = new byte[memoryStream.Length];
            Array.Copy(memoryStream.GetBuffer(), buffer, buffer.Length);
            // Compress the object's data.
            buffer = Compression.Compress(buffer);

            try
            {
                int plainTextLength = buffer.Length;
                // Encrypt the object's data.
                buffer = Cryptographer.EncryptSymmetric(
                    StringLiterals.SymmetricProvider, buffer);
                int cipherTextLength = buffer.Length;
                Trace.WriteLine(string.Format(
                    "{0} bytes encrypted into {1} bytes", 
                    plainTextLength, cipherTextLength));
                // Convert the compressed, encrypted object data 
                // into a base 64 string.
                objectString = Convert.ToBase64String(buffer);
            }
            catch (Exception ex)
            {
                objectString = null;
                if (ExceptionPolicy.HandleException(ex, 
                        StringLiterals.CaughtExceptionPolicy))
                {
                    throw;
                }
            }
        }
        return objectString;
    }
    ...
}


Listing Three

 ...
public static class Compression
{
    ...
    public static byte[] Compress(byte[] Buffer)
    {
        byte[] result = null;
        using (MemoryStream memoryStream = new MemoryStream())
        using (GZipStream gzipStream = new GZipStream(memoryStream, 
                                    CompressionMode.Compress, true))
        {
            // Write the buffer into the GZip stream.
            gzipStream.Write(Buffer, 0, Buffer.Length);
            gzipStream.Close();
            // Extract the compressed data from the GZip stream.
            result = new byte[memoryStream.Length];
            Array.Copy(memoryStream.GetBuffer(), result, result.Length);
        }
        if (Buffer.Length > 0)
        {
            Trace.WriteLine(string.Format(
              "{0} bytes compressed to {1} bytes ({2}%)", Buffer.Length, 
                 result.Length, ((double)result.Length / 
                    (double)Buffer.Length) * 100.0));
        }
        return result;
    }
    public static byte[] Decompress(byte[] Buffer)
    {
        byte[] result = null;
        using (MemoryStream memoryStream = new MemoryStream(Buffer))
        using (GZipStream gzipStream = new GZipStream(memoryStream, 
                                 CompressionMode.Decompress, true))
        {
            result = ReadBytes(gzipStream);
        }
        return result;
    }
}



3


