lixiaojun
2022-10-24 c795cfeb985235bb0e70fdc7f2e551330dfb4b31
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Expert.Untity
{
    public class FileTransferHelper
    {
        /// <summary>
        /// 流转化为字节 (方法内会释放流)
        /// </summary>
        public static byte[] Stream2Bytes(Stream stream)
        {
            if (stream == null)
                return null;
            byte[] bytes = new byte[stream.Length];
            stream.Seek(0, SeekOrigin.Begin);
            stream.Read(bytes, 0, bytes.Length);
            stream.Close();
            return bytes;
        }
 
        /// <summary>
        /// 流转化为字节 (方法内不会释放流)
        /// </summary>
        public static byte[] Stream2Bytes2(Stream stream)
        {
            if (stream == null)
                return null;
            byte[] bytes = new byte[stream.Length];
            stream.Seek(0, SeekOrigin.Begin);
            stream.Read(bytes, 0, bytes.Length);
            return bytes;
        }
 
        /// <summary>
        /// 字节转化为流
        /// </summary>
        public static Stream Bytes2Stream(byte[] bytes)
        {
            if (bytes == null)
                return null;
            Stream stream = new MemoryStream(bytes);
            return stream;
        }
 
        /// <summary>
        /// 字节保存至路径
        /// </summary>
        public static void Bytes2File(byte[] bytes, string path)
        {
            FileStream fs = new FileStream(path, FileMode.Create);
            fs.Write(bytes, 0, bytes.Length);
            fs.Dispose();
        }
 
        /// <summary>
        /// 文件转化为流
        /// </summary>
        public static Stream File2Stream(string filePath)
        {
            try
            {
                if (filePath == null)
                    return null;
                if (!System.IO.File.Exists(filePath))
                    return null;
 
                //必须先转成byte,否则出了using后,流会关闭
                byte[] bs = null;
                using (FileStream stream = new FileStream(filePath, FileMode.Open))
                {
                    bs = Stream2Bytes(stream);
                }
                if (bs == null || bs.Count() == 0)
                    return null;
                else
                    return new MemoryStream(bs);
            }
            catch (Exception)
            {
                return null;
            }
        }
 
        /// <summary>
        /// 文件转化为字节
        /// </summary>
        public static byte[] File2Bytes(string filePath)
        {
            try
            {
                //System.IO.File.WriteAllBytes 也可以
                if (filePath == null)
                    return null;
                if (!System.IO.File.Exists(filePath))
                    return null;
 
                //必须先转成byte,否则出了using后,流会关闭
                byte[] bs = null;
                using (FileStream stream = new FileStream(filePath, FileMode.Open))
                {
                    bs = Stream2Bytes(stream);
                }
                if (bs == null || bs.Count() == 0)
                    return null;
                else
                    return bs;
            }
            catch (Exception)
            {
                return null;
            }
        }
 
        /// <summary>
        /// 流变成文件 (方法内会释放流)
        /// </summary>
        public static void Stream2File(Stream fileStream, string fileFullName)
        {
            //判断文件是否可读
            if (!fileStream.CanRead)
            {
                throw new Exception("数据流不可读!");
            }
 
            //从开始读起
            if (fileStream.Position != 0)
                fileStream.Position = 0;
 
            //文件流传输
            FileStream targetStream = null;
            int fileSize = 0;
            using (targetStream = new FileStream(fileFullName, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                //定义文件缓冲区
                const int bufferLen = 4096;
                byte[] buffer = new byte[bufferLen];
                int count = 0;
 
                while ((count = fileStream.Read(buffer, 0, bufferLen)) > 0)
                {
                    targetStream.Write(buffer, 0, count);
                    fileSize += count;
                }
                targetStream.Close();
                fileStream.Close();
            }
        }
 
        /// <summary>
        /// 流变成文件 (方法内不会释放流)
        /// </summary>
        public static void Stream2File2(Stream fileStream, string fileFullName)
        {
            //判断文件是否可读
            if (!fileStream.CanRead)
            {
                throw new Exception("数据流不可读!");
            }
 
            //从开始读起
            if (fileStream.Position != 0)
                fileStream.Position = 0;
 
            //文件流传输
            FileStream targetStream = null;
            int fileSize = 0;
            using (targetStream = new FileStream(fileFullName, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                //定义文件缓冲区
                const int bufferLen = 4096;
                byte[] buffer = new byte[bufferLen];
                int count = 0;
 
                while ((count = fileStream.Read(buffer, 0, bufferLen)) > 0)
                {
                    targetStream.Write(buffer, 0, count);
                    fileSize += count;
                }
                targetStream.Close();
            }
        }
 
 
 
 
 
 
    }
}