songjun
2024-09-04 cc908053e0b5075fbfd27350b6da4b39bca9e550
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Compression;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
 
 
namespace BatchService.Framework.Utility
{
    public class ZIPHelper
    {
        /// <summary> 
        /// 将一个object对象序列化,返回一个byte[]         
        /// </summary> 
        /// <param name="obj">能序列化的对象</param>         
        /// <returns></returns> 
        public static byte[] ObjectToBytes(object obj)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                IFormatter formatter = new BinaryFormatter(); formatter.Serialize(ms, obj); return ms.GetBuffer();
            }
        }
        private static byte[] StreamToBytes(Stream stream)
        {
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            // 设置当前流的位置为流的开始
            stream.Seek(0, SeekOrigin.Begin);
            return bytes;
        }
        /// <summary> 
        /// 将一个序列化后的byte[]数组还原         
        /// </summary>
        /// <param name="Bytes"></param>         
        /// <returns></returns> 
        public static object BytesToObject(byte[] Bytes)
        {
            using (MemoryStream ms = new MemoryStream(Bytes))
            {
                IFormatter formatter = new BinaryFormatter(); return formatter.Deserialize(ms);
            }
        }
 
 
        public static byte[] Compress(byte[] inBytes)
        {
            byte[] bb;
            using (MemoryStream mStream = new MemoryStream(inBytes))
            {
                using (MemoryStream outStream = new System.IO.MemoryStream())
                {
                    using (var tinyStream = new GZipStream(outStream, CompressionMode.Compress))
                    {
                        mStream.CopyTo(tinyStream);
                    }
                    bb = outStream.ToArray();
                }
            }
            return bb;
        }
 
 
 
        public static byte[] Decompress(byte[] bytes)
        {
            try
            {
                byte[] output = new byte[1024 * 1024 * 10];
                using (var inStream = new MemoryStream(bytes))
                using (var bigStream = new GZipStream(inStream, CompressionMode.Decompress))
                using (var bigStreamOut = new MemoryStream())
                {
                    bigStream.CopyTo(bigStreamOut);
                    output = bigStreamOut.ToArray();
                    var output2 = Encoding.UTF8.GetString(bigStreamOut.ToArray());
                }
                return output;
            }
            catch (Exception e)
            {
                LogHelper.Error(e.Message);
                return null;
            }
 
        }
    }
 
}