public void ReceiveTextFileTest()
        {
            int linefeedChars = 1;    // Input is *nix style so only 1 linefeed char

            // Create a CvsStream based on a MemoryStream for ReceiveTextFile to receive the file from
            MemoryStream     memoryStream     = new MemoryStream();
            GZipOutputStream gzipOutputStream = new GZipOutputStream(memoryStream);
            CvsStream        cvsStream        = new CvsStream(gzipOutputStream);

            UncompressedFileHandlerTest.CreateTextStream(cvsStream);
            gzipOutputStream.Finish();    // This is essential to finish off the zipping
            cvsStream.BaseStream = memoryStream;
            cvsStream.Position   = 0;

            // Create a temporary file to receive the file to
            testFileName = Path.GetTempFileName();

            // Call the function under test
            CompressedFileHandler fileHandler = new CompressedFileHandler();

            fileHandler.ReceiveTextFile(cvsStream, testFileName,
                                        UncompressedFileHandlerTest.GetTextLen(UncompressedFileHandlerTest.TEXT_BLOCKS, linefeedChars));

            // check the received file
            UncompressedFileHandlerTest.CheckTextFile(testFileName);
        }
Exemple #2
0
        public static byte[] ZipText(string text)
        {
            if (text == null)
            {
                return(null);
            }

            using (Stream memOutput = new MemoryStream())
            {
                using (var zipOut = new GZipOutputStream(memOutput))
                {
                    using (StreamWriter writer = new StreamWriter(zipOut))
                    {
                        writer.Write(text);

                        writer.Flush();
                        zipOut.Finish();

                        byte[] bytes = new byte[memOutput.Length];
                        memOutput.Seek(0, SeekOrigin.Begin);
                        memOutput.Read(bytes, 0, bytes.Length);

                        return(bytes);
                    }
                }
            }
        }
Exemple #3
0
        public void GetDataSet()
        {
            SoapContext sc = HttpSoapContext.ResponseContext;

            if (null == sc)
            {
                throw new ApplicationException("Only SOAP requests allowed");
            }

            SqlConnection conn = new SqlConnection(@"data source=(local)\NetSDK;" +
                                                   "initial catalog=Northwind;integrated security=SSPI");

            SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM customers", conn);
            DataSet        ds = new DataSet("CustomerDS");

            da.Fill(ds, "Customers");

            // dispose of all objects that are no longer necessary
            da.Dispose();
            conn.Dispose();

            MemoryStream     memoryStream = new MemoryStream(1024);
            GZipOutputStream gzipStream   = new GZipOutputStream(memoryStream);

            ds.WriteXml(gzipStream);
            gzipStream.Finish();
            memoryStream.Seek(0, SeekOrigin.Begin);

            DimeAttachment dimeAttachment = new DimeAttachment("application/x-gzip",
                                                               TypeFormatEnum.MediaType,
                                                               memoryStream);

            sc.Attachments.Add(dimeAttachment);
        }
Exemple #4
0
        private void ReadComplete(IAsyncResult res)
        {
            ReadState rs = (ReadState)res.AsyncState;

            using (rs.file)
            {
                rs.file.EndRead(res);
                if (rs.compressLevel > 0)
                {
                    byte[] data = rs.data;
                    // don't compress things which are already compressed
                    if ((data.Length > 2) && ((data[0] != 0x1f) || (data[1] != 0x8b)))
                    {
                        MemoryStream compStream = new MemoryStream(rs.data.Length);
                        using (GZipOutputStream gzOut = new GZipOutputStream(compStream))
                        {
                            gzOut.SetLevel(rs.compressLevel);
                            gzOut.Write(rs.data, 0, rs.data.Length);
                            gzOut.Finish();
                            rs.data = compStream.ToArray();
                        }
                    }
                }
                lock (writeData)
                {
                    writeData[rs.writeDataIndex] = rs.data;
                    newWriteSignal.Release(1);
                }
            }
            outstandingReads.Increment();
        }
Exemple #5
0
        /// <summary>
        /// Gzip压缩缓存文件
        /// </summary>
        /// <param name="filePath">缓存文件路径</param>
        /// <returns>压缩后的二进制</returns>
        public static byte[] GZipStress(string filePath)
        {
            string path = filePath;

            if (!File.Exists(path))
            {
                path = string.Format("{0}/Config/{1}.boxcache", AppDomain.CurrentDomain.BaseDirectory, typeof(T).FullName);
            }
            using (FileStream ms = new FileStream(path, FileMode.Open))
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    GZipOutputStream outStream = new GZipOutputStream(stream);
                    int    readed;
                    byte[] buffer = new byte[2048];
                    do
                    {
                        readed = ms.Read(buffer, 0, buffer.Length);
                        outStream.Write(buffer, 0, readed);
                    }while (readed != 0);
                    outStream.Flush();
                    outStream.Finish();
                    return(stream.GetBuffer());
                }
            }
        }
