C#压缩和解压缩字节(GZip的使用)、压缩字节流写入压缩文件中

作用:此类在 .NET Framework 2.0 版中是新增的。提供用于压缩和解压缩流的方法和属性。

下面给出两个具体Demo:

//压缩字节
//1.创建压缩的数据流 
//2.设定compressStream为存放被压缩的文件流,并设定为压缩模式
//3.将需要压缩的字节写到被压缩的文件流
public static byte[] CompressBytes(byte[] bytes)
{
    using(MemoryStream compressStream = new MemoryStream())
    {
        using(var zipStream = new GZipStream(compressStream, CompressMode.ComPress))
            zipStream.Write(bytes, 0, bytes.Length);
        return compressStream.ToArray();
    }
}
//解压缩字节
//1.创建被压缩的数据流
//2.创建zipStream对象,并传入解压的文件流
//3.创建目标流
//4.zipStream拷贝到目标流
//5.返回目标流输出字节
public static byte[] Decompress(byte[] bytes)
{
    using(var compressStream = new MemoryStream(bytes))
    {
        using(var zipStream = new GZipStream(compressStream, CompressMode.DeCompress))
        {
            using(var resultStream = new MemoryStream())
            {
                zipStream.CopyTo(resultStream);
                return resultStream.ToArray();
            }
        }
    }
}
 
 
//压缩字节流写入压缩文件中
public static void ToJson(PropertiesDB db, string path)
        {
            JArray o = new JArray();
            o.Add(0);
            db.ObjectsAttr.ToList().ForEach(a =>
            {
                o.Add(new JArray { a.Name, a.Category, a.DataType, a.DataTypeContext, a.Description, a.DisplayName, a.Flags, a.DisplayPrecision });
            });
            //File.WriteAllText(Application.StartupPath + "\\objects_attrs.json", JsonConvert.SerializeObject(o));
            CompressBytes(path + "\\objects_attrs.json.gz", JsonConvert.SerializeObject(o));
    var avs = new List<long>();
            var offs = new List<int>();long eid = 0;
            int index = 0;
            db.ObjectsEav.OrderBy(x => x.EntityId).ToList().ForEach(v =>
              {
                  if (eid != v.EntityId)
                  {
                      eid = (long)v.EntityId;
                      offs.Add(index);
                  }
                  index++;
                  avs.Add((long)v.AttributeId);
                  avs.Add((long)v.ValueId);
              });
            //File.WriteAllText(Application.StartupPath + "\\objects_avs.json", JsonConvert.SerializeObject(avs));
            //File.WriteAllText(Application.StartupPath + "\\objects_offs.json", JsonConvert.SerializeObject(offs));
            CompressBytes(path + "\\objects_avs.json.gz", JsonConvert.SerializeObject(avs));
            CompressBytes(path + "\\objects_offs.json.gz", JsonConvert.SerializeObject(offs));

        }

        private static void CompressBytes(string filePath, string json)
        {
            FileStream fs = new FileStream(filePath, FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);

            using (MemoryStream compressStream = new MemoryStream())
            {
                var bytes = Encoding.UTF8.GetBytes(json);
                using (var zipStream = new GZipStream(compressStream, CompressionMode.Compress))
                    zipStream.Write(bytes, 0, bytes.Length);
                bytes = compressStream.ToArray();
                bw.Write(bytes);
                bw.Close();
                fs.Close();
            }
        }

猜你喜欢

转载自www.cnblogs.com/Aiti59/p/12530608.html