简体   繁体   English

如何通过FTP循环压缩所有文件?

[英]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. 我正在尝试使用C#从ftp服务器压缩文件并将zip文件保存在本地pc中的某个地方。我管理了可以压缩文件的代码。 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. 但是我不确定如何遍历ftp来压缩所有文件(最大我应该压缩100个文件,而压缩文件不应超过100兆字节。

That is the code I used to connect to ftp and Zip 1 file. 那就是我用来连接ftp和Zip 1文件的代码。 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: 这就是压缩文件的ZipFile方法:

 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();
    }

Have you considered using a foreach loop with a list. 您是否考虑过使用带列表的foreach循环。

Try something like 尝试类似

Filenamelist = new List <string>();

Then run a foreach loop to populate the list with your files. 然后运行一个foreach循环,用文件填充列表。 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. 然后从那里运行相同类型的事物,并使用foreach循环压缩您的函数。

Just a thought. 只是一个想法。 Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM