Example #1
0
        private void _AddOrUpdateSelectedFiles(String selectionCriteria,
                                               String directoryOnDisk,
                                               String directoryPathInArchive,
                                               bool recurseDirectories,
                                               bool wantUpdate)
        {
            if (directoryOnDisk == null && (Directory.Exists(selectionCriteria)))
            {
                directoryOnDisk = selectionCriteria;
                selectionCriteria = "*.*";
            }
            else if (String.IsNullOrEmpty(directoryOnDisk))
            {
                directoryOnDisk = ".";
            }

            // workitem 9176
            while (directoryOnDisk.EndsWith("\\")) directoryOnDisk = directoryOnDisk.Substring(0, directoryOnDisk.Length - 1);
            if (Verbose) StatusMessageTextWriter.WriteLine("adding selection '{0}' from dir '{1}'...",
                                                               selectionCriteria, directoryOnDisk);
            Ionic.FileSelector ff = new Ionic.FileSelector(selectionCriteria,
                                                           AddDirectoryWillTraverseReparsePoints);
            var itemsToAdd = ff.SelectFiles(directoryOnDisk, recurseDirectories);

            if (Verbose) StatusMessageTextWriter.WriteLine("found {0} files...", itemsToAdd.Count);

            OnAddStarted();

            AddOrUpdateAction action = (wantUpdate) ? AddOrUpdateAction.AddOrUpdate : AddOrUpdateAction.AddOnly;
            foreach (var item in itemsToAdd)
            {
                // workitem 10153
                string dirInArchive = (directoryPathInArchive == null)
                    ? null
                    // workitem 12260
                    : ReplaceLeadingDirectory(Path.GetDirectoryName(item),
                                              directoryOnDisk,
                                              directoryPathInArchive);

                if (File.Exists(item))
                {
                    if (wantUpdate)
                        this.UpdateFile(item, dirInArchive);
                    else
                        this.AddFile(item, dirInArchive);
                }
                else
                {
                    // this adds "just" the directory, without recursing to the contained files
                    AddOrUpdateDirectoryImpl(item, dirInArchive, action, false, 0);
                }
            }

            OnAddCompleted();
        }
Example #2
0
        public void Selector_SelectFiles()
        {
            Directory.SetCurrentDirectory(TopLevelDir);

            Trial[] trials = new Trial[]
                {
                    new Trial { Label = "name", C1 = "name = *.txt", C2 = "name = *.bin" },
                    new Trial { Label = "name (shorthand)", C1 = "*.txt", C2 = "*.bin" },
                    new Trial { Label = "size", C1 = "size < 7500", C2 = "size >= 7500" },
                    new Trial { Label = "size", C1 = "size = 8080", C2 = "size != 8080" },
                    new Trial { Label = "name & size",
                                C1 = "name = *.bin AND size > 7500",
                                C2 = "name != *.bin  OR  size <= 7500",
                    },
                    new Trial { Label = "name XOR name",
                                C1 = "name = *.bin XOR name = *4.*",
                                C2 = "(name != *.bin OR name = *4.*) AND (name = *.bin OR name != *4.*)",
                    },
                    new Trial { Label = "name XOR size",
                                C1 = "name = *.bin XOR size > 100k",
                                C2 = "(name != *.bin OR size > 100k) AND (name = *.bin OR size <= 100k)",
                    },
                    new Trial
                    {
                        Label = "mtime",
                        C1 = String.Format("mtime < {0}", twentyDaysAgo.ToString("yyyy-MM-dd")),
                        C2 = String.Format("mtime >= {0}", twentyDaysAgo.ToString("yyyy-MM-dd")),
                    },
                    new Trial
                    {
                        Label = "ctime",
                        C1 = String.Format("mtime < {0}", threeDaysAgo.ToString("yyyy-MM-dd")),
                        C2 = String.Format("mtime >= {0}", threeDaysAgo.ToString("yyyy-MM-dd")),
                    },
                    new Trial
                    {
                        Label = "atime",
                        C1 = String.Format("mtime < {0}", yesterdayAtMidnight.ToString("yyyy-MM-dd")),
                        C2 = String.Format("mtime >= {0}", yesterdayAtMidnight.ToString("yyyy-MM-dd")),
                    },
                    new Trial { Label = "size (100k)", C1="size > 100k", C2="size <= 100kb", },
                    new Trial { Label = "size (1mb)", C1="size > 1m", C2="size <= 1mb", },
                    new Trial { Label = "size (1gb)", C1="size > 1g", C2="size <= 1gb", },
                    new Trial { Label = "attributes (Hidden)", C1 = "attributes = H", C2 = "attributes != H" },
                    new Trial { Label = "attributes (ReadOnly)", C1 = "attributes = R", C2 = "attributes != R" },
                    new Trial { Label = "attributes (System)", C1 = "attributes = S", C2 = "attributes != S" },
                    new Trial { Label = "attributes (Archive)", C1 = "attributes = A", C2 = "attributes != A" },

                };


            string zipFileToCreate = Path.Combine(TopLevelDir, "Selector_SelectFiles.zip");
            Assert.IsFalse(File.Exists(zipFileToCreate), "The zip file '{0}' already exists.", zipFileToCreate);

            int count1, count2;
            //String filename = null;

            SetupFiles();
            var topLevelFiles = Directory.GetFiles(fodderDirectory, "*.*", SearchOption.TopDirectoryOnly);

            for (int m = 0; m < trials.Length; m++)
            {
                Ionic.FileSelector ff = new Ionic.FileSelector(trials[m].C1);
                var list = ff.SelectFiles(fodderDirectory);
                TestContext.WriteLine("=======================================================");
                TestContext.WriteLine("Selector: " + ff.ToString());
                TestContext.WriteLine("Criteria({0})", ff.SelectionCriteria);
                TestContext.WriteLine("Count({0})", list.Count);
                count1 = 0;
                foreach (string s in list)
                {
                    switch (m)
                    {
                        case 0:
                        case 1:
                            Assert.IsTrue(s.EndsWith(".txt"));
                            break;
                        case 2:
                            {
                                FileInfo fi = new FileInfo(s);
                                Assert.IsTrue(fi.Length < 7500);
                            }
                            break;
                        case 4:
                            {
                                FileInfo fi = new FileInfo(s);
                                bool x = s.EndsWith(".bin") && fi.Length > 7500;
                                Assert.IsTrue(x);
                            }
                            break;
                    }
                    count1++;
                }

                ff = new Ionic.FileSelector(trials[m].C2);
                list = ff.SelectFiles(fodderDirectory);
                TestContext.WriteLine("- - - - - - - - - - - - - - - - - - - - - - - - - - - -");
                TestContext.WriteLine("Criteria({0})", ff.SelectionCriteria);
                TestContext.WriteLine("Count({0})", list.Count);
                count2 = 0;
                foreach (string s in list)
                {
                    switch (m)
                    {
                        case 0:
                        case 1:
                            Assert.IsTrue(s.EndsWith(".bin"));
                            break;
                        case 2:
                            {
                                FileInfo fi = new FileInfo(s);
                                Assert.IsTrue(fi.Length >= 7500);
                            }
                            break;
                        case 4:
                            {
                                FileInfo fi = new FileInfo(s);
                                bool x = !s.EndsWith(".bin") || fi.Length <= 7500;
                                Assert.IsTrue(x);
                            }
                            break;
                    }
                    count2++;
                }
                Assert.AreEqual<Int32>(topLevelFiles.Length, count1 + count2);
            }
        }
