次の方法で共有


FTP, Console, Gzip compression sample code

using System;

using System.Collections.Generic;

using System.Text;

using System.Diagnostics; // Trace

using System.IO; // FTP

using System.Net; // FTP

using System.IO.Compression; // GZip compression

namespace FTP

{

class Program

{

static void Main(string[] args)

{

FtpWebRequest ftpWebRequest = null;

FtpWebResponse ftpWebResponse = null;

Stream responseStream = null;

try

{

ftpWebRequest = (FtpWebRequest)WebRequest.Create(@"ftp://ftp.microsoft.com/developr/readme.txt");

ftpWebRequest.Method = WebRequestMethods.Ftp.DownloadFile;

// Assumes the FTP site uses anonymous logon.

ftpWebRequest.Credentials = new NetworkCredential("anonymous", "janeDoe@contoso.com");

ftpWebResponse = (FtpWebResponse)ftpWebRequest.GetResponse();

responseStream = ftpWebResponse.GetResponseStream();

System.Diagnostics.Trace.WriteLine("File Download Complete, status: " + ftpWebResponse.StatusDescription);

WriteStreamToFile(responseStream, @"c:\ISV\ftpFile");

} // try

catch (Exception Ex)

{

Console.WriteLine(Ex.ToString()); // Log error somewhere

throw Ex; // rethrow the exception up the stack

} // catch

finally

{

// make sure to close system resources - file handles in this case

if (ftpWebResponse != null)

ftpWebResponse.Close();

if (responseStream != null)

responseStream.Close();

} // finally

} // Main()

public static void WriteStreamToFile(Stream stream, string fileName)

{

FileStream fileStream = null;

FileStream fileStreamCompressed = null;

MemoryStream memoryStream = null;

GZipStream compressedzipStream = null; // Use the memory stream for the compressed data.

int ibytes = 0;

Byte[] read = new Byte[4096]; //tcp packet size

Byte[] buffer = null;

string filenameCompressed = fileName + @".Gzip";

string filenameUnCompressed = fileName + @".txt";

String stringFormat = "";

try

{

if (stream.CanRead)

{

fileStream = new FileStream(filenameUnCompressed, FileMode.Create, FileAccess.Write);

ibytes = stream.Read(read, 0, 4096);

while (ibytes > 0)

{

fileStream.Write(read, 0, ibytes);

ibytes = stream.Read(read, 0, 4096);

}

fileStream.Flush();

fileStream.Close();

fileStream = null; // we will reopen for read so let's note it is closed.

Trace.WriteLine(filenameUnCompressed + " Written");

// Open uncompressed file read only

fileStream = new FileStream(filenameUnCompressed, FileMode.Open, FileAccess.Read, FileShare.Read);

buffer = new byte[fileStream.Length];

// The test file is small, so let's just read the entire thing

int count = fileStream.Read(buffer, 0, buffer.Length);

// Create a memory Stream to hold compressed bytes

memoryStream = new MemoryStream();

// Use the newly created memory stream for the compressed data.

compressedzipStream = new GZipStream(memoryStream, CompressionMode.Compress, true);

compressedzipStream.Write(buffer, 0, buffer.Length);

// Close the stream.

stringFormat = String.Format("Original file: {0}, size: {1} bytes\nCompressed file: {2}, size: {3} bytes"

, filenameUnCompressed, buffer.Length, filenameCompressed, memoryStream.Length);

//

// New in 2.0 enhanced Console functionality

//

Trace.WriteLine("New in 2.0 - enhanced Console functionality");

Console.BackgroundColor = ConsoleColor.Yellow;

Console.ForegroundColor = ConsoleColor.Blue;

Console.Title = "Compress FTP file";

Console.WriteLine(stringFormat);

Console.ResetColor();

//

// Read compressed memory stream and write it a compressed file

//

fileStreamCompressed = new FileStream(filenameCompressed, FileMode.Create, FileAccess.Write);

Byte[] bytes = new Byte[memoryStream.Length];

count = memoryStream.Read(bytes, 0, bytes.Length);

fileStreamCompressed.Write(bytes, 0, bytes.Length);

fileStreamCompressed.Flush();

fileStreamCompressed.Close(); // finaly would close but let's close it here to look at in explorer

Trace.WriteLine(filenameCompressed + " Written");

Console.WriteLine("Verify files " + fileName + ".* were written");

Console.BackgroundColor = ConsoleColor.DarkBlue;

Console.WriteLine("The Console can also prompt for input! Enter some Text and press enter");

Console.ResetColor();

string consoleInput = Console.ReadLine();

Console.BackgroundColor = ConsoleColor.DarkBlue;

Console.WriteLine("You entered: " + consoleInput);

Console.BackgroundColor = ConsoleColor.DarkRed;

Console.WriteLine("\nHAL agrees\n");

Console.ResetColor();

Console.WriteLine("Press Enter to Continue");

Console.ReadLine();

}

} // try

catch (Exception Ex)

{

Console.WriteLine(Ex.ToString()); // Log error somewhere

throw Ex; // rethrow the exception up the stack

} // catch

finally

{

// make sure to close system resources - file handles in this case

if (fileStream != null)

fileStream.Close();

if (compressedzipStream != null)

compressedzipStream.Close();

if (memoryStream != null)

memoryStream.Close();

if (fileStreamCompressed != null)

fileStreamCompressed.Close();

} // finally

} // WriteStreamToFile()

} // class Program

} // namespace FTP

Comments

  • Anonymous
    April 24, 2006
    This was my first webcast, so please be patient through the beginning.  It gets much better there...