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();
|
}
|
}
|
|
|
|
|
|
|
}
|
}
|