2013-07-23 68 views
0

我想從ftp服務器(使用C#)壓縮文件並將zip文件保存在我的本地pc的某處。我管理了一個可壓縮文件的代碼。但我不知道如何通過ftp來壓縮所有文件(最多我應該Zip 100文件和Zipfile不應超過100兆字節。如何通過FTP循環來壓縮所有文件?

這是我用來連接到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(); 
    } 
+0

看到這個答案,似乎是適當的。 http://stackoverflow.com/questions/4733707/zip-subfolders-using-zipoutputstream – priehl

回答

1

你有沒有使用foreach循環與列表考慮

嘗試類似於

Filenamelist = new List <string>(); 

然後運行foreach循環以用您的文件填充列表。像

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

,並從那裏的東西運行相同類型的東西用foreach循環來壓縮您的 功能。

只是一個想法。乾杯!

相關問題