Exemple #6
0
        public override void Initialize()
        {
            var tasks = new Task[options.Threads];

            for (var i = 0; i < options.Threads; i++)
            {
                var i1 = i;

                tasks[i1] = Task.Run(() =>
                {
                    var data = DataGenerator.GenerateString((int)(volume / options.Threads));

                    using var s = new MemoryStream();
                    using (var stream = new GZipOutputStream(s))
                    {
                        stream.SetLevel(9);

                        using var sw = new StreamWriter(stream);
                        sw.Write(data);
                        sw.Flush();
                        stream.Finish();

                        stream.IsStreamOwner = false;
                    }

                    s.Seek(0, SeekOrigin.Begin);

                    datas[i1] = s.ToArray();
                });
            }

            Task.WaitAll(tasks);
        }
        public static byte[] ObjectToBytes <TItem>(TItem obj, SerializationMode mode, TypeDescription typeDescription)
        {
            using (var output = new MemoryStream())
            {
                if (mode == SerializationMode.Json)
                {
                    var json = ObjectToJson(obj, typeDescription);

                    if (typeDescription.UseCompression)
                    {
                        using (var outZStream = new GZipOutputStream(output))
                        {
                            var writer = new BinaryWriter(outZStream);
                            writer.Write(json);
                            outZStream.Flush();
                            outZStream.Finish();
                        }
                    }
                    else
                    {
                        var writer = new BinaryWriter(output);
                        writer.Write(json);
                    }
                }
                else
                {
                    Serializer.SerializeWithLengthPrefix(output, obj, PrefixStyle.Fixed32);
                }


                return(output.ToArray());
            }
        }
Exemple #8
0
        public void DoubleClose()
        {
            var memStream = new TrackedMemoryStream();
            var s         = new GZipOutputStream(memStream);

            s.Finish();
#if NET451
            s.Close();
#elif NETCOREAPP1_0
            s.Dispose();
#endif
#if NET451
            s.Close();
#elif NETCOREAPP1_0
            s.Dispose();
#endif

            memStream = new TrackedMemoryStream();
            using (GZipOutputStream no2 = new GZipOutputStream(memStream)) {
#if NET451
                s.Close();
#elif NETCOREAPP1_0
                s.Dispose();
#endif
            }
        }
Exemple #9
0
        public byte[] Compress <ObjectType>(ObjectType ObjToCompress)
        {
            if (ObjToCompress == null)
            {
                return(null);
            }

            string toCompress = JsonConvert.SerializeObject(ObjToCompress, Formatting.None, new JsonSerializerSettings
            {
                NullValueHandling    = NullValueHandling.Ignore,
                DefaultValueHandling = DefaultValueHandling.Ignore
            });

            using (Stream memOutput = new MemoryStream())
            {
                GZipOutputStream zipOut = new GZipOutputStream(memOutput);
                zipOut.SetLevel(9);
                StreamWriter writer = new StreamWriter(zipOut);
                writer.Write(toCompress);
                writer.Flush();
                zipOut.Finish();
                byte[] bytes = new byte[memOutput.Length];
                memOutput.Seek(0, SeekOrigin.Begin);
                memOutput.Read(bytes, 0, bytes.Length);
                return(bytes);
            }
        }
        public static void ObjectToStream <TItem>(TItem obj, Stream stream, SerializationMode mode, bool compress)
        {
            if (mode == SerializationMode.Json)
            {
                var json = JsonConvert.SerializeObject(obj, JsonSettings());

                if (compress)
                {
                    var outZStream = new GZipOutputStream(stream);
                    var writer     = new BinaryWriter(outZStream);
                    writer.Write(json);

                    outZStream.Flush();
                    outZStream.Finish();
                }
                else
                {
                    var writer = new BinaryWriter(stream);
                    writer.Write(json);
                }
            }
            else
            {
                Serializer.SerializeWithLengthPrefix(stream, obj, PrefixStyle.Fixed32);
            }
        }
