2011-06-06 268 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"按钮点击