0

我正在尝试从 ftp 服务器压缩文件(使用 C#)并将压缩文件保存在我的本地 pc 中的某个位置。我管理了一个可以压缩文件的代码。但我不确定如何通过 ftp 循环压缩所有文件(我最多应该压缩 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();
    }
4

1 回答 1

1

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

尝试类似的东西

Filenamelist = new List <string>();

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

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

并从那里使用 foreach 循环运行相同类型的东西来压缩你的函数。

只是一个想法。干杯!

于 2013-07-23T19:04:55.983 回答