Ejemplo n.º 1
0
 private void Decompress(Stream input, Stream output)
 {
     using (var zlibStream = new ZlibStream(NonDisposingStream.Create(input), CompressionMode.Decompress))
     {
         zlibStream.CopyTo(output);
     }
 }
Ejemplo n.º 2
0
        public static XZFooter FromStream(Stream stream)
        {
            var footer = new XZFooter(new BinaryReader(NonDisposingStream.Create(stream), Encoding.UTF8));

            footer.Process();
            return(footer);
        }
Ejemplo n.º 3
0
 protected void ArchiveStreamRead(ReaderOptions readerOptions, IEnumerable <string> testArchives)
 {
     foreach (var path in testArchives)
     {
         using (var stream = NonDisposingStream.Create(File.OpenRead(path), true))
             using (var archive = ArchiveFactory.Open(stream, readerOptions))
             {
                 try
                 {
                     foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory))
                     {
                         entry.WriteToDirectory(SCRATCH_FILES_PATH,
                                                new ExtractionOptions()
                         {
                             ExtractFullPath = true,
                             Overwrite       = true
                         });
                     }
                 }
                 catch (IndexOutOfRangeException)
                 {
                     //SevenZipArchive_BZip2_Split test needs this
                     stream.ThrowOnDispose = false;
                     throw;
                 }
                 stream.ThrowOnDispose = false;
             }
         VerifyFiles();
     }
 }
Ejemplo n.º 4
0
 private void Compress(Stream input, Stream output, int compressionLevel)
 {
     using (var zlibStream = new ZlibStream(NonDisposingStream.Create(output), CompressionMode.Compress, (CompressionLevel)compressionLevel))
     {
         zlibStream.FlushMode = FlushType.Sync;
         input.CopyTo(zlibStream);
     }
 }
Ejemplo n.º 5
0
 internal Volume(Stream stream, ReaderOptions readerOptions, int index = 0)
 {
     Index         = index;
     ReaderOptions = readerOptions;
     if (readerOptions.LeaveStreamOpen)
     {
         stream = NonDisposingStream.Create(stream);
     }
     _actualStream = stream;
 }
Ejemplo n.º 6
0
 public GZipWriter(Stream destination, GZipWriterOptions?options = null)
     : base(ArchiveType.GZip, options ?? new GZipWriterOptions())
 {
     if (WriterOptions.LeaveStreamOpen)
     {
         destination = NonDisposingStream.Create(destination);
     }
     InitalizeStream(new GZipStream(destination, CompressionMode.Compress,
                                    options?.CompressionLevel ?? CompressionLevel.Default,
                                    WriterOptions.ArchiveEncoding.GetEncoding()));
 }
Ejemplo n.º 7
0
 internal override Stream GetCompressedStream()
 {
     if (!Header.HasData)
     {
         return(Stream.Null);
     }
     _decompressionStream = CreateDecompressionStream(GetCryptoStream(CreateBaseStream()), Header.CompressionMethod);
     if (LeaveStreamOpen)
     {
         return(NonDisposingStream.Create(_decompressionStream));
     }
     return(_decompressionStream);
 }
Ejemplo n.º 8
0
        protected Stream GetCryptoStream(Stream plainStream)
        {
            bool isFileEncrypted = FlagUtility.HasFlag(Header.Flags, HeaderFlags.Encrypted);

            if (Header.CompressedSize == 0 && isFileEncrypted)
            {
                throw new NotSupportedException("Cannot encrypt file with unknown size at start.");
            }

            if ((Header.CompressedSize == 0 &&
                 FlagUtility.HasFlag(Header.Flags, HeaderFlags.UsePostDataDescriptor)) ||
                Header.IsZip64)
            {
                plainStream = NonDisposingStream.Create(plainStream); //make sure AES doesn't close
            }
            else
            {
                plainStream = new ReadOnlySubStream(plainStream, Header.CompressedSize); //make sure AES doesn't close
            }

            if (isFileEncrypted)
            {
                switch (Header.CompressionMethod)
                {
                case ZipCompressionMethod.None:
                case ZipCompressionMethod.Deflate:
                case ZipCompressionMethod.Deflate64:
                case ZipCompressionMethod.BZip2:
                case ZipCompressionMethod.LZMA:
                case ZipCompressionMethod.PPMd:
                {
                    return(new PkwareTraditionalCryptoStream(plainStream, Header.ComposeEncryptionData(plainStream), CryptoMode.Decrypt));
                }

                case ZipCompressionMethod.WinzipAes:
                {
                    if (Header.WinzipAesEncryptionData != null)
                    {
                        return(new WinzipAesCryptoStream(plainStream, Header.WinzipAesEncryptionData, Header.CompressedSize - 10));
                    }
                    return(plainStream);
                }

                default:
                {
                    throw new InvalidOperationException("Header.CompressionMethod is invalid");
                }
                }
            }
            return(plainStream);
        }
