private void Init(
            Stream stream, SevenZipCompressor compressor, UpdateData updateData, bool directoryStructure)
        {
            _fileStream            = new InStreamWrapper(stream, false);
            _fileStream.BytesRead += IntEventArgsHandler;
            _actualFilesCount      = 1;

            try
            {
                _bytesCount = stream.Length;
            }
            catch (NotSupportedException)
            {
                _bytesCount = -1;
            }
            try
            {
                stream.Seek(0, SeekOrigin.Begin);
            }
            catch (NotSupportedException)
            {
                _bytesCount = -1;
            }
            CommonInit(compressor, updateData, directoryStructure);
        }
Exemplo n.º 2
0
 public int GetStream(string name, out IInStream inStream)
 {
     if (!File.Exists(name))
     {
         name = Path.Combine(Path.GetDirectoryName(_fileInfo.FullName), name);
         if (!File.Exists(name))
         {
             inStream = null;
             AddException(new FileNotFoundException("The volume \"" + name + "\" was not found. Extraction can be impossible."));
             return(1);
         }
     }
     _volumeFileNames.Add(name);
     if (_wrappers.ContainsKey(name))
     {
         inStream = _wrappers[name];
     }
     else
     {
         try
         {
             var wrapper = new InStreamWrapper(
                 new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), true);
             _wrappers.Add(name, wrapper);
             inStream = wrapper;
         }
         catch (Exception)
         {
             AddException(new FileNotFoundException("Failed to open the volume \"" + name + "\". Extraction is impossible."));
             inStream = null;
             return(1);
         }
     }
     return(0);
 }
Exemplo n.º 3
0
        /// <summary>
        /// Gets the stream for 7-zip library.
        /// </summary>
        /// <param name="index">File index</param>
        /// <param name="inStream">Input file stream</param>
        /// <returns>Zero if Ok</returns>
        public int GetStream(uint index, out
#if !MONO
                             ISequentialInStream
#else
                             HandleRef
#endif
                             inStream)
        {
            index -= _indexOffset;

            if (_files != null)
            {
                _fileStream = null;
                try
                {
                    string fullName = _files[index].FullName;
                    // mmmmm
                    if (File.Exists(fullName))
                    {
                        _fileStream = new InStreamWrapper(
                            new FileStream(fullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite),
                            true);

                        if (_compressor.ArchiveFormat == OutArchiveFormat.Zip)
                        {
                            _wrappersToDispose.Add(_fileStream);
                        }
                    }
                }
                catch (Exception e)
                {
                    AddException(e);
                    inStream = null;
                    return(-1);
                }
                inStream = _fileStream;
                if (!EventsForGetStream(index))
                {
                    return(-1);
                }
            }
            else
            {
                if (_streams == null)
                {
                    inStream = _fileStream;
                }
                else
                {
                    _fileStream = new InStreamWrapper(_streams[index], true);
                    inStream    = _fileStream;
                    if (!EventsForGetStream(index))
                    {
                        return(-1);
                    }
                }
            }
            return(0);
        }
