Example #1
0
        public void Extract()
        {
            try {
            using (var ar = new Archive(archive, this, format)) {
              var indices = new List<uint>();
              var files = new Dictionary<uint, IArchiveEntry>();
              var e = ar.GetNumberOfItems();
              for (uint i = 0; i < e; ++i) {
            var name = ar.GetProperty(i, ItemPropId.Path).GetString();
            if (format == FormatSplit) {
              name = Path.GetFileName(name);
            }
            if (!items.ContainsKey(name)) {
              continue;
            }

            var entry = items[name];
            if (entry.Destination == null) {
              continue;
            }
            indices.Add(i);
            files[i] = entry;
              }
              using (var callback = new ExtractCallback(this, files)) {
            ar.Extract(indices.ToArray(), (uint)indices.Count, ExtractMode.Extract, callback);
              }
            }
              }
              finally {
            CloseStreams();
              }
        }
Example #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!");
              }
        }
Example #3
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!");
            }
        }