Ejemplo n.º 9
0
        public void TestSharpCompressWithEmptyStream()
        {
            var expected = new Tuple <string, byte[]>[]
            {
                new Tuple <string, byte[]>("foo.txt", Array.Empty <byte>()),
                new Tuple <string, byte[]>("foo2.txt", new byte[10])
            };

            using (var memory = new MemoryStream())
            {
                Stream stream = new TestStream(memory, read: true, write: true, seek: false);

                using (IWriter zipWriter = WriterFactory.Open(stream, ArchiveType.Zip, CompressionType.Deflate))
                {
                    zipWriter.Write(expected[0].Item1, new MemoryStream(expected[0].Item2));
                    zipWriter.Write(expected[1].Item1, new MemoryStream(expected[1].Item2));
                }

                stream = new MemoryStream(memory.ToArray());
                File.WriteAllBytes(Path.Combine(SCRATCH_FILES_PATH, "foo.zip"), memory.ToArray());

                using (IReader zipReader = ZipReader.Open(NonDisposingStream.Create(stream, true)))
                {
                    var i = 0;
                    while (zipReader.MoveToNextEntry())
                    {
                        using (EntryStream entry = zipReader.OpenEntryStream())
                        {
                            MemoryStream tempStream = new MemoryStream();
                            const int    bufSize    = 0x1000;
                            byte[]       buf        = new byte[bufSize];
                            int          bytesRead  = 0;
                            while ((bytesRead = entry.Read(buf, 0, bufSize)) > 0)
                            {
                                tempStream.Write(buf, 0, bytesRead);
                            }

                            Assert.Equal(expected[i].Item1, zipReader.Entry.Key);
                            Assert.Equal(expected[i].Item2, tempStream.ToArray());
                        }
                        i++;
                    }
                }
            }
        }
Ejemplo n.º 10
0
        protected void ArchiveStreamReadExtractAll(IEnumerable <string> testArchives, CompressionType compression)
        {
            foreach (var path in testArchives)
            {
                using (var stream = NonDisposingStream.Create(File.OpenRead(path), true))
                {
                    try
                    {
                        using (var archive = ArchiveFactory.Open(stream))
                        {
                            Assert.True(archive.IsSolid);
                            using (var reader = archive.ExtractAllEntries())
                            {
                                UseReader(reader, compression);
                            }
                            VerifyFiles();

                            if (archive.Entries.First().CompressionType == CompressionType.Rar)
                            {
                                stream.ThrowOnDispose = false;
                                return;
                            }
                            foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory))
                            {
                                entry.WriteToDirectory(SCRATCH_FILES_PATH,
                                                       new ExtractionOptions
                                {
                                    ExtractFullPath = true,
                                    Overwrite       = true
                                });
                            }
                            stream.ThrowOnDispose = false;
                        }
                    }
                    catch (Exception)
                    {
                        // Otherwise this will hide the original exception.
                        stream.ThrowOnDispose = false;
                        throw;
                    }
                }
                VerifyFiles();
            }
        }
Ejemplo n.º 11
0
        public TarWriter(Stream destination, TarWriterOptions options)
            : base(ArchiveType.Tar, options)
        {
            finalizeArchiveOnClose = options.FinalizeArchiveOnClose;

            if (!destination.CanWrite)
            {
                throw new ArgumentException("Tars require writable streams.");
            }
            if (WriterOptions.LeaveStreamOpen)
            {
                destination = NonDisposingStream.Create(destination);
            }
            switch (options.CompressionType)
            {
            case CompressionType.None:
                break;

            case CompressionType.BZip2:
            {
                destination = new BZip2Stream(destination, CompressionMode.Compress, false);
            }
            break;

            case CompressionType.GZip:
            {
                destination = new GZipStream(destination, CompressionMode.Compress);
            }
            break;

            case CompressionType.LZip:
            {
                destination = new LZipStream(destination, CompressionMode.Compress);
            }
            break;

            default:
            {
                throw new InvalidFormatException("Tar does not support compression: " + options.CompressionType);
            }
            }
            InitalizeStream(destination);
        }
