Alma
Alma

Reputation: 4390

How I can loop through FTP to zip all the files?

I am trying to Zip files from ftp server(using C#) and save zip file somewhere in my Local pc.I managed a code that could zip a file. But I am not sure how to loop through the ftp to zip all the files (max I should Zip 100 files and Zipfile should not exceed 100 megabytes.

That is the code I used to connect to ftp and Zip 1 file. How I can Zip several Files?

 public void ProcessZipRequest(string strQueueID, string strBatchID, string strFtpPath)
    {


        int intReportCnt = 0;

        string strZipFileName = "Order-" + strBatchID + "-" + strQueueID + "-" + DateTime.Now.ToString("MM-dd-yyyy-HH-mm") + ".zip";
        strZipFileName = SafeFileName(strZipFileName);

        //MemoryStream ms = new MemoryStream();
        FileStream ms = new FileStream(@"c:\ZipFiles\nitest.zip", FileMode.Create);
        ZipOutputStream oZipStream = new ZipOutputStream(ms); // create zip stream

        oZipStream.SetLevel(9); // maximum compression

        intReportCnt += 1;

        string strRptFilename;

        if (strQueueID != null)
        {

            MemoryStream outputStream = new MemoryStream();

            // Get file path

         string ftpFilePath = @"ftp://12.30.228.20/AOTest/Images/Report/11/595/45694/62_s.jpg";



            // That is where I get 1 file from ftp (How loop here?)
            strRptFilename = ftpFilePath.Substring(ftpFilePath.LastIndexOf("/") + 1);

            FtpWebRequest reqFTP = (FtpWebRequest)System.Net.WebRequest.Create(ftpFilePath);
            reqFTP.UseBinary = true;
            reqFTP.KeepAlive = false;
            reqFTP.Credentials = new NetworkCredential("username", "password");
            reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
            reqFTP.Proxy = null;

            FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
            Stream ftpStream = response.GetResponseStream();

            long cl = response.ContentLength;
            int bufferSize = 2048;
            int readCount;
            byte[] buffer = new byte[bufferSize];

            readCount = ftpStream.Read(buffer, 0, bufferSize);
            while (readCount > 0)
            {
                outputStream.Write(buffer, 0, readCount);
                readCount = ftpStream.Read(buffer, 0, bufferSize);
            }

            ftpStream.Close();

            outputStream.Position = 0;

            //Where I zip the files 
            ZipFile(ref outputStream, strRptFilename, ref oZipStream);

            ftpStream.Close();
            outputStream.Close();


            if (response != null)
            {
                response.Close();
            }



        }

And that is ZipFile Method that zip the file:

 public void ZipFile(ref MemoryStream msFile, string strFilename, ref ZipOutputStream oZipStream)
    {
        ZipEntry oZipEntry = new ZipEntry(strFilename);
        oZipEntry.DateTime = DateTime.Now;
        oZipEntry.Size = msFile.Length;

        oZipStream.PutNextEntry(oZipEntry);

        StreamUtils.Copy(msFile, oZipStream, new byte[4096]);

        oZipStream.CloseEntry();
    }

Upvotes: 0

Views: 805

Answers (1)

trueamerican420
trueamerican420

Reputation: 221

Have you considered using a foreach loop with a list.

Try something like

Filenamelist = new List <string>();

Then run a foreach loop to populate the list with your files. Something like

 foreach (//listobject in //listname)
{
  Filenamelist.Add(file.Name).
}

and from there run the same type of thing with a foreach loop to zip your function.

Just a thought. Cheers!

Upvotes: 1

Related Questions