Beispiel #1
0
        public IExtractor ExtractFile(UInt64 index, Stream outputStream)
        {
            if (index >= (ulong)_Files.LongLength)
            {
                throw new ArgumentOutOfRangeException($"Index `{index}` is out of range.");
            }

            if (outputStream == null || !outputStream.CanWrite)
            {
                throw new ArgumentException($"Stream `{nameof(outputStream)}` is invalid or cannot be written to.");
            }

            SevenZipArchiveFile file = _Files[index];

            if (file.IsEmpty)
            {
                Trace.TraceWarning($"Filename: {file.Name} is a directory, empty file or anti file, nothing to output to stream.");
            }
            else
            {
                Trace.TraceInformation($"Filename: `{file.Name}`, file size: `{file.Size} bytes`.");

                var sx = new SevenZipStreamsExtractor(stream, header.RawHeader.MainStreamsInfo);
                sx.Extract((UInt64)file.UnPackIndex, outputStream);
            }

            return(this);
        }
Beispiel #2
0
        public IExtractor ExtractFile(UInt64 index, string outputDirectory)
        {
            if (index >= (ulong)_Files.LongLength)
            {
                throw new ArgumentOutOfRangeException($"Index `{index}` is out of range.");
            }

            SevenZipArchiveFile file = _Files[index];

            if (!preProcessFile(outputDirectory, file))
            {
                string fullPath = Path.Combine(outputDirectory, PreserveDirectoryStructure ? file.Name : Path.GetFileName(file.Name));

                // progress provider
                SevenZipProgressProvider szpp = null;
                if (ProgressDelegate != null)
                {
                    szpp = new SevenZipProgressProvider(_Files, new[] { index }, ProgressDelegate);
                }

                // extraction
                Trace.TraceInformation($"Filename: `{file.Name}`, file size: `{file.Size} bytes`.");
                var sx = new SevenZipStreamsExtractor(stream, header.RawHeader.MainStreamsInfo, Password);
                using (Stream fileStream = File.Create(fullPath))
                    sx.Extract((UInt64)file.UnPackIndex, fileStream, szpp);
                if (file.Time != null)
                {
                    File.SetLastWriteTimeUtc(fullPath, (DateTime)file.Time);
                }
            }

            return(this);
        }
Beispiel #3
0
        public IExtractor ExtractFile(UInt64 index, string outputDirectory)
        {
            if (index >= (ulong)_Files.LongLength)
            {
                throw new ArgumentOutOfRangeException($"Index `{index}` is out of range.");
            }

            SevenZipArchiveFile file = _Files[index];

            if (!Directory.Exists(outputDirectory))
            {
                Directory.CreateDirectory(outputDirectory);
            }

            if (!processFile(outputDirectory, file))
            {
                string fullPath = Path.Combine(outputDirectory, PreserveDirectoryStructure ? file.Name : Path.GetFileName(file.Name));
                if (File.Exists(fullPath) && !OverwriteExistingFiles && !SkipExistingFiles)
                {
                    throw new IOException($"File `{file.Name}` already exists.");
                }
                if (!File.Exists(fullPath) || OverwriteExistingFiles)
                {
                    Trace.TraceInformation($"Filename: `{file.Name}`, file size: `{file.Size} bytes`.");

                    var sx = new SevenZipStreamsExtractor(stream, header.RawHeader.MainStreamsInfo);
                    using (Stream fileStream = File.Create(fullPath))
                        sx.Extract((UInt64)file.UnPackIndex, fileStream);
                    if (file.Time != null)
                    {
                        File.SetLastWriteTimeUtc(fullPath, (DateTime)file.Time);
                    }
                }
                else
                {
                    Trace.TraceWarning($"File `{file.Name} already exists, skipping.");
                }
            }

            return(this);
        }