我如何使用GZipStream.Read方法读取数据

时间:2019-06-13 10:58:55

标签: c# stream memorystream gzipstream

我无法使用GZipStream.Read方法读取数据。但是我可以直接从MemoryStream中读取。我在做什么错了?

    public static void Main(string[] args)
    {
        var memStr = new MemoryStream();

        //Write
        var data = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        var gzipWr = new GZipStream(memStr, CompressionMode.Compress);
        gzipWr.Write(data, 0, data.Length);

        //Read
        var array1 = new byte[10];
        memStr.Position = 0;
        var gzipRd = new GZipStream(memStr, CompressionMode.Decompress);
        var res1 = gzipRd.Read(array1, 0, array1.Length); // => res1 = 0

        //Read
        var array2 = new byte[10];
        memStr.Position = 0;
        var res2 = memStr.Read(array2, 0, array2.Length); // => res2 = 10
    }

2 个答案:

答案 0 :(得分:1)

谢谢!该代码也有效,看来我必须在阅读之前关闭gzipWr:

    public static void Main(string[] args)
    {
        var memStr = new MemoryStream();

        //Write
        var data = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        using (var gzipWr = new GZipStream(memStr, CompressionMode.Compress, true))
            gzipWr.Write(data, 0, data.Length);

        //Read
        var array = new byte[10];
        memStr.Position = 0;
        using (var gzipRd = new GZipStream(memStr, CompressionMode.Decompress))
            gzipRd.Read(array, 0, array.Length); // => res = 10
    }

答案 1 :(得分:0)

尝试使用emp_id ..并尝试这样:

$result = DB::table("section as es")
                ->select('es.*', 'sul.created_at')
                ->leftJoin('section_update_logs as sul', 'es.id', '=', 'sul.section_id')
                ->orderBy('es.weight')
                ->orderBy('sul.created_at')
                ->orWhere('sul.emp_id', $emp_id)
                ->groupBy('es.id')
                ->get();