Exemple #1
0
        public void ReadTarFileTest()
        {
            using (Stream stream = File.OpenRead("Deb/libplist3_1.12-3.1_amd64.deb"))
                using (ArFile arFile = new ArFile(stream, leaveOpen: true))
                {
                    // Skip the debian version
                    arFile.Read();

                    // This is the tar file
                    arFile.Read();

                    Collection <string>    filenames = new Collection <string>();
                    Collection <string>    contents  = new Collection <string>();
                    Collection <TarHeader> headers   = new Collection <TarHeader>();

                    using (Stream entryStream = arFile.Open())
                        using (GZipDecompressor decompressedStream = new GZipDecompressor(entryStream, leaveOpen: true))
                            using (TarFile tarFile = new TarFile(decompressedStream, leaveOpen: true))
                            {
                                while (tarFile.Read())
                                {
                                    filenames.Add(tarFile.FileName);
                                    headers.Add((TarHeader)tarFile.FileHeader);

                                    using (Stream data = tarFile.Open())
                                        using (StreamReader reader = new StreamReader(data))
                                        {
                                            contents.Add(reader.ReadToEnd());
                                        }
                                }
                            }
                }
        }
Exemple #2
0
 public virtual void Decompress(string srcPath, string destPath)
 {
     using (IDecompressor dcmpr = new GZipDecompressor(srcPath, destPath))
     {
         dcmpr.OnCompleted += DecompressionCompleted;
         dcmpr.Decompress();
     }
 }
 public void GZipCompressorBytesExtesionWorks()
 {
     var compressor = new GZipCompressor();
     var compressed = compressor.Compress(Encoding.UTF8.GetBytes("test"));
     Assert.IsNotNull(compressed);
     var decompressor = new GZipDecompressor();
     var decompressed = decompressor.Decompress(compressed);
     Assert.IsNotNull(decompressed);
     Assert.AreEqual("test", Encoding.UTF8.GetString(decompressed));
 }
        public void GZipCompressorStringExtesionWorks()
        {
            var compressor = new GZipCompressor();
            var compressed = compressor.Compress("test");

            compressed.Should().NotBeNull();
            var decompressor = new GZipDecompressor();
            var decompressed = decompressor.Decompress(compressed);

            decompressed.Should().NotBeNull();
            Encoding.UTF8.GetString(decompressed).Should().Be("test");
        }
        public void GZipCompressorBytesExtesionWorks()
        {
            var compressor = new GZipCompressor();
            var compressed = compressor.Compress(Encoding.UTF8.GetBytes("test"));

            Assert.IsNotNull(compressed);
            var decompressor = new GZipDecompressor();
            var decompressed = decompressor.Decompress(compressed);

            Assert.IsNotNull(decompressed);
            Assert.AreEqual("test", Encoding.UTF8.GetString(decompressed));
        }
        private static byte[] GetDecompressData(byte[] compressedData)
        {
            var gZipDecompressor = new GZipDecompressor(new InMemoryFileService());

            var decompressedStream = new MemoryStream();

            gZipDecompressor.Execute(new MemoryStream(compressedData), decompressedStream, true);

            var decompressedBytes = decompressedStream.ToArray();

            return(decompressedBytes);
        }
Exemple #7
0
        public void TestCompliance2()
        {
            var path1  = TestUtil.GetTestDataDir() + "file2.gz";
            var path2  = TestUtil.GetTestDataDir() + "file2";
            var input  = new FileStream(path1, FileMode.Open);
            var output = new MemoryStream();

            GZipDecompressor.Decompress(input, output);
            int count;
            var rawData      = ReadFile(path2, out count);
            var uncompressed = output.ToArray();

            CollectionAssert.AreEqual(rawData.Take(count), uncompressed);
        }
Exemple #8
0
        public void TestCompliance1()
        {
            var path1  = TestUtil.GetTestDataDir() + "file1.gz";
            var path2  = TestUtil.GetTestDataDir() + "file1";
            var input  = new FileStream(path1, FileMode.Open);
            var output = new MemoryStream();

            GZipDecompressor.Decompress(input, output);
            var inputNotCompressed = new FileStream(path2, FileMode.Open);
            var rawData            = new byte[inputNotCompressed.Length];
            var count        = inputNotCompressed.Read(rawData, 0, rawData.Length);
            var uncompressed = output.ToArray();

            CollectionAssert.AreEqual(rawData.Take(count), uncompressed);
        }
