private ArchiveOpenCallback GetArchiveOpenCallback() { if (this._OpenCallback == null) { this._OpenCallback = string.IsNullOrEmpty(this.Password) ? new ArchiveOpenCallback(this._FileName) : new ArchiveOpenCallback(this._FileName, this.Password); } return(this._OpenCallback); }
public void Dispose() { if (!this._Disposed) { if (this._Opened) { try { this._Archive.Close(); this._Archive = (IInArchive)null; this._ArchiveFileData = (List <ArchiveFileInfo>)null; this._ArchiveProperties = (ReadOnlyCollection <ArchiveProperty>)null; this._ArchiveFileInfoCollection = (ReadOnlyCollection <ArchiveFileInfo>)null; this._InStream = (Stream)null; } catch (InvalidComObjectException ex) { } } if (this._OpenCallback != null) { try { this._OpenCallback.Dispose(); } catch (ObjectDisposedException ex) { } this._OpenCallback = (ArchiveOpenCallback)null; } if (this._ArchiveStream != null) { if (this._ArchiveStream is IDisposable) { try { if (this._ArchiveStream is DisposeVariableWrapper) { (this._ArchiveStream as DisposeVariableWrapper).DisposeStream = true; } (this._ArchiveStream as IDisposable).Dispose(); } catch (ObjectDisposedException ex) { } this._ArchiveStream = (IInStream)null; } } if (!string.IsNullOrEmpty(this._FileName)) { SevenZipLibraryManager.FreeLibrary((object)this, (Enum)this._Format); } } this._Disposed = true; GC.SuppressFinalize((object)this); }
public void ExtractArchive(string directory) { this.DisposedCheck(); this.ClearExceptions(); this.InitArchiveFileData(false); try { IInStream archiveStream; using ((archiveStream = this.GetArchiveStream(true)) as IDisposable) { ArchiveOpenCallback archiveOpenCallback = this.GetArchiveOpenCallback(); ulong maxCheckStartPosition = 32768; if (!this._Opened && this._Archive.Open(archiveStream, ref maxCheckStartPosition, (IArchiveOpenCallback)archiveOpenCallback) != 0) { if (!this.ThrowException((SevenZipBase)null, (Exception) new SevenZipArchiveException())) { return; } } this._Opened = true; using (ArchiveExtractCallback archiveExtractCallback = this.GetArchiveExtractCallback(directory, (int)this._FilesCount.Value, (List <uint>)null)) { try { this.CheckedExecute(this._Archive.Extract((uint[])null, uint.MaxValue, 0, (IArchiveExtractCallback)archiveExtractCallback), "The extraction has failed for an unknown reason with code ", (SevenZipBase)archiveExtractCallback); this.OnExtractionFinished(EventArgs.Empty); } finally { this.FreeArchiveExtractCallback(archiveExtractCallback); } } } } finally { this._Archive.Close(); this._ArchiveStream = (IInStream)null; this._Opened = false; } this.ThrowUserException(); }
public void ExtractFiles(string directory, params int[] indexes) { this.DisposedCheck(); this.ClearExceptions(); if (!SevenZipExtractor.CheckIndexes(indexes)) { if (!this.ThrowException((SevenZipBase)null, (Exception) new ArgumentException("The indexes must be more or equal to zero.", nameof(indexes)))) { return; } } this.InitArchiveFileData(false); uint[] numArray = new uint[indexes.Length]; for (int index = 0; index < indexes.Length; ++index) { numArray[index] = (uint)indexes[index]; } foreach (uint num in numArray) { uint?filesCount = this._FilesCount; if ((num < filesCount.GetValueOrDefault() ? 0 : (filesCount.HasValue ? 1 : 0)) != 0) { if (!this.ThrowException((SevenZipBase)null, (Exception) new ArgumentOutOfRangeException(nameof(indexes), "Index must be less than " + this._FilesCount.Value.ToString((IFormatProvider)CultureInfo.InvariantCulture) + "!"))) { return; } } } List <uint> actualIndexes = new List <uint>((IEnumerable <uint>)numArray); actualIndexes.Sort(); uint[] indexes1 = actualIndexes.ToArray(); if (this._IsSolid.Value) { indexes1 = SevenZipExtractor.SolidIndexes(indexes1); } try { IInStream archiveStream; using ((archiveStream = this.GetArchiveStream(actualIndexes.Count != 1)) as IDisposable) { ArchiveOpenCallback archiveOpenCallback = this.GetArchiveOpenCallback(); ulong maxCheckStartPosition = 32768; if (!this._Opened) { if (this._Archive.Open(archiveStream, ref maxCheckStartPosition, (IArchiveOpenCallback)archiveOpenCallback) != 0) { if (!this.ThrowException((SevenZipBase)null, (Exception) new SevenZipArchiveException())) { return; } } this._Opened = true; } using (ArchiveExtractCallback archiveExtractCallback = this.GetArchiveExtractCallback(directory, (int)this._FilesCount.Value, actualIndexes)) { try { this.CheckedExecute(this._Archive.Extract(indexes1, (uint)indexes1.Length, 0, (IArchiveExtractCallback)archiveExtractCallback), "The extraction has failed for an unknown reason with code ", (SevenZipBase)archiveExtractCallback); } finally { this.FreeArchiveExtractCallback(archiveExtractCallback); } } } this.OnExtractionFinished(EventArgs.Empty); } finally { if (actualIndexes.Count > 1) { this._Archive.Close(); this._ArchiveStream = (IInStream)null; this._Opened = false; } } this.ThrowUserException(); }
public void ExtractFile(int index, Stream stream) { this.DisposedCheck(); this.ClearExceptions(); if (!SevenZipExtractor.CheckIndexes(index)) { if (!this.ThrowException((SevenZipBase)null, (Exception) new ArgumentException("The index must be more or equal to zero.", nameof(index)))) { return; } } if (!stream.CanWrite) { if (!this.ThrowException((SevenZipBase)null, (Exception) new ArgumentException("The specified stream can not be written.", nameof(stream)))) { return; } } this.InitArchiveFileData(false); long num = (long)index; uint?filesCount = this._FilesCount; uint?nullable = filesCount.HasValue ? new uint?(filesCount.GetValueOrDefault() - 1U) : new uint?(); if ((num <= (long)nullable.GetValueOrDefault() ? 0 : (nullable.HasValue ? 1 : 0)) != 0) { if (!this.ThrowException((SevenZipBase)null, (Exception) new ArgumentOutOfRangeException(nameof(index), "The specified index is greater than the archive files count."))) { return; } } uint[] indexes = new uint[1] { (uint)index }; if (this._IsSolid.Value) { indexes = SevenZipExtractor.SolidIndexes(indexes); } IInStream archiveStream = this.GetArchiveStream(false); ArchiveOpenCallback archiveOpenCallback = this.GetArchiveOpenCallback(); ulong maxCheckStartPosition = 32768; if (!this._Opened) { if (this._Archive.Open(archiveStream, ref maxCheckStartPosition, (IArchiveOpenCallback)archiveOpenCallback) != 0) { this.ThrowException((SevenZipBase)null, (Exception) new SevenZipArchiveException()); } this._Opened = true; } using (ArchiveExtractCallback archiveExtractCallback = this.GetArchiveExtractCallback(stream, (uint)index, indexes.Length)) { try { this.CheckedExecute(this._Archive.Extract(indexes, (uint)indexes.Length, 0, (IArchiveExtractCallback)archiveExtractCallback), "The extraction has failed for an unknown reason with code ", (SevenZipBase)archiveExtractCallback); } finally { this.FreeArchiveExtractCallback(archiveExtractCallback); } } this.OnExtractionFinished(EventArgs.Empty); this.ThrowUserException(); }
private void GetArchiveInfo(bool disposeStream) { if (this._Archive == null) { if (!this.ThrowException((SevenZipBase)null, (Exception) new SevenZipArchiveException())) { ; } } else { IInStream archiveStream; using ((archiveStream = this.GetArchiveStream(disposeStream)) as IDisposable) { ArchiveOpenCallback archiveOpenCallback = this.GetArchiveOpenCallback(); ulong maxCheckStartPosition = 32768; if (!this._Opened) { if (this._Archive.Open(archiveStream, ref maxCheckStartPosition, (IArchiveOpenCallback)archiveOpenCallback) != 0) { if (!this.ThrowException((SevenZipBase)null, (Exception) new SevenZipArchiveException())) { return; } } this._Opened = !disposeStream; } this._FilesCount = new uint?(this._Archive.GetNumberOfItems()); uint?filesCount1 = this._FilesCount; if ((filesCount1.GetValueOrDefault() != 0U ? 0 : (filesCount1.HasValue ? 1 : 0)) != 0) { if (!this.ThrowException((SevenZipBase)null, (Exception) new SevenZipArchiveException())) { return; } } PropVariant var = new PropVariant(); this._ArchiveFileData = new List <ArchiveFileInfo>((int)this._FilesCount.Value); uint index1 = 0; while (true) { uint num = index1; uint?filesCount2 = this._FilesCount; if ((num >= filesCount2.GetValueOrDefault() ? 0 : (filesCount2.HasValue ? 1 : 0)) != 0) { try { ArchiveFileInfo archiveFileInfo = new ArchiveFileInfo() { Index = (int)index1 }; this._Archive.GetProperty(index1, ItemPropId.Path, ref var); archiveFileInfo.FileName = NativeMethods.SafeCast <string>(var, "[no name]"); this._Archive.GetProperty(index1, ItemPropId.LastWriteTime, ref var); archiveFileInfo.LastWriteTime = NativeMethods.SafeCast <DateTime>(var, DateTime.Now); this._Archive.GetProperty(index1, ItemPropId.CreationTime, ref var); archiveFileInfo.CreationTime = NativeMethods.SafeCast <DateTime>(var, DateTime.Now); this._Archive.GetProperty(index1, ItemPropId.LastAccessTime, ref var); archiveFileInfo.LastAccessTime = NativeMethods.SafeCast <DateTime>(var, DateTime.Now); this._Archive.GetProperty(index1, ItemPropId.Size, ref var); archiveFileInfo.Size = NativeMethods.SafeCast <ulong>(var, 0UL); this._Archive.GetProperty(index1, ItemPropId.Attributes, ref var); archiveFileInfo.Attributes = NativeMethods.SafeCast <uint>(var, 0U); this._Archive.GetProperty(index1, ItemPropId.IsDirectory, ref var); archiveFileInfo.IsDirectory = NativeMethods.SafeCast <bool>(var, false); this._Archive.GetProperty(index1, ItemPropId.Encrypted, ref var); archiveFileInfo.Encrypted = NativeMethods.SafeCast <bool>(var, false); this._Archive.GetProperty(index1, ItemPropId.Crc, ref var); archiveFileInfo.Crc = NativeMethods.SafeCast <uint>(var, 0U); this._Archive.GetProperty(index1, ItemPropId.Comment, ref var); archiveFileInfo.Comment = NativeMethods.SafeCast <string>(var, ""); this._ArchiveFileData.Add(archiveFileInfo); } catch (InvalidCastException ex) { this.ThrowException((SevenZipBase)null, (Exception) new SevenZipArchiveException("probably archive is corrupted.")); } ++index1; } else { break; } } uint archiveProperties = this._Archive.GetNumberOfArchiveProperties(); List <ArchiveProperty> archivePropertyList = new List <ArchiveProperty>((int)archiveProperties); for (uint index2 = 0; index2 < archiveProperties; ++index2) { string name; ItemPropId propId; ushort varType; this._Archive.GetArchivePropertyInfo(index2, out name, out propId, out varType); this._Archive.GetArchiveProperty(propId, ref var); if (propId == ItemPropId.Solid) { this._IsSolid = new bool?(NativeMethods.SafeCast <bool>(var, true)); } if (PropIdToName.PropIdNames.ContainsKey(propId)) { archivePropertyList.Add(new ArchiveProperty() { Name = PropIdToName.PropIdNames[propId], Value = var.Object }); } } this._ArchiveProperties = new ReadOnlyCollection <ArchiveProperty>((IList <ArchiveProperty>)archivePropertyList); if (!this._IsSolid.HasValue && this._Format == InArchiveFormat.Zip) { this._IsSolid = new bool?(false); } if (!this._IsSolid.HasValue) { this._IsSolid = new bool?(true); } } if (disposeStream) { this._Archive.Close(); this._ArchiveStream = (IInStream)null; } this._ArchiveFileInfoCollection = new ReadOnlyCollection <ArchiveFileInfo>((IList <ArchiveFileInfo>) this._ArchiveFileData); } }