从FTP下载压缩文件

时间:2010-06-30 22:10:10

标签: c#

我正在尝试使用C#(Winforms)执行此操作。 我正在使用的代码是给我一个字符串作为输出,但我需要一个压缩文件。

我正在使用以下代码

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);
}

我知道我必须使用SharpZipLib的开源库将此字符串转换为压缩文件。但是,我找不到任何示例代码来告诉我如何做到这一点。

如果有人能指导我完成整个过程,我将非常感激。

谢谢,

Sidhanshu

1 个答案:

答案 0 :(得分:1)

这可能就是这样。

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;
            }