Thursday, November 25, 2010

How to download ".ZIP" file from FTP server and unzip it into a local folder!

I think you guys can easily understand the logic of below code to get good understanding of how to do it.

please go through the code sample and comment lines, if you have any further suggestion and explanations please send me [usgamage@gmail.com]


NOTE: Here I am using a third party ".dll" file named "ICSharpCode.SharpZipLib.Zip.dll" to unzip the folder.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Xml;
using ICSharpCode.SharpZipLib.Zip;
using System.Collections;

namespace XMLFileDownloader
{
public class XMLFileDownloader
{
//Variable declarations for FTP login credentials to the FTP ServerURI
string ftpUSER = string.Empty;
string ftpPassword = string.Empty;
string ftpServerURI = string.Empty;
string LocDirPath = string.Empty;
string ZIPFileName = string.Empty;
bool deleteZipFile = false;
int Count = 0;

///
/// Method to get Configuration values from SSIS config File
///

///
///
public int ReadConfigData(string ConfigFile)
{
XmlDocument xmldoc = null;

try
{
xmldoc = new XmlDocument();
xmldoc.Load(ConfigFile);
XmlNodeList nodeList = xmldoc.DocumentElement.ChildNodes;

foreach (XmlElement element in nodeList)
{
if (element.Name == "Configuration")
{
switch (element.Attributes["Path"].InnerText)
{
case "ftpUSER": ftpUSER = element.ChildNodes[0].InnerText.ToString().Trim().Length != 0 ? element.ChildNodes[0].InnerText.ToString() : "";
break;
case "ftpPassword": ftpPassword = element.ChildNodes[0].InnerText.ToString().Trim().Length != 0 ? element.ChildNodes[0].InnerText.ToString() : "";
break;
case "ftpServerURI": ftpServerURI = element.ChildNodes[0].InnerText.ToString().Trim().Length != 0 ? element.ChildNodes[0].InnerText.ToString() : "";
break;
case "LocDirPath": LocDirPath = element.ChildNodes[0].InnerText.ToString().Trim().Length != 0 ? element.ChildNodes[0].InnerText.ToString() : "";
break;
case "ZIPFileName": ZIPFileName = element.ChildNodes[0].InnerText.ToString().Trim().Length != 0 ? element.ChildNodes[0].InnerText.ToString() : "";
break;
case "DeleteZIPFile": deleteZipFile = Convert.ToBoolean(element.ChildNodes[0].InnerText.ToString().Trim());
break;
}
}
}
}

catch (Exception Exception)
{
Console.WriteLine("Configuration file invalid" + Exception.Message);
}

return GetFileList();
}

///
/// Initiate config values
///

public int IniConfig(string ftpUser, string ftpPwd, string uri, string locFilelocation, bool deletefile)
{
ftpUSER = ftpUser;
ftpPassword = ftpPwd;
ftpServerURI = uri;
LocDirPath = locFilelocation;
deleteZipFile = deletefile;

int _totDownload = GetFileList();
if (_totDownload > 0)
{
ZipFiles();
}
return _totDownload;
}

///
/// Methos to Get Download .xml and .csv files into
/// the local directory
///

///
public int GetFileList()
{
string[] downloadFiles;
StringBuilder result = new StringBuilder();
WebResponse response = null;
StreamReader reader = null;
try
{
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerURI));
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUSER, ftpPassword);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
reqFTP.Proxy = null;
reqFTP.KeepAlive = false;
reqFTP.UsePassive = true;
response = reqFTP.GetResponse();
reader = new StreamReader(response.GetResponseStream());

string line = reader.ReadLine();
while (line != null)
{
result.Append(line);
result.Append("\n");
line = reader.ReadLine();
}

if (result.ToString() != null)
{
result.Remove(result.ToString().LastIndexOf('\n'), 1);

downloadFiles = result.ToString().Split('\n');

foreach (string file in downloadFiles)
{
if (Path.GetExtension(file) == ".zip")
{
if (Download(file))
{
Count = Count + 1;
Console.WriteLine("Tot. Zip file Downloaded: " + Count);
}
}
}
}
}

