haf / DotNetZip.Semverd

Please use System.IO.Compression! A fork of the DotNetZip project without signing with a solution that compiles cleanly. This project aims to follow semver to avoid versioning conflicts. DotNetZip is a FAST, FREE class library and toolset for manipulating zip files. Use VB, C# or any .NET language to easily create, extract, or update zip files.
Other
545 stars 218 forks source link

what is the best way to use the lib Ionic.Zip to download files directly to a zip file #254

Closed insinfo closed 1 year ago

insinfo commented 2 years ago

what is the best way to use the lib Ionic.Zip to download files directly to a zip file

I made this code below but I don't know if it's the best approach


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ionic.Zip;
using Renci.SshNet;
using Renci.SshNet.Sftp;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {

            var connectionInfo = new ConnectionInfo("192.168.133.13", "user",
                                        new PasswordAuthenticationMethod("isaque.neves", "pass"));
            using (var client = new SftpClient(connectionInfo))
            {
                client.Connect();
                DownloadDirectoryAsZip(client,"/var/www/teste", @"C:\MyCsharpProjects\fsbackup\download.zip");               
            }
            Console.WriteLine("end");
        }

        public static void DownloadDirectoryAsZip(SftpClient sftpClient, string sourceRemotePath, string destLocalPath)
        {
            ZipFile zip = new ZipFile();          
            DownloadDirectoryAsZipRec(zip, sftpClient, sourceRemotePath);
            zip.Save(destLocalPath);
            zip.Dispose();

         }
        private static void DownloadDirectoryAsZipRec(ZipFile zip,SftpClient sftpClient, string sourceRemotePath)
        {

            IEnumerable<SftpFile> files = sftpClient.ListDirectory(sourceRemotePath);
            foreach (SftpFile file in files)
            {
                if ((file.Name != ".") && (file.Name != ".."))
                {
                    string sourceFilePath = sourceRemotePath + "/" + file.Name;           
                    if (file.IsDirectory)
                    {
                        DownloadDirectoryAsZipRec(zip,sftpClient, sourceFilePath);
                    }
                    else
                    {
                        var memoryStream = new MemoryStream();                                               
                        sftpClient.DownloadFile(sourceFilePath, memoryStream);                                                
                        zip.AddEntry(sourceFilePath, memoryStream);                        
                    }
                }
            }
        }

    }
}
jshergal commented 1 year ago

Thank you for the question, but please note that the issue section is for reporting bugs/errors and or enhancement requests for the library. For questions on how to use the library please refer to a site such as StackOverflow. With that being said, the code above looks decent, although depending on the size of the directories you are downloading you could run into memory issues as each of those MemoryStream objects is going to be held in memory until you write the zip file. If that becomes an issue you can look into downloading the files into a temporary directory and then zipping from that.