Home > Net >  Compress multiple files into single zip. I want to user gives files path using commas separated
Compress multiple files into single zip. I want to user gives files path using commas separated

Time:12-27

I am tring this And its working properly. I want to user input files path using commas separated. I didn't understand how to take single single path and add to single zip file.

using System;
using System.IO;
using System.IO.Compression;

namespace ConvertMultipleFilesIntoZip
{
    internal class Program
    {
        static void Main(string[] args)
        {
            try
            {
                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))
                {
                    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");

                    /*archive.CreateEntryFromFile(@"d:\file1.txt", "file1.txt");
                    archive.CreateEntryFromFile(@"c:\Intel\file2.txt", "file2.txt");
                    archive.CreateEntryFromFile(@"d:\file1.txt", "file1.txt");
                    archive.CreateEntryFromFile(@"d:\sanju1.nupkg", "sanju1.nupkg");*/

                }
                Console.WriteLine("Zip file path is "   zipPath   " .");
            }
            catch (FileNotFoundException dirEx)
            {
                Console.WriteLine("File does not exist: "   dirEx.Message);
            }



        }

    }
} 


Question: Given files path with commas separated, I want to split the path and add every files add into a zip file.

CodePudding user response:

To split the file paths by commas and add each file to a zip file, you can use the following approach:

First, prompt the user for the file paths and store them in a string variable:

Console.WriteLine("Enter the file paths, separated by commas: ");
string filePaths = Console.ReadLine();

Split the file paths string into an array of strings using the Split method:

string[] paths = filePaths.Split(',');

Loop through the array of file paths and add each file to the zip file:

using (ZipArchive archive = ZipFile.Open(zipPath, ZipArchiveMode.Create))
{
    int i = 1;
    foreach (string path in paths)
    {
        archive.CreateEntryFromFile(path.Trim(), "file"   i   ".txt");
        i  ;
    }
}

This will split the file paths by commas, trim any leading or trailing whitespace from each path, and add each file to the zip file using a sequential file name (e.g. "file1.txt", "file2.txt", etc.).

  • Related