Exemple #11
0
    public static byte[] Compress(byte[] bytesToCompress)
    {
        byte[]       rebyte = null;
        MemoryStream ms     = new MemoryStream();

        GZipOutputStream s = new GZipOutputStream(ms);

        try
        {
            s.Write(bytesToCompress, 0, bytesToCompress.Length);
            s.Flush();
            s.Finish();
        }
        catch (System.Exception ex)
        {
#if UNITY_EDITOR
            Debug.Log(ex);
#endif
        }

        ms.Seek(0, SeekOrigin.Begin);

        rebyte = ms.ToArray();

        s.Close();
        ms.Close();

        s.Dispose();
        ms.Dispose();

        return(rebyte);
    }
Exemple #12
0
        public void TestGZip()
        {
            MemoryStream     ms        = new MemoryStream();
            GZipOutputStream outStream = new GZipOutputStream(ms);

            byte[]        buf = new byte[100000];
            System.Random rnd = new Random();
            rnd.NextBytes(buf);

            outStream.Write(buf, 0, buf.Length);
            outStream.Flush();
            outStream.Finish();

            ms.Seek(0, SeekOrigin.Begin);

            GZipInputStream inStream = new GZipInputStream(ms);

            byte[] buf2 = new byte[buf.Length];
            int    pos  = 0;

            while (true)
            {
                int numRead = inStream.Read(buf2, pos, 4096);
                if (numRead <= 0)
                {
                    break;
                }
                pos += numRead;
            }

            for (int i = 0; i < buf.Length; ++i)
            {
                Assert.AreEqual(buf2[i], buf[i]);
            }
        }
Exemple #13
0
        public byte[] Compress(byte[] buffer, long index, long count)
        {
            byte[]           arrBuffer = null;
            MemoryStream     ms        = new MemoryStream();
            GZipOutputStream objGzip   = new GZipOutputStream(ms);

            #region copy
            //const int BUFFER_SIZE = 1024 * 10;
            //byte[] arrBuffer = new byte[BUFFER_SIZE];
            //int nGetedCount = 0;
            //do
            //{
            //    nGetedCount = ms.Read(arrBuffer, 0, BUFFER_SIZE);
            //    objGzip.Write(arrBuffer, 0, nGetedCount);
            //} while (nGetedCount > 0);
            #endregion
            objGzip.Write(buffer, 0, buffer.Length);
            //objGzip.SetLevel(level);
            objGzip.Finish();
            arrBuffer = ms.ToArray();
            ms.Close();
            objGzip.Close();

            return(arrBuffer);
        }
Exemple #14
0
    // Sadico's Gzip Compression (but my comments)
    public static byte[] GzipData(float[] data)
    {
        if (data == null)
        {
            return(null);
        }

        using (Stream memOutput = new MemoryStream())
        {
            using (GZipOutputStream zipOut = new GZipOutputStream(memOutput))
            {
                using (BinaryWriter writer = new BinaryWriter(zipOut))
                {
                    byte[] floatSerialization = new byte[data.Length * 4];                       // Float uses 4 bytes (it's 32 bit)
                    Buffer.BlockCopy(data, 0, floatSerialization, 0, floatSerialization.Length); // Serializes the float[] to bytes

                    writer.Write(floatSerialization);                                            // Writes the bytes to the stream

                    writer.Flush();                                                              // Clears the buffer for memory cleanup
                    zipOut.Finish();                                                             // Finishes the stream

                    // Stores the data into a field to return
                    byte[] bytes = new byte[memOutput.Length];
                    memOutput.Seek(0, SeekOrigin.Begin);
                    memOutput.Read(bytes, 0, bytes.Length);

                    return(bytes);
                }
            }
        }
    }
Exemple #15
0
        //-------------------------------------------------------------------------
        public static byte[] CompressGZIP(byte[] buf)
        {
            if (null == buf || 0 == buf.Length)
            {
                Console.WriteLine("CompressGZIP buf is nil");
                return(buf);
            }

            byte[] bufCompress = buf;
            try
            {
                MemoryStream     ms        = new MemoryStream();
                GZipOutputStream outStream = new GZipOutputStream(ms);
                outStream.Write(buf, 0, buf.Length);
                outStream.Flush();
                outStream.Finish();

                bufCompress = ms.GetBuffer();
                Array.Resize(ref bufCompress, (int)outStream.Length);

                outStream.Close();
                ms.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("CompressGZIP 错误:" + ex.Message);
            }

            return(bufCompress);
        }
Exemple #16
0
        /// <summary>
        /// GZip压缩
        /// </summary>
        /// <param name="s">待压缩数据输入流</param>
        /// <param name="os">压缩后的数据输出流</param>
        public static void GZipCompress(Stream s, Stream os)
        {
            GZipOutputStream gos = new GZipOutputStream(os, 1024 * 4);

            Stdio.CopyStream(s, gos);
            gos.Finish();
        }
