Copying existing file inside zip folder

Feb 17, 2012 at 5:26 PM

Hi,

I need to copy an existing file inside the same zip folder. How can I do this?

I have following code but it gives me exception that "the file with same key already exists".

using (ZipFile zf = ZipFile.Read(targetFullFileName))
            {
                int noOfCopies = 3;
                foreach (ZipEntry e in zf)
                {                    
                    for (int i = 0; i < noOfCopies; i++)
                    {
                        zf.AddFile(e.FileName, "").FileName += "_Copy_" + i.ToString();
                    }
                    zf.Save();
                }
            }
Thanks.
Parekh
Mar 3, 2012 at 2:41 PM

 

Hello,

try this code:

 using (ZipFile zf = ZipFile.Read("Test.zip"))
            {
                int noOfCopies = 3;
                //read content
                for (int counter = 0; counter < 1; counter++)
                {
                    ZipEntry e = zf[counter];
                    var ms = new MemoryStream();
                    e.Extract(ms);
                    string content = Encoding.ASCII.GetString(ms.ToArray());
                    for (int i = 0; i < noOfCopies; i++)
                    {
                        zf.AddEntry(e.FileName + "_Copy_" + i + ".txt", content);
                    }
                }
                zf.Save();
            }

you can refactor the code accordingly and also you are adding entry in the same directory so make sure you handle this