开发者

To download a zipped file from FTP

开发者 https://www.devze.com 2023-01-05 12:21 出处:网络
I am trying to do开发者_运维技巧 this using C#(Winforms). The code I am using isgiving me a string as an output, but I need to have a zipped file.

I am trying to do开发者_运维技巧 this using C#(Winforms). The code I am using is giving me a string as an output, but I need to have a zipped file.

I am using the following code

try
{
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(
                            "ftp:SITENAME/FILENAME.zip");
    request.Method = WebRequestMethods.Ftp.DownloadFile;

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

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

    Stream responseStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(responseStream);
    //StreamWriter writer = new StreamWriter(responseStream);

    MessageBox.Show(reader.ReadtoEnd);

    MessageBox.Show("Download Complete, status {0}" + response.StatusCode);

    reader.Close();
    response.Close();
}
catch (NotSupportedException ne)
{
    MessageBox.Show(ne.Message);
}

I have an idea that I have to use Open source library from SharpZipLib to convert this string to a zipped file. But,I cannot find any sample code to show me how to do that.

I would really appreciate if someone can guide me through the process.

Thanks,

Sidhanshu


This might do just that.

using ICSharpCode.SharpZipLib.BZip2;

    public static string Unzip(byte[] compressedbytes)
            {
                string result;
                MemoryStream m_msBZip2 = null;
                BZip2InputStream m_isBZip2 = null;

                m_msBZip2 = new MemoryStream(compressedbytes);
                // read final uncompressed string size stored in first 4 bytes
                using (BinaryReader reader = new BinaryReader(m_msBZip2, System.Text.Encoding.ASCII))
                {
                    Int32 size = reader.ReadInt32();

                    m_isBZip2 = new BZip2InputStream(m_msBZip2);
                    byte[] bytesUncompressed = new byte[size];
                    m_isBZip2.Read(bytesUncompressed, 0, bytesUncompressed.Length);
                    m_isBZip2.Close();
                    m_msBZip2.Close();

                    result = Encoding.ASCII.GetString(bytesUncompressed, 0, bytesUncompressed.Length);

                    reader.Close();
                }

                return result;
            }

            public static byte[] Zip(string sBuffer)
            {

                byte[] result;

                using (MemoryStream m_msBZip2 = new MemoryStream())
                {
                    Int32 size = sBuffer.Length;
                    // Prepend the compressed data with the length of the uncompressed data (firs 4 bytes)
                    using (BinaryWriter writer = new BinaryWriter(m_msBZip2, System.Text.Encoding.ASCII))
                    {
                        writer.Write(size);

                        using (BZip2OutputStream m_osBZip2 = new BZip2OutputStream(m_msBZip2))
                        {
                            m_osBZip2.Write(Encoding.ASCII.GetBytes(sBuffer), 0, sBuffer.Length);
                            m_osBZip2.Close();
                        }

                        writer.Close();
                        result = m_msBZip2.ToArray();

                        m_msBZip2.Close();
                    }
                }

                return result;
            }
0

精彩评论

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