예제 #1
0
 private void FreeArchiveExtractCallback(ArchiveExtractCallback callback)
 {
     callback.Open -= (EventHandler <OpenEventArgs>)((s, e) => this._UnpackedSize = new long?((long)e.TotalSize));
     callback.FileExtractionStarted  -= this.FileExtractionStarted;
     callback.FileExtractionFinished -= this.FileExtractionFinished;
     callback.Extracting             -= this.Extracting;
     callback.FileExists             -= this.FileExists;
 }
예제 #2
0
        private ArchiveExtractCallback GetArchiveExtractCallback(Stream stream, uint index, int filesCount)
        {
            ArchiveExtractCallback archiveExtractCallback = string.IsNullOrEmpty(this.Password) ? new ArchiveExtractCallback(this._Archive, stream, filesCount, index, this) : new ArchiveExtractCallback(this._Archive, stream, filesCount, index, this.Password, this);

            archiveExtractCallback.Open += (EventHandler <OpenEventArgs>)((s, e) => this._UnpackedSize = new long?((long)e.TotalSize));
            archiveExtractCallback.FileExtractionStarted  += this.FileExtractionStarted;
            archiveExtractCallback.FileExtractionFinished += this.FileExtractionFinished;
            archiveExtractCallback.Extracting             += this.Extracting;
            archiveExtractCallback.FileExists             += this.FileExists;
            return(archiveExtractCallback);
        }
예제 #3
0
 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();
 }
예제 #4
0
        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();
        }
예제 #5
0
        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();
        }
예제 #6
0
 public int GetStream(uint index, out ISequentialOutStream outStream, AskMode askExtractMode)
 {
     outStream = (ISequentialOutStream)null;
     if (askExtractMode == AskMode.Extract)
     {
         string str1 = this._Directory;
         for (int index1 = 0; index1 < 1; ++index1)
         {
             if (!this._FileIndex.HasValue)
             {
                 if (this._ActualIndexes == null || this._ActualIndexes.Contains(index))
                 {
                     PropVariant var = new PropVariant();
                     this._Archive.GetProperty(index, ItemPropId.Path, ref var);
                     string str2 = NativeMethods.SafeCast <string>(var, "");
                     if (string.IsNullOrEmpty(str2))
                     {
                         if (this._FilesCount == 1)
                         {
                             string fileName = Path.GetFileName(this._Extractor.FileName);
                             string str3     = fileName.Substring(0, fileName.LastIndexOf('.'));
                             if (!str3.EndsWith(".tar", StringComparison.OrdinalIgnoreCase))
                             {
                                 str3 += ".tar";
                             }
                             str2 = str3;
                         }
                         else
                         {
                             str2 = "[no name] " + index.ToString((IFormatProvider)CultureInfo.InvariantCulture);
                         }
                     }
                     str1 += str2;
                     this._Archive.GetProperty(index, ItemPropId.IsDirectory, ref var);
                     try
                     {
                         str1 = ArchiveExtractCallback.ValidateFileName(str1);
                     }
                     catch (Exception ex)
                     {
                         this.AddException(ex);
                         break;
                     }
                     if (!NativeMethods.SafeCast <bool>(var, false))
                     {
                         this._Archive.GetProperty(index, ItemPropId.LastWriteTime, ref var);
                         DateTime time = NativeMethods.SafeCast <DateTime>(var, DateTime.MinValue);
                         if (File.Exists(str1))
                         {
                             FileOverwriteEventArgs e = new FileOverwriteEventArgs(str1);
                             this.OnFileExists(e);
                             if (e.Cancel)
                             {
                                 this.Canceled = true;
                                 return(-1);
                             }
                             if (string.IsNullOrEmpty(e.FileName))
                             {
                                 outStream = (ISequentialOutStream)this._FakeStream;
                                 break;
                             }
                             str1 = e.FileName;
                         }
                         try
                         {
                             this._FileStream = new OutStreamWrapper((Stream)File.Create(str1), str1, time, true);
                         }
                         catch (Exception ex)
                         {
                             if (ex is FileNotFoundException)
                             {
                                 this.AddException((Exception) new IOException("The file \"" + str1 + "\" was not extracted due to the File.Create fail."));
                             }
                             else
                             {
                                 this.AddException(ex);
                             }
                             outStream = (ISequentialOutStream)this._FakeStream;
                             break;
                         }
                         this._FileStream.BytesWritten += new EventHandler <IntEventArgs>(this.IntEventArgsHandler);
                         outStream = (ISequentialOutStream)this._FileStream;
                     }
                     else if (!Directory.Exists(str1))
                     {
                         try
                         {
                             Directory.CreateDirectory(str1);
                         }
                         catch (Exception ex)
                         {
                             this.AddException(ex);
                         }
                         outStream = (ISequentialOutStream)this._FakeStream;
                     }
                 }
                 else
                 {
                     outStream = (ISequentialOutStream)this._FakeStream;
                 }
             }
             else
             {
                 uint num       = index;
                 uint?fileIndex = this._FileIndex;
                 if (((int)num != (int)fileIndex.GetValueOrDefault() ? 0 : (fileIndex.HasValue ? 1 : 0)) != 0)
                 {
                     outStream       = (ISequentialOutStream)this._FileStream;
                     this._FileIndex = new uint?();
                 }
                 else
                 {
                     outStream = (ISequentialOutStream)this._FakeStream;
                 }
             }
         }
         this._DoneRate += 1f / (float)this._FilesCount;
         FileInfoEventArgs e1 = new FileInfoEventArgs(this._Extractor.ArchiveFileData[(int)index], PercentDoneEventArgs.ProducePercentDone(this._DoneRate));
         this.OnFileExtractionStarted(e1);
         if (e1.Cancel)
         {
             if (!string.IsNullOrEmpty(str1))
             {
                 this._FileStream.Dispose();
                 if (File.Exists(str1))
                 {
                     try
                     {
                         File.Delete(str1);
                     }
                     catch (Exception ex)
                     {
                         this.AddException(ex);
                     }
                 }
             }
             this.Canceled = true;
             return(-1);
         }
     }
     return(0);
 }