Exemple #17
0
 public void Compress(SoapMessage message)
 {
     if (tempStream.Length >= minLength)
     {
         MemoryStream     mems = new MemoryStream();
         GZipOutputStream zos  = new GZipOutputStream(mems);
         zos.Write(tempStream.GetBuffer(), 0, (int)tempStream.Length);
         zos.Finish();
         Console.WriteLine("msg len:" + tempStream.Length);
         // Convert the compressed content to a base 64 string
         string compString = Convert.ToBase64String(mems.GetBuffer(), 0, (int)mems.Length);
         byte[] compBytes  = Encoding.UTF8.GetBytes(compString);
         netStream.WriteByte((byte)'C');                     // Compressing flag
         netStream.Write(compBytes, 0, compBytes.Length);
         Console.WriteLine("cmp len:" + compBytes.Length);
         netStream.Flush();
         zos.Close();
     }
     else
     {
         netStream.WriteByte((byte)'N');                     // Not Compressing flag
         netStream.Write(tempStream.GetBuffer(), 0, (int)tempStream.Length);
         netStream.Flush();
     }
 }
Exemple #18
0
        /// <summary>
        /// 压缩数据。
        /// </summary>
        /// <param name="stream">要压缩的数据的二进制流。</param>
        /// <param name="compressedStream">压缩后的数据的二进制流。</param>
        /// <returns>是否压缩数据成功。</returns>
        public bool Compress(Stream stream, Stream compressedStream)
        {
            if (stream == null)
            {
                return(false);
            }

            if (compressedStream == null)
            {
                return(false);
            }

            try
            {
                GZipOutputStream gZipOutputStream = new GZipOutputStream(compressedStream);
                int bytesRead = 0;
                while ((bytesRead = stream.Read(m_CachedBytes, 0, CachedBytesLength)) > 0)
                {
                    gZipOutputStream.Write(m_CachedBytes, 0, bytesRead);
                }

                gZipOutputStream.Finish();
                ProcessHeader(compressedStream);
                return(true);
            }
            catch
            {
                return(false);
            }
            finally
            {
                Array.Clear(m_CachedBytes, 0, CachedBytesLength);
            }
        }
Exemple #19
0
        /// <summary>
        /// GZip压缩
        /// </summary>
        /// <param name="data">待压缩数据</param>
        /// <param name="offset">数据起始位置</param>
        /// <param name="len">数据长度</param>
        /// <param name="os">压缩后的数据输出流</param>
        public static void GZipCompress(byte[] data, int offset, int len, Stream os)
        {
            GZipOutputStream gos = new GZipOutputStream(os, 1024 * 4);

            gos.Write(data, offset, len);
            gos.Finish();
        }
Exemple #20
0
        public byte[] Compress(string message)
        {
            if (message == null)
            {
                return(null);
            }

            using (var dataStream = new MemoryStream())
                using (var zipStream = new GZipOutputStream(dataStream))
                {
                    zipStream.SetLevel((int)CompressionLevel);
                    var rawBytes = Encoding.UTF8.GetBytes(message);

                    zipStream.Write(rawBytes, 0, rawBytes.Length);

                    zipStream.Flush();
                    zipStream.Finish();

                    var compressedBytes = new byte[dataStream.Length];
                    dataStream.Seek(0, SeekOrigin.Begin);
                    dataStream.Read(compressedBytes, 0, compressedBytes.Length);

                    return(compressedBytes);
                }
        }
Exemple #21
0
        /// <summary>
        /// 지정된 데이타를 압축한다.
        /// </summary>
        /// <param name="input">압축할 Data</param>
        /// <returns>압축된 Data</returns>
        public override byte[] Compress(byte[] input)
        {
            if (IsDebugEnabled)
            {
                log.Debug(CompressorTool.SR.CompressStartMsg);
            }

            // check input data
            if (input.IsZeroLength())
            {
                if (IsDebugEnabled)
                {
                    log.Debug(CompressorTool.SR.InvalidInputDataMsg);
                }

                return(CompressorTool.EmptyBytes);
            }

            byte[] output;
            using (var compressedStream = new MemoryStream(input.Length)) {
                using (var gzs = new GZipOutputStream(compressedStream)) {
                    gzs.SetLevel(ZipLevel);
                    gzs.Write(input, 0, input.Length);
                    gzs.Finish();
                }
                output = compressedStream.ToArray();
            }

            if (IsDebugEnabled)
            {
                log.Debug(CompressorTool.SR.CompressResultMsg, input.Length, output.Length, output.Length / (double)input.Length);
            }

            return(output);
        }