Example #3
0
 /// <summary>
 /// Retrieve entries from the zipfile by specified criteria.
 /// </summary>
 ///
 /// <remarks>
 /// <para>
 /// This method allows callers to retrieve the collection of entries from the zipfile
 /// that fit the specified criteria.  The criteria are described in a string format, and
 /// can include patterns for the filename; constraints on the size of the entry;
 /// constraints on the last modified, created, or last accessed time for the file
 /// described by the entry; or the attributes of the entry.
 /// </para>
 ///
 /// <para>
 /// For details on the syntax for the selectionCriteria parameter, see <see
 /// cref="AddSelectedFiles(String)"/>.
 /// </para>
 ///
 /// <para>
 /// This method is intended for use with a ZipFile that has been read from storage.
 /// When creating a new ZipFile, this method will work only after the ZipArchive has
 /// been Saved to the disk (the ZipFile class subsequently and implicitly reads the Zip
 /// archive from storage.)  Calling SelectEntries on a ZipFile that has not yet been
 /// saved will deliver undefined results.
 /// </para>
 /// </remarks>
 ///
 /// <exception cref="System.Exception">
 /// Thrown if selectionCriteria has an invalid syntax.
 /// </exception>
 ///
 /// <example>
 /// <code>
 /// using (ZipFile zip1 = ZipFile.Read(ZipFileName))
 /// {
 ///     var UpdatedPhotoShopFiles = zip1.SelectEntries("*.psd", "UpdatedFiles");
 ///     foreach (ZipEntry e in UpdatedPhotoShopFiles)
 ///     {
 ///         // prompt for extract here
 ///         if (WantExtract(e.FileName))
 ///             e.Extract();
 ///     }
 /// }
 /// </code>
 /// <code lang="VB">
 /// Using zip1 As ZipFile = ZipFile.Read(ZipFileName)
 ///     Dim UpdatedPhotoShopFiles As ICollection(Of ZipEntry) = zip1.SelectEntries("*.psd", "UpdatedFiles")
 ///     Dim e As ZipEntry
 ///     For Each e In UpdatedPhotoShopFiles
 ///         ' prompt for extract here
 ///         If Me.WantExtract(e.FileName) Then
 ///             e.Extract
 ///         End If
 ///     Next
 /// End Using
 /// </code>
 /// </example>
 /// <param name="selectionCriteria">the string that specifies which entries to select</param>
 ///
 /// <param name="directoryPathInArchive">
 /// the directory in the archive from which to select entries. If null, then
 /// all directories in the archive are used.
 /// </param>
 ///
 /// <returns>a collection of ZipEntry objects that conform to the inclusion spec</returns>
 public ICollection<ZipEntry> SelectEntries(String selectionCriteria, string directoryPathInArchive)
 {
     Ionic.FileSelector ff = new Ionic.FileSelector(selectionCriteria,
                                                    AddDirectoryWillTraverseReparsePoints);
     return ff.SelectEntries(this, directoryPathInArchive);
 }
Example #4
0
        public void Selector_SelectFiles_GoodSyntax01()
        {
            string[] criteria = {
                "type = D",
                "type = F",
                "attrs = HRS",
                "attrs = L",
                "name = *.txt  OR (size > 7800)",
                "name = *.harvey  OR  (size > 7800  and attributes = H)",
                "(name = *.harvey)  OR  (size > 7800  and attributes = H)",
                "(name = *.xls)  and (name != *.xls)  OR  (size > 7800  and attributes = H)",
                "(name = '*.xls')",
                "(name = Ionic.Zip.dll) or ((size > 1mb) and (name != *.zip))",
                "(name = Ionic.Zip.dll) or ((size > 1mb) and (name != *.zip)) or (name = Joe.txt)",
                "(name=Ionic.Zip.dll) or ((size>1mb) and (name!=*.zip)) or (name=Joe.txt)",
                "(name=Ionic.Zip.dll)or((size>1mb)and(name!=*.zip))or(name=Joe.txt)",
            };

            foreach (string s in criteria)
            {
                TestContext.WriteLine("Selector: " + s);
                var ff = new Ionic.FileSelector(s);
            }
        }
Example #5
0
 public void Selector_SelectFiles_BadSyntax19()
 {
     Ionic.FileSelector ff = new Ionic.FileSelector(null);
     var list = ff.SelectFiles(".");
 }
Example #6
0
 public void Selector_SelectFiles_DirName_wi8245()
 {
     // workitem 8245
     //Directory.SetCurrentDirectory(TopLevelDir);
     SetupFiles();
     var ff = new Ionic.FileSelector("*.*");
     var result = ff.SelectFiles(fodderDirectory);
     Assert.IsTrue(result.Count > 1);
 }
Example #7
0
        public void Selector_EdgeCases()
        {
            string Subdir = Path.Combine(TopLevelDir, "A");

            Ionic.FileSelector ff = new Ionic.FileSelector("name = *.txt");
            var list = ff.SelectFiles(Subdir);

            ff.SelectionCriteria = "name = *.bin";
            list = ff.SelectFiles(Subdir);
        }
