使用FtpWebRequest下载时,zip的大小会增加

时间:2013-10-01 17:29:28

标签: c# .net file-io ftpwebrequest

我有一些供内部使用的实用程序类,它从FTP服务器下载文件。在过去,所有这些都是平面文本文件,它没有任何问题。但是,我们现在有一些压缩文件,我希望它下载。当这个文件在本地写入时,大小从~80 Kb变为~140 Kb。为什么文件在写入过程中被破坏?为什么这不适用于普通文本文件?使此代码适用于所有文件类型需要进行哪些更改?

        private static string GetFile(string file, bool inBound)
        {
            string path;
            if (inBound)
                path = Config.settings["FTPIn"] + file;
            else
                path = Config.settings["FTPOut"] + file;

            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(path);
            request.Method = WebRequestMethods.Ftp.DownloadFile;

            request.Credentials = new NetworkCredential(Config.Settings["FTPUser"], Config.Settings["FTPPass"]);
            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            string file_contents = System.String.Empty;

            using (Stream responseStream = response.GetResponseStream())
            {
                using (StreamReader reader = new StreamReader(responseStream))
                {

                    try
                    {

                        file_contents = reader.ReadToEnd();
                    }
                    catch (Exception e)
                    {
                        ResultLogger.LogVerbose(trGuid, "Exception while getting file from FTP share.");
                        ResultLogger.LogVerbose(trGuid, e.Message);
                        ResultLogger.LogVerbose(trGuid, e.StackTrace);
                    }
                    reader.Close();
                    response.Close();
                }
            }

            return file_contents;

        }

请注意,没有运行时错误(至少在其他代码尝试解压缩文件之前没有),在写入过程中文件只是被破坏了。

此外,调用代码如下;

      string file_contents = GetFile(fileName, inBound);
      File.WriteAllText(@".\" + fileName, file_contents);
      return new FileInfo(@".\" + fileName);

2 个答案:

答案 0 :(得分:1)

您不希望将原始数据读入字符串,您应该将其读入字节数组。 .NET字符串由双字节字符组成。

您应该使用BinaryReader,或者更好,直接将其写入文件,而不是先使用CopyTo()

将其读入内存
private static void GetFile(string file, bool inBound)
{
    // Removed the FTP setup code

    using (Stream responseStream = response.GetResponseStream())
    {
        using (Stream outputStream = File.OpenWrite(@".\" + file))
        {
            try
            {
                responseStream.CopyTo(outputStream);
            }
            catch (Exception e)
            {
                // Removed Exception code
            }
        }
    }
}

答案 1 :(得分:0)

您的问题是在下载二进制文件时需要设置request.UseBinary = true;。否则,FTP流将无效二进制值转换为有效的字符串值以通过网络发送。这是导致大小增加以及下载时“损坏”文件的原因,因为您目前没有考虑到这种差异。代码应如下所示:

更新:我刚刚注意到你也将它作为字符串返回。我修改了代码以正确读取并填充缓冲区以返回二进制文件。

    private static byte[] GetFile(string file, bool inBound)
{
    string path;
    if (inBound)
        path = Config.settings["FTPIn"] + file;
    else
        path = Config.settings["FTPOut"] + file;

    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(path);
    request.Method = WebRequestMethods.Ftp.DownloadFile;
    request.UseBinary = true;

    request.Credentials = new NetworkCredential(Config.Settings["FTPUser"], Config.Settings["FTPPass"]);
    FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    byte[] file_contents = null;

    using (Stream responseStream = response.GetResponseStream())
    {
        byte[] buffer = new byte[2048];
        using (MemoryStream ms = new MemoryStream())
        {
            int readCount = responseStream.Read(buffer, 0, buffer.Length);
            while (readCount > 0)
            {
                ms.Write(Buffer, 0, readCount);
                readCount = responseStream.Read(buffer, 0, buffer.Length);
            }

            file_contents = ms.ToArray();
        }
    }

    return file_contents;
}