/// <summary>
        /// Unpacks the whole archive to the specified directory.
        /// </summary>
        /// <param name="directory">The directory where the files are to be unpacked.</param>
        public void ExtractArchive(string directory)
        {
            DisposedCheck();
            ClearExceptions();
            InitArchiveFileData(false);

            try
            {
                IInStream archiveStream;

                using ((archiveStream = GetArchiveStream(true)) as IDisposable)
                {
                    var openCallback = GetArchiveOpenCallback();

                    if (!OpenArchive(archiveStream, openCallback))
                    {
                        return;
                    }

                    try
                    {
                        using (var aec = GetArchiveExtractCallback(directory, (int)_filesCount, null))
                        {
                            try
                            {
                                CheckedExecute(
                                    _archive.Extract(null, uint.MaxValue, 0, aec),
                                    SevenZipExtractionFailedException.DEFAULT_MESSAGE, aec);
                                OnEvent(ExtractionFinished, EventArgs.Empty, false);
                            }
                            finally
                            {
                                FreeArchiveExtractCallback(aec);
                            }
                        }
                    }
                    catch (Exception)
                    {
                        if (openCallback.ThrowException())
                        {
                            throw;
                        }
                    }
                }
            }
            finally
            {
                _archive?.Close();
                _archiveStream = null;
                _opened        = false;
            }

            ThrowUserException();
        }
Ejemplo n.º 2
0
 public void Close()
 {
     if (inArchive != null)
     {
         inArchive.Close();
         inArchive = null;
     }
     if (stream != null && stream is IDisposable)
     {
         ((IDisposable)stream).Dispose();
     }
     stream = null;
 }
        /// <summary>
        /// Retrieves all information about the archive.
        /// </summary>
        /// <exception cref="SevenZip.SevenZipArchiveException"/>
        private void GetArchiveInfo(bool disposeStream)
        {
            if (_archive == null)
            {
                if (!ThrowException(null, new SevenZipArchiveException()))
                {
                    return;
                }
            }
            else
            {
                IInStream archiveStream;
                using ((archiveStream = GetArchiveStream(disposeStream)) as IDisposable)
                {
                    var openCallback = GetArchiveOpenCallback();
                    if (!_opened)
                    {
                        if (!OpenArchive(archiveStream, openCallback))
                        {
                            return;
                        }
                        _opened = !disposeStream;
                    }
                    _filesCount      = _archive.GetNumberOfItems();
                    _archiveFileData = new List <ArchiveFileInfo>((int)_filesCount);
                    if (_filesCount != 0)
                    {
                        var data = new PropVariant();
                        try
                        {
                            #region Getting archive items data

                            for (uint i = 0; i < _filesCount; i++)
                            {
                                try
                                {
                                    var fileInfo = new ArchiveFileInfo {
                                        Index = (int)i
                                    };
                                    _archive.GetProperty(i, ItemPropId.Path, ref data);
                                    fileInfo.FileName = NativeMethods.SafeCast(data, "[no name]");
                                    _archive.GetProperty(i, ItemPropId.LastWriteTime, ref data);
                                    fileInfo.LastWriteTime = NativeMethods.SafeCast(data, DateTime.Now);
                                    _archive.GetProperty(i, ItemPropId.CreationTime, ref data);
                                    fileInfo.CreationTime = NativeMethods.SafeCast(data, DateTime.Now);
                                    _archive.GetProperty(i, ItemPropId.LastAccessTime, ref data);
                                    fileInfo.LastAccessTime = NativeMethods.SafeCast(data, DateTime.Now);
                                    _archive.GetProperty(i, ItemPropId.Size, ref data);
                                    fileInfo.Size = NativeMethods.SafeCast <ulong>(data, 0);
                                    if (fileInfo.Size == 0)
                                    {
                                        fileInfo.Size = NativeMethods.SafeCast <uint>(data, 0);
                                    }
                                    _archive.GetProperty(i, ItemPropId.Attributes, ref data);
                                    fileInfo.Attributes = NativeMethods.SafeCast <uint>(data, 0);
                                    _archive.GetProperty(i, ItemPropId.IsDirectory, ref data);
                                    fileInfo.IsDirectory = NativeMethods.SafeCast(data, false);
                                    _archive.GetProperty(i, ItemPropId.Encrypted, ref data);
                                    fileInfo.Encrypted = NativeMethods.SafeCast(data, false);
                                    _archive.GetProperty(i, ItemPropId.Crc, ref data);
                                    fileInfo.Crc = NativeMethods.SafeCast <uint>(data, 0);
                                    _archive.GetProperty(i, ItemPropId.Comment, ref data);
                                    fileInfo.Comment = NativeMethods.SafeCast(data, "");
                                    _archive.GetProperty(i, ItemPropId.Method, ref data);
                                    fileInfo.Method = NativeMethods.SafeCast(data, "");
                                    _archiveFileData.Add(fileInfo);
                                }
                                catch (InvalidCastException)
                                {
                                    ThrowException(null, new SevenZipArchiveException("probably archive is corrupted."));
                                }
                            }

                            #endregion

                            #region Getting archive properties

                            uint numProps  = _archive.GetNumberOfArchiveProperties();
                            var  archProps = new List <ArchiveProperty>((int)numProps);
                            for (uint i = 0; i < numProps; i++)
                            {
                                _archive.GetArchivePropertyInfo(i, out string propName, out var propId, out var varType);
                                _archive.GetArchiveProperty(propId, ref data);

                                if (propId == ItemPropId.Solid)
                                {
                                    _isSolid = NativeMethods.SafeCast(data, true);
                                }

                                // TODO Add more archive properties
                                if (PropIdToName.PropIdNames.ContainsKey(propId))
                                {
                                    archProps.Add(new ArchiveProperty
                                    {
                                        Name  = PropIdToName.PropIdNames[propId],
                                        Value = data.Object
                                    });
                                }
                                else
                                {
                                    Debug.WriteLine($"An unknown archive property encountered (code {((int)propId).ToString(CultureInfo.InvariantCulture)})");
                                }
                            }

                            _archiveProperties = new ReadOnlyCollection <ArchiveProperty>(archProps);

                            if (!_isSolid.HasValue && _format == InArchiveFormat.Zip)
                            {
                                _isSolid = false;
                            }

                            if (!_isSolid.HasValue)
                            {
                                _isSolid = true;
                            }

                            #endregion
                        }
                        catch (Exception)
                        {
                            if (openCallback.ThrowException())
                            {
                                throw;
                            }
                        }
                    }
                }

                if (disposeStream)
                {
                    _archive.Close();
                    _archiveStream = null;
                }

                _archiveFileInfoCollection = new ReadOnlyCollection <ArchiveFileInfo>(_archiveFileData);
            }
        }