Example #8
0
        private void _AddOrUpdateSelectedFiles(String selectionCriteria,
                                               String directoryOnDisk,
                                               String directoryPathInArchive,
                                               bool recurseDirectories,
                                               bool wantUpdate)
        {
            if (directoryOnDisk == null && (Directory.Exists(selectionCriteria)))
            {
                directoryOnDisk   = selectionCriteria;
                selectionCriteria = "*.*";
            }
            else if (String.IsNullOrEmpty(directoryOnDisk))
            {
                directoryOnDisk = ".";
            }

            // workitem 9176
            while (directoryOnDisk.EndsWith("\\"))
            {
                directoryOnDisk = directoryOnDisk.Substring(0, directoryOnDisk.Length - 1);
            }
            if (Verbose)
            {
                StatusMessageTextWriter.WriteLine("adding selection '{0}' from dir '{1}'...",
                                                  selectionCriteria, directoryOnDisk);
            }
            Ionic.FileSelector ff = new Ionic.FileSelector(selectionCriteria,
                                                           AddDirectoryWillTraverseReparsePoints);
            var itemsToAdd = ff.SelectFiles(directoryOnDisk, recurseDirectories);

            if (Verbose)
            {
                StatusMessageTextWriter.WriteLine("found {0} files...", itemsToAdd.Count);
            }

            OnAddStarted();

            AddOrUpdateAction action = (wantUpdate) ? AddOrUpdateAction.AddOrUpdate : AddOrUpdateAction.AddOnly;

            foreach (var item in itemsToAdd)
            {
                // workitem 10153
                string dirInArchive = (directoryPathInArchive == null)
                    ? null
                                      // workitem 12260
                    : ReplaceLeadingDirectory(Path.GetDirectoryName(item),
                                              directoryOnDisk,
                                              directoryPathInArchive);

                if (File.Exists(item))
                {
                    if (wantUpdate)
                    {
                        this.UpdateFile(item, dirInArchive);
                    }
                    else
                    {
                        this.AddFile(item, dirInArchive);
                    }
                }
                else
                {
                    // this adds "just" the directory, without recursing to the contained files
                    AddOrUpdateDirectoryImpl(item, dirInArchive, action, false, 0);
                }
            }

            OnAddCompleted();
        }
Example #9
0
 public ICollection <ZipEntry> SelectEntries(String selectionCriteria)
 {
     Ionic.FileSelector ff = new Ionic.FileSelector(selectionCriteria,
                                                    AddDirectoryWillTraverseReparsePoints);
     return(ff.SelectEntries(this));
 }
Example #10
0
        private void _AddOrUpdateSelectedFiles(String selectionCriteria,
                                     String directoryOnDisk,
                                     String directoryPathInArchive,
                                               bool recurseDirectories,
                                               bool wantUpdate)
        {
            //List<string> filesToAdd = null;
            if (directoryOnDisk == null && (Directory.Exists(selectionCriteria)))
            {
                //if (Verbose) StatusMessageTextWriter.WriteLine("adding selection '{0}' from dir '{1}'...",
                //selectionCriteria, directoryOnDisk);
                //Ionic.FileSelector ff = new Ionic.FileSelector("*.*");
                //filesToAdd = ff.SelectFiles(selectionCriteria, recurseDirectories);
                directoryOnDisk = selectionCriteria;
                selectionCriteria = "*.*";
            }
            else if (String.IsNullOrEmpty(directoryOnDisk))
            {
                directoryOnDisk = ".";
            }
            // workitem 9176
            while (directoryOnDisk.EndsWith("\\")) directoryOnDisk = directoryOnDisk.Substring(0, directoryOnDisk.Length - 1);
            if (Verbose) StatusMessageTextWriter.WriteLine("adding selection '{0}' from dir '{1}'...",
                                                               selectionCriteria, directoryOnDisk);
            Ionic.FileSelector ff = new Ionic.FileSelector(selectionCriteria,
                                                           AddDirectoryWillTraverseReparsePoints);
            var filesToAdd = ff.SelectFiles(directoryOnDisk, recurseDirectories);

            if (Verbose) StatusMessageTextWriter.WriteLine("found {0} files...", filesToAdd.Count);

            OnAddStarted();

            foreach (var f in filesToAdd)
            {
                if (directoryPathInArchive != null)
                {
                    string dirInArchive = Path.GetDirectoryName(f).Replace(directoryOnDisk, directoryPathInArchive);
                    if (wantUpdate)
                        this.UpdateFile(f, dirInArchive);
                    else
                        this.AddFile(f, dirInArchive);
                }
                else
                {
                    if (wantUpdate)
                        this.UpdateFile(f, null);
                    else
                        this.AddFile(f, null);
                }
            }

            OnAddCompleted();
        }