2011-06-06 87 views
1

1)我在從給定路徑讀取文本文件時遇到問題。如何從FTP提取zip文件後讀取文本文件路徑

2)當我從FTP下載的zip文件,我是解壓我的提取工作正常,

3)問題是,當我從ftp的,我下載的文件下載的文件是壓縮文件我解壓到一個文本文件,解壓後將其刪除的zip文件,當我

嘗試從指定路徑讀取文本文件,路徑讀取壓縮文件不是一個文本文件

4)我使用的代碼對於FTP和提取zipfile是,

private void DownloadMonth(string filePath, string fileName) 
    { 
     FtpWebRequest reqFTP; 
     try 
     { 
      if (!Directory.Exists(filePath)) 
      { 
       Directory.CreateDirectory(filePath); 

      } 

      FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite); 
      reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + ftpMonth + "/" + fileName)); 
      reqFTP.Method = WebRequestMethods.Ftp.DownloadFile; 
      reqFTP.UseBinary = true; 
      reqFTP.KeepAlive = true; 
      reqFTP.UsePassive = true; 
      reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); 
      FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); 
      Stream ftpStream = response.GetResponseStream(); 
      long c1 = response.ContentLength; 
      int bufferSize = 2048000; 
      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.Close(); 
      response.Close(); 
      string Path1 = (string)(Application.StartupPath + "\\TEMP\\TEMP_BACKFILL_" + "\\" + fileName); 
      DirectoryInfo di = new DirectoryInfo(Path1); 
      FileInfo fi = new FileInfo(Path1); 
      Decompress(fi); 
      File.Delete(Path1); 

     } 
     catch (Exception ex) 
     { 

     } 
    } 

代碼解壓縮

public static void Decompress(FileInfo fi) 
    { 
     // Get the stream of the source file. 
     using (FileStream inFile = fi.OpenRead()) 
     { 
      // Get original file extension, for example "doc" from report.doc.gz. 
      string curFile = fi.FullName; 
      string origName = curFile.Remove(curFile.Length - fi.Extension.Length); 

      //Create the decompressed file. 
      //using (FileStream outFile = File.Create(fi.FullName + "")) 

      //using (FileStream outFile = File.Create(System.Text.RegularExpressions.Regex.Replace(fi.FullName, ".txt$", "") + "")) 
      using (FileStream outFile = File.Create(origName + ".txt")) 
      { 
       using (GZipStream Decompress = new GZipStream(inFile, 
         CompressionMode.Decompress)) 
       { 
        //Copy the decompression stream into the output file. 
        byte[] buffer = new byte[4096]; 
        int numRead; 
        while ((numRead = Decompress.Read(buffer, 0, buffer.Length)) != 0) 
        { 
         outFile.Write(buffer, 0, numRead); 
        } 
        Console.WriteLine("Decompressed: {0}", fi.Name); 

       } 
      } 
     } 
    } 

我用它來下載從FTP文本文件和讀取文本文件中的代碼是

    private void button1_Click(object sender, EventArgs e) 
        { 
         this.DownloadMonth(a, name_Month); 
         string Path1 = (string)(Application.StartupPath + "\\TEMP\\TEMP_BACKFILL_" + "\\" + name_Month); 
         StreamReader reader1 = File.OpenText(Path1); 
         string str = reader1.ReadToEnd(); 
         reader1.Close(); 
         reader1.Dispose(); 
        } 

會有很大的升值,如果有人能解決我的問題。

在此先感謝

回答

0

它看起來像你想的不是文本文件讀你button1_Click方法二進制文件。這只是給它一個錯誤的文件名。你可以嘗試只使用:

string Path1 = Application.StartupPath + "\\TEMP\\TEMP_BACKFILL_\\" 
       + name_Month + ".txt"; 

(在button1_Click

...但真的是你應該能夠診斷什麼錯自己:

  • 是文件被正確下載?
  • 解壓縮工作嗎?
  • 之後的文本文件(在文件系統上)看起來好嗎?

如果一切都工作到這一點,然後下載並解壓縮代碼是無關緊要的,而這顯然只是讀中失敗單擊處理一部分。如果文本文件沒有正確創建,那麼在這個過程的早期顯然有些問題。

能夠自己診斷這類事情很重要 - 您有一個清晰的3步過程,並且只需查看文件系統就可以輕鬆找到該過程的結果,因此第一個要做的事情是弄清楚哪一點是失敗的,只能仔細觀察。

另外,在不同的地方,您可以手動調用Close來處理諸如流和讀者之類的內容。不要這樣做 - 請改用using聲明。您還應該看看File.ReadAllText作爲閱讀整個文本文件的更簡單的替代方法。

+0

是的,它仍然在讀一個zip文件 – 2011-06-06 05:30:15

+0

@G岜沙:這不會是,與我所做的更改。注意最後的'+「.txt」'。 – 2011-06-06 05:31:49

+0

是的,我注意到它,它仍然在讀文本文件,但它仍然在閱讀zip文件 – 2011-06-06 05:32:03

0

嘗試string Path1 = origName + ".txt"按鈕點擊