How to zip the multiple files into the folder until condition is met?

602 views Asked by At

Technology Used: C#, IonicZip library.

From the list of multiple log files(Let's say 10,000, each of reasonable amount of size). I have to zip these files in a folder. But then zipped folder's size must be approximately under 4MB. How can I have minimum possible number of zipped folders.

private static string ZipAndReturnFolderPath(IEnumerable<string> files, string saveToFolder)
{
    int listToSkip = 0;
    using (var zip = new ZipFile())
    {
        do
        {
            zip.AddFiles(files.Skip(listToSkip * 10).Take(10));
            zip.Save(saveToFolder);
            listToSkip++;
        }
        while ((new FileInfo(saveToFolder).Length < _lessThan4MB) && totalFilesRemaining > 0);                 
    }
    return saveToFolder;
}

Here, to make it concise, I have removed few lines of code. Parameter: files - holds the path of the total remaining files to be zipped(Don't worry about how I will maintain that). saveToFolder is the destination for the zipped folder(this will be unique each time the function is called).

I believe this works. I have checked the files it has been zipping and there I find no duplication. But, zipping files to a folder, checking the condition and then again repeating the same process for the next few files in the already zipped folder doesn't sound to be a good approach.

Am I doing anything wrong or is there any efficient way I can achieve this?

1

There are 1 answers

0
Leon Storey On

I think what you're after has been answered here already, using ZipOutputStream could be what you're after.