zip文件转换为byte字节流

距离上一次写博客已经是long long  ago 的事情了。。。。

最近项目告一段落,开始整理之前做过的东西。

废话不多说,直接贴代码

 1    public class FileBinaryConvertHelper
 2         {   
 3             /// <summary>
 4             /// 将文件转化位byte数组
 5             /// </summary>
 6             /// <param name="Path">文件地址</param>
 7             /// <returns>转换为的byte数组</returns>
 8             public static byte[] FileBytes(string Path)
 9             { 
10                 if(!System.IO.File.Exists(Path))
11                 {
12                     return new byte[0];
13                 }
14                 FileInfo fi = new FileInfo(Path);
15                 byte[] buff=new byte[fi.Length];
16                 FileStream fs = fi.OpenRead();
17                 fs.Read(buff,0,Convert.ToInt32(fs.Length));
18                 fs.Close();
19                 return buff;
20             }
21             /// <summary>
22             /// 将byte数组转换为文件并保存到指定地址
23             /// </summary>
24             /// <param name="buff">byte数组</param>
25             /// <param name="savepath">保存地址</param>
26             public static void BytesFile(byte[] buff,string savepath)
27             { 
28                 if(System.IO.File.Exists(savepath))
29                 {
30                     System.IO.File.Delete(savepath);
31                 }
32                 FileStream fs = new FileStream(savepath,FileMode.CreateNew);
33                 BinaryWriter bw = new BinaryWriter(fs);
34                 bw.Write(buff,0,buff.Length);
35                 fs.Close();
36             }
37         }
 1    private static void UploadFile()
 2         {
 3             OpenFileDialog dialog = new OpenFileDialog();
 4             dialog.Filter = "压缩文件|*.zip";
 5             dialog.CheckFileExists = true;
 6             dialog.ShowDialog();
 7             if(!string.IsNullOrWhiteSpace(dialog.FileName))
 8             {
 9                 try
10                 {
11                     byte[] byteArray = FileBinaryConvertHelper.FileBytes(dialog.FileName);//文件转换成byte二进制数组
12                     FileBinaryConvertHelper.BytesFile(byteArray,@"D:	est2\新建文件夹\ddd.zip");//二进制数组转换为zip文件
13                    
14                 }
15                 catch(Exception ex)
16                 {
17                 }
18             }
19         }

如有不当之处,请多多指教!

蟹蟹!

原文地址:https://www.cnblogs.com/1234aa/p/7735540.html