/* ----------------------------------------------------------------- */
 ///
 /// Terminate
 ///
 /// <summary>
 /// Invokes post processing and throws an exception if needed.
 /// </summary>
 ///
 /* ----------------------------------------------------------------- */
 internal static void Terminate(this IEnumerable <ArchiveItem> src,
                                ArchiveExtractCallback cb, PasswordQuery query)
 {
     if (cb.Result == OperationResult.OK)
     {
         return;
     }
     if (cb.Result == OperationResult.UserCancel)
     {
         throw new OperationCanceledException();
     }
     if (cb.Result == OperationResult.WrongPassword ||
         cb.Result == OperationResult.DataError && src.Any(x => x.Encrypted))
     {
         query.Reset();
         throw new EncryptionException();
     }
     throw cb.Exception ?? new System.IO.IOException($"{cb.Result}");
 }
Esempio n. 2
0
        public IDisposable Subscribe(IObserver <SevenZipArchiveFile> observer)
        {
            var subject = new Subject <SevenZipArchiveFile>();

            subject.Subscribe(observer);
            var archive = format.CreateInArchive(SevenZipFormat.GetClassIdFromKnownFormat(KnownSevenZipFormat.SevenZip));

            try
            {
                using (var fileStream = File.OpenRead(this.Name))
                {
                    var maxCheckStartPosition = 128UL * 1024UL;
                    if (archive.Open(new InStreamWrapper(fileStream), ref maxCheckStartPosition, new ArchiveOpenCallback()) != 0)
                    {
                        throw new IOException();
                    }
                    var indices = Enumerable.Range(0, (int)archive.GetNumberOfItems())
                                  .Select(index => (uint)index)
                                  .Where(index =>
                    {
                        var value = new PropVariant();
                        archive.GetProperty(index, ItemPropId.kpidIsFolder, ref value);
                        return(!(bool)value.GetObject());
                    })
                                  .ToArray();
                    using (var callback = new ArchiveExtractCallback(archive))
                    {
                        callback.Subscribe(subject);
                        archive.Extract(indices, (uint)indices.Length, 0, callback);
                    }
                }
                subject.OnCompleted();
                return(subject);
            }
            finally
            {
                Marshal.ReleaseComObject(archive);
            }
        }