public TarWriter(Stream destination, CompressionInfo compressionInfo) : base(ArchiveType.Tar) { if (!destination.CanWrite) { throw new ArgumentException("Tars require writable streams."); } switch (compressionInfo.Type) { case CompressionType.None: break; case CompressionType.BZip2: { destination = new BZip2Stream(destination, CompressionMode.Compress, false); } break; case CompressionType.GZip: { destination = new GZipStream(destination, CompressionMode.Compress, false); } break; default: { throw new InvalidFormatException("Tar does not support compression: " + compressionInfo.Type); } } InitalizeStream(destination, false); }
public static IReader Open(Stream stream, Options options) { Utility.CheckNotNull(stream, "stream"); RewindableStream stream2 = new RewindableStream(stream); stream2.StartRecording(); if (ZipArchive.IsZipFile(stream2, null)) { stream2.Rewind(true); return ZipReader.Open(stream2, null, options); } stream2.Rewind(false); if (GZipArchive.IsGZipFile(stream2)) { stream2.Rewind(false); GZipStream stream3 = new GZipStream(stream2, CompressionMode.Decompress); if (TarArchive.IsTarFile(stream3)) { stream2.Rewind(true); return new TarReader(stream2, CompressionType.GZip, options); } stream2.Rewind(true); return GZipReader.Open(stream2, options); } stream2.Rewind(false); if (BZip2Stream.IsBZip2(stream2)) { stream2.Rewind(false); BZip2Stream stream4 = new BZip2Stream(stream2, CompressionMode.Decompress, false, false); if (TarArchive.IsTarFile(stream4)) { stream2.Rewind(true); return new TarReader(stream2, CompressionType.BZip2, options); } } stream2.Rewind(false); if (TarArchive.IsTarFile(stream2)) { stream2.Rewind(true); return TarReader.Open(stream2, options); } stream2.Rewind(false); if (!RarArchive.IsRarFile(stream2, options)) { throw new InvalidOperationException("Cannot determine compressed stream type. Supported Reader Formats: Zip, GZip, BZip2, Tar, Rar"); } stream2.Rewind(true); return RarReader.Open(stream2, options); }
/// <summary> /// Opens a Reader for Non-seeking usage /// </summary> /// <param name="stream"></param> /// <param name="listener"></param> /// <param name="options"></param> /// <returns></returns> public static IReader OpenReader(Stream stream, IExtractionListener listener, Options options = Options.KeepStreamsOpen) { stream.CheckNotNull("stream"); RewindableStream rewindableStream = new RewindableStream(stream); rewindableStream.Recording = true; if (ZipArchive.IsZipFile(rewindableStream)) { return ZipReader.Open(rewindableStream, listener, options); } rewindableStream.Rewind(); rewindableStream.Recording = true; if (GZipReader.IsGZip(rewindableStream)) { rewindableStream.Rewind(); GZipStream testStream = new GZipStream(rewindableStream, CompressionMode.Decompress); rewindableStream.Recording = true; if (TarReader.IsTarFile(testStream)) { rewindableStream.Rewind(); return TarGZipReader.Open(rewindableStream, listener, options); } rewindableStream.Rewind(); return GZipReader.Open(rewindableStream, listener, options); } rewindableStream.Rewind(); rewindableStream.Recording = true; if (TarReader.IsTarFile(rewindableStream)) { rewindableStream.Rewind(); return TarReader.Open(rewindableStream, listener, options); } rewindableStream.Rewind(); rewindableStream.Recording = true; if (RarArchive.IsRarFile(rewindableStream)) { rewindableStream.Rewind(); return RarReader.Open(rewindableStream, listener, options); } throw new InvalidOperationException("Cannot determine compressed stream type."); }
/// <summary> /// Uncompress a GZip'ed byte array into a single string. /// </summary> /// /// <seealso cref="GZipStream.CompressString(String)"/> /// <seealso cref="GZipStream.UncompressBuffer(byte[])"/> /// /// <param name="compressed"> /// A buffer containing GZIP-compressed data. /// </param> /// /// <returns>The uncompressed string</returns> public static String UncompressString(byte[] compressed) { using (var input = new MemoryStream(compressed)) { Stream decompressor = new GZipStream(input, CompressionMode.Decompress); return ZlibBaseStream.UncompressString(compressed, decompressor); } }
/// <summary> /// Compress a string into a byte array using GZip. /// </summary> /// /// <remarks> /// Uncompress it with <see cref="GZipStream.UncompressString(byte[])"/>. /// </remarks> /// /// <seealso cref="GZipStream.UncompressString(byte[])"/> /// <seealso cref="GZipStream.CompressBuffer(byte[])"/> /// /// <param name="s"> /// A string to compress. The string will first be encoded /// using UTF8, then compressed. /// </param> /// /// <returns>The string in compressed form</returns> public static byte[] CompressString(String s) { using (var ms = new MemoryStream()) { Stream compressor = new GZipStream(ms, CompressionMode.Compress, CompressionLevel.BestCompression); ZlibBaseStream.CompressString(s, compressor); return ms.ToArray(); } }
public BinaryStateWriter() { _memoryStream = new MemoryStream(); OutStream = new GZipStream(_memoryStream, CompressionMode.Compress, true); }