コード例 #1
0
            public void WillDecodeDeflateResponse()
            {
                var testable        = new TestableJavaScriptReducer();
                var mockWebResponse = new Mock <WebResponse>();
                var ms     = new MemoryStream();
                var stream =
                    new System.IO.Compression.DeflateStream(ms, System.IO.Compression.CompressionMode.Compress);

                stream.Write(new UTF8Encoding().GetBytes("my response"), 0, "my response".Length);
                stream.Close();
                var encodedArray = ms.ToArray();

                ms.Close();
                stream.Dispose();
                ms.Dispose();
                mockWebResponse.Setup(x => x.GetResponseStream()).Returns(new MemoryStream(encodedArray));
                mockWebResponse.Setup(x => x.Headers).Returns(new WebHeaderCollection()
                {
                    { "Content-Encoding", "deflate" }
                });
                testable.Mock <IWebClientWrapper>().Setup(x => x.Download <JavaScriptResource>("http://host/js1.js")).Returns(mockWebResponse.Object);
                testable.Mock <IMinifier>().Setup(x => x.Minify <JavaScriptResource>("my response;\r\n")).Returns("min");

                var result = testable.ClassUnderTest.Process("http://host/js1.js::");

                testable.Mock <IStore>().Verify(
                    x =>
                    x.Save(Encoding.UTF8.GetBytes("min").MatchEnumerable(), result,
                           "http://host/js1.js::"), Times.Once());
                ms.Dispose();
            }
コード例 #2
0
        public void ExtractEntry(Entry entry, Stream outputStream, ZipProgressHandler progressHandler = null)
        {
            if (entry.Offset >= _Stream.Length)
            {
                throw new Exception("Invalid ZIP entry offset");
            }

            _Stream.Seek(entry.Offset, SeekOrigin.Begin);

            byte[] buffer = new byte[1024 * 1024];

            _Stream.Read(buffer, 0, 30);
            ushort fileNameLength   = BitConverter.ToUInt16(buffer, 26);
            ushort extraFieldLength = BitConverter.ToUInt16(buffer, 28);

            _Stream.Seek(entry.Offset + 30 + fileNameLength + extraFieldLength, SeekOrigin.Begin);

            Stream dataStream;

            switch (entry.CompressionMethod)
            {
            case 0:
                dataStream = _Stream;
                break;

            case 8:
                dataStream = new System.IO.Compression.DeflateStream(_Stream, System.IO.Compression.CompressionMode.Decompress, true);
                break;

            default:
                throw new Exception("Unsupported ZIP compression method: " + entry.CompressionMethod + " for " + entry.FileName);
            }

            uint done = 0;

            while (done < entry.UncompressedSize)
            {
                int todo = (int)Math.Min(entry.UncompressedSize - done, (uint)buffer.Length);
                if (dataStream.Read(buffer, 0, todo) != todo)
                {
                    throw new Exception("Cannot read data from ZIP stream for " + entry.FileName);
                }

                outputStream.Write(buffer, 0, todo);

                done += (uint)todo;

                if (progressHandler != null)
                {
                    progressHandler(done, entry.UncompressedSize);
                }
            }

            if (dataStream != _Stream)
            {
                dataStream.Dispose();
            }
        }
コード例 #3
0
        private void WriteFooter()
        {
            var data = BitConverter.GetBytes(_adler);

            Array.Reverse(data);

            _deflateStream.Dispose();
            _deflateStream = null;

            _stream.Write(data, 0, 4);

            if (!_leaveOpen)
            {
                _stream.Dispose();
            }

            _stream = null;
        }
