Ejemplo n.º 1
0
        protected Stream GetCryptoStream(Stream plainStream)
        {
            if ((Header.CompressedSize == 0)
#if !PORTABLE && !NETFX_CORE
                && ((Header.PkwareTraditionalEncryptionData != null) ||
                    (Header.WinzipAesEncryptionData != null)))
#else
                && (Header.PkwareTraditionalEncryptionData != null))
#endif
            {
                throw new NotSupportedException("Cannot encrypt file with unknown size at start.");
            }
            if ((Header.CompressedSize == 0) &&
                FlagUtility.HasFlag(Header.Flags, HeaderFlags.UsePostDataDescriptor))
            {
                plainStream = new NonDisposingStream(plainStream); //make sure AES doesn't close
            }
            else
            {
                plainStream = new ReadOnlySubStream(plainStream, Header.CompressedSize); //make sure AES doesn't close
            }
            if (Header.PkwareTraditionalEncryptionData != null)
            {
                return(new PkwareTraditionalCryptoStream(plainStream, Header.PkwareTraditionalEncryptionData,
                                                         CryptoMode.Decrypt));
            }
#if !PORTABLE && !NETFX_CORE
            if (Header.WinzipAesEncryptionData != null)
            {
                //only read 10 less because the last ten are auth bytes
                return(new WinzipAesCryptoStream(plainStream, Header.WinzipAesEncryptionData, Header.CompressedSize - 10));
            }
#endif
            return(plainStream);
        }
Ejemplo n.º 2
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 = new NonDisposingStream(destination);
            }
            switch (options.CompressionType)
            {
            case CompressionType.None:
                break;

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

            default:
            {
                throw new InvalidFormatException("Tar does not support compression: " + options.CompressionType);
            }
            }
            InitalizeStream(destination);
        }
Ejemplo n.º 3
0
        protected Stream GetCryptoStream(Stream plainStream)
        {
            if ((Header.CompressedSize == 0)

                && (Header.PkwareTraditionalEncryptionData != null))
            {
                throw new NotSupportedException("Cannot encrypt file with unknown size at start.");
            }
            if ((Header.CompressedSize == 0) &&
                FlagUtility.HasFlag(Header.Flags, HeaderFlags.UsePostDataDescriptor))
            {
                plainStream = new NonDisposingStream(plainStream); //make sure AES doesn't close
            }
            else
            {
                plainStream = new ReadOnlySubStream(plainStream, Header.CompressedSize); //make sure AES doesn't close
            }
            if (Header.PkwareTraditionalEncryptionData != null)
            {
                return(new PkwareTraditionalCryptoStream(plainStream, Header.PkwareTraditionalEncryptionData,
                                                         CryptoMode.Decrypt));
            }

            return(plainStream);
        }