Exemple #9
0
        /// <summary>
        /// Decompress the string value of a message.
        /// </summary>
        /// <param name="value">A string to decompress.</param>
        /// <param name="encoding">
        /// The encoding used to convert the string to a byte array.
        /// </param>
        /// <returns>The decompressed string.</returns>
        public string Decompress(string value, Encoding encoding)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (encoding == null)
            {
                throw new ArgumentNullException("encoding");
            }

            var decompressor = new GZipDecompressor();

            return(encoding.GetString(decompressor.Decompress(Convert.FromBase64String(value))));
        }
 public void GZipICompressorIDecompressorWorks()
 {
     var compressor = new GZipCompressor();
     var data = Encoding.UTF8.GetBytes("test");
     byte[] compressed;
     using (var inputStream = new MemoryStream(data))
     {
         compressed = compressor.Compress(inputStream);
     }
     Console.WriteLine(compressed.Length);
     Assert.IsNotNull(compressed);
     var decompressor = new GZipDecompressor();
     byte[] decompressed;
     using (var inputStream = new MemoryStream(compressed))
     {
         decompressed = decompressor.Decompress(inputStream);
     }
     Assert.IsNotNull(decompressed);
     Assert.AreEqual("test", Encoding.UTF8.GetString(decompressed));
 }
        public void GZipICompressorIDecompressorWorks()
        {
            var compressor = new GZipCompressor();
            var data       = Encoding.UTF8.GetBytes("test");

            byte[] compressed;
            using (var inputStream = new MemoryStream(data))
            {
                compressed = compressor.Compress(inputStream);
            }
            Console.WriteLine(compressed.Length);
            compressed.Should().NotBeNull();
            var decompressor = new GZipDecompressor();

            byte[] decompressed;
            using (var inputStream = new MemoryStream(compressed))
            {
                decompressed = decompressor.Decompress(inputStream);
            }
            decompressed.Should().NotBeNull();
            Encoding.UTF8.GetString(decompressed).Should().Be("test");
        }
        public void GZipICompressorIDecompressorWorks()
        {
            var compressor = new GZipCompressor();
            var data       = Encoding.UTF8.GetBytes("test");

            byte[] compressed;
            using (var inputStream = new MemoryStream(data))
            {
                compressed = compressor.Compress(inputStream);
            }
            Console.WriteLine(compressed.Length);
            Assert.IsNotNull(compressed);
            var decompressor = new GZipDecompressor();

            byte[] decompressed;
            using (var inputStream = new MemoryStream(compressed))
            {
                decompressed = decompressor.Decompress(inputStream);
            }
            Assert.IsNotNull(decompressed);
            Assert.AreEqual("test", Encoding.UTF8.GetString(decompressed));
        }
Exemple #13
0
        public void TestSelfCompliance()
        {
            var path1   = TestUtil.GetTestDataDir() + "file";
            var path2   = TestUtil.GetTestDataDir() + "file.gz";
            var path3   = TestUtil.GetTestDataDir() + "outFile";
            var stream1 = new FileStream(path1, FileMode.Open);
            var stream2 = new FileStream(path2, FileMode.Create);

            GZipCompressor.Compress(stream1, stream2);
            stream2.Seek(0, SeekOrigin.Begin);
            var stream3 = new FileStream(path3, FileMode.Create);

            GZipDecompressor.Decompress(stream2, stream3);
            stream1.Close();
            stream2.Close();
            stream3.Close();
            int count1, count2;
            var buffer1 = ReadFile(path1, out count1);
            var buffer2 = ReadFile(path3, out count2);

            CollectionAssert.AreEqual(buffer1.Take(count1), buffer2.Take(count2));
        }