コード例 #4
0
        public bool Save(string path)
        {
            try
            {
                using (var stream = new System.IO.FileStream(path, System.IO.FileMode.Create))
                {
                    var header = new PngHeader();

                    stream.Write(header.Data, 0, header.Data.Length);

                    PngChunk.Write(stream, "IHDR", PngChunk.CreateIHDRData(new PngChunk.IHDRData()
                    {
                        Width             = TransformedWidth,
                        Height            = TransformedHeight,
                        BitDepth          = 8,
                        ColorType         = PngChunk.ColorTypeCode.ColorUsedAlphaChannelUsed,
                        CompressionMethod = PngChunk.CompressionMethodCode.Deflate,
                        FilterMethod      = PngChunk.FilterTypeCode.None,
                        InterlaceMethod   = PngChunk.InterlaceMethodCode.None
                    }));

                    DataStructure = ImageDataStructure.Rgba;

                    var imageData = new byte[Data.Length + TransformedHeight];

                    for (int i = 0; i < TransformedHeight; i++)
                    {
                        imageData[i * TransformedWidth * 4 + i] = (byte)PngChunk.FilterTypeCode.None;
                        Array.Copy(Data, i * TransformedWidth * 4, imageData, i * TransformedWidth * 4 + i + 1, TransformedWidth * 4);
                    }

                    using (var idatStream = new MemoryStream())
                    {
                        idatStream.WriteByte(0x78);
                        idatStream.WriteByte(0x9C);

                        using (var deflate = new System.IO.Compression.DeflateStream(idatStream, System.IO.Compression.CompressionMode.Compress))
                        {
                            var window = 32768;
                            var count  = 0;

                            while (count * window < imageData.Length)
                            {
                                var buffer = imageData.Skip(count * window).Take(window).ToArray();
                                deflate.Write(buffer, 0, buffer.Length);
                                count++;
                            }

                            deflate.Dispose();
                            PngChunk.Write(stream, "IDAT", idatStream.ToArray());
                        }
                    }

                    PngChunk.Write(stream, "IEND", new byte[0]);
                }
            }
            catch (Exception)
            {
                return(false);
            }

            return(true);
        }
