-2

I try to send by FTP one .zip file. First I take one .txt file and added to archive .zip.

When I try to send this file everything is OK. But when a file is coming on the machine and I want to uncompress the .zip file is broken. On the sender machine .zip file is OK. Only on the ftp machine is broken.

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;


    namespace SEND_Plovdiv
    {

    public class WebRequestGetExample
    {

        public static void Main()
        {
            string[] lineOfContents = File.ReadAllLines(@"C:\\M\send.txt");

            string username = "";
            string password = "";
            foreach (var line in lineOfContents)
            {
                string[] tokens = line.Split(',');
                string user = tokens[0];
                string pass = tokens[1];

                username = user;
                password = pass;
            }

            string pathFile = @"C:\M\Telegrami\";
            string zipPath = @"C:\M\Plovdiv.zip";

            if (File.Exists(zipPath))
            {
                File.Delete(zipPath);
            }

            ZipFile.CreateFromDirectory(pathFile, zipPath, CompressionLevel.Fastest, false);


            // Get the object used to communicate with the server.
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://199.199.199.199/Plovdiv.zip");
                request.Method = WebRequestMethods.Ftp.UploadFile;

                // This example assumes the FTP site uses anonymous logon.  
                request.Credentials = new NetworkCredential(username, password);

                // Copy the contents of the file to the request stream.  
                StreamReader sourceStream = new StreamReader(@"C:\M\Plovdiv.zip");
                byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());

                sourceStream.Close();

                request.ContentLength = fileContents.Length;

                Stream requestStream = request.GetRequestStream();
                requestStream.Write(fileContents, 0, fileContents.Length);
                requestStream.Close();

                FtpWebResponse response = (FtpWebResponse)request.GetResponse();

                Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
                Console.ReadKey();

                response.Close();


        }
    }
}

I dont know what exactly do this part of code:

StreamReader sourceStream = new StreamReader(@"C:\M\Plovdiv.zip");
Let's Enkindle
  • 57
  • 2
  • 17
Ben Johnson
  • 753
  • 1
  • 9
  • 18
  • 1
    What is the **exact** value of `ftpServerIP`? What do you think `ConfigurationManager.AppSettings["199.199.199.199"];` does? – mjwills Feb 06 '18 at 12:04
  • I delete real IP address from this code and put 199.199.199.199 .. – Ben Johnson Feb 06 '18 at 12:06
  • You need to learn to debug your own code. We cannot magically guess how your app.config looks like and see what values are being used at runtime – Camilo Terevinto Feb 06 '18 at 12:09
  • I edit my question, sorry – Ben Johnson Feb 06 '18 at 12:11
  • There are no settings in you app.config file. So I'm guessing that the problem is, that you have some invalid values of ip, username and password. You should also use [settings](https://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/using-application-settings-and-user-settings) and not appSettings, which are considered obsolete. – Matyas Feb 06 '18 at 12:17
  • What is the **exact** value of `ftpServerIP`? _There is no point hiding it from us, the value is invalid anyway._ – mjwills Feb 06 '18 at 12:18
  • Should that be `"ftp://" + ftpServerIP + "/" + zipPath;` ? note the number of `/` – Marc Gravell Feb 06 '18 at 12:19
  • Sounds he's able to connect and access the Zip file but the file is corrupt. That would indicate to me that the connection info is good. – Troy Turley Feb 08 '18 at 02:36

1 Answers1

0

Switch from using the StreamReader to BinaryReader. Zip files are binary files not UTF8.

Here's a sample:

using (FileStream fs = File.Open(@"c:\1.bin",FileMode.Open))
            {
                    byte[] data = new BinaryReader(fs).ReadBytes((int)fs.Length);
                    Encoding.getstring....
            }

StreamReader vs BinaryReader?

Troy Turley
  • 659
  • 10
  • 28