开发者

How to read a text file path after extracting the zip file from FTP

1) I am having a problem in reading a text file from my given path .

2) When i download the zip file from ftp i extracted it my extracting is working fine ,

3) The problem is when i download the file from ftp the file which i download is zip file i extract it to a text file and delete the zip file after extracting it and when i

try to read text file from the given path, the path reads the zip file not a text file

4) the code i am using for FTP and extracting the zipfile is,

    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.R开发者_如何学运维eadWrite);
            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)
        {

        }
    }

Decompress Code

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

                }
            }
        }
    } 

The code i use to download the text file from FTP and read the text file is

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

There would be a great appreciation if someone can solve my problem.

Thanks In Advance


It looks like you're trying to read the binary file from your button1_Click method, instead of the text file. That's just a matter of giving it the wrong filename. You could try just using:

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

(in button1_Click)

... but really you should be able to diagnose what's going wrong yourself:

  • Is the file being downloaded correctly?
  • Is the decompression working?
  • Does the text file look okay afterwards (on the file system)?

If everything's working up to that point, then the download and decompression code is irrelevant, and it's obviously just the reading part in the click handler which is failing. If the text file isn't being created properly, then obviously something is wrong earlier in the process.

It's important to be able to diagnose this sort of thing yourself - you've got a clear 3-step process, and the results of that process should be easy to find just by looking on the file system, so the first thing to do is work out which bit is failing, and only look closely at that.

As an aside, in various places you're manually calling Close on things like streams and readers. Don't do that - use a using statement instead. You should also look at File.ReadAllText as a simpler alternative for reading the whole of a text file.


try string Path1 = origName + ".txt" in button click

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