Search code examples
c#zip

How to add multiple files in one zip file through the code


I try to add multiple files into one single zip files using console application in .NET.

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConvertMultipleFilesIntoZip
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //string SourcePath1 = @"d:\SRS_9.4.7.25_1033";
            string SourcePath2 = @"d:\sanju2.nuspec";
            string SourcePath1 = @"d:\sanju1.nuspec";
            string DestinationPath = @"d:\Result9.zip";
            if (File.Exists(DestinationPath))
            {
                File.Delete(DestinationPath);
            }
            string[] Files = { SourcePath1, SourcePath2 };
            foreach (string variableName in Files)
            {

                ZipFile.CreateFromDirectory(variableName, DestinationPath);

            }
            Console.WriteLine(DestinationPath);

        }
    }
}

I got this error message...

Unhandled Exception: System.IO.DirectoryNotFoundException: Could not find a part of the path 'd:\sanju1.nuspec'. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileSystemEnumerableIterator1.CommonInit() at System.IO.FileSystemEnumerableIterator1..ctor(String path, String originalUserPath, String searchPattern, SearchOption searchOption, SearchResultHandler1 resultHandler, Boolean checkHost) at System.IO.DirectoryInfo.EnumerateFileSystemInfos(String searchPattern, SearchOption searchOption) at System.IO.Compression.ZipFile.DoCreateFromDirectory(String sourceDirectoryName, String destinationArchiveFileName, Nullable1 compressionLevel, Boolean includeBaseDirectory, Encoding entryNameEncoding) at System.IO.Compression.ZipFile.CreateFromDirectory(String sourceDirectoryName, String destinationArchiveFileName) at ConvertMultipleFilesIntoZip.Program.Main(String[] args) in C:\Users\sanjeev_kushvaha\source\repos\ConvertMultipleFilesIntoZip\ConvertMultipleFilesIntoZip\Program.cs:line 27


Solution

  • In this code, I added multiple files from various location of machine into a specific location.

        using System;
        using System.IO;
        using System.IO.Compression;
        namespace ConvertMultipleFilesIntoZip
        {
           internal class Program
           {
              static void Main(string[] args)
              {
               
                    Console.WriteLine("..........Convert multiple files into a zip file...........");
                    Console.WriteLine();
                    Console.WriteLine();
                    string zipPath = @"d:\result" + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".zip";
    
                    using (ZipArchive archive = ZipFile.Open(zipPath, ZipArchiveMode.Create))
                    {
                        try
                        {
                            Console.WriteLine("Enter the 1st file path, Which you want to add: ");
                            string sourcePath1 = Console.ReadLine();
                            Console.WriteLine("Enter the 2st file path, Which you want to add: ");
                            string sourcePath2 = Console.ReadLine();
                            Console.WriteLine("Enter the 3st file path, Which you want to add: ");
                            string sourcePath3 = Console.ReadLine();
                            Console.WriteLine("Enter the 4st file path, Which you want to add: ");
                            string sourcePath4 = Console.ReadLine();
                            archive.CreateEntryFromFile(sourcePath1, "file1.txt");
                            archive.CreateEntryFromFile(sourcePath2, "file2.txt");
                            archive.CreateEntryFromFile(sourcePath3, "file1.txt");
                            archive.CreateEntryFromFile(sourcePath4, "sanju1.nupkg");
    
                      
                        }
                        catch (DirectoryNotFoundException dirEx)
                        {
                            Console.WriteLine("Directory not found: " + dirEx.Message);
                        }
                    }
                    Console.WriteLine("Zip file path is " + zipPath + " .");     
    
            }
    
        }
    }
    

    In this problem, First install System.IO.Compression.ZipFile from NuGet package manager then I used ZipArchive method which is present in System.IO.Compression. zipPath is path where I want to store our zip file. ZipArchiveMode.Create is basically showing what I want do with zip file.