コード例 #5
0
        public void Zlib_Streams_VariousSizes()
        {
            byte[] working = new byte[WORKING_BUFFER_SIZE];
            int n = -1;
            Int32[] Sizes = { 8000, 88000, 188000, 388000, 580000, 1580000 };

            for (int p = 0; p < Sizes.Length; p++)
            {
                // both binary and text files
                for (int m = 0; m < 2; m++)
                {
                    int sz = this.rnd.Next(Sizes[p]) + Sizes[p];
                    string FileToCompress = System.IO.Path.Combine(TopLevelDir, String.Format("Zlib_Streams.{0}.{1}", sz, (m == 0) ? "txt" : "bin"));
                    Assert.IsFalse(System.IO.File.Exists(FileToCompress), "The temporary file '{0}' already exists.", FileToCompress);
                    TestContext.WriteLine("Creating file {0}   {1} bytes", FileToCompress, sz);
                    if (m == 0)
                        CreateAndFillFileText(FileToCompress, sz);
                    else
                        _CreateAndFillBinary(FileToCompress, sz, false);

                    int crc1 = DoCrc(FileToCompress);
                    TestContext.WriteLine("Initial CRC: 0x{0:X8}", crc1);

                    // try both GZipStream and DeflateStream
                    for (int k = 0; k < 2; k++)
                    {
                        // compress with Alienlab.and System.IO.Compression
                        for (int i = 0; i < 2; i++)
                        {
                            string CompressedFileRoot = String.Format("{0}.{1}.{2}.compressed", FileToCompress,
                                              (k == 0) ? "GZIP" : "DEFLATE",
                                              (i == 0) ? "Ionic" : "BCL");

                            int x = k + i * 2;
                            int z = (x == 0) ? 4 : 1;
                            // why 4 trials??   (only for GZIP and Alienlab.
                            for (int h = 0; h < z; h++)
                            {
                                string CompressedFile = (x == 0)
                                    ? CompressedFileRoot + ".trial" + h
                                    : CompressedFileRoot;

                                using (var input = System.IO.File.OpenRead(FileToCompress))
                                {
                                    using (var raw = System.IO.File.Create(CompressedFile))
                                    {
                                        Stream compressor = null;
                                        try
                                        {
                                            switch (x)
                                            {
                                                case 0: // k == 0, i == 0
                                                    compressor = new Alienlab.Zlib.GZipStream(raw, CompressionMode.Compress, true);
                                                    break;
                                                case 1: // k == 1, i == 0
                                                    compressor = new Alienlab.Zlib.DeflateStream(raw, CompressionMode.Compress, true);
                                                    break;
                                                case 2: // k == 0, i == 1
                                                    compressor = new System.IO.Compression.GZipStream(raw, System.IO.Compression.CompressionMode.Compress, true);
                                                    break;
                                                case 3: // k == 1, i == 1
                                                    compressor = new System.IO.Compression.DeflateStream(raw, System.IO.Compression.CompressionMode.Compress, true);
                                                    break;
                                            }
                                            //TestContext.WriteLine("Compress with: {0} ..", compressor.GetType().FullName);

                                            TestContext.WriteLine("........{0} ...", System.IO.Path.GetFileName(CompressedFile));

                                            if (x == 0)
                                            {
                                                if (h != 0)
                                                {
                                                    Alienlab.Zlib.GZipStream gzip = compressor as Alienlab.Zlib.GZipStream;

                                                    if (h % 2 == 1)
                                                        gzip.FileName = FileToCompress;

                                                    if (h > 2)
                                                        gzip.Comment = "Compressing: " + FileToCompress;

                                                }
                                            }

                                            n = -1;
                                            while ((n = input.Read(working, 0, working.Length)) != 0)
                                            {
                                                compressor.Write(working, 0, n);
                                            }

                                        }
                                        finally
                                        {
                                            if (compressor != null)
                                                compressor.Dispose();
                                        }
                                    }
                                }

                                // now, decompress with Alienlab.and System.IO.Compression
                                // for (int j = 0; j < 2; j++)
                                for (int j = 1; j >= 0; j--)
                                {
                                    using (var input = System.IO.File.OpenRead(CompressedFile))
                                    {
                                        Stream decompressor = null;
                                        try
                                        {
                                            int w = k + j * 2;
                                            switch (w)
                                            {
                                                case 0: // k == 0, j == 0
                                                    decompressor = new Alienlab.Zlib.GZipStream(input, CompressionMode.Decompress, true);
                                                    break;
                                                case 1: // k == 1, j == 0
                                                    decompressor = new Alienlab.Zlib.DeflateStream(input, CompressionMode.Decompress, true);
                                                    break;
                                                case 2: // k == 0, j == 1
                                                    decompressor = new System.IO.Compression.GZipStream(input, System.IO.Compression.CompressionMode.Decompress, true);
                                                    break;
                                                case 3: // k == 1, j == 1
                                                    decompressor = new System.IO.Compression.DeflateStream(input, System.IO.Compression.CompressionMode.Decompress, true);
                                                    break;
                                            }

                                            //TestContext.WriteLine("Decompress: {0} ...", decompressor.GetType().FullName);
                                            string DecompressedFile =
                                                String.Format("{0}.{1}.decompressed", CompressedFile, (j == 0) ? "Ionic" : "BCL");

                                            TestContext.WriteLine("........{0} ...", System.IO.Path.GetFileName(DecompressedFile));

                                            using (var s2 = System.IO.File.Create(DecompressedFile))
                                            {
                                                n = -1;
                                                while (n != 0)
                                                {
                                                    n = decompressor.Read(working, 0, working.Length);
                                                    if (n > 0)
                                                        s2.Write(working, 0, n);
                                                }
                                            }

                                            int crc2 = DoCrc(DecompressedFile);
                                            Assert.AreEqual<UInt32>((UInt32)crc1, (UInt32)crc2);

                                        }
                                        finally
                                        {
                                            if (decompressor != null)
                                                decompressor.Dispose();
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            TestContext.WriteLine("Done.");
        }
コード例 #6
0
ファイル: ZipFile.cs プロジェクト: sysprogs/BSPTools
        public void ExtractEntry(Entry entry, Stream outputStream, ZipProgressHandler progressHandler = null)
        {
            if (entry.Offset >= _Stream.Length)
                throw new Exception("Invalid ZIP entry offset");

            _Stream.Seek(entry.Offset, SeekOrigin.Begin);

            byte[] buffer = new byte[1024 * 1024];

            _Stream.Read(buffer, 0, 30);
            ushort fileNameLength = BitConverter.ToUInt16(buffer, 26);
            ushort extraFieldLength = BitConverter.ToUInt16(buffer, 28);

            _Stream.Seek(entry.Offset + 30 + fileNameLength + extraFieldLength, SeekOrigin.Begin);

            Stream dataStream;

            switch (entry.CompressionMethod)
            {
                case 0:
                    dataStream = _Stream;
                    break;
                case 8:
                    dataStream = new System.IO.Compression.DeflateStream(_Stream, System.IO.Compression.CompressionMode.Decompress, true);
                    break;
                default:
                    throw new Exception("Unsupported ZIP compression method: " + entry.CompressionMethod + " for " + entry.FileName);
            }

            uint done = 0;
            while (done < entry.UncompressedSize)
            {
                int todo = (int)Math.Min(entry.UncompressedSize - done, (uint)buffer.Length);
                if (dataStream.Read(buffer, 0, todo) != todo)
                    throw new Exception("Cannot read data from ZIP stream for " + entry.FileName);

                outputStream.Write(buffer, 0, todo);

                done += (uint)todo;

                if (progressHandler != null)
                    progressHandler(done, entry.UncompressedSize);
            }

            if (dataStream != _Stream)
                dataStream.Dispose();
        }