用C#进行Byte与文件(zip)的相互转换以及zip的解压缩

13 篇文章 0 订阅

用C#进行Byte与文件的相互转换

工具类

using System;
using System.Collections.Generic;
using System.IO;
namespace Protocol
{
    [Serializable]
    public class FileBinaryConvertHelper
    {
        /// <summary>
        /// 将文件转化位byte数组
        /// </summary>
        /// <param name="Path">文件地址</param>
        /// <returns>转换为的byte数组</returns>
        public static byte[] FileBytes(string Path)
        {
            if (!System.IO.File.Exists(Path))
            {
                return new byte[0];
            }
            FileInfo fi = new FileInfo(Path);
            byte[] buff = new byte[fi.Length];
            FileStream fs = fi.OpenRead();
            fs.Read(buff, 0, Convert.ToInt32(fs.Length));
            fs.Close();
            return buff;
        }
        /// <summary>
        /// 将byte数组转换为文件并保存到指定地址
        /// </summary>
        /// <param name="buff">byte数组</param>
        /// <param name="savepath">保存地址</param>
        public static void BytesFile(byte[] buff, string savepath)
        {
            if (System.IO.File.Exists(savepath))
            {
                System.IO.File.Delete(savepath);
            }
            FileStream fs = new FileStream(savepath, FileMode.CreateNew);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(buff, 0, buff.Length);
            fs.Close();
        }
    }
}

入口

void Start()
{
         //byte[] byteArray zip转化而来的字节数组      path1 保存路径
         FileBinaryConvertHelper.BytesFile(byte[] byteArray, path1);//二进制数组转换为zip文件
         UnityEngine.Debug.Log("转化 zip成功 ");
         ZipFile.ExtractToDirectory(path1, path2);
         UnityEngine.Debug.Log("解压 zip成功 +" );
}

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果要使用 C# 读取有密码的 ZIP 文件,可以使用 `System.IO.Compression` 命名空间提供的 `ZipArchive` 类并指定密码。 以下是一个示例代码: ```csharp using System.IO; using System.IO.Compression; string zipFilePath = "example.zip"; string password = "password"; using (var archive = ZipFile.OpenRead(zipFilePath)) { foreach (var entry in archive.Entries) { // 指定密码 entry.ExtractWithPassword(password); // 处理解压出来的文件 } } ``` 需要注意的是,需要使用扩展方法 `ExtractWithPassword` 来解压密码保护的 ZIP 文件。这个扩展方法可以在 `ZipArchiveExtensions` 类中找到,需要使用 `System.IO.Compression.ZipFileExtensions` 命名空间。 以下是 `ExtractWithPassword` 扩展方法的示例代码: ```csharp using System.IO; using System.IO.Compression; namespace System.IO.Compression { public static class ZipArchiveExtensions { public static void ExtractWithPassword(this ZipArchiveEntry entry, string password) { using (var stream = entry.Open()) { using (var zipStream = new ZipInputStream(stream)) { zipStream.Password = password; zipStream.GetNextEntry(); var buffer = new byte[4096]; var outputFolder = Path.GetDirectoryName(entry.FullName); Directory.CreateDirectory(outputFolder); using (var streamWriter = File.Create(Path.Combine(outputFolder, entry.Name))) { StreamUtils.Copy(zipStream, streamWriter, buffer); } } } } } } ``` 需要注意的是,这里使用了 `ICSharpCode.SharpZipLib` 库来进行解压操作,需要先安装这个库。可以使用 NuGet 包管理器来安装,或者手动下载并添加到项目中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值