1)我在从给定路径读取文本文件时遇到问题。
2)当我从ftp下载zip文件时,我提取它,我的提取工作正常,
3)问题是当我从ftp下载文件时我下载的文件是zip文件我将其解压缩为文本文件并在解压缩后删除zip文件并且当我
尝试从给定路径读取文本文件,路径读取zip文件而不是文本文件
4)我用于FTP的代码和解压缩文件是
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 :(得分:0)
您似乎正在尝试从button1_Click
方法读取二进制文件,而不是 text 文件。这只是给它错误的文件名。您可以尝试使用:
string Path1 = Application.StartupPath + "\\TEMP\\TEMP_BACKFILL_\\"
+ name_Month + ".txt";
(在button1_Click
)
...但是你应该能够自己诊断出错:
如果一切正常,那么下载和解压缩代码就无关紧要了,显然只是点击处理程序中的读取部分失败了。如果没有正确创建文本文件,那么显然在此过程中出现了问题。
能够自己诊断这类事情很重要 - 你有一个明确的三步过程,只需查看文件系统就可以轻松找到该过程的结果,所以首先要做的事情是要做的就是弄清楚哪一点失败了,只关注那个。
顺便说一句,在不同的地方,您可以手动调用Close
来查看流和读者等内容。不要这样做 - 改为使用using
语句。您还应该将File.ReadAllText
视为阅读整个文本文件的更简单的替代方法。
答案 1 :(得分:0)
点击按钮
尝试string Path1 = origName + ".txt"