Exemple #22
0
        /// <summary>
        /// 压缩数据。
        /// </summary>
        /// <param name="bytes">要压缩的数据的二进制流。</param>
        /// <param name="offset">要压缩的数据的二进制流的偏移。</param>
        /// <param name="length">要压缩的数据的二进制流的长度。</param>
        /// <param name="compressedStream">压缩后的数据的二进制流。</param>
        /// <returns>是否压缩数据成功。</returns>
        public bool Compress(byte[] bytes, int offset, int length, Stream compressedStream)
        {
            if (bytes == null)
            {
                return(false);
            }

            if (offset < 0 || length < 0 || offset + length > bytes.Length)
            {
                return(false);
            }

            if (compressedStream == null)
            {
                return(false);
            }

            try
            {
                GZipOutputStream gZipOutputStream = new GZipOutputStream(compressedStream);
                gZipOutputStream.Write(bytes, offset, length);
                gZipOutputStream.Finish();
                ProcessHeader(compressedStream);
                return(true);
            }
            catch
            {
                return(false);
            }
        }
Exemple #23
0
        public override void Run()
        {
            var tasks = new Task[options.Threads];

            for (var i = 0; i < options.Threads; i++)
            {
                var i1 = i;
                tasks[i] = ThreadAffinity.RunAffinity(1uL << i, () =>
                        {
                        using (Stream s = new MemoryStream())
                        {
                            using var stream = new GZipOutputStream(s);
                            stream.SetLevel(9);

                            using var sw = new StreamWriter(stream);
                            sw.Write(datas[i1]);
                            sw.Flush();
                            stream.Finish();
                        }

                        BenchmarkRunner.ReportProgress();
                    });
            }

            Task.WaitAll(tasks);
        }
Exemple #24
0
        public void OriginalFilename()
        {
            var content = "FileContents";


            using var ms = new MemoryStream();
            using (var outStream = new GZipOutputStream(ms)
            {
                IsStreamOwner = false
            })
            {
                outStream.FileName = "/path/to/file.ext";

                var writeBuffer = Encoding.ASCII.GetBytes(content);
                outStream.Write(writeBuffer, 0, writeBuffer.Length);
                outStream.Flush();
                outStream.Finish();
            }

            ms.Seek(0, SeekOrigin.Begin);

            using (var inStream = new GZipInputStream(ms))
            {
                var readBuffer = new byte[content.Length];
                inStream.Read(readBuffer, 0, readBuffer.Length);
                Assert.AreEqual(content, Encoding.ASCII.GetString(readBuffer));
                Assert.AreEqual("file.ext", inStream.GetFilename());
            }
        }
Exemple #25
0
 /// <summary>
 /// Closes this Filter and calls the base class implementation.
 /// </summary>
 public override void Close()
 {
     if (m_stream != null)
     {
         m_stream.Finish();
     }
     base.Close();
 }
Exemple #26
0
        public void WriteAfterFinish()
        {
            var memStream = new TrackedMemoryStream();
            var s         = new GZipOutputStream(memStream);

            s.Finish();

            Assert.Throws <InvalidOperationException>(() => s.WriteByte(value: 7), "Write should fail");
        }
 public static long GZipStream(Stream inputStream, Stream outputStream)
 {
     using (GZipOutputStream compressionStream = new GZipOutputStream(outputStream))
     {
         compressionStream.SetLevel(9);
         inputStream.CopyTo(compressionStream);
         compressionStream.Finish();
         return(compressionStream.Length);
     }
 }
Exemple #28
0
        public override void Close()
        {
            if (st == null)
            {
                return;
            }

            st.Finish();
            st = null;
            base.Close();
        }
Exemple #29
0
        public void DoubleFooter()
        {
            var memStream = new TrackedMemoryStream();
            var s         = new GZipOutputStream(memStream);

            s.Finish();
            Int64 length = memStream.Length;

            s.Close();
            Assert.AreEqual(length, memStream.ToArray().Length);
        }
Exemple #30
0
    public static byte[] compress(byte[] data)
    {
        MemoryStream     memoryStream     = new MemoryStream();
        GZipOutputStream gZipOutputStream = new GZipOutputStream(memoryStream);

        gZipOutputStream.Write(data, 0, data.Length);
        gZipOutputStream.Flush();
        gZipOutputStream.Finish();
        gZipOutputStream.Close();
        return(memoryStream.ToArray());
    }