Ejemplo n.º 4
0
 protected void ArchiveStreamRead(ReaderOptions readerOptions, IEnumerable <string> testArchives)
 {
     foreach (var path in testArchives)
     {
         using (var stream = new NonDisposingStream(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.º 5
0
        public static XZIndex FromStream(Stream stream, bool indexMarkerAlreadyVerified)
        {
            var index = new XZIndex(new BinaryReader(NonDisposingStream.Create(stream), Encoding.UTF8), indexMarkerAlreadyVerified);

            index.Process();
            return(index);
        }
Ejemplo n.º 6
0
        protected void Write(CompressionType compressionType, string archive, string archiveToVerifyAgainst, Encoding encoding = null)
        {
            using (Stream stream = File.OpenWrite(Path.Combine(SCRATCH2_FILES_PATH, archive)))
            {
                WriterOptions writerOptions = new WriterOptions(compressionType)
                {
                    LeaveStreamOpen = true,
                };

                writerOptions.ArchiveEncoding.Default = encoding ?? Encoding.Default;

                using (var writer = WriterFactory.Open(stream, type, writerOptions))
                {
                    writer.WriteAll(ORIGINAL_FILES_PATH, "*", SearchOption.AllDirectories);
                }
            }
            CompareArchivesByPath(Path.Combine(SCRATCH2_FILES_PATH, archive),
                                  Path.Combine(TEST_ARCHIVES_PATH, archiveToVerifyAgainst));

            using (Stream stream = File.OpenRead(Path.Combine(SCRATCH2_FILES_PATH, archive)))
            {
                ReaderOptions readerOptions = new ReaderOptions();

                readerOptions.ArchiveEncoding.Default = encoding ?? Encoding.Default;

                using (var reader = ReaderFactory.Open(NonDisposingStream.Create(stream), readerOptions))
                {
                    reader.WriteAllToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions()
                    {
                        ExtractFullPath = true
                    });
                }
            }
            VerifyFiles();
        }
Ejemplo n.º 7
0
 private void Decompress(Stream input, Stream output)
 {
     using (var zlibStream = new ZlibStream(NonDisposingStream.Create(input), CompressionMode.Decompress))
     {
         zlibStream.CopyTo(output);
     }
 }
Ejemplo n.º 8
0
        public static XZHeader FromStream(Stream stream)
        {
            var header = new XZHeader(new BinaryReader(NonDisposingStream.Create(stream), Encoding.UTF8));

            header.Process();
            return(header);
        }
Ejemplo n.º 9
0
        protected void ArchiveStreamReadExtractAll(IEnumerable <string> testArchives, CompressionType compression)
        {
            foreach (var path in testArchives)
            {
                using (var stream = new NonDisposingStream(File.OpenRead(path), true))
                    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;
                    }
                VerifyFiles();
            }
        }
Ejemplo n.º 10
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.º 11
0
 internal Volume(Stream stream, ReaderOptions readerOptions)
 {
     ReaderOptions = readerOptions;
     if (readerOptions.LeaveStreamOpen)
     {
         stream = new NonDisposingStream(stream);
     }
     _actualStream = stream;
 }
Ejemplo n.º 12
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.º 13
0
 public GZipWriter(Stream destination, GZipWriterOptions options = null)
     : base(ArchiveType.GZip, options ?? new GZipWriterOptions())
 {
     if (WriterOptions.LeaveStreamOpen)
     {
         destination = new NonDisposingStream(destination);
     }
     InitalizeStream(new GZipStream(destination, CompressionMode.Compress,
                                    options?.CompressionLevel ?? CompressionLevel.Default,
                                    WriterOptions.ArchiveEncoding.GetEncoding()));
 }
Ejemplo n.º 14
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 = new NonDisposingStream(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 !NO_FILE
                    if (Header.WinzipAesEncryptionData != null)
                    {
                        return(new WinzipAesCryptoStream(plainStream, Header.WinzipAesEncryptionData, Header.CompressedSize - 10));
                    }
#endif
                    return(plainStream);
                }

                default:
                {
                    throw new ArgumentOutOfRangeException();
                }
                }
            }

            return(plainStream);
        }
Ejemplo n.º 15
0
 protected void Read(IEnumerable <string> testArchives, CompressionType expectedCompression)
 {
     foreach (var path in testArchives)
     {
         using (var stream = new NonDisposingStream(new ForwardOnlyStream(File.OpenRead(path)), true))
             using (var reader = ReaderFactory.Open(stream))
             {
                 UseReader(this, reader, expectedCompression);
                 stream.ThrowOnDispose = false;
             }
     }
 }
Ejemplo n.º 16
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.º 17
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.º 18
0
 static void Main(string[] args)
 {
     Parser.Default.ParseArguments <CommandLineOptions>(args).WithParsed(options =>
     {
         bool unzip       = options.Operation == 'e';
         using var stream = new NonDisposingStream(File.OpenRead(options.InputFile), true);
         using var reader = ArchiveFactory.Open(stream);
         try
         {
             foreach (var entry in reader.Entries.Where(entry => !entry.IsDirectory))
             {
                 if (unzip)
                 {
                     if (options.SkipExistingFiles && File.Exists(Path.Combine(options.OutputDirectory, entry.Key)))
                     {
                         Console.WriteLine($"Skipping {entry.Key}");
                         continue;
                     }
                     if (options.Verbose)
                     {
                         Console.WriteLine($"Processing {entry.Key}");
                     }
                     entry.WriteToDirectory(options.OutputDirectory,
                                            new ExtractionOptions()
                     {
                         ExtractFullPath = true,
                         Overwrite       = true
                     });
                 }
                 else
                 {
                     Console.WriteLine(entry.ToString());
                 }
             }
         }
         catch (IndexOutOfRangeException)
         {
             //SevenZipArchive_BZip2_Split test needs this
             stream.ThrowOnDispose = false;
             throw;
         }
         stream.ThrowOnDispose = false;
     });
 }
Ejemplo n.º 19
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 = new NonDisposingStream(destination);
            }
            InitalizeStream(destination);
        }
Ejemplo n.º 20
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.º 21
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.º 22
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));
 }