Ejemplo n.º 1
0
        public void Open(IEnumerator<string> passwords)
        {
            if (passwords == null) {
            throw new ArgumentNullException("passwords");
              }
              var opened = false;
              try {
            this.passwordCollection = passwords;
            var formats = new Dictionary<Guid, bool>();
            formats.Add(format, false);
            foreach (Guid f in AllFormats) {
              formats[f] = false;
            }
            foreach (Guid f in formats.Keys) {
              if (opened) {
            break;
              }
              for (; !passwordDefined; ) {
            try {
              using (var ar = new Archive(archive, this, f)) {
                nextPassword = true;
                IArchiveEntry minCrypted = null;
                uint minIndex = 0;
                var e = ar.GetNumberOfItems();
                if (e != 0) {
                  format = f;
                  opened = true;
                  passwordDefined = !string.IsNullOrEmpty(currentPassword);
                }
                else {
                  if (!passwordRequested) {
                    break;
                  }
                }
                for (uint i = 0; i < e; ++i) {
                  if (ar.GetProperty(i, ItemPropId.IsDir).GetBool()) {
                    continue;
                  }
                  var name = ar.GetProperty(i, ItemPropId.Path).GetString();
                  if (format == FormatSplit) {
                    name = Path.GetFileName(name);
                  }

                  var size = ar.GetProperty(i, ItemPropId.Size).GetUlong();
                  var packedSize = ar.GetProperty(i, ItemPropId.PackedSize).GetUlong();
                  if (packedSize == 0) {
                    packedSize = size;
                  }
                  var isCrypted = ar.GetProperty(i, ItemPropId.Encrypted).GetBool();
                  var crc = ar.GetProperty(i, ItemPropId.CRC).GetUint();
                  IArchiveEntry entry = new SevenZipItemInfo(name, crc, isCrypted, size, packedSize);
                  items[name] = entry;
                  if (isCrypted && (minCrypted == null || minCrypted.CompressedSize > packedSize)) {
                    minCrypted = entry;
                    minIndex = i;
                  }
                }
                if (minCrypted != null && !passwordDefined) {
                  passwords.Reset();
                  var files = new Dictionary<uint, IArchiveEntry>();
                  files[minIndex] = minCrypted;
                  for (; !passwordDefined; ) {
                    using (var callback = new ExtractCallback(this, files)) {
                      try {
                        ar.Extract(new uint[] { minIndex }, 1, ExtractMode.Test, callback);
                        passwordDefined = true;
                      }
                      catch (IOException) {
                        nextPassword = true;
                        continue;
                      }
                    }
                  }
                }
                else {
                  if (items.Count != 0) {
                    passwordDefined = true;
                  }
                  else {
                    if (!passwordRequested) {
                      opened = false;
                      break;
                    }
                  }
                }
              }
            }
            catch (IndexOutOfRangeException) {
              throw new ArchiveException("Password missing");
            }
              }
            }
              }
              finally {
            CloseStreams();
              }
              if (!opened) {
            throw new ArchiveException("Invalid archive!");
              }
        }
Ejemplo n.º 2
0
        public void Open(IEnumerator <string> passwords)
        {
            if (passwords == null)
            {
                throw new ArgumentNullException("passwords");
            }
            var opened = false;

            try {
                this.passwordCollection = passwords;
                var formats = new Dictionary <Guid, bool>();
                formats.Add(format, false);
                foreach (Guid f in AllFormats)
                {
                    formats[f] = false;
                }
                foreach (Guid f in formats.Keys)
                {
                    if (opened)
                    {
                        break;
                    }
                    for (; !passwordDefined;)
                    {
                        try {
                            using (var ar = new Archive(archive, this, f)) {
                                nextPassword = true;
                                IArchiveEntry minCrypted = null;
                                uint          minIndex   = 0;
                                var           e          = ar.GetNumberOfItems();
                                if (e != 0)
                                {
                                    format          = f;
                                    opened          = true;
                                    passwordDefined = !string.IsNullOrEmpty(currentPassword);
                                }
                                else
                                {
                                    if (!passwordRequested)
                                    {
                                        break;
                                    }
                                }
                                for (uint i = 0; i < e; ++i)
                                {
                                    if (ar.GetProperty(i, ItemPropId.IsDir).GetBool())
                                    {
                                        continue;
                                    }
                                    var name = ar.GetProperty(i, ItemPropId.Path).GetString();
                                    if (format == FormatSplit)
                                    {
                                        name = Path.GetFileName(name);
                                    }

                                    var size       = ar.GetProperty(i, ItemPropId.Size).GetUlong();
                                    var packedSize = ar.GetProperty(i, ItemPropId.PackedSize).GetUlong();
                                    if (packedSize == 0)
                                    {
                                        packedSize = size;
                                    }
                                    var           isCrypted = ar.GetProperty(i, ItemPropId.Encrypted).GetBool();
                                    var           crc       = ar.GetProperty(i, ItemPropId.CRC).GetUint();
                                    IArchiveEntry entry     = new SevenZipItemInfo(name, crc, isCrypted, size, packedSize);
                                    items[name] = entry;
                                    if (isCrypted && (minCrypted == null || minCrypted.CompressedSize > packedSize))
                                    {
                                        minCrypted = entry;
                                        minIndex   = i;
                                    }
                                }
                                if (minCrypted != null && !passwordDefined)
                                {
                                    passwords.Reset();
                                    var files = new Dictionary <uint, IArchiveEntry>();
                                    files[minIndex] = minCrypted;
                                    for (; !passwordDefined;)
                                    {
                                        using (var callback = new ExtractCallback(this, files)) {
                                            try {
                                                ar.Extract(new uint[] { minIndex }, 1, ExtractMode.Test, callback);
                                                passwordDefined = true;
                                            }
                                            catch (IOException) {
                                                nextPassword = true;
                                                continue;
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    if (items.Count != 0)
                                    {
                                        passwordDefined = true;
                                    }
                                    else
                                    {
                                        if (!passwordRequested)
                                        {
                                            opened = false;
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                        catch (IndexOutOfRangeException) {
                            throw new ArchiveException("Password missing");
                        }
                    }
                }
            }
            finally {
                CloseStreams();
            }
            if (!opened)
            {
                throw new ArchiveException("Invalid archive!");
            }
        }