Exemplo n.º 4
0
        public void SetOperationResult(OperationResult operationResult)
        {
            if (operationResult != OperationResult.Ok && ReportErrors)
            {
                if (_fileStream != null)
                {
                    try
                    {
                        _fileStream.Dispose();
                    }
                    catch (ObjectDisposedException) { }
                    _fileStream = null;
                }

                switch (operationResult)
                {
                case OperationResult.CrcError:
                    AddException(new ExtractionFailedException("File is corrupted. Crc check has failed."));
                    break;

                case OperationResult.DataError:
                    AddException(new ExtractionFailedException("File is corrupted. Data error has occured."));
                    break;

                case OperationResult.UnsupportedMethod:
                    AddException(new ExtractionFailedException("Unsupported method error has occured."));
                    break;
                }
            }
            if (_fileStream != null)
            {
                _fileStream.BytesRead -= IntEventArgsHandler;
                //Specific Zip implementation - can not Dispose files for Zip.
                if (_compressor.ArchiveFormat != OutArchiveFormat.Zip)
                {
                    try
                    {
                        _fileStream.Dispose();
                    }
                    catch (ObjectDisposedException) {}
                }
                else
                {
                    _wrappersToDispose.Add(_fileStream);
                }
                _fileStream = null;
                //remove useless GC.collect
//                GC.Collect();
                // Issue #6987
                //GC.WaitForPendingFinalizers();
            }
            OnFileCompressionFinished(EventArgs.Empty);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Gets the stream for 7-zip library.
        /// </summary>
        /// <param name="index">File index</param>
        /// <param name="inStream">Input file stream</param>
        /// <returns>Zero if Ok</returns>
        public int GetStream(uint index, out
#if !MONO
                             ISequentialInStream
#else
                             HandleRef
#endif
                             inStream)
        {
            index -= _indexOffset;
            if (_files != null)
            {
                _fileStream = null;
                try
                {
                    if (File.Exists(_files[index].FullName))
                    {
                        _fileStream = new InStreamWrapper(
                            new FileStream(_files[index].FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite),
                            true);
                    }
                }
                catch (Exception e)
                {
                    AddException(e);
                    inStream = null;
                    return(-1);
                }
                inStream = _fileStream;
                if (!EventsForGetStream(index))
                {
                    return(-1);
                }
            }
            else
            {
                if (_streams == null)
                {
                    inStream = _fileStream;
                }
                else
                {
                    _fileStream = new InStreamWrapper(_streams[index], true);
                    inStream    = _fileStream;
                    if (!EventsForGetStream(index))
                    {
                        return(-1);
                    }
                }
            }
            return(0);
        }
Exemplo n.º 6
0
 public int GetStream(string name, out IInStream inStream)
 {
     if (!File.Exists(name))
     {
         name = Path.Combine(Path.GetDirectoryName(_fileInfo.FullName), name);
         if (!File.Exists(name))
         {
             inStream = null;
             AddException(new FileNotFoundException("The volume \"" + name + "\" was not found. Extraction can be impossible."));
             return 1;
         }
     }
     _volumeFileNames.Add(name);
     if (_wrappers.ContainsKey(name))
     {
         inStream = _wrappers[name];
     }
     else
     {
         try
         {
             var wrapper = new InStreamWrapper(
                 new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), true);
             _wrappers.Add(name, wrapper);
             inStream = wrapper;
         }
         catch (Exception)
         {
             AddException(new FileNotFoundException("Failed to open the volume \"" + name + "\". Extraction is impossible."));
             inStream = null;
             return 1;
         }
     }
     return 0;
 }
Exemplo n.º 7
0
 public void SetOperationResult(OperationResult operationResult)
 {
     if (operationResult != OperationResult.Ok && ReportErrors)
     {
         switch (operationResult)
         {
             case OperationResult.CrcError:
                 AddException(new ExtractionFailedException("File is corrupted. Crc check has failed."));
                 break;
             case OperationResult.DataError:
                 AddException(new ExtractionFailedException("File is corrupted. Data error has occured."));
                 break;
             case OperationResult.UnsupportedMethod:
                 AddException(new ExtractionFailedException("Unsupported method error has occured."));
                 break;
         }
     }
     if (_fileStream != null)
     {
         
             _fileStream.BytesRead -= IntEventArgsHandler;
             //Specific Zip implementation - can not Dispose files for Zip.
             if (_compressor.ArchiveFormat != OutArchiveFormat.Zip)
             {
                 try
                 {
                     _fileStream.Dispose();                            
                 }
                 catch (ObjectDisposedException) {}
             }
             else
             {
                 _wrappersToDispose.Add(_fileStream);
             }                                
         _fileStream = null;
         GC.Collect();
         // Issue #6987
         //GC.WaitForPendingFinalizers();
     }
     OnFileCompressionFinished(EventArgs.Empty);
 }