Ejemplo n.º 12
0
        public ZipWriter(Stream destination, ZipWriterOptions zipWriterOptions)
            : base(ArchiveType.Zip, zipWriterOptions)
        {
            zipComment = zipWriterOptions.ArchiveComment ?? string.Empty;
            isZip64    = zipWriterOptions.UseZip64;
            if (destination.CanSeek)
            {
                streamPosition = destination.Position;
            }

            compressionType  = zipWriterOptions.CompressionType;
            compressionLevel = zipWriterOptions.DeflateCompressionLevel;

            if (WriterOptions.LeaveStreamOpen)
            {
                destination = NonDisposingStream.Create(destination);
            }
            InitalizeStream(destination);
        }
Ejemplo n.º 13
0
        private void ReadImpl(string testArchive, CompressionType expectedCompression, ReaderOptions options)
        {
            using (var file = File.OpenRead(testArchive))
            {
                using (var protectedStream = NonDisposingStream.Create(new ForwardOnlyStream(file), throwOnDispose: true))
                {
                    using (var testStream = new TestStream(protectedStream))
                    {
                        using (var reader = ReaderFactory.Open(testStream, options))
                        {
                            UseReader(reader, expectedCompression);
                            protectedStream.ThrowOnDispose = false;
                            Assert.False(testStream.IsDisposed, "{nameof(testStream)} prematurely closed");
                        }

                        // Boolean XOR -- If the stream should be left open (true), then the stream should not be diposed (false)
                        // and if the stream should be closed (false), then the stream should be disposed (true)
                        var message = $"{nameof(options.LeaveStreamOpen)} is set to '{options.LeaveStreamOpen}', so {nameof(testStream.IsDisposed)} should be set to '{!testStream.IsDisposed}', but is set to {testStream.IsDisposed}";
                        Assert.True(options.LeaveStreamOpen != testStream.IsDisposed, message);
                    }
                }
            }
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Opens a Reader for Non-seeking usage
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static IReader Open(Stream stream, ReaderOptions?options = null)
        {
            stream.CheckNotNull(nameof(stream));
            options = options ?? new ReaderOptions()
            {
                LeaveStreamOpen = false
            };
            RewindableStream rewindableStream = new RewindableStream(stream);

            rewindableStream.StartRecording();
            if (ZipArchive.IsZipFile(rewindableStream, options.Password))
            {
                rewindableStream.Rewind(true);
                return(ZipReader.Open(rewindableStream, options));
            }
            rewindableStream.Rewind(false);
            if (GZipArchive.IsGZipFile(rewindableStream))
            {
                rewindableStream.Rewind(false);
                GZipStream testStream = new GZipStream(rewindableStream, CompressionMode.Decompress);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, options, CompressionType.GZip));
                }
                rewindableStream.Rewind(true);
                return(GZipReader.Open(rewindableStream, options));
            }

            rewindableStream.Rewind(false);
            if (BZip2Stream.IsBZip2(rewindableStream))
            {
                rewindableStream.Rewind(false);
                BZip2Stream testStream = new BZip2Stream(NonDisposingStream.Create(rewindableStream), CompressionMode.Decompress, false);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, options, CompressionType.BZip2));
                }
            }

            rewindableStream.Rewind(false);
            if (LZipStream.IsLZipFile(rewindableStream))
            {
                rewindableStream.Rewind(false);
                LZipStream testStream = new LZipStream(NonDisposingStream.Create(rewindableStream), CompressionMode.Decompress);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, options, CompressionType.LZip));
                }
            }
            rewindableStream.Rewind(false);
            if (RarArchive.IsRarFile(rewindableStream, options))
            {
                rewindableStream.Rewind(true);
                return(RarReader.Open(rewindableStream, options));
            }

            rewindableStream.Rewind(false);
            if (TarArchive.IsTarFile(rewindableStream))
            {
                rewindableStream.Rewind(true);
                return(TarReader.Open(rewindableStream, options));
            }
            rewindableStream.Rewind(false);
            if (XZStream.IsXZStream(rewindableStream))
            {
                rewindableStream.Rewind(true);
                XZStream testStream = new XZStream(rewindableStream);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, options, CompressionType.Xz));
                }
            }
            throw new InvalidOperationException("Cannot determine compressed stream type.  Supported Reader Formats: Zip, GZip, BZip2, Tar, Rar, LZip, XZ");
        }
Ejemplo n.º 15
0
 public override Stream OpenEntryStream()
 {
     //ensure new stream is at the start, this could be reset
     stream.Seek(0, SeekOrigin.Begin);
     return(NonDisposingStream.Create(stream));
 }