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

时间:2013-07-23 18:51:45

标签: c# zip

我正在尝试从ftp服务器(使用C#)压缩文件并将zip文件保存在我的本地pc中。我管理了一个可以压缩文件的代码。但我不知道如何循环ftp来压缩所有文件(最大我应该Zip 100文件和Zipfile不应超过100兆字节。

这是我用来连接ftp和Zip 1文件的代码。如何压缩几个文件?

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



        }

这就是压缩文件的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();
    }

1 个答案:

答案 0 :(得分:1)

您是否考虑过使用带有列表的foreach循环?

尝试类似

的内容
Filenamelist = new List <string>();

然后运行foreach循环以使用您的文件填充列表。像

这样的东西
 foreach (//listobject in //listname)
{
  Filenamelist.Add(file.Name).
}

并从那里运行相同类型的东西与foreach循环拉链你的  功能

只是一个想法。干杯!