public void UpdateZip_RemoveEntry_ByFilename()
        {
            for (int k = 0; k < 2; k++)
            {
                int j;
                int entriesToBeAdded = 0;
                string filename = null;
                string repeatedLine = null;

                // select the name of the zip file
                string zipFileToCreate = Path.Combine(TopLevelDir, String.Format("UpdateZip_RemoveEntry_ByFilename-{0}.zip", k));

                // create the subdirectory
                string subdir = Path.Combine(TopLevelDir, String.Format("A{0}", k));
                Directory.CreateDirectory(subdir);

                // create a bunch of files
                int numFilesToCreate = _rnd.Next(13) + 24;

                for (j = 0; j < numFilesToCreate; j++)
                {
                    filename = String.Format("file{0:D3}.txt", j);
                    repeatedLine = String.Format("This line is repeated over and over and over in file {0}",
                                     filename);
                    TestUtilities.CreateAndFillFileText(Path.Combine(subdir, filename), repeatedLine, _rnd.Next(34000) + 5000);
                    entriesToBeAdded++;
                }

                // Add the files to the zip, save the zip.
                // in pass 2, remove one file, then save again.
                Directory.SetCurrentDirectory(TopLevelDir);
                using (ZipFile zip1 = new ZipFile())
                {
                    String[] filenames = Directory.GetFiles(String.Format("A{0}", k));

                    foreach (String f in filenames)
                        zip1.AddFile(f, "");

                    zip1.Comment = "UpdateTests::UpdateZip_RemoveEntry_ByFilename(): This archive will be updated.";
                    zip1.Save(zipFileToCreate);

                    // conditionally remove a single entry, on the 2nd trial
                    if (k == 1)
                    {
                        int chosen = _rnd.Next(filenames.Length);
                        zip1.RemoveEntry(zip1[chosen]);
                        zip1.Save();
                    }
                }

                // Verify the files are in the zip
                Assert.AreEqual<int>(TestUtilities.CountEntries(zipFileToCreate), entriesToBeAdded - k,
                    "Trial {0}: the Zip file has the wrong number of entries.", k);

                if (k == 0)
                {
                    // selectively remove a few files in the zip archive
                    var filesToRemove = new List<string>();
                    int numToRemove = _rnd.Next(numFilesToCreate - 4) + 1;
                    using (ZipFile zip2 = ZipFile.Read(zipFileToCreate))
                    {
                        for (j = 0; j < numToRemove; j++)
                        {
                            // select a new, uniquely named file to create
                            do
                            {
                                filename = String.Format("file{0:D3}.txt", _rnd.Next(numFilesToCreate));
                            } while (filesToRemove.Contains(filename));
                            // add this file to the list
                            filesToRemove.Add(filename);
                            zip2.RemoveEntry(filename);

                        }

                        zip2.Comment = "This archive has been modified. Some files have been removed.";
                        zip2.Save();
                    }


                    // extract all files, verify none should have been removed,
                    // and verify the contents of those that remain
                    using (ZipFile zip3 = ZipFile.Read(zipFileToCreate))
                    {
                        foreach (string s1 in zip3.EntryFileNames)
                        {
                            Assert.IsFalse(filesToRemove.Contains(s1),
                                           String.Format("File ({0}) was not expected.", s1));

                            zip3[s1].Extract("extract");
                            repeatedLine = String.Format("This line is repeated over and over and over in file {0}",
                                             s1);

                            // verify the content of the updated file.
                            var sr = new StreamReader(Path.Combine("extract", s1));
                            string sLine = sr.ReadLine();
                            sr.Close();

                            Assert.AreEqual<string>(repeatedLine, sLine,
                                        String.Format("The content of the originally added file ({0}) in the zip archive is incorrect.", s1));

                        }
                    }

                    // Verify the files are in the zip
                    Assert.AreEqual<int>(TestUtilities.CountEntries(zipFileToCreate),
                                         entriesToBeAdded - filesToRemove.Count,
                                         "The updated Zip file has the wrong number of entries.");
                }
            }
        }
        public void Progress_ReadFile()
        {
            Directory.SetCurrentDirectory(TopLevelDir);
            string  zipFileToCreate = Path.Combine(TopLevelDir, "Progress_ReadFile.zip");
            string dirToZip = Path.GetFileNameWithoutExtension(Path.GetRandomFileName());

            var files = TestUtilities.GenerateFilesFlat(dirToZip);

            using (ZipFile zip = new ZipFile())
            {
                zip.AddFiles(files);
                zip.Save(zipFileToCreate);
            }

            int count = TestUtilities.CountEntries(zipFileToCreate);
            Assert.IsTrue(count>0);

            var options = new ReadOptions {
                    StatusMessageWriter = new StringWriter(),
                    ReadProgress = ReadProgress1
            };
            using (ZipFile zip = ZipFile.Read(zipFileToCreate, options))
            {
                // this should be fine
                zip.RemoveEntry(zip[1]);
                zip.Save();
            }
            TestContext.WriteLine(options.StatusMessageWriter.ToString());
            Assert.AreEqual<Int32>(count, TestUtilities.CountEntries(zipFileToCreate)+1);
        }
        public void Create_RenameRemoveAndRenameAgain_wi8047()
        {
            string filename = "file.test";
            string dirToZip = Path.GetFileNameWithoutExtension(Path.GetRandomFileName());
            var files = TestUtilities.GenerateFilesFlat(dirToZip);

            for (int m = 0; m < 2; m++)
            {
                string zipFileToCreate = Path.Combine(TopLevelDir, String.Format("Create_RenameRemoveAndRenameAgain_wi8047-{0}.zip", m));

                using (var zip = new ZipFile())
                {
                    // select a single file from the list
                    int n = _rnd.Next(files.Length);

                    // insert the selected file into the zip, and also rename it
                    zip.UpdateFile(files[n]).FileName = filename;

                    // conditionally save
                    if (m > 0) zip.Save(zipFileToCreate);

                    // remove the original file
                    zip.RemoveEntry(zip[filename]);

                    // select another file from the list, making sure it is not the same file
                    int n2 = 0;
                    while ((n2 = _rnd.Next(files.Length)) == n) ;

                    // insert that other file and rename it
                    zip.UpdateFile(files[n2]).FileName = filename;
                    zip.Save(zipFileToCreate);
                }

                Assert.AreEqual<int>(1, TestUtilities.CountEntries(zipFileToCreate), "Trial {0}: The Zip file has the wrong number of entries.", m);
            }
        }