Exemplo n.º 8
0
        /// <summary>
        /// Gets the stream for 7-zip library.
        /// </summary>
        /// <param name="index">File index</param>
        /// <param name="inStream">Input file stream</param>
        /// <returns>Zero if Ok</returns>
        public int GetStream(uint index, out 
#if !MONO
		                     ISequentialInStream
#else
		                     HandleRef
#endif
		                     inStream)
        {
            index -= _indexOffset;
            if (_files != null)
            {
                _fileStream = null;
                try
                {
                    if (File.Exists(_files[index].FullName))
                    {
                        _fileStream = new InStreamWrapper(
                            new FileStream(_files[index].FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite),
                            true);
                    }
                }
                catch (Exception e)
                {
                    AddException(e);
                    inStream = null;
                    return -1;
                }
                inStream = _fileStream;
                if (!EventsForGetStream(index))
                {
                    return -1;
                }
            }
            else
            {
                if (_streams == null)
                {
                    inStream = _fileStream;
                }
                else
                {
                    _fileStream = new InStreamWrapper(_streams[index], true);
                    inStream = _fileStream;
                    if (!EventsForGetStream(index))
                    {
                        return -1;
                    }
                }
            }
            return 0;
        }
Exemplo n.º 9
0
 private void Init(
     Stream stream, SevenZipCompressor compressor, UpdateData updateData, bool directoryStructure)
 {
     _fileStream = new InStreamWrapper(stream, false);
     _fileStream.BytesRead += IntEventArgsHandler;
     _actualFilesCount = 1;
     try
     {
         _bytesCount = stream.Length;
     }
     catch (NotSupportedException)
     {
         _bytesCount = -1;
     }
     try
     {
         stream.Seek(0, SeekOrigin.Begin);
     }
     catch (NotSupportedException)
     {
         _bytesCount = -1;
     }
     CommonInit(compressor, updateData, directoryStructure);
 }
Exemplo n.º 10
0
        public void SetOperationResult(OperationResult operationResult)
        {
            if (operationResult != OperationResult.Ok && ReportErrors)
            {
                switch (operationResult)
                {
                case OperationResult.CrcError:
                    AddException(new ExtractionFailedException("File is corrupted. Crc check has failed."));
                    break;

                case OperationResult.DataError:
                    AddException(new ExtractionFailedException("File is corrupted. Data error has occured."));
                    break;

                case OperationResult.UnsupportedMethod:
                    AddException(new ExtractionFailedException("Unsupported method error has occured."));
                    break;

                case OperationResult.Unavailable:
                    AddException(new ExtractionFailedException("File is unavailable."));
                    break;

                case OperationResult.UnexpectedEnd:
                    AddException(new ExtractionFailedException("Unexpected end of file."));
                    break;

                case OperationResult.DataAfterEnd:
                    AddException(new ExtractionFailedException("Data after end of archive."));
                    break;

                case OperationResult.IsNotArc:
                    AddException(new ExtractionFailedException("File is not archive."));
                    break;

                case OperationResult.HeadersError:
                    AddException(new ExtractionFailedException("Archive headers error."));
                    break;

                case OperationResult.WrongPassword:
                    AddException(new ExtractionFailedException("Wrong password."));
                    break;

                default:
                    AddException(new ExtractionFailedException($"Unexpected operation result: {operationResult}"));
                    break;
                }
            }
            if (_fileStream != null)
            {
                _fileStream.BytesRead -= IntEventArgsHandler;
                //Specific Zip implementation - can not Dispose files for Zip.
                if (_compressor.ArchiveFormat != OutArchiveFormat.Zip)
                {
                    try
                    {
                        _fileStream.Dispose();
                    }
                    catch (ObjectDisposedException) { }
                }
                else
                {
                    _wrappersToDispose.Add(_fileStream);
                }
                _fileStream = null;
            }
            OnFileCompressionFinished(EventArgs.Empty);
        }