Unzip from FTP

Jan 10, 2011 at 11:00 AM

Hi I am trying to save a .zip file from the FTP server (Compressed and send their by me using DotNetZip) but whenever the file is saved it saves it as 0kb, the file is supposed to be 287kb

I have tested this on my local Machine and it works

Here is the code for the compress and upload

private void btnExportTestData_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Busy Uploading...");
            XmlDocument data = new XmlDocument();
            DataRow rowData = TestData().Rows[0];
            data.LoadXml(rowData[0].ToString());
            data.Save("C:/PATH/VCI_TestDataMB.xml");

            ZipDocument("C:/PATH/VCI_TestDataMB.xml", "C:/PATH/VCI_TestDataMB.zip");
            ExportToFTP("C:/PATH/VCI_TestDataMB.zip", "/FOLDER/PATH/VCI_TestDataMB.zip");
        }

private void ExportToFTP(string sFileName, string sSaveAs)
        {
            string sAdress = ftp://ADRESS;

            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(sAdress + sSaveAs);
            request.Method = WebRequestMethods.Ftp.UploadFile;
            request.Credentials = new NetworkCredential("USERNAME", "PASSWORD");

            Stream ftpStream = request.GetRequestStream();
            FileStream file = File.OpenRead(sFileName);

            int length = 1024;
            byte[] buffer = new byte[length];
            int bytesRead = 0;

            do
            {
                bytesRead = file.Read(buffer, 0, length);
                ftpStream.Write(buffer,0,bytesRead);
            } while (bytesRead != 0);

            file.Close();
            ftpStream.Close();

            MessageBox.Show("Upload Complete!");

        }

and here is the code for the download, unzip and save

        private void ImportTestXML()
        {
            string sAdress = ftp://ADRESS;

            FtpWebRequest request = (FtpWebRequest) WebRequest.Create(sAdress + "/FOLDER/PATH/VCI_TestDataMB.zip");
            //request.UseBinary = true;
            request.Method = WebRequestMethods.Ftp.DownloadFile;

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

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

            Stream responseStream = response.GetResponseStream();
            FileStream FileS = File.OpenWrite("C:/PATH/VCI_TestDataMB.zip");
            StreamReader reader = new StreamReader(responseStream);

            FileS.Write(StrToByteArray(reader.ReadToEnd()),0,0);
            FileS.Close();

            UnZipCocument("C:/PATH/VCI_TestDataMB.zip", "C:/PATH");

            reader.Close();
            response.Close();  
        }

Oh yes, it is a WinApp.

 

Any ideas why it would do that

Jan 10, 2011 at 1:23 PM

Hi

Don't just close streams, wrap them in using clauses or if you must call dispose directly. Always check whether objects implements IDisposable. You must always either wrap them in using clauses or call Dispose when you're done with them. You might have been lucky that it worked on your machine.

private void ExportToFTP(string sFileName, string sSaveAs)
{
    string sAdress = ftp: //ADRESS;

    FtpWebRequest request = (FtpWebRequest) WebRequest.Create(sAdress + sSaveAs);
    request.Method = WebRequestMethods.Ftp.UploadFile;
    request.Credentials = new NetworkCredential("USERNAME", "PASSWORD");

    using (Stream ftpStream = request.GetRequestStream())
    {
        using (FileStream file = File.OpenRead(sFileName))
        {

            int length = 1024;
            byte[] buffer = new byte[length];
            int bytesRead = 0;

            do
            {
                bytesRead = file.Read(buffer, 0, length);
                ftpStream.Write(buffer, 0, bytesRead);
            } while (bytesRead != 0);

            file.Close();
        }
        ftpStream.Close();
    }

    MessageBox.Show("Upload Complete!");
}

Remember to do the same to ImportTestXML:

 

private void ImportTestXML()
{
    string sAdress = ftp://ADRESS;

    FtpWebRequest request = (FtpWebRequest) WebRequest.Create(sAdress + "/FOLDER/PATH/VCI_TestDataMB.zip");
    //request.UseBinary = true;
    request.Method = WebRequestMethods.Ftp.DownloadFile;

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

    using(FtpWebResponse response = (FtpWebResponse)request.GetResponse())
    {
        using(Stream responseStream = response.GetResponseStream())
        {
            using(StreamReader reader = new StreamReader(responseStream))
            {
                using (FileStream FileS = File.OpenWrite("C:/PATH/VCI_TestDataMB.zip"))
                {
                    FileS.Write(StrToByteArray(reader.ReadToEnd()), 0, 0);
                    FileS.Close();
                }

                UnZipCocument("C:/PATH/VCI_TestDataMB.zip", "C:/PATH");
                reader.Close();
            }
        }
        response.Close();
    }
}

 

 

Jan 10, 2011 at 2:10 PM
Hi CZietsman thanks for the help, I changed my code as per your example and still the same happens.

I have reason to believe that the problem may be in the following line in my ImportTestXML()

FileS.Write(StrToByteArray(reader.ReadToEnd()), 0, 0);

The 0kb file is the one created with

  using (FileStream FileS = File.OpenWrite("C:/PATH/VCI_TestDataMB.zip"))

but the program gets an error while trying to Write the file to the FileStream.

I do not have MS Visual Studio on the Production server where the app is running so I am unable to debug the code.

File.OpenWrite takes 3 Parameters

1. byte[] buffer
2. int offset
3. int count

StrToByteArray() looks as follow

        public static byte[] StrToByteArray(string str)
        {
            return Encoding.ASCII.GetBytes(str);
        }
Any futher advise?
Jan 11, 2011 at 11:52 AM

I don't think you want to use StreamReader here, since zip files are binary. I'm guessing that the reader is terminating early because of this. You'll probably have to uncomment "request.UseBinary = true;".

Try pumping the response stream manually using something like:

using (var responseStream = response.GetResponseStream())
{
    using (var fileStream = File.OpenWrite("C:/PATH/VCI_TestDataMB.zip"))
    {
        int b;
        while((b = responseStream.ReadByte()) != -1)
        {                            
            fileStream.WriteByte((byte) b);
        }
    }
}
This might not be the best solution, but at least it'll help with isolating your problem. I don't think it is a problem with DotNetZip.