none
Unzip a file from ftp RRS feed

Answers

  • Hi Noam1248,

    Thank you for posting here.

    For your question,  please try the code below.

    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;
    
    namespace test4
    
    {
    
        class Program
    
        {
    
            static void Main(string[] args)
    
            {
    
                string username = "xxx";
    
                string password = "xxxx";
    
                string location = @"ftp://xxxxx";
    
                byte[] buffer = null;
    
                using (MemoryStream ms = new MemoryStream())
    
                {
    
                    FtpWebRequest fwrDownload = (FtpWebRequest)WebRequest.Create($"{location}/test1.zip");
    
                    fwrDownload.Method = WebRequestMethods.Ftp.DownloadFile;
    
                    fwrDownload.Credentials = new NetworkCredential(username, password);
    
                    using (FtpWebResponse response = (FtpWebResponse)fwrDownload.GetResponse())
    
                    using (Stream stream = response.GetResponseStream())
    
                    {
    
                        byte[] buf = new byte[1024];
    
                        int byteCount;
    
                        do
    
                        {
    
                            byteCount = stream.Read(buf, 0, buf.Length);
    
                            ms.Write(buf, 0, byteCount);
    
                        } while (byteCount > 0);
    
                        buffer = ms.ToArray();
    
                    }
    
                }
    
                using (MemoryStream ms = new MemoryStream(buffer))
    
                using (ZipArchive archive = new ZipArchive(ms))
    
                {
    
                    foreach (ZipArchiveEntry entry in archive.Entries)
    
                    {
    
                        FtpWebRequest fwrUpload = (FtpWebRequest)WebRequest.Create($"{location}/{entry.FullName}");
    
                        fwrUpload.Method = WebRequestMethods.Ftp.UploadFile;
    
                        fwrUpload.Credentials = new NetworkCredential(username, password);
    
                        byte[] fileContents = null;
    
                        using (StreamReader sr = new StreamReader(entry.Open()))
    
                        {
    
                            fileContents = Encoding.UTF8.GetBytes(sr.ReadToEnd());
    
                        }
    
                        if (fileContents != null)
    
                        {
    
                            fwrUpload.ContentLength = fileContents.Length;
    
                            using (Stream requestStream = fwrUpload.GetRequestStream())
    
                            {
    
                                requestStream.Write(fileContents, 0, fileContents.Length);
    
                            }
    
                        }
    
                    }
    
                }
    
                Console.ReadKey();
    
            }
    
        }
    
    }

    Result:

    Thursday, November 29, 2018 6:53 AM
    Moderator

All replies

  • Download it (with FtpWebRequest for example)

    Then unzip it with ZipFile

    Wednesday, November 28, 2018 5:20 PM
  • Hi Noam1248,

    Thank you for posting here.

    For your question,  please try the code below.

    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;
    
    namespace test4
    
    {
    
        class Program
    
        {
    
            static void Main(string[] args)
    
            {
    
                string username = "xxx";
    
                string password = "xxxx";
    
                string location = @"ftp://xxxxx";
    
                byte[] buffer = null;
    
                using (MemoryStream ms = new MemoryStream())
    
                {
    
                    FtpWebRequest fwrDownload = (FtpWebRequest)WebRequest.Create($"{location}/test1.zip");
    
                    fwrDownload.Method = WebRequestMethods.Ftp.DownloadFile;
    
                    fwrDownload.Credentials = new NetworkCredential(username, password);
    
                    using (FtpWebResponse response = (FtpWebResponse)fwrDownload.GetResponse())
    
                    using (Stream stream = response.GetResponseStream())
    
                    {
    
                        byte[] buf = new byte[1024];
    
                        int byteCount;
    
                        do
    
                        {
    
                            byteCount = stream.Read(buf, 0, buf.Length);
    
                            ms.Write(buf, 0, byteCount);
    
                        } while (byteCount > 0);
    
                        buffer = ms.ToArray();
    
                    }
    
                }
    
                using (MemoryStream ms = new MemoryStream(buffer))
    
                using (ZipArchive archive = new ZipArchive(ms))
    
                {
    
                    foreach (ZipArchiveEntry entry in archive.Entries)
    
                    {
    
                        FtpWebRequest fwrUpload = (FtpWebRequest)WebRequest.Create($"{location}/{entry.FullName}");
    
                        fwrUpload.Method = WebRequestMethods.Ftp.UploadFile;
    
                        fwrUpload.Credentials = new NetworkCredential(username, password);
    
                        byte[] fileContents = null;
    
                        using (StreamReader sr = new StreamReader(entry.Open()))
    
                        {
    
                            fileContents = Encoding.UTF8.GetBytes(sr.ReadToEnd());
    
                        }
    
                        if (fileContents != null)
    
                        {
    
                            fwrUpload.ContentLength = fileContents.Length;
    
                            using (Stream requestStream = fwrUpload.GetRequestStream())
    
                            {
    
                                requestStream.Write(fileContents, 0, fileContents.Length);
    
                            }
    
                        }
    
                    }
    
                }
    
                Console.ReadKey();
    
            }
    
        }
    
    }

    Result:

    Thursday, November 29, 2018 6:53 AM
    Moderator
  • working thanks!
    Thursday, November 29, 2018 12:16 PM