catch (Exception ex)
{
if (reader != null)
{
reader.Close();
}
if (response != null)
{
response.Close();
}

Console.WriteLine(ex.Message);
;
}

finally
{
downloadFiles = null;
}

return Count;
}

///
/// This method list all ".zip" files and pass each filename fileunzip method
///

///
public void ZipFiles()
{
int totFiles = 0;
try
{
ArrayList _zipFileList = GenerateFileList(LocDirPath); // generate file list

if (_zipFileList.Count > 0)
{
foreach (string singleFile in _zipFileList)
{
UnZipFiles(singleFile);
totFiles = totFiles + 1;
Console.WriteLine("Total Files extracted: " + totFiles);
}
}
}
catch (Exception ex)
{
Console.WriteLine("Error in listing all '.ZIP' files into string array" + ex.Message);
}
}

///
/// This method list all .zip files in given directory
///

///
///
public static ArrayList GenerateFileList(string Dir)
{
ArrayList fils = new ArrayList();
bool Empty = true;

foreach (string file in Directory.GetFiles(Dir)) // add each file in directory
{
if (file.Contains(".zip"))
{
fils.Add(file);
Empty = false;
}
}

if (Empty)
{
if (Directory.GetDirectories(Dir).Length == 0)
// if directory is completely empty, add it
{
fils.Add(Dir + @"/");
}
}
return fils; // return file list
}

///
/// This method will extract the given .zip file into the local dir.
///

public void UnZipFiles(string zipPathAndFile)
{
ZipInputStream s = new ZipInputStream(File.OpenRead(zipPathAndFile));
ZipEntry theEntry;
string tmpEntry = String.Empty;
while ((theEntry = s.GetNextEntry()) != null)
{
string fileName = Path.GetFileName(theEntry.Name);
if (fileName != String.Empty)
{
if (theEntry.Name.IndexOf(".ini") < 0)
{
string[] _str = theEntry.Name.Split('/');
string _currentFile = _str[_str.Length - 1].ToString();

string fullPath = LocDirPath + "\\" + _currentFile;
fullPath = fullPath.Replace("\\ ", "\\");
string fullDirPath = Path.GetDirectoryName(fullPath);
if (!Directory.Exists(fullDirPath)) Directory.CreateDirectory(fullDirPath);
FileStream streamWriter = File.Create(fullPath);
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = s.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
else
{
break;
}
}
streamWriter.Close();
}
}
}
s.Close();
if (deleteZipFile)
File.Delete(zipPathAndFile);
}

///
/// Method to download individual file
///

///
public bool Download(string file)
{
bool isDownloaded = false;
try
{
string uri = ftpServerURI + "/" + file;
Uri serverUri = new Uri(uri);
if (serverUri.Scheme != Uri.UriSchemeFtp)
{
return isDownloaded;
}
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerURI + "/" + file));
reqFTP.Credentials = new NetworkCredential(ftpUSER, ftpPassword);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Proxy = null;
reqFTP.UsePassive = true;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream responseStream = response.GetResponseStream();
FileStream writeStream = new FileStream(LocDirPath + "\\" + file, FileMode.Create);
int Length = 2048;
Byte[] buffer = new Byte[Length];
int bytesRead = responseStream.Read(buffer, 0, Length);
while (bytesRead > 0)
{
writeStream.Write(buffer, 0, bytesRead);
bytesRead = responseStream.Read(buffer, 0, Length);
}
writeStream.Close();
response.Close();

isDownloaded = true;
}

catch (WebException e)
{
Console.WriteLine(e.Message, "Download Error");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message, "Download Error");
}

return isDownloaded;
}
}
}

1 comment:

  1. Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write ups thanks once again.
    flash to html5 converter

    ReplyDelete