0

Good afternoon, please tell me how can I add files with the same name to the archive? To be like copying, the file becomes file(1).* and there are two files file and file(1) . I am using Ionic.Zip

string BackupDir = @"C:\Users\Desktop\dir\backup.zip";
string PathToFolder = @"C:\Users\Desktop\dir";
string[] AllFiles = Directory.GetFiles(PathToFolder, "*.*", SearchOption.AllDirectories);
using (ZipFile zip = new ZipFile(BackupDir, Encoding.UTF8))
{

    foreach (string file in AllFiles)
    {
        try
        {
            DateTime FileCreationTime = File.GetCreationTime(file);
            if (FileCreationTime >= DateTime.Now - new TimeSpan(60, 0, 0, 0))
            {
                Console.WriteLine(file);
                zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestSpeed;
                zip.AddFile(file, "");
            }

        }    

        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
        zip.Save(BackupDir);    
    }
}
Crowcoder
  • 11,250
  • 3
  • 36
  • 45
Alex
  • 3
  • 2
  • Does this answer your question? [Add Folders to Root of Zip Using Ionic Zip Library](https://stackoverflow.com/questions/13129141/add-folders-to-root-of-zip-using-ionic-zip-library) – Markus Meyer Aug 18 '22 at 11:19
  • Unfortunately not, I need to add two files so that one is renamed to file(1).* and the archive contains file.* and file(1).* – Alex Aug 18 '22 at 11:25
  • Are you sure that files with the same name exists in the same directory? – Markus Meyer Aug 18 '22 at 11:26
  • @MarkusMeyer There may or may not be files, but if a file occurs with the same name in an existing folder tree, then it needs to be renamed – Alex Aug 18 '22 at 11:29
  • `AddFile` has a return value change its `FileName` property eg. `zip.AddFile(file, "").FileName = "Hello World";` see here: https://stackoverflow.com/questions/6413224/dotnetzip-rename-file-entry-in-zip-file-while-compressing (same applies to Ionic.Zip) – Rand Random Aug 18 '22 at 11:44
  • I'm not familiar with ionic zip, but based on their questionable documentation you could try [checking for the file](https://documentation.help/DotNetZip/fec7a4e6-c9d6-93cf-18fa-bcbf50de7d5e.htm) in the archive and if it exists use [AddEntry](https://documentation.help/DotNetZip/7bc513f6-c21c-6a02-3964-f2a571308a33.htm) instead of AddFile so that you can give it a new name. – Crowcoder Aug 18 '22 at 11:45
  • @Crowcoder my main problem is that how do i know if the archive already has a file with the same name? friends thank you for your help – Alex Aug 18 '22 at 11:58
  • @Alex you use the `ContainsEntry` method that I linked to. – Crowcoder Aug 18 '22 at 12:00

2 Answers2

0

Try this:

//keep track of fileNames
var fileNames = new HashSet<string>(StringCompaer.OridialIgnoreCase);

foreach (string file in AllFiles)
{
    try
    {
        DateTime FileCreationTime = File.GetCreationTime(file);
        if (FileCreationTime >= DateTime.Now - new TimeSpan(60, 0, 0, 0))
        {
            Console.WriteLine(file);
            zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestSpeed;

            var fileName = Path.GetFileName(file);
            if (fileNames.Add(fileName))
                 zip.AddFile(file, ""); //fileName could be added to fileNames = it is unique
            else
            {
                //start with 1
                int counter = 1;

                //loop till you found a fileName thats not occupied
                while (true)
                {
                    //build the new file name 
                    var newFileName = $"{Path.GetFileNameWithoutExtension(fileName)} - {counter}{Path.GetExtension(fileName}";
                    if (fileNames.Add(newFileName))
                    {
                        fileName = newFileName; //use the new fileName
                        break; //break the loop
                    }
                    
                    //increase counter if newFileName is already in the list fileNames
                    counter++;
                }

                var zipFileEntry = zip.AddFile(file, "");
                zipFileEntry.FileName = fileName;
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
    }

    zip.Save(BackupDir);    
}
Rand Random
  • 7,300
  • 10
  • 40
  • 88
-1

Check if file exists and add suffix to it's name.

Something like (Pseudocode):

int nameCount = 1;
fileName = file.Name;

while(fileName exists in archive)
{
 nameCount++;
 fileName  = file.Name + "_" + nameCount;
}
  • Generating a new name won't work because the file has to actually exist in order to add it to the archive. And if this is psuedocode you might want to mention it so OP doesn't expect this to compile. – Crowcoder Aug 18 '22 at 11:25