Exemple #1
0
 /// <summary>
 /// Try to open <paramref name="filename"/> as archive.
 /// </summary>
 /// <returns>
 /// ArcFile object if file is opened successfully, null otherwise.
 /// </returns>
 public static ArcFile TryOpen(string filename)
 {
     return(TryOpen(VFS.FindFile(filename)));
 }
Exemple #2
0
        /// <summary>
        /// Try to open <paramref name="filename"/> as archive.
        /// </summary>
        /// <returns>
        /// ArcFile object if file is opened successfully, null otherwise.
        /// </returns>
        public static ArcFile TryOpen(string filename)
        {
            var entry = VFS.FindFile(filename);

            if (entry.Size < 4)
            {
                return(null);
            }
            var ext  = new Lazy <string> (() => Path.GetExtension(filename).TrimStart('.').ToLowerInvariant());
            var file = VFS.OpenView(entry);

            try
            {
                uint signature = file.View.ReadUInt32(0);
                for (;;)
                {
                    var range = FormatCatalog.Instance.LookupSignature <ArchiveFormat> (signature);
                    // check formats that match filename extension first
                    if (range.Skip(1).Any()) // if range.Count() > 1
                    {
                        range = range.OrderByDescending(f => f.Extensions.Any(e => e == ext.Value));
                    }
                    foreach (var impl in range)
                    {
                        try
                        {
                            var arc = impl.TryOpen(file);
                            if (null != arc)
                            {
                                file = null; // file ownership passed to ArcFile
                                return(arc);
                            }
                        }
                        catch (OperationCanceledException X)
                        {
                            FormatCatalog.Instance.LastError = X;
                            return(null);
                        }
                        catch (Exception X)
                        {
                            // ignore failed open attmepts
                            Trace.WriteLine(string.Format("[{0}] {1}: {2}", impl.Tag, filename, X.Message));
                            FormatCatalog.Instance.LastError = X;
                        }
                    }
                    if (0 == signature)
                    {
                        break;
                    }
                    signature = 0;
                }
            }
            finally
            {
                if (null != file)
                {
                    file.Dispose();
                }
            }
            return(null);
        }