Beispiel #1
0
 internal PscxSevenZipReader(PscxCmdlet command, FileInfo file, ArchiveFormat format) :
     base(command, file, format)
 {
     command.WriteDebug(String.Format("Created {0} reader for {1}.", format, file));
 }
Beispiel #2
0
            public int GetStream(uint index, out ISequentialOutStream outStream, AskMode askExtractMode)
            {
                //return 0x80004004L; // E_ABORT
                // S_FALSE = 1
                // S_OK = 0

                // todo: check if index matches an entry in _entries, setup the stream.
                Debug.WriteLine(String.Format("Mode: {0}; ArchiveCallback->GetStream {1}", askExtractMode, index));
                _currentIndex = index;
                if (askExtractMode == AskMode.kSkip)
                {
                    var stream = new OutStreamWrapper(new MemoryStream());
                    outStream = stream;
                    return(0);
                }
                string _transform_pattern = null;
                string _transform_text    = null;

                // if transform is specified, make sure it is well formed
                if (_transform != null)
                {
                    // substitute pattern specified in _transform
                    // _transform is split by "/", i.e. "regex/replacement"
                    string[] transform_data = _transform.Split('/');
                    if (transform_data.Length == 2)
                    {
                        _transform_pattern = transform_data[0];
                        _transform_text    = transform_data[1];
                        // remove any escape chars from the text
                        _transform_text = Regex.Unescape(_transform_text);
                        _command.WriteDebug("ArchiveCallback->GetStream transform_pattern: " + _transform_pattern);
                        _command.WriteDebug("ArchiveCallback->GetStream transform_text: " + _transform_text);
                    }
                    else
                    {
                        // bad transform, error...
                        _command.WriteWarning("Transform must be in the form regex/text!");
                        outStream = null;
                        return(1); // S_FALSE
                    }
                }

                try
                {
                    if ((_entries.ContainsKey(index)) && (askExtractMode == AskMode.kExtract))
                    {
                        // ensure output directory is base
                        _currentEntry = _entries[index];
                        if (_transform_pattern != null)
                        {
                            // substitute pattern specified in _transform_pattern with _transform_text
                            Regex transformer = new Regex(_transform_pattern);
                            // only run through replacement if there is a match for this entry
                            if (transformer.IsMatch(_currentEntry))
                            {
                                string newFilename = transformer.Replace(_currentEntry, _transform_text);
                                _command.WriteVerbose("Transform: " + _currentEntry + " to: " + newFilename);
                                _currentFileName = Path.Combine(_outputPath, newFilename);
                            }
                            else
                            {
                                _currentFileName = Path.Combine(_outputPath, _entries[index]);
                            }
                        }
                        else
                        {
                            _currentFileName = Path.Combine(_outputPath, _entries[index]);
                        }

                        string fileDir = Path.GetDirectoryName(_currentFileName);
                        if (!string.IsNullOrEmpty(fileDir))
                        {
                            Directory.CreateDirectory(fileDir);
                        }
                        _fileStream = new OutStreamWrapper(File.Create(_currentFileName));
                        outStream   = _fileStream;
                    }
                    else
                    {
                        outStream = null;
                    }
                }
                catch (PipelineStoppedException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    _command.WriteWarning("ArchiveCallBack->GetStream error: " + ex.ToString());
                    outStream = null;

                    return(1); // S_FALSE
                }
                return(0);     // S_OK
            }