Exemple #14
0
        public void TestBasicGZipRoundtrip2()
        {
            const string testString = "ユチエフ-8は素晴らしいです";
            var          data       = Encoding.UTF8.GetBytes(testString);
            var          dataStream = new MemoryStream(data, 0, data.Length);

            var encodedDataStream = new MemoryStream();

            GZipCompressor.Compress(dataStream, encodedDataStream);
            var encodedData       = encodedDataStream.ToArray();
            var decodedDataStream = new MemoryStream();

            encodedDataStream.Seek(0, SeekOrigin.Begin);
            GZipDecompressor.Decompress(encodedDataStream, decodedDataStream);
            var decodedData = decodedDataStream.ToArray();

            var resultString = Encoding.UTF8.GetString(decodedData);

            Console.WriteLine("{0} {1} {2}", data.Length, encodedData.Length, decodedData.Length);
            Console.WriteLine(encodedData.Aggregate("", (s, b) => s + ", " + b));
            Console.WriteLine(testString);
            Console.WriteLine(resultString);
            Assert.AreEqual(testString, resultString);
        }
        private static async Task ReadControlArchive(ArFile archive, DebPackage package)
        {
            package.ControlExtras = new Dictionary <string, DebPackageControlFileData>();
            package.Md5Sums       = new Dictionary <string, string>();
            using (Stream stream = archive.Open())
                using (GZipDecompressor decompressedStream = new GZipDecompressor(stream, leaveOpen: true))
                    using (TarFile tarFile = new TarFile(decompressedStream, leaveOpen: true))
                    {
                        while (tarFile.Read())
                        {
                            switch (tarFile.FileName)
                            {
                            case "./control":
                            case "control":
                                using (Stream controlFile = tarFile.Open())
                                {
                                    package.ControlFile = ControlFileParser.Read(controlFile);
                                }

                                break;

                            case "./md5sums":
                                using (var sums = new StreamReader(tarFile.Open()))
                                {
                                    string line;
                                    while ((line = await sums.ReadLineAsync()) != null)
                                    {
                                        var s = line.Split(new[] { "  " }, 2, StringSplitOptions.None);
                                        package.Md5Sums[s[1]] = s[0];
                                    }
                                }

                                break;

                            case "./preinst":
                                package.PreInstallScript = tarFile.ReadAsUtf8String();
                                break;

                            case "./postinst":
                                package.PostInstallScript = tarFile.ReadAsUtf8String();
                                break;

                            case "./prerm":
                                package.PreRemoveScript = tarFile.ReadAsUtf8String();
                                break;

                            case "./postrm":
                                package.PostRemoveScript = tarFile.ReadAsUtf8String();
                                break;

                            case "./":
                                tarFile.Skip();
                                break;

                            default:
                                package.ControlExtras[tarFile.FileName] = new DebPackageControlFileData
                                {
                                    Mode     = tarFile.FileHeader.FileMode,
                                    Contents = tarFile.ReadAsUtf8String()
                                };
                                break;
                            }
                        }
                    }
        }
Exemple #16
0
        private void decompress(byte[] data)
        {
            DataInputStream stream = new DataInputStream(data);

            if (_keys != null && _keys.Length != 0)
            {
                stream.decodeXTEA(_keys);
            }

            int compression = stream.readUnsignedByte();

            _compression = (CompressionType)(compression > 2 ? 2 : compression);

            int compressedLength = stream.readInt();

            if (compressedLength < 0 || compressedLength > 1000000)
            {
                throw new InvalidOperationException("INVALID ARCHIVE HEADER");
            }

            int length;

            switch (_compression)
            {
            case CompressionType.RAW:
                _data = new byte[compressedLength];
                checkRevision(stream, compressedLength);

                stream.Read(_data, 0, compressedLength);
                break;

            case CompressionType.BZIP:
                length = stream.readInt();

                if (length <= 0)
                {
                    _data = null;
                }
                else
                {
                    _data = new byte[length];
                    checkRevision(stream, compressedLength);
                    BZip2Decompressor.Decompress(_data, data);
                }
                break;

            default:                     // GZIP
                length = stream.readInt();

                if (length <= 0 || length > 1000000000)
                {
                    _data = null;
                }
                else
                {
                    _data = new byte[length];
                    checkRevision(stream, compressedLength);
                    GZipDecompressor.Decompress(_data, stream);
                }
                break;
            }
        }
Exemple #17
0
 public void SetUp()
 {
     _gzipDecompressor = new GZipDecompressor();
 }