0

I am trying to download zip files on the basis of multiple selection meaning the user selects documents and presses the download files button and then the zip file is generated.

Everything is working correctly. My zip files are also being downloaded. But sometimes when I press download button again and again , it give me the below error. I have noticed that this error is not generated when I download any new files. But when I download those files which I have already downloaded mutiiple times, then this error is generated

 An item with the same key has already been added.

Note this error is generated very rare. And I cant seem to figure out why after multiple google searches. I am posting my code below. Can anyone help me?

using (ZipFile zip = new ZipFile())
        {  
            foreach (DataRow row in dt.Rows)
            {
             //some code  
                zip.AddFile(filePath, "files");   //here the error is 
     generated
            }
            Response.Clear();
            //Response.AddHeader("Content-Disposition", "attachment; filename=DownloadedFile.zip"); 
            Response.ContentType = "application/zip";
            zip.Save(Response.OutputStream);
            Response.End();
Unknown
  • 35
  • 6
  • "An item with the same key has already been added" is an error a `Dictionary` object would throw. Look for the dictionary that already contains the key of the file you are trying to download. – Robert Harvey Jun 15 '21 at 13:41
  • @RobertHarvey I dont undestand by dictionary you mean the folder in which my documents are being uploaded? Aso what does key mean here? I am not using any key. – Unknown Jun 15 '21 at 13:42
  • 1
    In your case, it seems pretty apparent that you're adding a file path to a `ZipFile` object that already has the name you're adding. – Robert Harvey Jun 15 '21 at 13:43
  • @RobertHarvey meaning when zip file with the same name is downloaded again? – Unknown Jun 15 '21 at 13:43
  • Do you have duplicate rows in the datatable? – jdweng Jun 15 '21 at 13:47
  • 1
    You can't put the same file name into a zip file twice, unless you use folders to separate the duplicated names. It's not about a Google search, and it's not a bug in the ZipFile class; you simply need to find out where you're duplicating your names. – Robert Harvey Jun 15 '21 at 13:48
  • @jdweng nope all files names are different. – Unknown Jun 15 '21 at 13:49
  • @RobertHarvey hi everytime my zip file is downloaded It downloads with the current time. So I dont think my zip file name can be duplicated. – Unknown Jun 15 '21 at 13:50
  • The time can be duplicated within a one-second window, unless you're also capturing ticks in the file name. – Robert Harvey Jun 15 '21 at 13:51
  • @RobertHarvey I am capturing date + hours minutes seconds. Can this be the cause? – Unknown Jun 15 '21 at 13:52
  • 1
    Unless you plan on waiting one second between files, it absolutely can be the cause. – Robert Harvey Jun 15 '21 at 13:55
  • @RobertHarvey I do wait for some time after uploading a new zip file but sometimes this error still comes – Unknown Jun 15 '21 at 13:56

1 Answers1

2

Adding the current time to a file name will not insure uniqueness if you process two of them during the same one-second interval.

Try replacing zip.AddFile(filePath, "files"); with this code:

while(true)
{
    int i = 1;
    string originalPath = filePath;
    try
    {
        zip.AddFile(filePath, "files"); 
        break;
    }
    catch (ArgumentException e)
    {
        filePath = string.Format(originalPath + " ({0})", i);
        i++;
        continue;
    }
}

This alters the file name in the same way that Windows does when you try to copy files into a folder where the file names already exist.

Then you won't need to include the time in the file name to insure uniqueness.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501